CoreDNS not working: how to fix DNS resolution in your cluster

Reproduced on k3s v1.31.5 (k3d) with kubectl v1.36.1 and CoreDNS 1.12.0. The OOM, conntrack and NodeLocal DNSCache cases were not reproduced in the lab.

What you see

$ kubectl exec dnsutils -- cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.43.0.10
options ndots:5

$ kubectl exec dnsutils -- nslookup kubernetes.default
Server:		10.43.0.10
Address:	10.43.0.10#53

Name:	kubernetes.default.svc.cluster.local
Address: 10.43.0.1

# same healthy cluster, busybox:1.36
$ kubectl exec client2 -- nslookup kubernetes.default
** server can't find kubernetes.default: NXDOMAIN

# CoreDNS scaled to zero
$ kubectl get endpoints kube-dns -n kube-system
NAME       ENDPOINTS   AGE
kube-dns   <none>      7m56s
$ kubectl exec dnsutils -- nslookup kubernetes.default
;; connection timed out; no servers could be reached

# typo in the Corefile: the running pod keeps the old config
$ kubectl logs -n kube-system -l k8s-app=kube-dns
[ERROR] plugin/reload: Corefile changed but reload failed: starting with listener file descriptors: /etc/coredns/Corefile:15 - Error during parsing: Unknown directive 'forwrd'

# after a restart
$ kubectl get pods -n kube-system -l k8s-app=kube-dns
NAME                       READY   STATUS             RESTARTS     AGE
coredns-7d49c878d7-x8zmh   0/1     CrashLoopBackOff   3 (5s ago)   60s
$ kubectl logs -n kube-system coredns-7d49c878d7-x8zmh --previous
/etc/coredns/Corefile:15 - Error during parsing: Unknown directive 'forwrd'

# upstream that does not answer
$ kubectl exec dnsutils -- nslookup example.com
** server can't find example.com: SERVFAIL
$ kubectl logs -n kube-system -l k8s-app=kube-dns
[ERROR] plugin/errors: 2 example.com. A: read udp 10.42.0.25:48516->10.255.255.1:53: i/o timeout

# egress NetworkPolicy that allows only TCP 443
$ kubectl exec dnsutils -- nslookup kubernetes.default
;; connection timed out; no servers could be reached

What it means

A pod with the default dnsPolicy, ClusterFirst, gets a /etc/resolv.conf that points at the cluster IP of the kube-dns Service in kube-system. That Service sends queries to the CoreDNS pods. CoreDNS answers cluster.local names from the Kubernetes API and forwards other names upstream, by default to the node's own resolver.

The resolv.conf also has a search list and options ndots:5. A name with fewer than five dots is tried with each search domain before it is tried as written. That is why a short name like web works inside its namespace. It also means one lookup of an outside name can turn into several queries.

The error tells you how far the query got. A timeout means no CoreDNS pod answered. NXDOMAIN means the name was not found with any search domain. SERVFAIL for outside names means CoreDNS could not get an answer from upstream.

Common causes

CoreDNS has no ready pods

The CoreDNS pods are scaled to zero, stuck in Pending or crashing. The kube-dns Service has no endpoints and every lookup in the cluster fails.

How to tell: kubectl get endpoints kube-dns -n kube-system shows <none>. nslookup from a pod says connection timed out and no servers could be reached.

A bad edit to the Corefile

A typo in the coredns ConfigMap stops CoreDNS from parsing it. Running pods keep their old config, because the reload plugin refuses the bad one. The next restart or rollout crashes every new pod.

How to tell: Running pods log plugin/reload: Corefile changed but reload failed. New pods go into CrashLoopBackOff and their previous logs show Error during parsing with the line number.

The test tool gives a false result

The nslookup in newer busybox images does not use the search list the way the pod's resolver does. In our lab busybox:1.36 returned NXDOMAIN for kubernetes.default on a healthy cluster. dnsutils and busybox:1.28 resolved it.

How to tell: NXDOMAIN for kubernetes.default from busybox, while the full name kubernetes.default.svc.cluster.local resolves from the same pod.

The upstream resolver does not answer

CoreDNS cannot reach the server it forwards to, or the upstream limits how many queries it accepts. Cluster names still work but outside names fail.

How to tell: SERVFAIL for outside names only. CoreDNS logs show plugin/errors lines with i/o timeout to the upstream address.

A NetworkPolicy blocks port 53

A default deny egress policy, or one that allows only some ports, also blocks DNS. Pods in that namespace cannot reach the kube-dns Service at all.

How to tell: Lookups time out from one namespace while CoreDNS is healthy. kubectl get networkpolicy shows an Egress policy with no rule for UDP and TCP port 53.

CoreDNS runs out of memory or replicas

CoreDNS has too little memory or too few replicas for the query volume. It is OOMKilled or slow during spikes and lookups time out.

How to tell: CoreDNS pods restart with Last State OOMKilled, or their memory sits near the limit. Failures come and go with load.

Wrong name or dnsPolicy

A short name only resolves in the pod's own namespace. A pod with dnsPolicy Default uses the node's resolver and cannot resolve cluster names at all. A hostNetwork pod needs ClusterFirstWithHostNet to use cluster DNS.

How to tell: NXDOMAIN for a short name while the full name works. With dnsPolicy Default, /etc/resolv.conf shows the node's nameserver, not the kube-dns cluster IP.

How to diagnose it

  1. kubectl run dnsutils -n <namespace> --rm -it --restart=Never --image=registry.k8s.io/e2e-test-images/jessie-dnsutils:1.3 -- nslookup kubernetes.default

    Run it in the namespace that has the problem. An answer with the kubernetes Service IP means cluster DNS works from there. Then repeat with an outside name.

  2. kubectl exec <pod> -n <namespace> -- cat /etc/resolv.conf

    nameserver should be the cluster IP of the kube-dns Service in kube-system. Check the search list and ndots. A node address here means dnsPolicy is not ClusterFirst.

  3. kubectl get pods -n kube-system -l k8s-app=kube-dns -o wide

    Every CoreDNS pod should be Running and ready. Restarts point at a crash or an OOM kill. Pods on one node only means one node failure takes DNS down.

  4. kubectl get endpoints kube-dns -n kube-system

    <none> or a blank value means no CoreDNS pod is ready, so every lookup times out.

  5. kubectl logs -n kube-system -l k8s-app=kube-dns --tail=50

    Error during parsing points at the Corefile. plugin/errors lines with i/o timeout to an outside address point at the upstream. Add --previous for a pod that keeps restarting.

  6. kubectl get configmap coredns -n kube-system -o yaml

    Read the Corefile, the forward line and anything changed recently. On k3s, extra config goes in the coredns-custom ConfigMap, which the import lines load.

  7. kubectl get networkpolicy -n <namespace>

    An Egress policy that selects the pod must allow UDP and TCP port 53 to the CoreDNS pods, or lookups time out.

How to fix it

Get CoreDNS running again

Fix what the CoreDNS pods fail on. For a bad Corefile, restore the last working ConfigMap and restart CoreDNS.

kubectl rollout restart deployment/coredns -n kube-system
kubectl rollout status deployment/coredns -n kube-system

Trade-off: Test Corefile changes on a cluster you do not share first. In our lab a restart with a bad Corefile replaced the only CoreDNS pod with a crashing one and DNS stopped for the whole cluster.

Give CoreDNS more memory and replicas

Raise the memory limit and run at least two replicas on different nodes. A topology spread constraint or pod anti-affinity keeps them apart. On larger clusters the cluster-proportional-autoscaler can scale replicas with node count.

Trade-off: More replicas use more memory on every cluster. On managed clusters the CoreDNS add-on can overwrite manual edits, so make the change through the add-on settings.

Run NodeLocal DNSCache

NodeLocal DNSCache runs a DNS cache as a DaemonSet on every node. Pods ask the cache on their own node, which cuts the load on CoreDNS and keeps most lookups out of the conntrack table.

Trade-off: It is one more DaemonSet to run and upgrade. If the cache fails on a node, pods on that node lose DNS until it is back.

Lower ndots for pods that call outside names

Set ndots lower in the pod's dnsConfig so outside names are tried as written first. This cuts the extra search list queries for each lookup.

spec:
  dnsConfig:
    options:
    - name: ndots
      value: "2"

Trade-off: Cluster names with that many dots, such as <service>.<namespace>.svc with ndots 2, then go upstream first and fail before the search list is tried. Use full names for cluster Services in those pods.

Allow DNS in your NetworkPolicies

Add an egress rule to every default deny policy that allows UDP and TCP port 53 to the CoreDNS pods.

egress:
- to:
  - namespaceSelector:
      matchLabels:
        kubernetes.io/metadata.name: kube-system
    podSelector:
      matchLabels:
        k8s-app: kube-dns
  ports:
  - port: 53
    protocol: UDP
  - port: 53
    protocol: TCP

Trade-off: Allow port 53 only to the CoreDNS pods, not to every address. With NodeLocal DNSCache the pods talk to a node local address, which needs its own rule.

How to stop it coming back

  • Run at least two CoreDNS replicas on different nodes, with a PodDisruptionBudget.
  • Keep the working Corefile in git and test every edit before it reaches a shared cluster.
  • Alert on CoreDNS restarts and on its error metrics, which it serves on port 9153.
  • Add a DNS allow rule to every default deny egress policy.
  • Use dnsutils or busybox:1.28 for DNS tests, not newer busybox images.

How KubeGraf investigates it

KubeGraf watches your clusters for failures such as CrashLoopBackOff, OOMKilled pods and stuck rollouts. How KubeGraf handles Kubernetes incidents.

Frequently asked questions

How do I test Kubernetes DNS resolution?

Start a temporary dnsutils pod and run nslookup kubernetes.default, then look up an outside name. If cluster names fail, look at CoreDNS. If only outside names fail, look at the upstream resolver.

Why does nslookup kubernetes.default fail?

A timeout means the pod cannot reach CoreDNS or no CoreDNS pod is ready. NXDOMAIN from a recent busybox image can be a false result, because its nslookup can fail on a healthy cluster. Try dnsutils or busybox 1.28 before you change anything.

Why is CoreDNS in CrashLoopBackOff?

Read its previous logs. A typo in the Corefile shows Error during parsing with the line number. Restarts with Last State OOMKilled mean the memory limit is too low for the query volume.

Why does DNS fail only for external names?

CoreDNS answers cluster names itself but forwards outside names upstream. SERVFAIL with i/o timeout errors in the CoreDNS logs means the upstream does not answer.

How do I make cluster DNS more reliable?

Run at least two CoreDNS replicas on different nodes with enough memory. Run NodeLocal DNSCache so pods use a cache on their own node. Lower ndots for workloads that mostly call outside names.

Last updated . Try KubeGraf Freemium.

Related

Kubernetes documentation