Skip to content
LF Cert Trainer

Lesson 117 · Section 9: Networking

Lesson 117: Explore DNS: follow a lookup through a live cluster and fix it when it breaks

A hands-on walk along the path of a DNS lookup: the tools to query from inside a Pod, the five stations between a Pod and an answer and the command that checks each one, how the kind of failure tells you where to look, and worked repairs for the faults an exam is likely to plant. · 20 min read · 2 min video

1/8Explore DNS
0:00 / 2:14

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why explore instead of memorise

The last two lessons explained cluster DNS: the names in "DNS in Kubernetes" and the server in "CoreDNS in Kubernetes". In the exam, and at work, DNS never appears as a question about theory. It appears as "the frontend cannot reach the backend" or "fix name resolution in this cluster", on a cluster you have never seen.

What helps then is a route you have walked before: the path one lookup takes, a command for every station on it, and the habit of letting the failure tell you where to begin. This lesson walks that route on a healthy cluster first, then breaks it in the ways that are likely to be planted.

The tools

All DNS tests are run from inside a Pod, because nodes do not use cluster DNS.

ToolTypical useNotes
nslookup <name>Quick "does it resolve, and who answered?"Uses the search list. Present in busybox
nslookup <name> <server>Ask one specific server, for example a CoreDNS Pod IPSkips the Service
dig <fqdn> +shortExact answers, record types (SRV, PTR)Does not use the search list unless you add +search
host <name>Like nslookup, one line per answerIn the dnsutils image
cat /etc/resolv.confWhat the Pod was told to doWorks in almost every image
getent hosts <name>Resolve exactly as an application wouldFor images without nslookup

Two ways to get a Pod with tools:

# one lookup, then gone
kubectl run dns-test --rm -it --restart=Never --image=busybox:1.28 -- nslookup kubernetes.default

# a Pod that stays, with dig and host (the manifest from the Kubernetes documentation)
kubectl apply -f https://k8s.io/examples/admin/dns/dnsutils.yaml
kubectl exec -it dnsutils -- dig web.apps.svc.cluster.local +short

A healthy answer has two parts: the server that answered and the result.

Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      kubernetes.default
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local

kubernetes.default is the best test name: the Service kubernetes in default exists in every cluster.

How to walk the path

Five stations between a Pod and an answerqueryselectsdelivered toreadsreturns1 resolv.confin the client Pod2 Service kube-dnsClusterIP reachable3 EndpointsCoreDNS Pod IPs4 CoreDNS PodsRunning and Ready5 Corefileplugins and upstreamAnsweran IP address
Five stations between a Pod and an answer. A lookup passes these stations in order. Each one has a single command that checks it, and the first station that fails is the fault.

Parts:

  • 1 resolv.conf (in the client Pod)
  • 2 Service kube-dns (ClusterIP reachable)
  • 3 Endpoints (CoreDNS Pod IPs)
  • 4 CoreDNS Pods (Running and Ready)
  • 5 Corefile (plugins and upstream)
  • Answer (an IP address)

Connections:

  • 1 resolv.conf to 2 Service kube-dns: query
  • 2 Service kube-dns to 3 Endpoints: selects
  • 3 Endpoints to 4 CoreDNS Pods: delivered to
  • 4 CoreDNS Pods to 5 Corefile: reads
  • 5 Corefile to Answer: returns
StationCommandHealthy result
1. The Pod's resolverkubectl exec <pod> -- cat /etc/resolv.confnameserver is the kube-dns ClusterIP, search starts with the Pod's namespace
2. The Servicekubectl -n kube-system get svc kube-dnsExists, ClusterIP matches station 1, ports 53/UDP and 53/TCP
3. The endpointskubectl -n kube-system get endpointslices -l k8s-app=kube-dnsOne address per CoreDNS Pod
4. The CoreDNS Podskubectl -n kube-system get pods -l k8s-app=kube-dns -o wide1/1 Running, few restarts
5. The configurationkubectl -n kube-system logs -l k8s-app=kube-dns and get cm coredns -o yamlNo errors, kubernetes and forward lines are sensible

Reading the failure first

What fails tells you where to lookcheckcheckcheckTwo lookupscluster + externalBoth time outserver not reachedCluster name failsexternal worksExternal failscluster worksStations 1 to 4resolv.conf to PodsThe name itselfor kubernetes pluginforwardand upstream DNS
What fails tells you where to look. Test one cluster name and one external name from the affected Pod. The combination of results points at one part of the path.

Parts:

  • Two lookups (cluster + external)
  • Both time out (server not reached)
  • Cluster name fails (external works)
  • External fails (cluster works)
  • Stations 1 to 4 (resolv.conf to Pods)
  • The name itself (or kubernetes plugin)
  • forward (and upstream DNS)

Connections:

  • Two lookups to Both time out
  • Two lookups to Cluster name fails
  • Two lookups to External fails
  • Both time out to Stations 1 to 4: check
  • Cluster name fails to The name itself: check
  • External fails to forward: check

The text of the error is evidence. From the affected Pod, look up kubernetes.default and an external name such as kubernetes.io:

What you seeWhat it meansWhere to look
connection timed out; no servers could be reached for bothThe query never got an answerStations 1 to 4, kube-proxy on that node, a NetworkPolicy that blocks port 53
NXDOMAIN or can't resolve for one cluster name, other cluster names workThe server works. This name does not existThe name: namespace missing, typo, Service not created
Every cluster name fails, external names workCoreDNS runs but has no cluster dataThe kubernetes line of the Corefile, CoreDNS's permissions, its connection to the API server
Cluster names work, external names fail (SERVFAIL or timeout)Forwarding is brokenThe forward line, the node's /etc/resolv.conf, the upstream servers
Works from some nodes onlyPer-node problemkubelet clusterDNS on that node, its kube-proxy, its network plugin

What the CoreDNS Pods' status means

StatusUsual causeEvidence
PendingNo network plugin yet, or no node to schedule onkubectl describe pod: node NotReady, FailedScheduling
ContainerCreatingThe network plugin fails on that nodeEvents: FailedCreatePodSandBox
CrashLoopBackOffCorefile syntax error, or a forwarding loopLogs: Error during parsing, Loop ... detected
Running but 0/1Cannot reach the API server, or lost its permissionsLogs: Still waiting on: "kubernetes", forbidden
No Pods at allDeployment scaled to zero or deletedkubectl -n kube-system get deploy coredns

What you see when you turn on logging

Adding the log plugin to the Corefile makes CoreDNS print each query. It also shows the effect of ndots:5:

Why an external name costs four queriestry 1try 2try 3try 4foundPodkubernetes.io+ default.svc…NXDOMAIN+ svc.cluster.localNXDOMAIN+ cluster.localNXDOMAINkubernetes.io.as writtenAnswerfrom upstream
Why an external name costs four queries. With ndots:5, a name with fewer than five dots is tried with every search suffix before it is tried as written. Only the last query succeeds.

Parts:

  • Pod (kubernetes.io)
  • + default.svc… (NXDOMAIN)
  • + svc.cluster.local (NXDOMAIN)
  • + cluster.local (NXDOMAIN)
  • kubernetes.io. (as written)
  • Answer (from upstream)

Connections:

  • Pod to + default.svc…: try 1
  • + default.svc… to + svc.cluster.local: try 2
  • + svc.cluster.local to + cluster.local: try 3
  • + cluster.local to kubernetes.io.: try 4
  • kubernetes.io. to Answer: found
[INFO] 10.244.1.5:41822 - 6327 "A IN kubernetes.io.default.svc.cluster.local. udp 58 false 512" NXDOMAIN qr,aa,rd 151 0.000146s
[INFO] 10.244.1.5:36912 - 2281 "A IN kubernetes.io.svc.cluster.local. udp 50 false 512" NXDOMAIN qr,aa,rd 143 0.000091s
[INFO] 10.244.1.5:52301 - 9014 "A IN kubernetes.io.cluster.local. udp 46 false 512" NXDOMAIN qr,aa,rd 139 0.000087s
[INFO] 10.244.1.5:47733 - 4410 "A IN kubernetes.io. udp 31 false 512" NOERROR qr,rd,ra 60 0.012337s

Three NXDOMAIN lines before the real answer are normal, not a fault. Each line gives the client's Pod IP, the record type, the exact name asked and the result code, which makes the log the best witness when someone claims "DNS is broken": you can see what was really asked.

Under the hood · Details worth knowing
  • Ask a CoreDNS Pod directly to split the path in two: nslookup kubernetes.default.svc.cluster.local 10.244.0.2. If that works and the Service address does not, CoreDNS is fine and the fault is between the Pod and the Service: kube-proxy, the endpoints or a NetworkPolicy.
  • Policies and DNS. A NetworkPolicy that isolates a namespace for egress blocks lookups unless it allows UDP and TCP port 53 to the DNS Pods. The symptom is a timeout from Pods in that namespace only.
  • dig needs full names. dig web.apps asks for exactly web.apps. and gets NXDOMAIN. Use the full name or +search.
  • Images without tools. kubectl debug -it <pod> --image=busybox:1.28 --target=<container> adds a temporary container that shares the Pod's network and resolver, so you test with the Pod's own /etc/resolv.conf.
  • Stale answers. Answers are cached for up to 30 seconds by CoreDNS, and some applications cache for much longer. After a fix, test with a fresh Pod before deciding that it did not work.

Using it

Example · Explore a healthy cluster

Run the whole route once, so that you know what "good" looks like.

kubectl run client --image=busybox:1.28 --restart=Never -- sleep 3600
kubectl exec client -- cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
kubectl -n kube-system get svc kube-dns
kubectl -n kube-system get endpointslices -l k8s-app=kube-dns
kubectl -n kube-system get pods -l k8s-app=kube-dns -o wide
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   12d
NAME             ADDRESSTYPE   PORTS        ENDPOINTS               AGE
kube-dns-7xv9k   IPv4          9153,53,53   10.244.0.2,10.244.0.3   12d
NAME                       READY   STATUS    RESTARTS   AGE   IP           NODE
coredns-6f6b679f8f-4kq2n   1/1     Running   0          12d   10.244.0.2   controlplane
coredns-6f6b679f8f-x9wlb   1/1     Running   0          12d   10.244.0.3   controlplane

The same three numbers appear at every station: 10.96.0.10 in the Pod and on the Service, 10.244.0.2 and 10.244.0.3 in the endpoints and on the Pods. Now the lookups:

kubectl exec client -- nslookup kubernetes.default
kubectl exec client -- nslookup kube-dns.kube-system
kubectl exec client -- nslookup kubernetes.io
kubectl exec client -- nslookup 10.96.0.10

The last one is a reverse lookup and answers kube-dns.kube-system.svc.cluster.local.

Example · Fix: the application uses the wrong name

Task: "The webapp Pod in default reports Can't connect to MySQL server on 'mysql'. Fix it without changing the database."

kubectl exec webapp -- nslookup mysql
kubectl exec webapp -- nslookup kubernetes.default
nslookup: can't resolve 'mysql'
Name:      kubernetes.default
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local

DNS works, the name does not. Find where the Service really is:

kubectl get svc -A | grep mysql
payroll   mysql   ClusterIP   10.103.132.104   <none>   3306/TCP   20m

The Service is in payroll, so from default it is mysql.payroll. The Pod belongs to a Deployment, so change the Deployment:

kubectl set env deploy/webapp DB_HOST=mysql.payroll
kubectl exec deploy/webapp -- nslookup mysql.payroll
Name:      mysql.payroll
Address 1: 10.103.132.104 mysql.payroll.svc.cluster.local

Example · Fix: every lookup times out

kubectl exec client -- nslookup kubernetes.default
;; connection timed out; no servers could be reached
command terminated with exit code 1

Nothing answered, so walk the stations. Station 1 and 2 agree on 10.96.0.10. Station 3:

kubectl -n kube-system get endpointslices -l k8s-app=kube-dns
NAME             ADDRESSTYPE   PORTS     ENDPOINTS   AGE
kube-dns-7xv9k   IPv4          <unset>   <unset>     12d

No endpoints. Either there are no ready Pods, or the Service does not select them:

kubectl -n kube-system get pods -l k8s-app=kube-dns
kubectl -n kube-system get svc kube-dns -o jsonpath='{.spec.selector}'
NAME                       READY   STATUS    RESTARTS   AGE
coredns-6f6b679f8f-4kq2n   1/1     Running   0          12d
coredns-6f6b679f8f-x9wlb   1/1     Running   0          12d
{"k8s-app":"core-dns"}

The Pods are healthy and labelled k8s-app=kube-dns. The Service selects core-dns. Correct the selector:

kubectl -n kube-system patch svc kube-dns -p '{"spec":{"selector":{"k8s-app":"kube-dns"}}}'
kubectl -n kube-system get endpointslices -l k8s-app=kube-dns
kubectl exec client -- nslookup kubernetes.default

Had station 4 shown no Pods, the fix would have been kubectl -n kube-system scale deploy coredns --replicas=2.

Example · Fix: CoreDNS crashes after a configuration change

kubectl -n kube-system get pods -l k8s-app=kube-dns
kubectl -n kube-system logs -l k8s-app=kube-dns --tail=3
NAME                       READY   STATUS             RESTARTS      AGE
coredns-6f6b679f8f-8hx2m   0/1     CrashLoopBackOff   5 (20s ago)   3m
coredns-6f6b679f8f-jq9rd   0/1     CrashLoopBackOff   5 (14s ago)   3m
/etc/coredns/Corefile:8 - Error during parsing: Unknown directive 'kubernets'

The log gives the file, the line and the word. Line 8 of the Corefile is line 8 below Corefile: | in the ConfigMap:

kubectl -n kube-system edit cm coredns        # kubernets -> kubernetes
kubectl -n kube-system rollout restart deploy coredns
kubectl -n kube-system rollout status deploy coredns
kubectl exec client -- nslookup kubernetes.default

If the log says Loop (127.0.0.1:... -> :53) detected for zone "." instead, the Corefile is fine and CoreDNS is forwarding to itself: point forward at real upstream servers (forward . 8.8.8.8), or fix the kubelet's resolvConf as described in the previous lesson.

Example · Fix: only names outside the cluster fail

kubectl exec client -- nslookup kubernetes.default
kubectl exec client -- nslookup kubernetes.io
Name:      kubernetes.default
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local
;; connection timed out; no servers could be reached

The path to CoreDNS works, and so does the kubernetes plugin. What is left is forwarding:

kubectl -n kube-system get cm coredns -o yaml | grep forward
kubectl -n kube-system logs -l k8s-app=kube-dns --tail=2
        forward . 10.0.0.53
[ERROR] plugin/errors: 2 kubernetes.io. A: read udp 10.244.0.2:51234->10.0.0.53:53: i/o timeout

CoreDNS forwards to 10.0.0.53, which does not answer. Restore the default (forward . /etc/resolv.conf) or name a working server, restart the Deployment and repeat both lookups.

Tricks

Common mistakes

  • Testing from the node's shell. The node does not use cluster DNS, so the result says nothing.
  • Using dig with a short name and concluding that DNS is broken. dig ignores the search list.
  • Restarting CoreDNS as the first step. If the fault is a wrong name, a selector or a kubelet setting, a restart changes nothing and costs time.
  • Fixing the Pod instead of its Deployment. An environment variable changed with kubectl edit pod is refused or lost. Change the owner.
  • Testing from a different namespace or node than the failing application.
  • Reading the three NXDOMAIN lines of the search list as errors.
  • Skipping the retest. A fix is finished when the original lookup, from the original place, succeeds.

Recap

  • Test from inside a Pod, with kubernetes.default and one external name.
  • A lookup passes resolv.conf, the kube-dns Service, its endpoints, the CoreDNS Pods and the Corefile. Check them in order.
  • Timeouts point at the path, NXDOMAIN at the name, external-only failures at forward.
  • The label k8s-app=kube-dns finds the Pods, the logs and the endpoints.
  • Repeat the failing lookup after every fix.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.

  • Name resolution is down in the whole cluster. Follow a lookup from a pod to CoreDNS, find every fault on the way and repair it.

    Fix · Level 3 · challenge · target 20 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.