<p>I am following the following tutorial to configure MongoDB as a stateful set within my Kubernetes cluster on GCP.</p>
<p><a href="https://codelabs.developers.google.com/codelabs/cloud-mongodb-statefulset/index.html?index=..%2F..index#0" rel="nofollow noreferrer">https://codelabs.developers.google.com/codelabs/cloud-mongodb-statefulset/index.html?index=..%2F..index#0</a></p>
<p>I am able to access the database using &quot;kubectl exec -ti mongo-0 mongo&quot; as shown in the tutorial.
However, my Node JS- Mongoose Application is unable to connect to it throwing the following error</p>
<pre><code>MongoDB connection error: { MongooseError [MongooseServerSelectionError]: connect ECONNREFUSED 10.16.0.22:27017
    at new MongooseServerSelectionError (/usr/src/app/node_modules/mongoose/lib/error/serverSelection.js:24:11)
    at NativeConnection.Connection.openUri (/usr/src/app/node_modules/mongoose/lib/connection.js:823:32)
    at Mongoose.connect (/usr/src/app/node_modules/mongoose/lib/index.js:333:15)
    at Object.&lt;anonymous&gt; (/usr/src/app/app.js:6:10)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
    at internal/main/run_main_module.js:21:11
  message: 'connect ECONNREFUSED 10.16.0.22:27017',
  name: 'MongooseServerSelectionError',
  reason:
   TopologyDescription {
     type: 'ReplicaSetNoPrimary',
     setName: null,
     maxSetVersion: null,
     maxElectionId: null,
     servers:
      Map {
        'mongo-0.mongo:27017' =&gt; [ServerDescription],
        'mongo-1.mongo:27017' =&gt; [ServerDescription],
        'mongo-2.mongo:27017' =&gt; [ServerDescription] },
     stale: false,
     compatible: true,
     compatibilityError: null,
     logicalSessionTimeoutMinutes: null,
     heartbeatFrequencyMS: 10000,
     localThresholdMS: 15,
     commonWireVersion: null },
  [Symbol(mongoErrorContextSymbol)]: {} }
(node:29) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 10.16.0.22:27017
    at new MongooseServerSelectionError (/usr/src/app/node_modules/mongoose/lib/error/serverSelection.js:24:11)
    at NativeConnection.Connection.openUri (/usr/src/app/node_modules/mongoose/lib/connection.js:823:32)
    at Mongoose.connect (/usr/src/app/node_modules/mongoose/lib/index.js:333:15)
    at Object.&lt;anonymous&gt; (/usr/src/app/app.js:6:10)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)
    at internal/main/run_main_module.js:21:11
(node:29) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:29) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

</code></pre>
<p>app.js connection code is as follows</p>
<pre><code>mongoose.connect(
  process.env.MONGO_CONNECTION_STRING,
  {
    useUnifiedTopology: true,
    useNewUrlParser: true,
  }
);
mongoose.Promise = global.Promise;
let db = mongoose.connection;
db.on(&quot;error&quot;, console.error.bind(console, &quot;MongoDB connection error:&quot;));
</code></pre>
<p>And Environment variable is as follows in deployment file.</p>
<pre><code>env:
  - name: MONGO_CONNECTION_STRING
    value: &quot;mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo:27017/test&quot;

</code></pre>
<p>Mongo pods status</p>
<pre><code>mongo-0                                         2/2     Running     0          8m35s
mongo-1                                         2/2     Running     0          7m49s
mongo-2                                         2/2     Running     0          6m54s
</code></pre>
<p>kubectl get statefulset</p>
<pre><code>NAME     READY   AGE
mongo    3/3     9m31s
</code></pre>
<p>Service</p>
<pre><code>NAME                                  TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)                      AGE
mongo                                 ClusterIP      None           &lt;none&gt;          27017/TCP                    27m
</code></pre>
<p>rs.config() output</p>
<pre><code>{
    &quot;_id&quot; : &quot;rs0&quot;,
    &quot;version&quot; : 1,
    &quot;protocolVersion&quot; : NumberLong(1),
    &quot;members&quot; : [
        {
            &quot;_id&quot; : 0,
            &quot;host&quot; : &quot;mongo-0.mongo:27017&quot;,
            &quot;arbiterOnly&quot; : false,
            &quot;buildIndexes&quot; : true,
            &quot;hidden&quot; : false,
            &quot;priority&quot; : 1,
            &quot;tags&quot; : {
                
            },
            &quot;slaveDelay&quot; : NumberLong(0),
            &quot;votes&quot; : 1
        },
        {
            &quot;_id&quot; : 1,
            &quot;host&quot; : &quot;mongo-1.mongo:27017&quot;,
            &quot;arbiterOnly&quot; : false,
            &quot;buildIndexes&quot; : true,
            &quot;hidden&quot; : false,
            &quot;priority&quot; : 1,
            &quot;tags&quot; : {
                
            },
            &quot;slaveDelay&quot; : NumberLong(0),
            &quot;votes&quot; : 1
        },
        {
            &quot;_id&quot; : 2,
            &quot;host&quot; : &quot;mongo-2.mongo:27017&quot;,
            &quot;arbiterOnly&quot; : false,
            &quot;buildIndexes&quot; : true,
            &quot;hidden&quot; : false,
            &quot;priority&quot; : 1,
            &quot;tags&quot; : {
                
            },
            &quot;slaveDelay&quot; : NumberLong(0),
            &quot;votes&quot; : 1
        }
    ],
    &quot;settings&quot; : {
        &quot;chainingAllowed&quot; : true,
        &quot;heartbeatIntervalMillis&quot; : 2000,
        &quot;heartbeatTimeoutSecs&quot; : 10,
        &quot;electionTimeoutMillis&quot; : 10000,
        &quot;catchUpTimeoutMillis&quot; : 60000,
        &quot;getLastErrorModes&quot; : {
            
        },
        &quot;getLastErrorDefaults&quot; : {
            &quot;w&quot; : 1,
            &quot;wtimeout&quot; : 0
        },
        &quot;replicaSetId&quot; : ObjectId(&quot;5f16a0f3671c091fe183af72&quot;)
    }
}

</code></pre>
<p>Any help is appreciated.</p>

Zain Saleem
<p>Figured it out</p>
<p>Tutorial has a step missing to initiate the set.</p>
<p>After connecting to set using the command</p>
<pre><code>kubectl exec -ti mongo-0 mongo
</code></pre>
<p>Just Run following two commands</p>
<pre><code>rs.initiate({_id: &quot;rs0&quot;, version: 1, members: [
       { _id: 0, host : &quot;mongo-0.mongo:27017&quot; },
       { _id: 1, host : &quot;mongo-1.mongo:27017&quot; },
       { _id: 2, host : &quot;mongo-2.mongo:27017&quot; }
 ]});

rs.slaveOk()
</code></pre>
