How to Connect to MongoDB running on localhost from Minikube

3/27/2020

I am learning about Kubernetes am trying move from docker-compose to use Kubernetes but having problem with connecting to MongoDB running on localhost.

I don't have any problems connecting using following docker-compose.yaml by using 'network_mode: "host"'

//docker-compose.yaml
    version: '3'
    services:

      eureka:
        image: sage-eureka
        ports:
          - "8080:8080"
        network_mode: "host"

But I'm having issues in Kubernetes. I used Kompose to convert the docker-compose.

//application.properties
    spring.data.mongodb.host=localhost (tried adding mongo instead of localhost when used ExternalName and ClusterIP)
    spring.data.mongodb.port=27017
    spring.data.mongodb.database=MyMongo

// eureka-service.yaml
apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.21.0 (992df58d8)
  creationTimestamp: null
  labels:
    io.kompose.service: eureka
  name: eureka
spec:
  ports:
  - name: "8080"
    port: 8080
    targetPort: 8080
  - name: "27017"
    port: 27017
    targetPort: 27017
  selector:
    io.kompose.service: eureka
status:
  loadBalancer: {}

//eureka-deployment.yaml

 apiVersion: apps/v1
    kind: Deployment
    metadata:
      annotations:
        kompose.cmd: kompose convert
        kompose.version: 1.21.0 (992df58d8)
      creationTimestamp: null
      labels:
        io.kompose.service: eureka
      name: eureka
    spec:
      replicas: 1
      selector:
        matchLabels:
          io.kompose.service: eureka
      strategy: {}
      template:
        metadata:
          annotations:
            kompose.cmd: kompose convert
            kompose.version: 1.21.0 (992df58d8)
          creationTimestamp: null
          labels:
            io.kompose.service: eureka
        spec:
          hostNetwork: true
          dnsPolicy: Default
          containers:
          - args:
            - --spring.profiles.active=local
            image: sage-eureka
            imagePullPolicy: Never
            name: sageeureka
            ports:
            - containerPort: 8080
            - containerPort: 27017
            resources: {}
          restartPolicy: Always
          serviceAccountName: ""
          volumes: null
    status: {}

I'm also getting on external ip address when I've added type to LoadBalencer and I've also tried creating Service type ExternalName and NodePort for Mongo as shown in Kubernetes best practices: mapping external services:

eureka       ClusterIP      10.102.22.91   <none>                 8080/TCP,27017/TCP   45h
kubernetes   ClusterIP      10.96.0.1      <none>                 443/TCP              4d2h
mongo        ExternalName   <none>         host.docker.internal   <none>               45h

I've referred to similar posts here but I don't know exactly what needs to be done and following these post are also not working for me as I don't know what I'm doing wrong:

I also tried creating the mongo-serivce as in 2nd post like this:

kind: Service
apiVersion: v1
metadata:
  name: mongo
spec:
  type: ExternalName
  externalName: host.docker.internal

Please Help!. Thank you.

-- Sujal
google-kubernetes-engine
kubectl
kubernetes
minikube

1 Answer

4/2/2020

How to connect from Host to Pod in Minikube

If the issue is that you can't access your Pods (via the service) on Minikube.

Let's check your Service first.

$ kubectl get service eureka
NAME     TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)          AGE
eureka   LoadBalancer   10.104.196.32   <pending>      8080:30167/TCP   25m

Note that, when running Minikube, a LoadBalancer service won’t acquire an external IP address.

Locally, Minikube doesn’t integrate with another service to run external load balancers.

But in most other environments, when you use a Kubernetes LoadBalancer, it will provision a load balancer external to your cluster, for example an Elastic Load Balancer (ELB) in AWS, or a Cloud Load Balancer in GKE.

Because Minikube doesn’t integrate with a separate local load balancer, you have to ask Minikube to simulate the connection using minikube service:

$ minikube service eureka

That will provide you with the Minikube’s IP address and the port number Minikube routes to 'eureka', so and you can copy and paste that into a browser/other app to test the app.

Additional info can be found in Minikube's documentation:

To access the hello-minikube Deployment, expose it as a Service:

kubectl expose deployment hello-minikube --type=NodePort --port=8080

Get the URL of the exposed Service to view the Service details:

minikube service hello-minikube --url

EDIT

How to connect from Pod to localhost on Minikube.Host

don't have any problems connecting using following docker-compose.yaml by using 'network_mode: "host"'

True.

If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated. So if you run a container which binds to port 80 and you use host networking, the container’s application is available on port 80 on the host’s IP address.

Since minikube runs everything from within a VM, networking can get fairly complicated

Once Minikube is running and it’s network has been established, you should be able to visit your localhost server on both 127.0.0.1 and 192.168.99.1 (If not, subsequent steps will not work). If successful, this means that the Minikube VM can access your host machine’s localhost on 192.168.99.1 (127.0.0.1 from Minikube would still be a Minicube's localhost)

That is why in this very case instead of localhost your app shall connect to 192.168.99.1.

kind: Service
apiVersion: v1
metadata:
  name: mongo
spec:
  type: ExternalName
  externalName: host.docker.internal

Please make sure that host.docker.internal is resolved to the IP address of Host machine.

Additionally you can edit /etc/hosts of Minikube and add any domain name for that IP, so you'll be able connecting to it.

Hope that explains how to access your app from outside and access specifically "localhost" when using Minikube.

-- Nick
Source: StackOverflow