<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 that the problem is similar to:
<a href="https://stackoverflow.com/questions/46523321/mongoerror-connect-econnrefused-127-0-0-127017">mongodb-error</a> but still you have two database listen on the same port.</p>

<p>In the context of two mongoDB databases listening on the same port:</p>

<p>The answer differs depending on what OS is being considered. In general though:</p>

<ul>
<li>For TCP, no. You can only have one application listening on the same port at one time. Now if you had 2 network cards, you could have one application listen on the first IP and the second one on the second IP using the same port number.</li>
<li>For UDP (Multicasts), multiple applications can subscribe to the same port.</li>
</ul>

<p>But since Linux Kernel 3.9 and later, support for multiple applications listening to the same port was added using the SO_REUSEPORT option. More information is available at this lwn.net article.</p>

<p><strong>But there is workaround.</strong> </p>

<p>Run containers on different port and set up Apache or Nginx. As Apache/Nginx works on port 80, you'll not lose any traffic, as 80 is common port.</p>

<p>I recommend Nginx - I find it much easier to set up reverse proxy with Nginx and it's lighter on resource compared to Apache.
For nginx, you need to set up it and learn more about Server Blocks:
How To Install Nginx on Ubuntu 16.04.
How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04.
In Server Blocks, you need to use proxy_pass, for which you can learn more about on <a href="https://nginx.org/en/docs/" rel="nofollow noreferrer">nginx</a> site.</p>
