I wrote a simple script in my docker ENTRYPOINT
to use dnsmasq
if [ ! -f /etc/resolv.dnsmasq ];then
cp /etc/resolv.conf /etc/resolv.dnsmasq
sed -i 's/^nameserver.*/nameserver 127.0.0.1/' /etc/resolv.conf
dnsmasq -r /etc/resolv.dnsmasq
else
dnsmasq -r /etc/resolv.dnsmasq
fi
My logic is simple, the first time k8s starts my docker contain, it will update nameserver
in /etc/resolv.conf
to the correct value and my script will copy it to /etc/resolv.dnsmasq
and change nameserver
in /etc/resolv.conf
to 127.0.0.1
to use dnsmasq service.
When docker restarts because /etc/resolv.dnsmasq
remains so dnsmasq
will just start. It works most of time but when k8s restarts container due to health check failure it will fail to work. When that happen my resolv.dnsmasq
has nameserver 127.0.0.1
,
/var/www/html # cat /etc/resolv.dnsmasq
nameserver 127.0.0.1
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:2 timeout:2
From the comment and answer I got so far I realize it was because when the container was recreated then I have a modified /etc/resolv.conf
and no /etc/resolv.dnsmasq
. so the then
part is run.
But why will I have nameserver 127.0.0.1
in /etc/resolv.conf
if it is recreated from the image ?
How can I deal with that and make dnsmasq work ?
--- update---
Through our investigation it seems that sometimes Kubernetes will kill the running docker container and start a new container from image, but with the pause container exists the modified /etc/resolve.conf
also exists. When that happens the restarted container does not have /etc/resolv.dnsmasq
but it shares the /etc/resolv.conf
that was already modified nameserver 127.0.0.1
I am not sure the whole logic behind the pause container. What is the use of a pause image in Kubernetes? said "The pause container holds the network namespace for the pod." So does that mean /etc/resolv.conf
remains in my case? Can someone help to answer it ?
/etc/resolv.conf
if it is recreated from the image ? – NabokovpostStart
hook in your Deployment? You would move yourENTRYPOINT
script there. It should fix your problem in k8s cluster but it requires to change Dockerfile. Let me know what you think and if you are planning to use this container standalone or only in cluster. – Abie