<p>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</p>
<p>when I check my pods and services, everything is running all good:</p>
<pre><code>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    &lt;none&gt;        27017/TCP   2m

auth-srv         ClusterIP   10.96.108.116   &lt;none&gt;        3000/TCP    2m3s

kubernetes       ClusterIP   10.96.0.1       &lt;none&gt;        443/TCP     2m20s




</code></pre>
<p>My Mongo Deployment file:</p>
<pre><code>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

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

start();
</code></pre>
<p>When I check my logs, I get this error:</p>
<pre><code>[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' =&gt; [ServerDescription] },     
   stale: false,                                                          
   compatible: true,                                                      
   compatibilityError: null,                                              
   logicalSessionTimeoutMinutes: null,                                    
   heartbeatFrequencyMS: 10000,                                           
   localThresholdMS: 15,                                                  
   commonWireVersion: null                                                
 }                                                                        
</code></pre>
<p>Any clues what may cause the problem?</p>

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