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

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

<pre><code>//docker-compose.yaml
    version: '3'
    services:

      eureka:
        image: sage-eureka
        ports:
          - "8080:8080"
        network_mode: "host"
</code></pre>

<p>But I'm having issues in Kubernetes. I used <a href="https://www.digitalocean.com/community/tutorials/how-to-migrate-a-docker-compose-workflow-to-kubernetes" rel="nofollow noreferrer">Kompose</a> to convert the docker-compose.</p>

<pre><code>//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: {}
</code></pre>

<p>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 <a href="https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services" rel="nofollow noreferrer">Kubernetes best practices: mapping external services</a>: </p>

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

<p>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:</p>

<ul>
<li><a href="https://stackoverflow.com/questions/45742999/com-mongodb-mongosocketopenexception-exception-opening-socketmongodb-docker">com.mongodb.MongoSocketOpenException: Exception opening socket(MongoDB, Docker)</a></li>
<li><a href="https://stackoverflow.com/questions/49289009/during-local-development-with-kubernetes-minikube-how-should-i-connect-to-postg">During local development with Kubernetes/minikube, how should I connect to postgres database running on localhost?</a></li>
</ul>

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

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: mongo
spec:
  type: ExternalName
  externalName: host.docker.internal
</code></pre>

<p>Please Help!. Thank you.</p>

<p>Similar solution to <a href="https://stackoverflow.com/questions/50952240/connect-to-local-database-from-inside-minikube-cluster/67093139#67093139">Connect to local database from inside minikube cluster</a></p>
<p>The issue is <code>host.minikube.internal</code> (not <code>host.docker.internal</code> -- this one is for kubernetes host at docker desktop) is not visible at kubernetes container somehow. See this <a href="https://github.com/kubernetes/minikube/issues/8439" rel="nofollow noreferrer">issue</a>. It is only visible at minikube ssh instance.</p>
<p>So it needs a workaround to attach this host to containers inside k8s.</p>
<p>Or just hard code the ip address in k8s container as the host. Find ip address from command <code>minikube ssh 'grep host.minikube.internal /etc/hosts | cut -f1'</code>.</p>
<p>For example, if the ip is <code>192.168.65.2</code>. Then use this ip as service host in k8s container, instead of <code>127.0.0.1/localhost</code>.</p>
