<p>I want to create MongoDB replica of the three machines, it needs to specify the IP-addresses of these machines? But they run into a pod's and have a dynamic IP. If you try to specify the DNS-name service MongoDB he says</p>

<blockquote>
  <p>No host described in new configuration XXXXX for replica set
  app_replica maps to this node</p>
</blockquote>

<p>How to configure MongoDB replica for c k8s?</p>

<p>I use DNS-addons for k8s.
And I try to initialize the cluster as follows:</p>

<pre><code>var config = {
    "_id" : "app_replica",
    "members" : [
        {
            "_id" : 0,
            "host" : "mongodb-node-01"
        },
        {
            "_id" : 1,
            "host" : "mongodb-node-02"
        },
        {
            "_id" : 2,
            "host" : "mongodb-node-03",
            "arbiterOnly" : true
        }
    ]
}

rs.initiate(config)
</code></pre>

<p><strong>Config Service:</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: "mongodb-node-01"
  labels:
    app: "mongodb-node-01"
spec:
  ports:
  - port: 27017
    targetPort: 27001
  selector:
    app: "mongodb-node-01"
</code></pre>

<p><strong>Config Replication Controller:</strong></p>

<pre><code>apiVersion: v1
kind: ReplicationController
metadata:
  name: "mongodb-node-01"
  labels:
    app: "mongodb-node-01"
spec:
  replicas: 1
  selector:
    app: "mongodb-node-01"
  template:
    metadata:
      labels:
        app: "mongodb-node-01"
    spec:
      containers:
      - name: "mongodb-node-01"
        image: 192.168.0.139:5000/db/mongo
        command:
          - mongod
          - "--replSet"
          - "app_replica"
          - "--smallfiles"
          - "--noprealloc"
        env:
        - name: ENV
          value: "prod"
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-persistent-storage
          mountPath: /data/db
          readOnly: false
      volumes:
      - name: mongo-persistent-storage
        hostPath:
          path: /data/mongo/mongodb-node-01
      nodeSelector:
        database: "true"
        mongodb01: "true"
</code></pre>

<p>You need to setup multiple deployments and services. Take a look at this zookeeper example - <a href="https://gist.github.com/bprashanth/8160d0cf1469b4b125af95f697433934" rel="nofollow">https://gist.github.com/bprashanth/8160d0cf1469b4b125af95f697433934</a></p>

<p>You do not rely on node/machine IPs. Instead you rely on stable DNS names of multiple services.</p>
