How to auto update /etc/hosts file entries inside running Pod without entering the pod

2/22/2019

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

We working on containerisation of SAP application server and so far succeeded in achieving this using Kubernetes.

apiVersion: v1
kind: Pod
spec:
  hostNetwork: true

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

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

We would like to achieve this for our project requirement.

-- Jayesh
kubernetes
sap

1 Answer

2/22/2019

Kubernetes does have several different ways of affecting name resolution, your request is most similar to here and related pages.

Here is an extract, emphasis mine.

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.

Modification not using HostAliases is not suggested because the file is managed by Kubelet and can be overwritten on during Pod creation/restart.

An example Pod specification using HostAliases is as follows:

apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "127.0.0.1"
    hostnames:
    - "foo.local"
    - "bar.local"
  - ip: "10.1.2.3"
    hostnames:
    - "foo.remote"
    - "bar.remote"
  containers:
  - name: cat-hosts
    image: busybox
    command:
    - cat
    args:
    - "/etc/hosts"

One issue here is that you will need to update and restart the Pods with a new set of HostAliases if your network IPs change. That might cause downtime in your system.

Are you sure you need this mechanism and not a service that points to an external endpoint?

-- Paul Annetts
Source: StackOverflow