<p>I am using a statefulset to  deploy mognodb to kubernetes.</p>
<p>I have two pods called:</p>
<p>mongo-replica-0.mongo:27017 and mongo-replica-1.mongo:27017 (.mongo is added because of the kube service)</p>
<p>I am running this command from a kube job after the pods are started</p>
<pre class="lang-bash prettyprint-override"><code>mongo &quot;mongodb://mongo-replica-0.mongo:27017&quot; -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --eval &quot;rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'mongo-replica-0.mongo:27017' }, { _id: 1, host: 'mongo-replica-1.mongo:27017' },] })&quot;
</code></pre>
<p>I receive this error:</p>
<blockquote>
<p>&quot;errmsg&quot; : &quot;The hosts mongo-replica-0.mongo:27017 and mongo-replica-1.mongo:27017 all map to this node in new configuration with {version: 1, term: 0} for replica set rs0</p>
</blockquote>
<p>How can I initiate my replicaset?</p>

Brandon Kauffman
<p>I needed to set the service's IP to null and session affinity to null to make the service headless. When mongo tried to intercommunicate with sthe service originally, it saw the service IP and thought it was referencing itself. After the updates it succeeded.</p>
<p>Terraform setting:</p>
<pre><code>resource &quot;kubernetes_service&quot; &quot;mongodb-service&quot; {
  metadata {
    name      = &quot;mongo&quot;
    namespace = kubernetes_namespace.atlas-project.id
    labels = {
      &quot;name&quot; = &quot;mongo&quot;
    }
  }
  spec {

    selector = {
      app = &quot;mongo&quot;
    }
    cluster_ip       = null
    session_affinity = null
    port {
      port        = 27017
      target_port = 27017
    }

    type = &quot;LoadBalancer&quot;
  }
  lifecycle {
    ignore_changes = [spec[0].external_ips]
  }
}
</code></pre>
