Home > Enterprise >  how to configure dns resolution for mongo to connect to atlas from inside a k8s cluster
how to configure dns resolution for mongo to connect to atlas from inside a k8s cluster

Time:03-22

I have several pages that I found with similar question and most answer tell us to white list our IP. However I have allowed access from anywhere 0.0.0.0/0 in the atlas, and have installed the latest version of mongoose(6.2.6 ; which is supposed to have support for the protocol (mongodb srv).

The connection works perfectly when I run locally using npm start or even from a dockerized container. But, when I deploy to a k8s cluster, I get an error saying:

querySrv ENOTFOUND _mongodb._tcp.mongodb-cluster0.zvnxj.mongodb.net

The deployment and service file are as:

deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ns-my-workflow-api
  name: my-workflow-api
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-workflow-api
  template:
    metadata:
      labels: 
        app: my-workflow-api
    spec:
      containers:
        - name: my-workflow-api
          image: "myname/my-workflow-api:1.0.0"
          ports:
            - containerPort: 3000
          imagePullPolicy: IfNotPresent
          resources:
              limits:
                cpu: "256m"   

The service.yaml has the contents:

apiVersion: v1
kind: Service
metadata:
  namespace: ns-my-workflow-api
  name: my-workflow-api
spec:
  selector:
    app: my-workflow-api
  type: LoadBalancer
  ports:
    - name: http
      port: 8000
      targetPort: 3000
      protocol: TCP  

The namespace.yaml has the contents:

apiVersion: v1
kind: Namespace
metadata:
  name: ns-my-workflow-api

I also tried the deployment.yaml with the dns rule:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ns-my-workflow-api
  name: my-workflow-api
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-workflow-api
  template:
    metadata:
      labels: 
        app: my-workflow-api
    spec:
      dnsPolicy: Default            # <------ this rule
      containers:
        - name: my-workflow-api
          image: "myname/my-workflow-api:1.0.0"
          ports:
            - containerPort: 3000
          imagePullPolicy: IfNotPresent
          resources:
              limits:
                cpu: "256m"

Once I changed the connection url to use 2.0.14 or earlier I was able to connect. The connection string started with mongodb://....

While I have managed to make the connection work with the workaround using an old-style connection string, and it seems to be some sort of dns resolution issue, how do I make the newer protocols work to connect to atlas from inside the cluster? Thanks in advance

CodePudding user response:

I was able to solve it using this to start minikube:

minikube start --driver=docker

It seems there's some dns resolution issue with the underlying oracle's virtualbox driver(Maybe some configuration and setup issue as well)

  • Related