<p>How can we auto-update (<em>delete, create, change</em>) entries in <code>/etc/hosts</code> file of running Pod without actually entering the pod?</p>

<p>We working on containerisation of <em>SAP</em> application server and so far succeeded in achieving this using <em>Kubernetes</em>.</p>

<pre><code>apiVersion: v1
kind: Pod
spec:
  hostNetwork: true
</code></pre>

<p>Since we are using host network approach, all entries of our VMs <code>/etc/hosts</code> file are getting copied whenever a new pod is created.</p>

<p>However, once pod has been created and in running state, any changes to VMs <code>/etc/hosts</code> file are not getting transferred to already running pod.</p>

<p>We would like to achieve this for our project requirement.</p>

<p>Kubernetes does have several different ways of affecting name resolution, your request is most similar to <a href="https://kubernetes.io/docs/concepts/services-networking/add-entries-to-pod-etc-hosts-with-host-aliases/" rel="nofollow noreferrer">here</a> and related pages.</p>
<p>Here is an extract, emphasis mine.</p>
<blockquote>
<p>Adding entries to a Pod’s /etc/hosts file provides Pod-level override of hostname resolution when DNS and other options are not applicable. In 1.7, users can add these custom entries with the HostAliases field in PodSpec.</p>
<p><strong>Modification not using HostAliases is not suggested because the file is managed by Kubelet and can be overwritten on during Pod creation/restart.</strong></p>
</blockquote>
<p>An example Pod specification using <code>HostAliases</code> is as follows:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: &quot;127.0.0.1&quot;
    hostnames:
    - &quot;foo.local&quot;
    - &quot;bar.local&quot;
  - ip: &quot;10.1.2.3&quot;
    hostnames:
    - &quot;foo.remote&quot;
    - &quot;bar.remote&quot;
  containers:
  - name: cat-hosts
    image: busybox
    command:
    - cat
    args:
    - &quot;/etc/hosts&quot;
</code></pre>
<p>One issue here is that you will need to update and restart the Pods with a new set of <code>HostAliases</code> if your network IPs change. That might cause downtime in your system.</p>
<p>Are you sure you need this mechanism and not <a href="https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors" rel="nofollow noreferrer">a service that points to an external endpoint</a>?</p>
