Day 13: CoreDNS and Pod DNS
Goal: You read a pod’s
resolv.conf, measure whatndots:5costs in queries, watch CoreDNS at work through its log, enter a pod without tools usingkubectl debugand attach the zonefirma.labfrom Day 6 as a stub domain – through two NATs · Duration: approx. 90 min · Prerequisite: Day 12 (clusterndbgwithwebandwerkzeug), Day 6 (unitdns-router)
Briefing
- Every pod gets a
resolv.confthat points to CoreDNS (10.96.0.10, servicekube-dns). CoreDNS answers names undercluster.localitself and passes everything else on. - Short names like
webwork via the search list:default.svc.cluster.local,svc.cluster.local,cluster.localand whatever the node brings along.ndots:5means: a name with fewer than five dots is first tried with every search domain, and only then as it stands. - CoreDNS’s configuration is the Corefile in the
ConfigMap
coredns. Each block starts with a zone (.:53= everything); inside are plugins that work in order. - Many container images have neither
dignorss.kubectl debugattaches an additional container with tools to a running pod – in the same network namespace, likensenter -non Day 11.
| Name in the pod | becomes | found via |
|---|---|---|
web |
web.default.svc.cluster.local |
search list, first entry |
kube-dns.kube-system |
kube-dns.kube-system.svc.cluster.local |
search list, second entry |
example.com |
example.com |
only after four futile search domains |
example.com. |
example.com |
immediately: the trailing dot says “complete” |
Drill 1: A Pod’s resolv.conf
# on netz
# shortcut for commands in the tools pod
W="kubectl exec werkzeug --"
# what does the pod use to resolve names?
$W cat /etc/resolv.conf
# dig without the search list: to dig, "web" is a name without a domain
$W dig +short web; echo "exit=$?"
# dig with the search list (+search) and getent, which resolves like any program in the pod
$W dig +short +search web
$W getent hosts web
# fully qualified names, also across namespaces
$W dig +short web.default.svc.cluster.local
$W dig +short kube-dns.kube-system.svc.cluster.localExpected (the ClusterIP of web
differs):
search default.svc.cluster.local svc.cluster.local cluster.local localdomain
nameserver 10.96.0.10
options ndots:5
exit=0
10.96.157.184
10.96.157.184 web.default.svc.cluster.local web.default.svc.cluster.local web
10.96.157.184
10.96.0.10
Two traps at once: dig web returns
nothing and still exit code 0 – dig uses
the search list only with +search. Programs in the pod
(curl, Java, Python) always use it. If you use dig to test
what an application sees, use +search or simply
getent hosts. And localdomain at the end of
the search list doesn’t come from Kubernetes but from the node – here
from Docker’s resolv.conf.
Save your progress? Sign up for free
Drill 2: What ndots:5 Costs
One call to example.com from the pod. Which DNS queries
go out?
# on netz
# capture all DNS queries of the pod in the background (-l = write line by line),
# then a curl, then stop the capture and count the questions
$W sh -c 'timeout 6 tcpdump -ni eth0 -l udp port 53 2>/dev/null > /tmp/dns.txt & sleep 1; curl -s -o /dev/null -m3 http://example.com; sleep 1; kill %1 2>/dev/null; wait; grep -oE "(A|AAAA)\? [^ ]+" /tmp/dns.txt'Expected (the order of A and AAAA can vary):
AAAA? example.com.default.svc.cluster.local.
A? example.com.default.svc.cluster.local.
AAAA? example.com.svc.cluster.local.
A? example.com.svc.cluster.local.
AAAA? example.com.cluster.local.
AAAA? example.com.localdomain.
A? example.com.cluster.local.
AAAA? example.com.
A? example.com.localdomain.
A? example.com.
Ten queries for one name: five variants, each with A
(IPv4) and AAAA (IPv6). Eight of them end with NXDomain.
Inside the cluster these are milliseconds, but every query puts load on
CoreDNS, and with a slow upstream it adds up. A look at the full lines
(cat /tmp/dns.txt) shows something else: the queries go
straight to the pod IP of CoreDNS, not to
10.96.0.10 – the socket LB from Day 12.
With a trailing dot:
# on netz
# the same call with the fully qualified name "example.com."; only the questions are counted
$W sh -c 'timeout 6 tcpdump -ni eth0 -l udp port 53 2>/dev/null > /tmp/dns.txt & sleep 1; curl -s -o /dev/null -m3 http://example.com.; sleep 1; kill %1 2>/dev/null; wait; grep -cE "(A|AAAA)\? " /tmp/dns.txt'Expected:
2
Two instead of ten. If you call external names often, write them with
a dot or set dnsConfig.options: [{name: ndots, value: "2"}]
in the pod. Then names with two dots (api.example.com)
count as complete right away – web and
web.default still find their services.
Save your progress? Sign up for free
Drill 3: CoreDNS at Work
Without the log plugin, CoreDNS writes no queries to the
log. You fetch the Corefile as a file, insert log and play
it back:
# on netz
cd ~/ndbg/k8s
# the Corefile from the ConfigMap as a file
kubectl -n kube-system get cm coredns -o jsonpath='{.data.Corefile}' > Corefile
# how many times have the CoreDNS pods reloaded so far? (--tail=-1 = whole log)
VORHER=$(kubectl -n kube-system logs -l k8s-app=kube-dns --tail=-1 | grep -c 'Reloading complete')
# insert a line "log" after "errors" (same indentation)
sed -i 's/^ errors$/ errors\n log/' Corefile
head -4 Corefile
# recreate the ConfigMap from the file and apply it:
# --dry-run=client -o yaml only build the YAML, don't create it
# | kubectl apply -f - then apply it as a change to the existing ConfigMap
kubectl -n kube-system create configmap coredns --from-file=Corefile --dry-run=client -o yaml | kubectl apply -f -Expected:
.:53 {
errors
log
health {
configmap/coredns configured
You don’t have to restart CoreDNS: the reload plugin
checks the file every 30 seconds. Until the changed ConfigMap arrives in
the pod, however, up to another minute passes. Wait until
both pods have written a new “Reloading complete”
message – that is, two more than before. Incidentally, a change that
only affects a comment doesn’t trigger a reload; then you wait
forever.
# on netz
# check every 10 s until two new "Reloading complete" have appeared
until [ "$(kubectl -n kube-system logs -l k8s-app=kube-dns --tail=-1 | grep -c 'Reloading complete')" -ge $((VORHER + 2)) ]; do sleep 10; done
# one query that exists and one that doesn't
$W dig +short +search web > /dev/null
$W dig +short nichtda.default.svc.cluster.local
# the two lines in the log
kubectl -n kube-system logs -l k8s-app=kube-dns --tail=200 | grep -E '"(A|AAAA) IN (web|nichtda)'Expected (after waiting one to two minutes; ports and times differ):
[INFO] 10.244.1.135:54336 - 41101 "A IN web.default.svc.cluster.local. udp 70 false 1232" NOERROR qr,aa,rd 92 0.002525499s
[INFO] 10.244.1.135:42409 - 21925 "A IN nichtda.default.svc.cluster.local. udp 74 false 1232" NXDOMAIN qr,aa,rd 144 0.00252846s
Each line tells you: who asks (pod IP and port),
what (type and name), the answer
(NOERROR, NXDOMAIN, SERVFAIL) and
how long it took. aa means
“authoritative”: CoreDNS knows the answer itself. If an application
keeps asking for a name that doesn’t exist, you see it here
immediately.
Save your progress? Sign up for free
Drill 4: Into a Pod Without Tools
The web pods have a shell, but neither dig
nor ss. kubectl debug attaches an
ephemeral container:
# on netz
# name of the first web pod
P=$(kubectl get pod -l app=web -o jsonpath='{.items[0].metadata.name}')
# attach a debug container:
# --image which image (netshoot brings all network tools)
# --target=web additionally share the process namespace of the container "web"
# -c diag name of the debug container
# -- sh -c ... what it should run; it shares the network with the pod anyway
kubectl debug $P --image=nicolaka/netshoot:v0.17 --target=web -c diag -- sh -c 'ss -ltn; dig +short +search web'
# wait briefly, then read the debug container's output
sleep 15
kubectl logs $P -c diagExpected:
Targeting container "web". If you don't see processes from this container it may be because the container runtime doesn't support this feature.
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 4096 *:8080 *:*
10.96.157.184
ss shows port 8080 of the web container:
all containers of a pod share one network. An ephemeral container stays
in the pod description until the pod is deleted; it can’t be restarted.
For interactive use, pass -it instead of a command at the
end. The same works for the node:
kubectl debug node/ndbg-worker -it --image=… starts a pod
in the node’s network.
Save your progress? Sign up for free
Exercise
A batch job is supposed to call web. Its pod:
# on netz
cd ~/ndbg/k8s
cat > batch.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: batch}
spec:
dnsPolicy: Default
containers:
- name: batch
image: nicolaka/netshoot:v0.17
command: ["sleep", "infinity"]
EOF
kubectl apply -f batch.yaml
kubectl wait --for=condition=Ready pod/batch --timeout=120s
kubectl exec batch -- curl -sS -m3 http://web/hostname“It works in the tools pod, but not in the batch pod.” Find the difference and fix it.
Solution
# on netz
# error, the pod's resolv.conf, internet names still work
kubectl exec batch -- curl -sS -m3 http://web/hostname
kubectl exec batch -- cat /etc/resolv.conf
kubectl exec batch -- dig +short example.comExpected:
curl: (6) Could not resolve host: web (Domain name not found)
command terminated with exit code 6
search localdomain
nameserver 172.18.0.1
options edns0 trust-ad ndots:0
172.66.147.243
104.20.23.154
No CoreDNS, no search list: dnsPolicy: Default doesn’t
mean “standard” but “take the node’s resolv.conf” – here
Docker’s with 172.18.0.1. Internet names work, names in the
cluster don’t. The right value is
ClusterFirst, and because it is the
default, you simply leave the field out. It can’t be changed on a
running pod:
# on netz
# attempt to change the field on the pod
kubectl patch pod batch -p '{"spec":{"dnsPolicy":"ClusterFirst"}}' 2>&1 | grep Forbidden | cut -c1-78
# delete the line from the file and replace the pod (--force = delete and recreate)
sed -i '/dnsPolicy: Default/d' batch.yaml
kubectl replace --force -f batch.yaml
kubectl wait --for=condition=Ready pod/batch --timeout=120s
kubectl exec batch -- curl -s -m3 http://web/hostname; echoExpected (the pod name differs):
The Pod "batch" is invalid: spec: Forbidden: pod updates may not change fields
pod "batch" deleted from default namespace
pod/batch replaced
pod/batch condition met
web-69856b5777-2np6w
You need Default almost only for pods with
hostNetwork: true, and exactly those then often have this
very problem – for them there is
ClusterFirstWithHostNet.
Save your progress? Sign up for free
Challenge
The pods are supposed to resolve the internal zone
firma.lab from Day 6. It is answered by the dnsmasq in the
router at 10.1.0.1. Add a stub domain to
the Corefile: everything under firma.lab to
10.1.0.1.
# on netz
cd ~/ndbg/k8s
# counter value for waiting on the reload
VORHER=$(kubectl -n kube-system logs -l k8s-app=kube-dns --tail=-1 | grep -c 'Reloading complete')
# separate server block for the zone firma.lab at the end of the Corefile
cat >> Corefile <<'EOF'
firma.lab:53 {
errors
log
forward . 10.1.0.1
}
EOF
kubectl -n kube-system create configmap coredns --from-file=Corefile --dry-run=client -o yaml | kubectl apply -f -Wait as in Drill 3 until both pods have reloaded, then query
web.firma.lab. It doesn’t work. Find out why and fix it
with one command.
Solution
# on netz
# wait for the reload as in Drill 3
until [ "$(kubectl -n kube-system logs -l k8s-app=kube-dns --tail=-1 | grep -c 'Reloading complete')" -ge $((VORHER + 2)) ]; do sleep 10; done
# the query from the pod; time measures how long it takes
time $W dig web.firma.lab | grep -E "status:|Query time"
# what CoreDNS reports about it
kubectl -n kube-system logs -l k8s-app=kube-dns --since=1m | grep -i firma | tail -2Expected (addresses and times differ):
;; communications error to 10.96.0.10#53: timed out
;; communications error to 10.96.0.10#53: timed out
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 33918
;; Query time: 4004 msec
real 0m14.783s
[INFO] 10.244.1.135:39335 - 33918 "A IN web.firma.lab. udp 54 false 1232" SERVFAIL qr,rd 31 6.016224011s
[ERROR] plugin/errors: 2 web.firma.lab. A: read udp 10.244.0.34:36879->10.1.0.1:53: i/o timeout
The block takes effect – CoreDNS asks 10.1.0.1 – but the
answer never comes. Almost 15 seconds for a SERVFAIL:
dig waited twice for 5 seconds each on CoreDNS, and CoreDNS
itself waited 6 seconds on the router. Where does a packet to
10.1.0.1 end up? CoreDNS runs on the control plane, so
check from there and then from netz:
# on netz
# route from the node's point of view (a Docker container)
sudo docker exec ndbg-control-plane ip route get 10.1.0.1
# route from netz's point of view
ip route get 10.1.0.1Expected:
10.1.0.1 via 172.18.0.1 dev eth0 src 172.18.0.2 uid 0
cache
10.1.0.1 via 172.16.0.1 dev eth0 src 172.16.0.123 uid 1000
cache
The node sends everything to netz
(172.18.0.1), which is correct. But netz
doesn’t know 10.1.0.0/24 and sends the packet via its
default route into the home network (172.16.0.1) – there is
no router with this zone there. Until now nobody needed this: the
namespaces only talked to each other and out through the router. The one
route:
# on netz
# network A (10.1.0.0/24) sits behind the router, reachable via up0
sudo ip route add 10.1.0.0/24 via 192.168.100.2 dev up0
ip route get 10.1.0.1
time $W dig web.firma.lab | grep -E "status:|Query time|^web"
# who is asking from dnsmasq's point of view?
sudo journalctl -u dns-router --since "-1min" --no-pager -o cat | grep "query.*web.firma.lab" | tail -1Expected:
10.1.0.1 via 192.168.100.2 dev up0 src 192.168.100.1 uid 1000
cache
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35554
web.firma.lab. 0 IN A 10.2.0.10
;; Query time: 28 msec
real 0m0.607s
dnsmasq[52313]: query[A] web.firma.lab from 192.168.100.1
The answer comes in 28 ms. And dnsmasq sees
192.168.100.1 as the sender – the address of
netz on up0, not CoreDNS’s pod address. The
query was translated twice: by Cilium (pod IP → node IP
172.18.0.2, masquerade for everything that leaves the
cluster) and by Docker (node IP → 192.168.100.1, masquerade
of the kind network). That is also why the answer comes back without the
router having to know the pod network.
The route lives in memory; Appendix A sets it during the rebuild.
Save your progress? Sign up for free
Checkpoint
# on netz
# search list and ndots in the tools pod
kubectl exec werkzeug -- grep -E "^(search|options)" /etc/resolv.conf
# cluster name and stub domain resolve
kubectl exec werkzeug -- getent hosts web
kubectl exec werkzeug -- dig +short web.firma.lab
# Corefile: log twice (both blocks) and the firma.lab block
kubectl -n kube-system get cm coredns -o jsonpath='{.data.Corefile}' | grep -cE "^ log$|^firma.lab:53"
# route to the router
ip route show 10.1.0.0/24Expected:
search default.svc.cluster.local svc.cluster.local cluster.local localdomain
options ndots:5
10.96.157.184 web.default.svc.cluster.local web.default.svc.cluster.local web
10.2.0.10
3
10.1.0.0/24 via 192.168.100.2 dev up0
Clean up
# on netz
kubectl delete pod batchExpected:
pod "batch" deleted from default namespace
The log plugin stays on for Day 14. In a real cluster with lots of traffic it writes a great many lines; there you switch it on only for troubleshooting.
Interview note
“A pod can’t resolve a service name.” First
cat /etc/resolv.conf in the pod: does the
nameserver point to CoreDNS, and is the search list right?
(dnsPolicy!) Then test with getent hosts, not
with dig without +search. Then CoreDNS’s log
(log plugin): does the query arrive, and with which answer?
Bonus points: ndots:5 explains why an external name costs
ten queries, and with a SERVFAIL after several seconds it
pays to look at the route to the upstream, not at CoreDNS.
Enterprise note
Stub domains for internal zones (corp.example, Active
Directory) belong in almost every enterprise cluster. They depend on a
route and a firewall rule outside of Kubernetes – and are therefore the
first to fail during network changes, without anything having changed in
the cluster. Monitor the response times of the upstreams (CoreDNS
provides metrics for this on port 9153) and keep the Corefile in Git;
otherwise managed distributions overwrite a hand-edited ConfigMap at the
next update.