<p>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.</p>

<p><strong>What I did.</strong></p>

<p>Use rancher to create an cluster with Amazon EKS.</p>

<p>Deployed a nodejs app in 'default' namespace.</p>

<p>Installed MongoDB replicaset from the rancher app catalog with default settings.</p>

<ul>
<li>Service/Deployment name is mongodb-replicaset</li>
<li>namespace is also mongodb-replicaset</li>
</ul>

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

<p>I get the error.</p>

<blockquote>
  <p>MongoNetworkError: failed to connect to server [mongodb-replicaset-:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb-replicaset mongodb-replicaset:27017]</p>
</blockquote>

<p>Then I read in <a href="https://kubernetes.io/docs/concepts/services-networking/service/#discovering-services" rel="nofollow noreferrer">kubernetes documentation</a> that to access a service in a different namespace you need to also specify the namespace along with the service name.</p>

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

<blockquote>
  <p>MongoError: no primary found in replicaset or invalid replica set name</p>
</blockquote>

<p>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.</p>

<p>To quote from <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id" rel="nofollow noreferrer">this document</a></p>

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

<p>so in your case, your pod DNS would be written as:</p>

<p><code>mongodb-replicaset.mongodb-replicaset.svc.cluster.local</code></p>
