简体   繁体   English

无法从 kubernetes 集群连接到 mongodb atlas 集群

[英]Unable to connect to mongodb atlas cluster from a kubernetes cluster

Hi All I am unable to connect to mongo atlas database as a service outside the kubernetes cluster from my kubernetes cluster.大家好我无法从我的 kubernetes 集群连接到 mongo atlas 数据库作为 kubernetes 集群之外的服务。

here are the steps I followed.这是我遵循的步骤。

In my code in nodejs used dbUri= 'mongodb://username:password' + process.env.MONGO_URL +'/database-name'在我的 nodejs 代码中使用 dbUri='mongodb://username:password' + process.env.MONGO_URL +'/database-name'

Here is the cluster ip service config这是集群 ip 服务配置

kind: Service
apiVersion: v1
metadata:
  name: mongo-cluster-ip-service
spec:
  type: ClusterIP
  ports:
  - port: 27017
    targetPort: 27017

then I have the endpoints for the same service然后我有相同服务的端点

kind: Endpoints
apiVersion: v1
metadata:
  name: mongo-cluster-ip-service
subsets:
  - addresses:
    - ip: 35.187.27.116
    ports:
    - port: 27017
  - addresses:
    - ip: 35.241.213.79
    ports:
    - port: 27017
  - addresses:
    - ip: 104.155.120.154
    ports:
    - port: 27017

Then I have my deployment which wants to use it for the pods through an env variable然后我有我的部署,它想通过 env 变量将它用于 pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-reg-auth-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: user-reg-auth
  template:
    metadata:
      labels:
        app: user-reg-auth
    spec:
      imagePullSecrets:
        - name: ofinowregcred # this is a manually applied secret to the cluster using kubectl create secret
      containers:
      - name: user-reg-auth
        image: ofinow/user-reg-auth
        ports:
        - containerPort: 8080
        # resources:
        #   requests:
        #     memory: "64Mi"
        #     cpu: "250m"
        #   limits:
        #     memory: "128Mi"
        #     cpu: "500m"
        env:
          - name: MONGO_URL
            value: mongo-cluster-ip-service

Now the problem is the mongo-cluster-ip-service is not getting resolved, So I am unable to connect.现在的问题是 mongo-cluster-ip-service 没有得到解决,所以我无法连接。 i kindly request for help我恳请帮助

I followed the google best practices guide https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services我遵循了谷歌最佳实践指南https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-mapping-external-services

Try either of these;尝试其中任何一个;

solution A溶液A

  1. Add the endpoint of the Kubernetes cluster to the MongoDB network access IP whitelist.将Kubernetes集群的端点加入MongoDB网络访问IP白名单。

  2. Under the pod spec of your k8s pod (or deployment) manifest, add a dnsPolicy field with value set to Default .在 k8s pod(或部署)清单的 pod 规范下,添加一个dnsPolicy字段,其值设置为Default Hence, your Pods (your container basically) will connect to mongo through the name resolution configuration of the master node.因此,您的 Pod(基本上是您的容器)将通过主节点的名称解析配置连接到 mongo。

solution B溶液B

  1. Add all node endpoints in your k8s cluster to the MongoDB network access IP whitelist.将您的 k8s 集群中的所有节点端点添加到 MongoDB 网络访问 IP 白名单。

  2. Under the pod spec of your k8s pod (or deployment) manifest, add a dnsPolicy field with the value set to ClusterFirstWithHostNet .在 k8s pod(或部署)清单的 pod 规范下,添加一个dnsPolicy字段,其值设置为ClusterFirstWithHostNet Since the pods run with your host network, they gain access to services listening on localhost.由于 pod 与您的主机网络一起运行,因此它们可以访问在 localhost 上侦听的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM