<p>I installed mongodb as a replicaset with 3 replicas on my k8s cluster using the bitnami helm chart.</p>
<p>So I get these pods:</p>
<pre><code>mongodb-0.mongodb-headless.mongodb.svc.cluster.local:27017
mongodb-1.mongodb-headless.mongodb.svc.cluster.local:27017
mongodb-2.mongodb-headless.mongodb.svc.cluster.local:27017
</code></pre>
<p>Now I would like to get access using mongodb compass.</p>
<p>I set a port forward (at 27017 I'm running a local mongodb)</p>
<pre><code>kubectl port-forward svc/mongodb-headless -n mongodb 27018:27017
</code></pre>
<p>and tried to connect compass with the uri</p>
<pre><code>mongodb://localhost:27018
</code></pre>
<p>But this gives me the error</p>
<pre><code>getaddrinfo ENOTFOUND mongodb-0.mongodb-headless.mongodb.svc.cluster.local
</code></pre>
<p>What am I doing wrong to connect to my k8s cluster mongodb using compass?</p>
<hr />
<p><em>Update</em></p>
<pre><code>% kubectl get all -n mongodb

NAME                    READY   STATUS    RESTARTS   AGE
pod/mongodb-0           1/1     Running   0          25h
pod/mongodb-1           1/1     Running   0          25h
pod/mongodb-2           1/1     Running   0          25h
pod/mongodb-arbiter-0   1/1     Running   0          2d14h

NAME                               TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)     AGE
service/mongodb-arbiter-headless   ClusterIP   None         &lt;none&gt;        27017/TCP   2d14h
service/mongodb-headless           ClusterIP   None         &lt;none&gt;        27017/TCP   2d14h

NAME                               READY   AGE
statefulset.apps/mongodb           3/3     2d14h
statefulset.apps/mongodb-arbiter   1/1     2d14h
</code></pre>
<p><strong>values.yaml for bitnami helm chart</strong></p>
<pre><code>image:
  registry: docker.io
  repository: bitnami/mongodb
  digest: &quot;sha256:916202d7af766dd88c2fff63bf711162c9d708ac7a3ffccd2aa812e3f03ae209&quot; # tag: 4.4.15
  pullPolicy: IfNotPresent
architecture: replicaset
replicaCount: 2
updateStrategy:
  type: RollingUpdate
containerPorts:
  mongodb: 27017
auth:
  enabled: true
  rootUser: root
  rootPassword: &quot;password&quot;
  usernames: [&quot;user&quot;]
  passwords: [&quot;userpass&quot;]
  databases: [&quot;db&quot;]

service:
  portName: mongodb
  ports:
    mongodb: 27017

persistence:
  enabled: true
  accessModes:
    - ReadWriteOnce
  size: 8Gi

volumePermissions:
  enabled: true

livenessProbe:
  enabled: false
readinessProbe:
  enabled: false
</code></pre>

<p> Just have recreated your setup. Everything works fine</p>
<pre><code>$ k create ns mongo-test
namespace/mongo-test created

$ k -n mongo-test create -f mongo-svc-sts.yaml
statefulset.apps/mongo created
service/mongo-headless created
</code></pre>
<p>result</p>
<pre><code>$ k -n mongo-test get all
NAME          READY   STATUS    RESTARTS   AGE
pod/mongo-0   1/1     Running   0          44s
pod/mongo-1   1/1     Running   0          40s
pod/mongo-2   1/1     Running   0          27s

NAME                     TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)     AGE
service/mongo-headless   ClusterIP   None         &lt;none&gt;        27017/TCP   13m

NAME                     READY   AGE
statefulset.apps/mongo   3/3     45s
</code></pre>
<p>port forward</p>
<pre><code>$ k -n mongo-test port-forward svc/mongo-headless 27018:27017
Forwarding from 127.0.0.1:27018 -&gt; 27017
Forwarding from [::1]:27018 -&gt; 27017
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
Handling connection for 27018
</code></pre>
<p>compass
<a href="https://i.stack.imgur.com/lH9qU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lH9qU.png" alt="enter image description here" /></a></p>
<p>mongo-svc-sts.yaml</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: &quot;mongo&quot;
  replicas: 3
  selector:
    matchLabels:
      app.kubernetes.io/name: mongo
      app.kubernetes.io/component: backend
  template:
    metadata:
      labels:
        app.kubernetes.io/name: mongo
        app.kubernetes.io/component: backend
    spec:
      tolerations:
        - operator: Exists
      containers:
      - name: mongo
        image: mongo:latest
        args:
          - --bind_ip
          - 0.0.0.0
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 27017
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mongo
    service: logging
  name: mongo-headless
spec:
  type: ClusterIP
  clusterIP: None
  ports:
  - port: 27017
  selector:
    app.kubernetes.io/name: mongo
    app.kubernetes.io/component: backend

</code></pre>
<p>to be able to help you pls use that YAMLs and post the outputs. If it does not work probably you should debug your k8s installation</p>
