ECONNREFUSED MongoDB running in kubernetes cluster

Viewed 514

I'm a beginner to docker and kubernetes. I have docker containers running inside pods on the kubernetes cluster and I'm trying to connect to mongo db but it keeps failing

when I check my pods and services, everything is running all good:

NAME                               READY   STATUS    RESTARTS   AGE

auth-depl-65565b6884-rxcvj         1/1     Running   0          58s

auth-mongo-depl-6c87496969-9sxj4   1/1     Running   0          56s


NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE

auth-mongo-srv   ClusterIP   10.106.91.10    <none>        27017/TCP   2m

auth-srv         ClusterIP   10.96.108.116   <none>        3000/TCP    2m3s

kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP     2m20s




My Mongo Deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-mongo-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo-depl
  template:
    metadata:
      labels:
        app: mongo-depl
    spec:
      containers:
        - name: mongo-depl
          image: mongo
---
apiVersion: v1
kind: Service
metadata:
  name: auth-mongo-srv
spec:
  selector:
    type: ClusterIP
    app: mongo-depl
  ports:
    - name: db
      protocol: TCP
      port: 27017
      targetPort: 27017

Connection to DB:

const start = async (): Promise<void> => {
  try {
    await mongoose.connect("mongodb://auth-mongo-srv:27017/auth", {
      useCreateIndex: true,
      useFindAndModify: false,
      useNewUrlParser: true,
      useUnifiedTopology: true
    });
    console.log("connected to db");
  } catch (error) {
    console.log(error);
  }
};

start();

When I check my logs, I get this error:

[auth] MongooseServerSelectionError: connect ECONNREFUSED 10.106.91.10:27017
 reason: TopologyDescription {                                            
   type: 'Single',                                                        
   setName: null,                                                         
   maxSetVersion: null,                                                   
   maxElectionId: null,                                                   
   servers: Map(1) { 'auth-mongo-srv:27017' => [ServerDescription] },     
   stale: false,                                                          
   compatible: true,                                                      
   compatibilityError: null,                                              
   logicalSessionTimeoutMinutes: null,                                    
   heartbeatFrequencyMS: 10000,                                           
   localThresholdMS: 15,                                                  
   commonWireVersion: null                                                
 }                                                                        

Any clues what may cause the problem?

3 Answers

You seem to have missconfigured the selector for the service. You have added a label type: ClusterIP so your mongo pods are not picked by this service because they do not have such label. You probably wanted to add type: ClusterIP under the spec, not selector. So remove label type: ClusterIP from the selector and optionally put it under spec (ClusterIP type is the default for the service).

You must configure a port on your pod's container such as:

    spec:
      containers:
        - name: mongo-depl
          image: mongo
          ports:
          - containerPort: 27017

See Connecting Applications with Services , stating

Create [a] Pod, and note that it has a container port specification

thanks I now see my mistake – Kevin M plug Sep 15 at 11:06

That's because of the Kubernetes (k8s) service is just "an abstract way to expose an application running on a set of Pods as a network service" (link).

That "set of Pods" is the Endpoint. you can check Endpoints with the kubectl get ep command. That is why the very first step is to check if there are endpoints for the auth-mongo-srv service.

The Service spec you have created results in:

$ kubectl get svc auth-mongo-srv -o wide 
NAME             TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)     AGE   SELECTOR
auth-mongo-srv   ClusterIP   10.0.9.213   <none>        27017/TCP   30s   app=mongo-depl,type=ClusterIP

$ kubectl get ep auth-mongo-srv 
NAME             ENDPOINTS   AGE
auth-mongo-srv   <none>      8m47s

As you can see, no endpoint for that service is created, because k8s couldn't match the Service selector with any Pod that lists the same set of selectors.

In this case it is needed to adjust auth-mongo-srv.

Sometimes you might want to create Endpoint's manually ( "Headless Services" and services without selectors).

Related