How to change the DNS servers within K3S CoreDNS

Photo by Growtika @ Unsplash
Darko Gjorgjijoski

Recently, we set up a custom DNS server based on dnsmasq within our organization to handle internal DNS requests. However, i noticed that our K3S pods were not recognizing the internal hosts defined with the custom DNS server. Initially, i assumed that Kubernetes would use the operating system’s DNS configuration specified in /etc/resolv.conf, but i discovered that this was not the case. Instead, K3S’s DNS service, CoreDNS, uses its own internal DNS servers.

I created a simple guide on how to configure K3S’s CoreDNS service to include the host’s nameservers defined in /etc/resolv.conf as well:

1. Adjust k3s configuration

Append kubelet-arg in k3s/config.yaml that tells k3s to load the host’s machine /etc/resolv.conf file:

echo 'kubelet-arg:' >> /etc/rancher/k3s/config.yaml
echo '- "resolv-conf=/etc/resolv.conf"' >> /etc/rancher/k3s/config.yaml

2. Restart k3s service

Restart the k3s service so the config file gets loaded

systemctl restart k3s

3. Re-create CoreDNS pods

Lastly, we need to kill the CoreDNS related pods so they will be recreated and will include the newly appended DNS server.

kubectl get pod -n kube-system -l k8s-app=kube-dns --no-headers | awk '{print $1}' | xargs -I{} kubectl delete pod -n kube-system {}

Leave the first comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Secured By miniOrange