3
votes

Hi All I am unable to connect to mongo atlas database as a service outside the kubernetes cluster from my kubernetes cluster.

here are the steps I followed.

In my code in nodejs used dbUri= 'mongodb://username:password' + process.env.MONGO_URL +'/database-name'

Here is the cluster ip service config

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

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

1
You need to provide a lot more information - there are so many things that could be going wrong and without any context it is impossible for anyone to answer that.Markoorn
kind: Endpoints apiVersion: v1 metadata: name: mongo 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: 27017suprith
Please rather amend your question to include the relevant details - please also read stackoverflow.com/help/how-to-ask It's important to ask a question that makes sense to others, try looking at what you've asked as if you're an outsider trying to get context and then you'll see little real information you've provided.Markoorn
@Markoorn dont mind for the bad description. I have updated now.suprith
@suprith did you resolve the issue?Volodymyr I.

1 Answers

0
votes

Try either of these;

solution A

  1. Add the endpoint of the Kubernetes cluster to the MongoDB network access IP whitelist.

  2. Under the pod spec of your k8s pod (or deployment) manifest, add a dnsPolicy field with value set to Default. Hence, your Pods (your container basically) will connect to mongo through the name resolution configuration of the master node.

solution B

  1. Add all node endpoints in your k8s cluster to the MongoDB network access IP whitelist.

  2. Under the pod spec of your k8s pod (or deployment) manifest, add a dnsPolicy field with the value set to ClusterFirstWithHostNet. Since the pods run with your host network, they gain access to services listening on localhost.