<p>There is a running a kubernetes cluster with a Web server, a Redis server, and a MongoDB server which is using official docker image: <em><code>mongo:4.4.0</code></em></p>
<p>I exposed MongoDB server with name <code>mongodb</code> and Redis server with name <code>redis</code>:</p>
<pre class="lang-sh prettyprint-override"><code>NAME      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)     AGE
backend   ClusterIP   172.19.14.124   &lt;none&gt;        7001/TCP    15h
mongodb   ClusterIP   172.19.10.24    &lt;none&gt;        27017/TCP   15h
redis     ClusterIP   172.19.6.203    &lt;none&gt;        6379/TCP    15h
</code></pre>
<p>Now, from backend webserver, I cannot connect to MongoDB server with <code>mongo --host mongodb</code> but connect to Redis using <code>redis -h redis</code> works.</p>
<p>Then checked by <em><code>kubectl exec -it mongodb-5f4bd8c8d-hbsfx -- bash</code></em>:</p>
<ol>
<li><p><code>mongo mongodb</code> works, it connected to <code>127.0.0.1</code></p>
</li>
<li><p><code>mongo 172.19.10.24</code> fail,  it results in<br />
<code>Error: couldn't connect to server 172.19.10.24:27017, connection attempt failed: SocketException: Error connecting to 172.19.10.24:27017 :: caused by :: Connection refused :</code></p>
</li>
<li><p><code>ping mongodb</code> works well.</p>
</li>
</ol>
<p>From the <code>ps</code> result, it already bind all:</p>
<p><code>mongodb      1  0.2  2.5 1588248 98272 ?       Ssl  Aug20   2:02 mongod --bind_ip_all</code></p>
<p>What's wrong and how to fix it? thx.</p>
<hr />
<h2>update</h2>
<p>command <code>ss</code> result from pod</p>
<pre><code>LISTEN     0        0                  0.0.0.0:27017            0.0.0.0:*        users:((&quot;mongod&quot;,pid=1,fd=9))
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  namespace: tabby
  name: mongodb
  labels:
    app: mongodb
spec:
  ports:
  - port: 27017
    targetPort: 27017
  selector:
    name: mongodb
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: tabby
  name: mongodb
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      imagePullSecrets:
      - name: regcred
      containers:
      - name: mongodb
        image: mongo:4.4.0
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: data
          mountPath: /data/db
      volumes:
      - name: data
        emptyDir: {}
</code></pre>

<p>The service selector has <code>name: mongodb</code> but deployment has got <code>app: mongodb</code> label.  Because of this mismatch service is not selecting the pods. If you check the <code>Endpoints</code> section of the service using <code>kubectl describe svc mongodb -n tabby</code> you will not see any Pod IPs because of the above reason.</p>
<p>Change the service as follows.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  namespace: tabby
  name: mongodb
  labels:
    app: mongodb
spec:
  ports:
  - port: 27017
    targetPort: 27017
  selector:
    app: mongodb
</code></pre>
