首页 > 解决方案 > 如何从 Minikube 连接到在 localhost 上运行的 MongoDB

问题描述

我正在了解 Kubernetes,我正在尝试从 docker-compose 迁移到使用 Kubernetes,但在连接到在 localhost 上运行的 MongoDB 时遇到问题。

我使用以下 docker-compose.yaml 连接没有任何问题'network_mode: "host"'

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

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

但是我在 Kubernetes 中遇到了问题。我使用Kompose来转换 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: {}

当我向 LoadBalencer 添加类型时,我也获得了外部 IP 地址,并且我还尝试为 Mongo 创建服务类型 ExternalName 和 NodePort,如Kubernetes 最佳实践中所示:映射外部服务

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

我在这里提到了类似的帖子,但我不确切知道需要做什么,并且关注这些帖子也不适合我,因为我不知道我做错了什么:

我还尝试像这样在第二篇文章中创建 mongo-service:

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

请帮忙!。谢谢你。

标签: kubernetesgoogle-kubernetes-enginekubectlminikube

解决方案


从 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.


推荐阅读