1

Good morning, I deployed a MongoDB ReplicaSet cluster (3 nodes, 1 primary, 2 secondary) on Kubernetes using the following guide to do it:

Guide

Deploy it was successfully and I can connect by mongo shell to my remote MongoDB Cluster by user/pass, (each node of my MongoDB cluster has a PublicIP to connect it). I have created a DB and I have inserted data into it to check Replication and it works fine.

The problem comes when I try to connect to MongoDB cluster with a Java Application. Java code:

MongoCredential credential = 
MongoCredential.createCredential("user", 
"dbs","password".toCharArray());
Builder mongoBuilder = MongoClientOptions.builder();
MongoClientOptions options = mongoBuilder.build();

return new MongoClient(Arrays.asList(new ServerAddress("ip1",27017),
new ServerAddress("ip2", 27017),
new ServerAddress("ip3", 27017)),credential, options);

This solutions throws an Java connection error as follows:

[ERROR] - Exception thrown in MongoDBManagement.mongoDBManagement: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@102a1baf. Client view of cluster state is {type=REPLICA_SET, servers=[{address=mongod-1.mongodb-service.default.svc.cluster.local:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongod-1.mongodb-service.default.svc.cluster.local}, caused by {java.net.UnknownHostException: mongod-1.mongodb-service.default.svc.cluster.local}}, {address=mongod-2.mongodb-service.default.svc.cluster.local:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongod-2.mongodb-service.default.svc.cluster.local}, caused by {java.net.UnknownHostException: mongod-2.mongodb-service.default.svc.cluster.local}}, {address=mongod-0.mongodb-service.default.svc.cluster.local:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: mongod-0.mongodb-service.default.svc.cluster.local}, caused by {java.net.UnknownHostException: mongod-0.mongodb-service.default.svc.cluster.local}}]; nested exception is com.mongodb.MongoTimeoutException:

Solved

I have found the problem, when you deploy a repSet, you must launch this command:

rs.initiate({_id: "MainRepSet", version: 1, members: [
       { _id: 0, host : "ip1:27017" },
       { _id: 1, host : "ip2:27017" }
       { _id: 2, host : "ip3:27017" }
]})

And I launched this command with the following hostanmes:

mongod-0.mongodb-service.default.svc.cluster.local;
mongod-1.mongodb-service.default.svc.cluster.local;
mongod-2.mongodb-service.default.svc.cluster.local

When I changed this by my publicIPs works fine.

Thanks

  • I see you using `ip1`, `ip2` and so on as ip's on your code. My guess is you can't do that unless you're spinning everything up (mongo and app) from the same docker-compose file. Otherwise, use a valid IP, or localhost. – cristianoms Feb 26 '20 at 12:37
  • I am using ip1, ip2, and ip3 instead of real ips to post on stack overflow, but on real code we have the valid IPs. – Juan Carlos Martin Feb 26 '20 at 13:04
  • The exception is an unknown host exception. It cannot find that host. Can your pods access the mongodb nodes? – Burak Serdar Feb 26 '20 at 14:43
  • Have you clarified on which namespace your mongodb service shall be created? – Soroosh Sarabadani Feb 26 '20 at 15:59
  • Hi @BurakSerdar yes, they can make ping between them. – Juan Carlos Martin Feb 26 '20 at 16:25
  • I also see a mongo timeout exception. Can you curl to hostname:27017 from your pod? Maybe the port is not open, or not accessible. – Burak Serdar Feb 26 '20 at 16:27
  • Hi @SorooshSarabadani all services are on namespace default, bu the cluster pods are on the same namespace. Thanks – Juan Carlos Martin Feb 26 '20 at 16:28
  • Hi @BurakSerdar yes, I can make a curl to the other nodes to port 27017 and says: "It looks like you are trying to access MongoDB over HTTP on the native driver port", so it is open. Thanks – Juan Carlos Martin Feb 26 '20 at 16:31
  • You can curl this from the pod, right, not from outside? If that's the case the exception doesn't make much sense. This is where it is trying to connect: mongod-2.mongodb-service.default.svc.cluster.local:27017 – Burak Serdar Feb 26 '20 at 16:34
  • HI @BurakSerdar I found the problem thanks to your tips, thank u so much. Kind regards – Juan Carlos Martin Feb 27 '20 at 12:19

0 Answers0