Kubernetes: Cannot access mongodb replicaset service from a different namespace

8/30/2018

If there's something wrong with the way I phrased the question please tell, so I can be better next time or edit the question.

What I did.

Use rancher to create an cluster with Amazon EKS.

Deployed a nodejs app in 'default' namespace.

Installed MongoDB replicaset from the rancher app catalog with default settings.

  • Service/Deployment name is mongodb-replicaset
  • namespace is also mongodb-replicaset

When I use mongodb://mongodb-replicaset:27017/tradeit_system?replicaSet=rs as connection string.

I get the error.

MongoNetworkError: failed to connect to server [mongodb-replicaset-:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb-replicaset mongodb-replicaset:27017]

Then I read in kubernetes documentation that to access a service in a different namespace you need to also specify the namespace along with the service name.

So I did this mongodb://mongodb-replicaset.mongodb-replicaset:27017/tradeit_system?replicaSet=rss as the connection url I get the error.

MongoError: no primary found in replicaset or invalid replica set name

-- enzio902
kubernetes
rancher
replicaset

1 Answer

8/30/2018

So you have to include the namespace in the hoststring if if you want to access it as well as reference the cluster domain, which you aren't doing.

To quote from this document

The domain managed by this Service takes the form: $(service name).$(namespace).svc.cluster.local, where “cluster.local” is the cluster domain.

so in your case, your pod DNS would be written as:

mongodb-replicaset.mongodb-replicaset.svc.cluster.local

-- mstorkson
Source: StackOverflow