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
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 :
UPDATE :
You can configure mongo helm to do cluster https://artifacthub.io/packages/helm/bitnami/mongodb
And you hosts file :
external_IP1 mongodb-0.mongodb-headless.mongodb.svc.cluster.local
external_IP2 mongodb-1.mongodb-headless.mongodb.svc.cluster.local
You can create 3x nodePort services for the task , example exposure for mongodb-0 pod:
And you can connect with COMPASS via the k8s worker nodes exposed nodePort’s as follow:
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
Just have recreated your setup. Everything works fine
result
port forward
compass
mongo-svc-sts.yaml
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
It’s work with port forward and compass with some configuration in compass, using advence options with Direct Connection :
And don’t add replicat set option.