Unable to access mongodb-replicaset with local pymongo client

7/28/2021

I have deployed mongodb on Kubernetes using mongodb-operator (verison: 1.9.2). The ReplicaSet consists of 3 instances: 1 primary and 2 secondary. I am able to access the ReplicaSet from any of given Kubernetes pods but if I try to do the same from the local machine with pymongo client I am getting error as shown below. Can anyone suggest the reason for such behavior?

pymongo.errors.ServerSelectionTimeoutError: mongodb-2.mongodb-svc.mongodb-new.svc.cluster.local:27017: [Errno -2] Name or service not known,mongodb-0.mongodb-svc.mongodb-new.svc.cluster.local:27017: [Errno -2] Name or service not known,mongodb-1.mongodb-svc.mongodb-new.svc.cluster.local:27017: [Errno -2] Name or service not known, Timeout: 30s, Topology Description: <TopologyDescription id: 610292f511a5060cc91f8a11, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mongodb-0.mongodb-svc.mongodb-new.svc.cluster.local', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mongodb-0.mongodb-svc.mongodb-new.svc.cluster.local:27017: [Errno -2] Name or service not known',)>, <ServerDescription ('mongodb-1.mongodb-svc.mongodb-new.svc.cluster.local', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mongodb-1.mongodb-svc.mongodb-new.svc.cluster.local:27017: [Errno -2] Name or service not known',)>, <ServerDescription ('mongodb-2.mongodb-svc.mongodb-new.svc.cluster.local', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mongodb-2.mongodb-svc.mongodb-new.svc.cluster.local:27017: [Errno -2] Name or service not known',)>]>
-- steve steel
kubernetes
mongodb
pymongo

1 Answer

8/12/2021

I have posted Community wiki answer for better visibility.

As steve steel has mentioned in the comment, he has found the solution in this link:

I have found this link helpful: https://stackoverflow.com/questions/44730112/exposing-mongodb-on-kubernetes-statefulsets-to-external-world

-- Mikołaj Głodziak
Source: StackOverflow