Connecting mongodb to app in kubernetes cluster

11/2/2021

I want to connect to mongo pod in k8s and the uri address is wrong. The address is

mongo "mongodb://eli:1234@calocalc-chart-mongodb-0.mongodb-headless.default.svc.cluster.local:27017/calocalcdb?replicaSet=rs0&authSource=admin"

Whatever i do it gives me this error:

root@mongo:/# mongo "mongodb://eli:1234@calocalc-chart-mongodb-0.mongodb-headless.default.svc.cluster.local:27017/calocalcdb?replicaSet=rs0&authSource=admin"
MongoDB shell version v5.0.3
connecting to: mongodb://calocalc-chart-mongodb-0.mongodb-headless.default.svc.cluster.local:27017/calocalcdb?authSource=admin&compressors=disabled&gssapiServiceName=mongodb&replicaSet=rs0
{"t":{"$date":"2021-11-02T10:15:00.806Z"},"s":"I",  "c":"NETWORK",  "id":4333208, "ctx":"ReplicaSetMonitor-TaskExecutor","msg":"RSM host selection timeout","attr":{"replicaSet":"rs0","error":"FailedToSatisfyReadPreference: Could not find host matching read preference { mode: \"nearest\" } for set rs0"}}
Error: Could not find host matching read preference { mode: "nearest" } for set rs0, rs0/calocalc-chart-mongodb-0.mongodb-headless.default.svc.cluster.local:27017 :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1

I also tried to add a username and password at the beginning of the URI and it gives the same results. The name of the pod is correct and i installed it via helm and disabled the arbiter. I have 3 mongo pods but i just want to connect to a single one right now and when it will work i will try to connect to the other 2 as well.

If any other info is required pls tell me and i will update this thread.

-- DlekaShelHaHaim
kubernetes
kubernetes-helm
mongodb
pymongo
python

0 Answers