<p>In this application, nodejs pods are running inside kubernetes, and mongodb itself sitting outside at host as localhost.</p>
<p>This indeed not good design, but its only for dev environment. In production a separte mongodb server will be there, as such option to have a non loopback ip in endpoint, so will not be a problem in Production.</p>
<p>Have considered following options for dev environment</p>
<ol>
<li><p>Use localhost connect string to connect to mongodb, but it will refer to  pod's own localhost not host's localhost</p>
</li>
<li><p>Use headless service and provide localhost ip and port in endpoint. However endpoint doesn't allow loopback</p>
</li>
</ol>
<p>Suggest if there is a way to access mongodb database at host's localhost from inside cluster (pod /  nodejs application).</p>

<p>I'm running on docker for windows, and for me just using <code>host.docker.internal</code> instead of <code>localhost</code> seems to work fine.</p>
<p>For example, my mongodb connection string looks like this:</p>
<pre><code>mongodb://host.docker.internal:27017/mydb
</code></pre>
<p>As an aside, my <code>hosts</code> file includes the following lines (which I didn't add, I guess the <code>docker desktop</code> installation did that):</p>
<pre><code># Added by Docker Desktop
192.168.1.164 host.docker.internal
192.168.1.164 gateway.docker.internal
</code></pre>
