skip to Main Content

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

4

Answers


  1. For what I know (and I use), it isn’t possible to use port forwarding to connect mongo replica set with Compass (you can not use a local port twice or more times…)
    To me that because compass use the topology that replica set use. So you need to be compliant of that topology from your client computer.

    So you can expose the mongo pods with Cluster IP, manage your hosts file to match kubernetes nodes.

    And use standard connection string like you are in kubernetes :
    enter image description here

    UPDATE :
    You can configure mongo helm to do cluster https://artifacthub.io/packages/helm/bitnami/mongodb
    enter image description here

    And you hosts file :

    external_IP1 mongodb-0.mongodb-headless.mongodb.svc.cluster.local

    external_IP2 mongodb-1.mongodb-headless.mongodb.svc.cluster.local

    Login or Signup to reply.
  2. You can create 3x nodePort services for the task , example exposure for mongodb-0 pod:

    kind: Service 
    apiVersion: v1 
    metadata:
      name: mongodb-0
    spec:
      type: NodePort
      selector:
        statefulset.kubernetes.io/pod-name: mongodb-0
      ports:
        - nodePort: 30117
          port: 27017
          targetPort: 27017
          protocol: TCP
    

    And you can connect with COMPASS via the k8s worker nodes exposed nodePort’s as follow:

      connect: mongodb://workernode:30117
    

    assuming that you have created 3x nodePort’s -> 30117,30118,30119 for the three pods and your bitnami helmchart is using statefulset to manage the pods so the pod names are: mongodb-0,1,2

    Login or Signup to reply.
  3.  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

    Login or Signup to reply.
  4. It’s work with port forward and compass with some configuration in compass, using advence options with Direct Connection :
    enter image description here
    And don’t add replicat set option.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search