<p>I'm trying to orchestrate a MongoDB cluster inside Kubernetes, working with StatefulSets and Headless Services. It's been a while I'm trying to figure out what's wrong in this but can't find anything I could use.</p>

<p>While doing my research I found <a href="https://github.com/pkdone/gke-mongodb-shards-demo" rel="nofollow noreferrer">this repository</a> which I cloned and adapted to the use case. You can find one of the manifests (the one for the mongoS instance) <a href="https://pastebin.com/XsM02DvS" rel="nofollow noreferrer">right here</a>. </p>

<p>I then run the <code>generate.sh</code> script shipped within the repository, but with everything related to <code>gcloud</code> or <code>persistentVolume</code> commented, leaving only the <code>statefulSet</code>/<code>Service</code> spawning processes. Everything seems to be working fine, but whenever the script reaches the block </p>

<pre><code># Wait for the mongos to have started properly
echo "Waiting for the first mongos to come up (`date`)..."
echo " (IGNORE any reported not found &amp; connection errors)"
echo -n "  "
until kubectl --v=0 exec mongos-router-0 -c mongos-container -- mongo --quiet --eval 'db.getMongo()'; do
    sleep 2
    echo -n "  "
done
echo "...first mongos is now running (`date`)"
echo
</code></pre>

<p>It just gives this error message indifenitely:</p>

<pre><code>2019-03-11T14:02:03.531+0000 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:343:13
@(connect):1:21
exception: connect failed
command terminated with exit code 1
</code></pre>

<p>Thus I tried to run <code>mongo</code> from inside the pod directly, but the same error happened. Then I just tried to run <code>mongo</code> using different ports just inside the container, running <code>mongo --port 27018</code> (also tried 27019 and 27020), just to see which kind of errors it gives with non-exposed ports, and it seems that it gives the same error.</p>

<p>How could I run mongo inside a container with a running mongos on port 27017? It seems that I have to run <code>mongod</code> before running the <code>mongo</code> shell, even if <code>mongos</code> already started. It's really mind-boggling and I feel like I'm missing something. I only have this error inside the mongos pods, while the mongo-configdb and mongod-maindb seems to be running smoothly (and running the mongo shell inside them doesn't give any errors. Moreover, when doing <code>rs.status()</code> inside one of those it works fine).</p>

<p>You can find some <code>mongos</code> logs <a href="https://pastebin.com/6pHGaFac" rel="nofollow noreferrer">there</a>.</p>

<p><strong>TL;DR</strong> 
How could I run mongo inside a container with a running mongos on port 27017?</p>

<p><strong>N.B.:</strong> I'm using the image <strong>mongo:3.6</strong> from the official dockerhub repository.</p>

Yoiro
<p>I don't really understand why, but changing the running port from 27017 to 27020 worked fine. If someone passes by and can explain how, I'd be glad to hear his/her explanation as <code>netstat -tunelp</code> (as well a <code>ps aux</code>) while grepping port 27017 on both of my k8s nodes does not give any output related to port 27017...</p>
