Search code examples
mongodbkubernetesmongodb-compassmongodb-replica-set

How to connect to mongodb replicaset (k8s) using compass


I installed mongodb as a replicaset with 3 replicas on my k8s cluster using the bitnami helm chart.

So I get these pods:

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

Now I would like to get access using mongodb compass.

I set a port forward (at 27017 I'm running a local mongodb)

kubectl port-forward svc/mongodb-headless -n mongodb 27018:27017

and tried to connect compass with the uri

mongodb://localhost:27018

But this gives me the error

getaddrinfo ENOTFOUND mongodb-0.mongodb-headless.mongodb.svc.cluster.local

What am I doing wrong to connect to my k8s cluster mongodb using compass?


Update

% 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         <none>        27017/TCP   2d14h
service/mongodb-headless           ClusterIP   None         <none>        27017/TCP   2d14h

NAME                               READY   AGE
statefulset.apps/mongodb           3/3     2d14h
statefulset.apps/mongodb-arbiter   1/1     2d14h

values.yaml for bitnami helm chart

image:
  registry: docker.io
  repository: bitnami/mongodb
  digest: "sha256:916202d7af766dd88c2fff63bf711162c9d708ac7a3ffccd2aa812e3f03ae209" # tag: 4.4.15
  pullPolicy: IfNotPresent
architecture: replicaset
replicaCount: 2
updateStrategy:
  type: RollingUpdate
containerPorts:
  mongodb: 27017
auth:
  enabled: true
  rootUser: root
  rootPassword: "password"
  usernames: ["user"]
  passwords: ["userpass"]
  databases: ["db"]

service:
  portName: mongodb
  ports:
    mongodb: 27017

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

volumePermissions:
  enabled: true

livenessProbe:
  enabled: false
readinessProbe:
  enabled: false

Solution

  •  Just have recreated your setup. Everything works fine

    $ 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
    

    result

    $ 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         <none>        27017/TCP   13m
    
    NAME                     READY   AGE
    statefulset.apps/mongo   3/3     45s
    

    port forward

    $ k -n mongo-test port-forward svc/mongo-headless 27018:27017
    Forwarding from 127.0.0.1:27018 -> 27017
    Forwarding from [::1]:27018 -> 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
    

    compass enter image description here

    mongo-svc-sts.yaml

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: mongo
    spec:
      serviceName: "mongo"
      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
    
    

    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