Skip to content

Instantly share code, notes, and snippets.

@polarlights
Forked from superseb/defaultdns.md
Created July 10, 2021 15:17
Show Gist options
  • Save polarlights/ecb576c6292bb05cf674d6f20ed22e90 to your computer and use it in GitHub Desktop.
Save polarlights/ecb576c6292bb05cf674d6f20ed22e90 to your computer and use it in GitHub Desktop.
Change default DNS nameserver used by Kubernetes pods

Change default DNS nameserver used by Kubernetes pods

This can be applied generically but usually applies to Linux nodes that have a local caching nameserver running, which means pointing to an IP in the loopback range (127.0.0.0/8). Ubuntu 18.04 Bionic Beaver does this by default.

Option 1: Change host configuration

sudo systemctl mask systemd-resolved
rm -f /etc/resolv.conf
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

Option 2: Change default resolv.conf by adding kubelet parameter

The parameter will make sure that the kubelet will use a different file as /etc/resolv.conf.

From https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/:

--resolv-conf string
Resolver configuration file used as the basis for the container DNS resolution configuration. (default "/etc/resolv.conf")

You can create the cluster using the following snippet in the Edit as YAML under Cluster Options.

services:
  kubelet:
    extra_args:
      resolv-conf: /host/etc/mycustomresolv.conf

The referenced file must be present on the host filesystem (/etc is mounted in the kubelet under /host/etc):

echo "nameserver 8.8.8.8" > /etc/mycustomresolv.conf

Option 3: Configure kube-dns to use a different upstream using ConfigMap

Configure kube-dns to use an upstream nameserver instead of the one in /etc/resolv.conf:

Save in configmap.yml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: kube-dns
  namespace: kube-system
data:
  upstreamNameservers: |
    ["8.8.8.8"]
kubectl create -f configmap.yml

DNS troubleshooting

Retrieve nameserver kube-dns is using:

kubectl exec -ti -n kube-system $(kubectl get --no-headers=true pods -l k8s-app=kube-dns -o custom-columns=:metadata.name -n kube-system) -c kubedns -- cat /etc/resolv.conf

Host should have net.ipv4.ip_forward set to 1:

sysctl -w net.ipv4.ip_forward=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment