Unable to connect to mongodb atlas cluster from a kubernetes cluster

1/16/2019

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

-- suprith
kubernetes
minikube
mongodb
mongodb-cluster

0 Answers