<p>I have a MongoDB replica set running in Kubernetes (AWS EKS), created using Helm charts from Bitnami. The services are configured to be external facing and set to NodePort.</p>
<pre><code>mongo-mongodb-0-external         NodePort    10.100.83.252  27017:30030/TCP

mongo-mongodb-1-external         NodePort    10.100.15.184  27017:30031/TCP

mongo-mongodb-2-external         NodePort    10.100.90.128  27017:30032/TCP

mongo-mongodb-arbiter-headless   ClusterIP   None           27017/TCP

mongo-mongodb-headless           ClusterIP   None           27017/TCP
</code></pre>
<p>On my laptop, I can connect Mongo CLI to the replica but connection fails from MongoDB Compass and Studio 3T.</p>
<p>The following works from Mongo CLI from my laptop...</p>
<p><code>mongo 'mongodb://root:mypassword@k8s_node_ip:30030,k8s_node_ip:30031,k8s_node_ip:30032/mydb?authSource=admin'</code></p>
<p>...and this works...</p>
<p><code>mongo admin --host &quot;k8s_node_ip:30030,k8s_node_ip:30031,k8s_node_ip:30032&quot; --authenticationDatabase admin -u root -p mypassword</code></p>
<p>...but this fails in MongoDB Compass and Studio 3T...</p>
<p><code>mongodb://root:mypassword@k8s_node_ip:30030,k8s_node_ip:30031,k8s_node_ip:30032/mydb?authSource=admin</code></p>
<p>The error message is:</p>
<p><em>getaddrinfo ENOTFOUND mongo-mongodb-0.mongo-mongodb-headless.mynamespace.svc.cluster.local</em></p>
<p>Bizarrely, the following standalone connection works in Studio 3T:</p>
<p><code>mongodb://root:mypassword@k8s_node_ip:30030/?serverSelectionTimeoutMS=5000&amp;connectTimeoutMS=10000&amp;authSource=admin&amp;authMechanism=SCRAM-SHA-256</code></p>

Confounder
<p>When connecting to a replica set, the host:port pairs in the connection string are a seedlist.</p>
<p>The driver/client will attempt to connect to each host in the seedlist in turn until it gets a connection.</p>
<p>It runs the <code>isMaster</code> command to determine which node is primary, and to get a list of all replica set members.</p>
<p>Then is drops the original seedlist connection, and attempts to connect to each replica set member using the host and port information retrieved.</p>
<p>The host information returned by the <code>isMaster</code> usually matches the entry in <code>rs.conf()</code>, which are the hostnames used to initiate the replica set.</p>
<p>In your Kubernetes cluster, the nodes have internal hostnames that are used to initiate the replica set, but that your external clients can't resolve.</p>
<p>In order to get this to work, you will need to have the mongod nodes isMaster command return a different set of hostnames depending on where the client request is coming from.  This is similar to split-horizon DNS.</p>
<p>Look over the <a href="https://docs.mongodb.com/kubernetes-operator/master/tutorial/deploy-replica-set/#deploy-a-replica-set" rel="nofollow noreferrer">Deploy a Replica Set</a> documentation for mongodb/kubernetes, and the <a href="https://docs.mongodb.com/kubernetes-operator/master/reference/k8s-operator-specification/#spec.connectivity.replicaSetHorizons" rel="nofollow noreferrer">replicaSetHorizons</a> setting.</p>
