How to Connect to MongoDB running on localhost from Minikube

Suj*_*jal 2 kubernetes google-kubernetes-engine kubectl minikube

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"
Run Code Online (Sandbox Code Playgroud)

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: {}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Please Help!. Thank you.

smi*_*trl 5

与从 minikube 集群内部连接到本地数据库类似的解决方案

问题是host.minikube.internal(不是host.docker.internal——这个是针对 docker 桌面上的 kubernetes 主机)在 kubernetes 容器中不可见。看到这个问题。它仅在 minikube ssh 实例中可见。

因此需要一种解决方法将此主机附加到 k8s 内的容器。

或者只是将 k8s 容器中的 IP 地址硬编码为主机。从命令查找 IP 地址minikube ssh 'grep host.minikube.internal /etc/hosts | cut -f1'

例如,如果 ip 是192.168.65.2. 然后使用这个ip作为k8s容器中的服务主机,而不是127.0.0.1/localhost.