<p>I have a k8s dev cluster (in aks). I previously used the bitnami chart for deployment of a single MongoDB pod and I never had the following problem.</p>
<p>As I'm now using a feature that only works with replicaSets, I recently moved to using the MongoDBCommunity Operator to create a single instance MongoDB replicaSet running in its own namespace (in dev). My application is connecting to and using this instance with no issues at all and my new feature works like a dream.</p>
<pre class="lang-bash prettyprint-override"><code>$ k get pods -n db
NAME                                          READY   STATUS    RESTARTS   AGE
mongodb-kubernetes-operator-748c85557-bffz5   1/1     Running   0          2d16h
nga-mongodb-0                                 2/2     Running   0          2d19h
</code></pre>
<p>Now I want to use the MongoDB extension in vscode to help with debugging, and that's where the problem is. As I mentioned, with the bitnami chart I had no problem -- I provided the db connection string and it just worked.</p>
<p>When I try to do the same with this new deployment though I get the following error:</p>
<pre class="lang-bash prettyprint-override"><code>getaddrinfo ENOTFOUND nga-mongodb-0.nga-mongodb-svc.db.svc.cluster.local
</code></pre>
<p>The operator creates a secret containing the connection string in 2 forms, for the service and for the pod, both give the same error</p>
<p>Svc:</p>
<pre class="lang-bash prettyprint-override"><code>mongodb+srv://username:password@nga-mongodb-svc.db.svc.cluster.local/admin?ssl=false
</code></pre>
<p>Pod:</p>
<pre class="lang-bash prettyprint-override"><code>mongodb://username:password@nga-mongodb-0.nga-mongodb-svc.db.svc.cluster.local:27017/admin?ssl=false
</code></pre>
<p>I have noticed that neither of those strings includes a replica name, which I find odd, but I use the svc based string for my application and it works fine so it should work ok when port-forwarding.</p>
<p>My port-forwarding statement is pretty basic:</p>
<pre class="lang-bash prettyprint-override"><code>k port-forward -n db nga-mongodb-0 27017:27017
</code></pre>
<p>And if I try <code>curl localhost:27017</code> I get the expected message</p>
<pre class="lang-bash prettyprint-override"><code>It looks like you are trying to access MongoDB over HTTP on the native driver port.
</code></pre>
<p>Which does suggest that I have a working port forwarder.</p>
<p>The connection string I use (assuming the pod connection string) then changes to:</p>
<pre class="lang-bash prettyprint-override"><code>mongodb://username:password@localhost:27017/admin?ssl=false
</code></pre>
<p>I suspect, but don't know, that the replica set is trying to forward the client to the primary (which I already connect to if I use the pod connection string) and is sending a kubernetes URL which doesn't translate well via the port-forwarding method. So, I doubt that this is something weird the operator has done.</p>
<p>Has anyone else actually got this working or know how to get it working?</p>

Software Engineer
<p>So, based on writing up the question and therefore organising my thoughts, I have found the reason this is happening and the answer.</p>
<p>I had suggested in my question that this might be a redirect problem, and now I have proof that this is the issue. I've added the following entry to my hosts file:</p>
<pre><code>127.0.0.1       nga-mongodb-0.nga-mongodb-svc.db.svc.cluster.local
</code></pre>
<p>And now it works. So, the MongoDB replicaSet is trying to forward me to the primary (even though I'm already connected to the primary) and is doing so by, quite rightly, returning the only host it knows. By adding that hostname to the <code>/etc/hosts</code> file my machine resolves the name to the localhost and back through the forwarded connection. I haven't actually tried this with the svc approach, but I doubt it would work as I believe it'll constantly redirect.</p>
