Routing between Kubernetes cluster and Docker container in the VM

11/30/2018

I have setup Kubernates cluster in the VM (Ubuntu 18.04.1 LTS) on the Azure cloud using preconfigured scripts.

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

Docker containers:

$ 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->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->8080/tcp   kube-master

Kubernates nodes:

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

Kubernates service:

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

Kubernates pods:

$ 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   <none>

MongoDB container's IP address is 172.17.0.2

Kubernates master container IP address is 10.192.0.2

Kubernates node 1 container IP address is 10.192.0.3

Kubernates node 2 container IP address is 10.192.0.4

As CMS pod is running on 10.244.2.13 which is inside the k8s container.

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

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

CMS pod's log

MongoError: failed to connect to server [172.17.0.2:27017] on first connect [MongoError: connect EHOSTUNREACH 172.17.0.2:27017]

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

Please let me know if you need further information. Thanks!

-- Ramesh Murugesan
dind
docker
kubernetes

1 Answer

12/3/2018

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 docker ps -a, you have exposed 27017 port for MongoDB container, therefore <hostIP>:27017 should be used, not 172.17.0.2:27017.

By default in Kubernetes, there are no restrictions to connect outside the cluster.

Also, you may have firewall rules in Azure that forbid connections between hosts.

-- Artem Golenyaev
Source: StackOverflow