<p>I have the following service... </p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mongo
  labels:
    app: mongo
spec:
  ports:
  - name: mongo
    port: 27017
  clusterIP: None
  selector:
    app: mongo
</code></pre>

<p>And the following stateful set... </p>

<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: mongo
  selector:
    matchLabels:
      app: mongo
  replicas: 3
  template:
    metadata:
      labels:
        app: mongo
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo
          image: mongo
          command:
            - mongod
            - "--replSet"
            - rs0
            - "--smallfiles"
            - "--noprealloc"
          ports:
            - name: mongo
              containerPort: 27017
        - name: mongo-sidecar
          image: cvallance/mongo-k8s-sidecar
          env:
            - name: "VERSION"
              value: "2"
            - name: MONGO_SIDECAR_POD_LABELS
              value: "role=mongo"
</code></pre>

<p>However, my code can't make a connection, using the url <code>mongo:27017</code>. I've tried connecting to <code>mongo</code>, <code>mongo-0.mongo:27017</code>, loads of others. If I exec into a container and run <code>$ nslookup mongo</code> I get... </p>

<pre><code>Name:      mongo
Address 1: 10.1.0.80 mongo-0.mongo.default.svc.cluster.local
Address 2: 10.1.0.81 mongo-1.mongo.default.svc.cluster.local
Address 3: 10.1.0.82 mongo-2.mongo.default.svc.cluster.local
</code></pre>

<p>Hitting <code>$ curl mongo:27017</code> or <code>$ telnet mongo 27017</code> gives me a connection refused error. </p>

<p>Add <code>bind_ip</code> to command:</p>

<pre><code>      command:
        - mongod
        - "--replSet"
        - rs0
        - "--smallfiles"
        - "--noprealloc"          
        - "--bind_ip"
        - "0.0.0.0"
</code></pre>

<p>This <a href="https://docs.mongodb.com/manual/reference/program/mongod/#core-options" rel="nofollow noreferrer">option</a> tells the monogodb daemon to listen on all IPv4 addresses, rather than the default of <code>localhost</code>.</p>
