<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>You can actually do this in the way that you initially were expecting to.</p>

<p>Thanks to this answer for helping me get there - <a href="https://stackoverflow.com/a/33888424/370364">https://stackoverflow.com/a/33888424/370364</a></p>

<p>You can use the following approach to shove hosts into your container's /etc/hosts file</p>

<pre><code>command: ["/bin/sh","-c"]
args: ["echo '192.168.200.200 node1.example.com' &gt;&gt; /etc/hosts &amp;&amp; commandX"] 
</code></pre>

<p>If you want to dynamically set the ip at pod start time you can create a pod from stdin and pass it through sed to perform substitution before passing it to kubectl.</p>

<p>So the pod yaml would look like the following</p>

<pre><code>command: ["/bin/sh","-c"]
args: ["echo 'NODE_1_IP node1.example.com' &gt;&gt; /etc/hosts &amp;&amp; commandX"] 
</code></pre>

<p>Then execute it with</p>

<pre><code>cat pod1.yaml | sed -- "s|NODE_1_IP|${node1ip}|" | kubectl create -f -
</code></pre>

<p>I realise that this is not way that kubernetes intended for this kind of thing to be achieved but we are using this for starting up a test pod locally and we need to point it at the default network device on the local machine. Creating a service just to satisfy the test pod seems like overkill. So we do this instead.</p>
