<p>I hope everyone here is doing good. I am trying to find a way to add entries to the containers /etc/hosts file while spinning up a pod. I was just wondering to know if there is any option/parameter that I could mention in my "pod1.json" which adds the entries to the containers /etc/hosts when its being created. Something like "--add-host node1.example.com:${node1ip}" that serves the same purpose for docker as shown below.</p>

<pre><code>docker run \
--name mongo \
-v /home/core/mongo-files/data:/data/db \
-v /home/core/mongo-files:/opt/keyfile \
--hostname="node1.example.com" \
--add-host node1.example.com:${node1ip} \
--add-host node2.example.com:${node2ip} \
--add-host node3.example.com:${node3ip} \
-p 27017:27017 -d mongo:2.6.5 \
--smallfiles \
--keyFile /opt/keyfile/mongodb-keyfile \
--replSet "rs0"
</code></pre>

<p>Any pointers are highly appreciated. Thank you.</p>

<p>Regards,
Aj</p>

<p>Kubernetes uses the <a href="https://github.com/kubernetes/kubernetes/blob/master/docs/design/networking.md" rel="nofollow">IP-per-pod model</a>. If I understand correctly, you want to create three mongo pods, and write IP addresses of the three pods in <code>/etc/hosts</code> of each container. Modifying the <code>/etc/host</code> files directly might not be a good idea for many reasons (e.g., the pod may die and be replaced).</p>

<p>For peer discovery in kubernetes, you need to </p>

<ol>
<li>Find out the IP addresses of the peers.</li>
<li>Update your application with the addresses.</li>
</ol>

<p>(1) is achievable using <a href="https://github.com/kubernetes/kubernetes/blob/master/docs/user-guide/services.md#headless-services" rel="nofollow">Headless Service</a>. (2) requires your to write a sidecar container to run along side with your mongo containers, performs (1), and configures your application. The sidecar container is highly application-specific and you may want to read some <a href="https://stackoverflow.com/questions/30041699/can-i-call-rs-initiate-and-rs-add-from-node-js-using-the-mongodb-driver">related stackoverflow questions</a> about doing this for mongodb. </p>

<p>As for (1), you can create a <a href="https://github.com/kubernetes/kubernetes/blob/master/docs/user-guide/services.md#headless-services" rel="nofollow">Headless Service</a> by using this <a href="https://github.com/kubernetes/kubernetes/blob/master/examples/nodesjs-mongodb/mongo-service.yaml" rel="nofollow">service.yaml</a> with the clusterIP set to None.</p>

<pre><code>spec:
  clusterIP: None
</code></pre>

<p>Then, you can create a replication controller which creates the desired number of mongo pods. For example, you can use <a href="https://github.com/kubernetes/kubernetes/blob/master/examples/nodesjs-mongodb/mongo-controller.yaml" rel="nofollow">mongo-controller.yaml</a>, replaces the <code>gcePersistentDisk</code> with a desired local disk volume type (e.g. <a href="https://github.com/kubernetes/kubernetes/blob/master/docs/user-guide/volumes.md#emptydir" rel="nofollow"><code>emptyDir</code></a> or <a href="https://github.com/kubernetes/kubernetes/blob/master/docs/user-guide/volumes.md#hostpath" rel="nofollow"><code>hostPath</code></a>), and change the replica number to 3.</p>

<p>Each of the mongo pod will be assigned an IP address automatically, and is labeled with <code>name=mongo</code>. The headless service uses a label selector to find the pods. When querying DNS with the service name from a node or a container, it will return a list of IP addresses of the mongo pods.</p>

<p>E.g.,</p>

<pre><code>$ host mongo
mongo.default.svc.cluster.local has address 10.245.0.137
mongo.default.svc.cluster.local has address 10.245.3.80
mongo.default.svc.cluster.local has address 10.245.1.128   
</code></pre>

<p>You can get the addresses in the sidecar container you wrote and configure mongodb-specific accordingly.</p>
