InsightsDevOps
Kubernetes DNS: why one lookup becomes six, and what to do about it
How ndots and search domains multiply DNS queries in every pod, how to measure the effect on CoreDNS, and the options for reducing it, from trailing dots and dnsConfig to autopath and NodeLocal DNSCache.
Published November 19, 20267 min read
Sooner or later a Kubernetes team meets DNS as a production problem. CoreDNS pods run hot, an application logs the occasional five-second pause on an outbound call, or a dashboard shows a large share of NXDOMAIN answers. Nothing is misconfigured in the usual sense. The cause is a default that every pod inherits, and it makes a single lookup of an external name cost several queries.
What a pod's resolv.conf says
For a pod in the checkout namespace, the kubelet writes a file like this (Kubernetes DNS reference):
nameserver 10.32.0.10
search checkout.svc.cluster.local svc.cluster.local cluster.local
options ndots:5The search line lets a pod resolve payments or payments.billing without writing the full name. The ndots:5 option decides when that list is used: a name with fewer than five dots is treated as possibly relative, so the resolver tries it with each search domain appended before it tries the name as written.
That is helpful for in-cluster names and expensive for everything else. Take api.example.com, which has two dots:
| Attempt | Name queried | Answer |
|---|---|---|
| 1 | api.example.com.checkout.svc.cluster.local | NXDOMAIN |
| 2 | api.example.com.svc.cluster.local | NXDOMAIN |
| 3 | api.example.com.cluster.local | NXDOMAIN |
| 4 | api.example.com | answer |
Most resolvers ask for A and AAAA records separately, so that is eight queries, six of them wasted. If the node's own resolv.conf has search domains, which many cloud images do, the pod inherits those as well, and the list grows. A service that makes a few hundred outbound calls per second without a connection pool or client-side DNS cache sends thousands of queries per second to CoreDNS.
Measure before changing anything
The CoreDNS prometheus plugin is enabled in the default Corefile and exposes response codes and request counts. Two queries show whether this is your problem:
# Share of answers that are NXDOMAIN
sum(rate(coredns_dns_responses_total{rcode="NXDOMAIN"}[5m]))
/ sum(rate(coredns_dns_responses_total[5m]))
# Total query rate per CoreDNS pod
sum by (pod) (rate(coredns_dns_requests_total[5m]))A high NXDOMAIN share in a cluster whose workloads mostly call external APIs points to search-path expansion. Some NXDOMAIN is normal: services probing for names that do not exist, or IPv6 lookups for hosts without AAAA records, which usually return NOERROR with no data rather than NXDOMAIN.
To see what one workload sends, enable the CoreDNS log plugin temporarily on a test cluster, or run tcpdump on port 53 from a debug container. Do not leave query logging on in production: it adds load to the component you are trying to relieve.
Fix 1: use names that skip the search list
The cheapest fix needs no cluster change. A name that ends in a dot is absolute, and the resolver sends it as written:
env:
- name: PAYMENTS_API_URL
value: "https://api.example.com./v1"This works at the DNS layer but breaks other layers. TLS clients may compare the host name, trailing dot included, with the certificate, and HTTP Host headers and virtual-host routing can behave differently. Test each client library before rolling it out widely. For in-cluster calls, use the full payments.billing.svc.cluster.local. rather than payments.billing.
Fix 2: lower ndots for the pods that need it
dnsConfig merges extra options into what the cluster policy generates. It has been stable since Kubernetes 1.14:
spec:
dnsConfig:
options:
- name: ndots
value: "2"
containers:
- name: checkout
image: registry.internal/checkout/api:4.1.0With ndots:2, api.example.com (two dots) is tried as written first. Short in-cluster names still work: payments has no dots and payments.billing has one, so both still go through the search list. The trade-off is a name with two or more dots, such as payments.billing.svc. That is now tried as an absolute name first, and the lookup fails there before falling back to the search list, which costs one extra round trip. Teams that use only service or service.namespace for internal calls lose nothing.
Set it per workload, starting with the heaviest callers of external services, rather than rewriting cluster defaults. The dnsPolicy stays ClusterFirst, which is what pods get when the field is not set. Only dnsPolicy: None requires you to provide every resolver setting yourself.
Fix 3: let CoreDNS search on the server side
The CoreDNS autopath plugin follows the search path itself and returns the first answer that is not NXDOMAIN, so the pod makes one query instead of several:
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods verified
fallthrough in-addr.arpa ip6.arpa
}
autopath @kubernetes
forward . /etc/resolv.conf
cache 30
}It comes with costs. The plugin needs pods verified, and the kubernetes plugin documentation notes that this mode uses substantially more memory because CoreDNS then watches all pods. It also works out the search path from the source IP of the query. If a pod's IP is reused by a pod in another namespace before CoreDNS has seen the update, the answer can come from the wrong namespace's search path. And if the server-side search ends in a negative answer, clients fall back to searching every path themselves, which cancels the saving. Prefer Fix 2 where you can, and keep autopath for clusters where you cannot touch pod specs.
Fix 4: put a cache on every node
NodeLocal DNSCache (stable since Kubernetes 1.18) runs a caching agent as a DaemonSet on each node. Pods query it on the same node, which avoids the DNAT rules and connection tracking that the path to the CoreDNS Service goes through. Only cache misses for cluster names go on to CoreDNS.
It addresses two problems that ndots tuning does not:
- Conntrack pressure. UDP conntrack entries are not removed on close. They wait for a timeout, 30 seconds by default for
nf_conntrack_udp_timeout. Heavy DNS traffic fills the table, and races in conntrack can drop packets. - Slow failures. The local agent can connect to CoreDNS over TCP, and TCP conntrack entries are removed when the connection closes. The documentation attributes timeouts of up to 30 seconds to dropped UDP packets (3 retries and a 10-second timeout), and TCP upstream avoids that. Pods still use UDP to the local agent, so applications need no change.
A cache also absorbs repeated lookups of the same external name, so it lowers load even for pods that still send the extra search-path queries. Those queries still happen, however. The cache does not remove the NXDOMAIN lookups, but it answers them from memory on the node, with negative caching available.
Check the CoreDNS cache and scale
Whatever else you change, look at three CoreDNS settings:
- The
cacheplugin. Its default maximum TTL is 3600 seconds for successful answers and 1800 for denials (cache plugin). Thecache 30line in the example above caps both at 30 seconds, which is a common choice because it keeps changes visible quickly.successanddenialblocks size each cache separately, andprefetchrefreshes popular entries before they expire. - Replica count. CoreDNS is usually sized with the cluster-proportional autoscaler or by hand. Scale it on query rate and CPU, and spread replicas across nodes and zones with topology spread constraints, so that losing one node does not remove most of your DNS capacity.
- Search domain limits. Kubernetes accepts up to 32 search domains and 2048 characters in total, but older runtimes (containerd 1.5.5 or earlier, CRI-O 1.21 or earlier) apply smaller limits, and pods can then stay in
Pending. This matters if your node image adds many domains of its own.
Which fix to use
| Situation | Start with |
|---|---|
| A few services call external APIs heavily | ndots in dnsConfig for those workloads |
| Intermittent 5-second pauses, conntrack table errors | NodeLocal DNSCache |
| Whole cluster has high NXDOMAIN share and you cannot edit specs | autopath, with memory headroom |
| One client library with a known hot path | Absolute names, after testing TLS and routing |
These combine well. A node-local cache plus a lower ndots for the heaviest callers handles most clusters, and neither needs changes to application code.
Verify the change
Compare before and after, using the same traffic:
- Record the NXDOMAIN share and request rate per CoreDNS pod from the queries above.
- Roll out the change to one workload or one node pool.
- Check that internal names still resolve from that pod, both the short forms your code uses and the full
svc.cluster.localones:kubectl exec -it <pod> -- getent hosts payments.billing. - Watch the CoreDNS request rate, the NXDOMAIN share and the application's outbound-call latency percentiles for a day or two before widening the rollout.
DNS failures show up as slow or failing requests elsewhere, so a rollout that only checks that pods are running will miss them. Keep the old resolv.conf behaviour one kubectl rollout undo away until the numbers have moved the way you expected.