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
Official competencies this lesson covers
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.
| Tool | Typical use | Notes |
|---|---|---|
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 IP | Skips the Service |
dig <fqdn> +short | Exact answers, record types (SRV, PTR) | Does not use the search list unless you add +search |
host <name> | Like nslookup, one line per answer | In the dnsutils image |
cat /etc/resolv.conf | What the Pod was told to do | Works in almost every image |
getent hosts <name> | Resolve exactly as an application would | For 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
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
| Station | Command | Healthy result |
|---|---|---|
| 1. The Pod's resolver | kubectl exec <pod> -- cat /etc/resolv.conf | nameserver is the kube-dns ClusterIP, search starts with the Pod's namespace |
| 2. The Service | kubectl -n kube-system get svc kube-dns | Exists, ClusterIP matches station 1, ports 53/UDP and 53/TCP |
| 3. The endpoints | kubectl -n kube-system get endpointslices -l k8s-app=kube-dns | One address per CoreDNS Pod |
| 4. The CoreDNS Pods | kubectl -n kube-system get pods -l k8s-app=kube-dns -o wide | 1/1 Running, few restarts |
| 5. The configuration | kubectl -n kube-system logs -l k8s-app=kube-dns and get cm coredns -o yaml | No errors, kubernetes and forward lines are sensible |
Reading the failure first
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 see | What it means | Where to look |
|---|---|---|
connection timed out; no servers could be reached for both | The query never got an answer | Stations 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 work | The server works. This name does not exist | The name: namespace missing, typo, Service not created |
| Every cluster name fails, external names work | CoreDNS runs but has no cluster data | The 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 broken | The forward line, the node's /etc/resolv.conf, the upstream servers |
| Works from some nodes only | Per-node problem | kubelet clusterDNS on that node, its kube-proxy, its network plugin |
What the CoreDNS Pods' status means
| Status | Usual cause | Evidence |
|---|---|---|
Pending | No network plugin yet, or no node to schedule on | kubectl describe pod: node NotReady, FailedScheduling |
ContainerCreating | The network plugin fails on that node | Events: FailedCreatePodSandBox |
CrashLoopBackOff | Corefile syntax error, or a forwarding loop | Logs: Error during parsing, Loop ... detected |
Running but 0/1 | Cannot reach the API server, or lost its permissions | Logs: Still waiting on: "kubernetes", forbidden |
| No Pods at all | Deployment scaled to zero or deleted | kubectl -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:
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.
digneeds full names.dig web.appsasks for exactlyweb.apps.and getsNXDOMAIN. 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
digwith a short name and concluding that DNS is broken.digignores 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 podis refused or lost. Change the owner. - Testing from a different namespace or node than the failing application.
- Reading the three
NXDOMAINlines 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.defaultand one external name. - A lookup passes
resolv.conf, thekube-dnsService, its endpoints, the CoreDNS Pods and the Corefile. Check them in order. - Timeouts point at the path,
NXDOMAINat the name, external-only failures atforward. - The label
k8s-app=kube-dnsfinds 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.