<p>My goal is to create a <code>StatefulSet</code> in the <code>production</code> namespace and the <code>staging</code> namespace. I am able to create the production StatefulSet however when deploying one to the staging namespace, I receive the error:</p>

<pre><code>failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
</code></pre>

<p>The YAML I am using for the staging setup is as so:</p>

<p><strong>staging-service.yml</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mongodb-staging
  namespace: staging
  labels:
    app: ethereumdb
    environment: staging
spec:
  ports:
  - name: http
    protocol: TCP
    port: 27017
    targetPort: 27017
  clusterIP: None
  selector:
    role: mongodb
    environment: staging
</code></pre>

<p><strong>staging-statefulset.yml</strong></p>

<pre><code>apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: mongodb-staging
  namespace: staging
  labels:
    app: ethereumdb
    environment: staging
  annotations:
        prometheus.io.scrape: "true"
spec:
  serviceName: "mongodb-staging"
  replicas: 1
  template:
    metadata:
      labels:
        role: mongodb
        environment: staging
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: role
                operator: In
                values:
                - mongo
              - key: environment
                operator: In
                values:
                - staging
            topologyKey: "kubernetes.io/hostname"
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo
          image: mongo
          command:
            - mongod
            - "--replSet"
            - rs0
            - "--smallfiles"
            - "--noprealloc"
            - "--bind_ip_all"
            - "--wiredTigerCacheSizeGB=0.5"
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: mongo-persistent-storage
              mountPath: /data/db
        - name: mongo-sidecar
          image: cvallance/mongo-k8s-sidecar
          env:
            - name: MONGO_SIDECAR_POD_LABELS
              value: "role=mongodb,environment=staging"
            - name: KUBERNETES_MONGO_SERVICE_NAME
              value: "mongodb-staging"
  volumeClaimTemplates:
  - metadata:
      name: mongo-persistent-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: fast-storage
      resources:
        requests:
          storage: 1Gi
</code></pre>

<p>The <code>production</code> namespace deployment differs only in:</p>

<ul>
<li><code>--replSet</code> value (<code>rs0</code> instead of <code>rs1</code>)</li>
<li>Use of the name 'production' to describe values</li>
</ul>

<p>Everything else remains identical in both deployments.</p>

<p>The only thing I can imagine is that it is not possible to run both these deployments on the port <code>27017</code> despite being in separate namespaces.</p>

<p>I am stuck as to what is causing the <code>failed to connect to server</code> error described above.</p>

<p><strong>Full log of the error</strong></p>

<pre><code>Error in workloop { MongoError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.&lt;anonymous&gt; (/opt/cvallance/mongo-k8s-sidecar/node_modules/mongodb-core/lib/topologies/server.js:336:35)
    at Pool.emit (events.js:182:13)
    at Connection.&lt;anonymous&gt; (/opt/cvallance/mongo-k8s-sidecar/node_modules/mongodb-core/lib/connection/pool.js:280:12)
    at Object.onceWrapper (events.js:273:13)
    at Connection.emit (events.js:182:13)
    at Socket.&lt;anonymous&gt; (/opt/cvallance/mongo-k8s-sidecar/node_modules/mongodb-core/lib/connection/connection.js:189:49)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:182:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
  name: 'MongoError',
  message:
   'failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]' }
</code></pre>

Nick
<p>It seems like the error you are getting is from the mongo-sidecar container in the pod. As for why the mongo container is failing, can you obtain more detailed information? It could be something like a failed PVC.</p>
