<p>Can anyone point out how to connect to the mongo db instance using mongo client using either command line client or from .net core programs with connection strings?</p>

<p>We have created a sample cluster in digitalocean with a namespace, let's say <em>mongodatabase</em>. </p>

<p>We installed the mongo statefulset with 3 replicas. We are able to successfully connect with the below command
 kubectl --kubeconfig=configfile.yaml -n <em>mongodatabase</em> exec -ti mongo-0 mongo
But when we connect from a different namespace or from default namespace with the pod names in the below format, it doesn't work.</p>

<pre><code> kubectl --kubeconfig=configfile.yaml  exec -ti mongo-0.mongo.mongodatabase.cluster.svc.local mongo
</code></pre>

<p>where <em>mongo-0.mongo.mongodatabase.cluster.svc.local</em> is in <em>pod-0.service_name.namespace.cluster.svc.local</em> (also tried pod-0.statfulset_name.namespace.cluster.svc.local and pod-0.service_name.statefulsetname.namespace.cluster.svc.local) etc.,</p>

<p>Can any one help with the correct dns name/connection string to be used while connecting with mongo client in command line and also from the programs like java/.net core etc.,?</p>

<p>Also should we use kubernetes deployment instead of statefulsets here?</p>

Muthu
<p>You need to reference the mongo service by namespaced dns.  So if your mongo service is <code>mymongoapp</code> and it is deployed in <code>mymongonamespace</code>, you should be able to access it as <code>mymongoapp.mymongonamespace</code>.</p>
<p>To test, I used the <code>bitnami/mongodb</code> docker client.  As follows:</p>
<p>From within <code>mymongonamespace</code>, this command works</p>
<pre><code>$ kubectl config set-context --current --namespace=mymongonamespace
$ kubectl run mongodbclient --rm --tty -i --image bitnami/mongodb --command -- mongo --host mymongoapp
</code></pre>
<p>But when I switched to namespace default it didn't work</p>
<pre><code>$ kubectl config set-context --current --namespace=default
$ kubectl run mongodbclient --rm --tty -i --image bitnami/mongodb --command -- mongo --host mymongoapp
</code></pre>
<p>Qualifying the host with the namespace then works</p>
<pre><code>$ kubectl run mongodbclient --rm --tty -i --image bitnami/mongodb --command -- mongo --host mymongoapp.mymongonamespace
</code></pre>
