How to control /etc/hosts file from over writing

10/16/2018

I am deploying a Kops Kubernetes cluster on AWS with Debian Jessie image.

Mine is a hybrid environment where my artifactory is in a physical env in our DC. Now I have been facing an issue, my worker nodes are unable to pull images from my artifactory unless I specify the artifactory FQDN and IP in the /etc/hosts file.

So this is a manual edit, it works all fine after I do this. So I went ahead and added the data in my additional userdata of the Kops worker node group, but I am seeing after some time the hosts file on worker nodes is getting overwritten and also this is evident upon node reboot.

So how can I resolve this!!

-- Mohammed Ali
debian
debian-jessie
kubernetes
linux

2 Answers

10/16/2018

Another workaround for this is to put it in your /etc/rc.local file:

If the file exists add this to the end:

echo '<ip-address-of-artifactory> <fqdn-of-artifactory>' >> /etc/hosts

If the file doesn't exist, create it:

$ cat << EOF > /etc/rc.local
#!/bin/sh -e
#
echo '<ip-address-of-artifactory> <fqdn-of-artifactory>' >> /etc/hosts
EOF
$ chmod 755 /etc/rc.local
$ reboot # check that it works
-- Rico
Source: StackOverflow

10/16/2018

The real answer is to run your own DNS server, or at least use DNS hostnames to resolve. If your router supports it, you can set local hostnames (machine-1.local)

If that isn't possible, you could try a solution like puppet if you own the virtual machines. Also, I believe Kubernetes does have a DNS addon. Also, you could use a crontab for on boot to write to the hosts file, but that's a dirty solution.

In addition, your hosts file would get rewritten for every DHCP renew. You could use static IPs, but again, DNS is the way to go.

-- Eamonn Nugent
Source: StackOverflow