Network
DNS in the cluster
CoreDNS and ndots:5
Why a lookup for api.example.com can hit CoreDNS four times before it goes out, and how search domains work. Every Pod gets the same resolv.conf - one nameserver, three search suffixes, ndots:5 - and together they decide what gets asked, and in what order.
- Nameserver
- 10.96.0.10 · ClusterIP of kube-dns
- Search
- 3 suffixes · default.svc.cluster.local svc.cluster.local cluster.local
- ndots
- 5 · names with fewer dots expand first
- Queries here
- 5 from one Pod · only 1 left the cluster
Read the whole lesson as text
1. The question forms
Inside web-…-x2kqp the app needs to call http://web/api/orders, so it asks getaddrinfo for the name web. The answer depends on /etc/resolv.conf, which Kubernetes writes into every Pod: nameserver 10.96.0.10, three search domains, options ndots:5. web has zero dots - fewer than five - so the resolver must try the search list before the bare name. Candidate 1 is web.default.svc.cluster.local.
- The app asks the resolver library for the name web.
- web has zero dots, so the search list is tried before the name itself.
2. Ask kube-DNS
Candidate 1 goes out as a UDP packet to 10.96.0.10 - not a Pod, but the ClusterIP of the kube-dns Service. The same kube-proxy rules as any other Service rewrite the destination on node-2 to the CoreDNS Pod, 10.244.1.3 on node-1. From there it is ordinary Pod-to-Pod traffic: VXLAN, UDP port 8472, VNI 1. DNS rides on the same machinery it helps you use.
- The first question leaves the Pod, addressed to the kube-dns Service.
- node-2's rules rewrite the destination to the CoreDNS Pod on node-1.
- The query crosses to node-1 wrapped in VXLAN, like any Pod-to-Pod packet.
3. Answered from memory
Zoom into node-1. CoreDNS looks nothing up per question: its kubernetes plugin keeps every Service and EndpointSlice in memory, fed by a watch on the API server. The name web.default.svc.cluster.local is Service web in namespace default, so it answers instantly - with the ClusterIP, 10.96.120.15, and a TTL of 30 seconds, the ttl the Corefile sets for the kubernetes plugin.
- The question reaches the CoreDNS Pod and is answered from memory, no lookup involved.
- The answer is the Service's ClusterIP, not a Pod address.
4. An outside name
The app then needs an outside name, api.example.com - one dot, still fewer than five, so the search list runs first. Three questions come back NXDOMAIN, about 1.5 ms apart: CoreDNS is authoritative for cluster.local and knows those longer names don't exist, so nothing leaves the cluster. Only the fourth question, the bare name, makes CoreDNS forward to the upstream resolver at 198.51.100.53. Counting the first lookup, five queries have now left this Pod.
- The outside name is tried against the first suffix, and doesn't exist.
- The second suffix misses too, answered inside the cluster.
- And the third. None of this traffic has left the cluster yet.
- Only now does CoreDNS ask an upstream resolver, outside the cluster.
- Twenty-two milliseconds later the upstream knows the name.
- CoreDNS passes the answer back, and the app finally has an address.
5. The payoff
This is why the search list exists: from any Pod in the cluster, the bare name web just works, because the resolver expands it with the caller's namespace suffixes first. The app connects to 10.96.120.15:80, and the same kube-proxy DNAT turns that into a real Pod - here 10.244.2.18, on the app's own node. Internal names cost one query. External names pay for three misses first. That asymmetry is the trade.
- The app connects to the ClusterIP that query 1 returned.
- kube-proxy's rules turn the Service address into a Pod, exactly as before.
6. The fixes
Two fixes skip the misses. A trailing dot marks a name absolute - api.example.com. is queried once, search list untouched. Or lower ndots in the Pod's dnsConfig: with ndots:2, api.example.com already has enough dots to be tried as-is first. Short internal names are unaffected either way: web has zero dots and will always try the search list first, which is exactly what you want.
- With the trailing dot, the same lookup is a single question, forwarded once.
- Or the Pod spec itself can lower the threshold.
Summary
One short internal name cost a single query in about a millisecond; one ordinary external name cost four. The difference is ndots:5 plus a three-entry search list - settings Kubernetes puts in every Pod so short names work everywhere. For a workload that mostly calls Services that's the right default; for a Pod that mostly calls outside, use absolute names or lower ndots.
Next lesson: Ingress and Gateway API