title: Unable to create Mongo Replication Set in Kubernetes
body: <p>I want to create a 3 node Mongo Replica set in Kubernetes. I have created a headless service as below </p>
  
  <pre><code>apiVersion: v1
  kind: Service
  metadata:
    name: mongo
    labels:
      name: mongo
  spec:
    ports:
    - port: 27017
      targetPort: 27017
    clusterIP: None
    selector:
      role: mongo
  </code></pre>
  
  <p>I have also created a 3 node stateful set as below -</p>
  
  <pre><code>apiVersion: apps/v1beta1
  kind: StatefulSet
  metadata:
    name: mongo
  spec:
    serviceName: "mongo"
    replicas: 3
    template:
      metadata:
        labels:
          role: mongo
          environment: test
      spec:
        terminationGracePeriodSeconds: 10
        containers:
          - name: mongo
            image: mongo
            command:
              - mongod
              - "--replSet"
              - rs0
              - "--smallfiles"
              - "--noprealloc"
            ports:
              - containerPort: 27017
            volumeMounts:
              - name: mongo-persistent-storage
                mountPath: /data/db
            resources:
              limits:
                cpu: 500m
                memory: 512Mi
              requests:
                cpu: 400m
                memory: 256Mi
    volumeClaimTemplates:
    - metadata:
        name: mongo-persistent-storage
        persistentVolumeClaim:
          claimName: fast
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
          requests:
            storage: 10Gi
  </code></pre>
  
  <p>I have created the stateful set and the pods are up and running. Now if i login to one of the containers and set the config for the replication set in the mongo shell, I am getting error. The commands I enter are -</p>
  
  <pre><code>&gt; config = {
  ... "_id" : "rs0",
  ... "members" : [
  ...   {
  ...     _id: 1,
  ...     host: 'mongo-0.mongo.demo.svc.cluster.local:27017'
  ...   },
  ...   {
  ...     _id: 2,
  ...     host: 'mongo-1.mongo.demo.svc.cluster.local:27017',
  ...   },
  ...   {
  ...     _id: 3,
  ...     host: 'mongo-2.mongo.demo.svc.cluster.local:27017'
  ...   }
  ... ]
  ... }
  
  &gt; rs.initiate(config)
  </code></pre>
  
  <p>When I do the following, I get the below error - </p>
  
  <pre><code>"errmsg" : "replSetInitiate quorum check failed because not all proposed set members responded affirmatively: mongo-1.mongo.demo.svc.cluster.local:27017 failed with Connection refused, mongo-2.mongo.demo.svc.cluster.local:27017 failed with Connection refused"
  "code" : 74,
  "codeName" : "NodeNotFound",
  </code></pre>
  
  <p>I dont know how to debug this, because the containers are up and running. Can somebody help me with this? Thanks</p>