<p>I have setup Kubernates cluster in the VM (Ubuntu 18.04.1 LTS) on the Azure cloud using <a href="https://github.com/kubernetes-sigs/kubeadm-dind-cluster#using-preconfigured-scripts" rel="nofollow noreferrer">preconfigured scripts</a>. </p>

<p>MongoDB docker container is running along with K8s cluster. My aim is to connect MongoDB to CMS container which is running inside the K8s.</p>

<p>Docker containers:</p>

<pre><code>$ docker ps -a
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS              PORTS                       NAMES
3883f7b397cf        mongo                                 "docker-entrypoint.s…"   5 hours ago         Up 5 hours          0.0.0.0:27017-&gt;27017/tcp    mongodb
299239d90cbb        mirantis/kubeadm-dind-cluster:v1.12   "/sbin/dind_init sys…"   27 hours ago        Up 27 hours         8080/tcp                    kube-node-2
34c8bd5fad2e        mirantis/kubeadm-dind-cluster:v1.12   "/sbin/dind_init sys…"   27 hours ago        Up 27 hours         8080/tcp                    kube-node-1
15a2d6521e6e        mirantis/kubeadm-dind-cluster:v1.12   "/sbin/dind_init sys…"   27 hours ago        Up 27 hours         127.0.0.1:32768-&gt;8080/tcp   kube-master
</code></pre>

<p>Kubernates nodes:</p>

<pre><code>$ kubectl get services -o wide
NAME            TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes      ClusterIP      10.96.0.1      &lt;none&gt;        443/TCP        26h   &lt;none&gt;
mycms           LoadBalancer   10.97.53.114   &lt;pending&gt;     80:31664/TCP   18s   app=mycms,tier=frontend
</code></pre>

<p>Kubernates service:</p>

<pre><code>$ kubectl get services -o wide
NAME            TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE    SELECTOR
kubernetes      ClusterIP      10.96.0.1      &lt;none&gt;        443/TCP        26h    &lt;none&gt;
mycms           LoadBalancer   10.97.53.114   &lt;pending&gt;     80:31664/TCP   112s   app=mycms,tier=frontend
</code></pre>

<p>Kubernates pods:</p>

<pre><code>$ kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE    IP            NODE          NOMINATED NODE
mycms-dc4978ffc-khvj2           1/1     Running   0          4m8s   10.244.2.13   kube-node-1   &lt;none&gt;
</code></pre>

<p>MongoDB container's IP address is 172.17.0.2</p>

<p>Kubernates master container IP address is 10.192.0.2</p>

<p>Kubernates node 1 container IP address is 10.192.0.3</p>

<p>Kubernates node 2 container IP address is 10.192.0.4</p>

<p>As CMS pod is running on 10.244.2.13 which is inside the k8s container.</p>

<p>For testing, I have installed mongo-client on the host and test the connection which works well.</p>

<p>But CMS doesn't reach MongoDB container (I am passing Mongo String to pod in an environmental variable).</p>

<p>CMS pod's log</p>

<pre><code>MongoError: failed to connect to server [172.17.0.2:27017] on first connect [MongoError: connect EHOSTUNREACH 172.17.0.2:27017]
</code></pre>

<p>How do I route MongoDB container and CMS container? Is anything wrong/missed in my approach? </p>

<p>Please let me know if you need further information. Thanks!</p>

Ramesh Murugesan
<p>You need to use the IP address of the host where Docker is installed, not internal MongoDB container's IP address, to connect to MongoDB from the Kubernetes cluster or form any other host. According to the results of your <code>docker ps -a</code>, you have exposed <code>27017</code> port for MongoDB container, therefore <code>&lt;hostIP&gt;:27017</code> should be used, not <code>172.17.0.2:27017</code>.</p>

<p>By default in Kubernetes, there are no restrictions to connect outside the cluster. </p>

<p>Also, you may have firewall rules in Azure that forbid connections between hosts.</p>
