Search Domains and ndots

A Pod looks up one external name, api.example.com, and the cluster DNS gets eight queries for it, then eight more on the next call. Why does one lookup cost so many, and how do you cut them down to two?

Play the animation 5 steps, animated, plays on a computer or a tablet

Search Domains and ndots, Kubernetes diagram

Search Domains and ndots, step by step

  1. With the default ClusterFirst policy the Kubelet configures a resolv.conf where the nameserver is the kube-dns Service, search domains come from the namespace and the cluster domain, and ndots:5 is set. A name with fewer than 5 dots counts as relative, so the resolver tries it with every search domain first and as written last. Node search domains, if any, go last in the list.

    Play from step 1
  2. The name api has zero dots, so it is relative. The first candidate, api.default.svc.cluster.local, is a Service in the same namespace, so CoreDNS answers with its ClusterIP and the walk stops there. One name tried, and still two queries, because getaddrinfo asks for A and AAAA together.

    Play from step 2
  3. The name api.shop has one dot, still under 5. The first candidate, api.shop.default.svc.cluster.local, does not exist and comes back NXDOMAIN. The second, api.shop.svc.cluster.local, is that Service in the shop namespace, and this time an answer comes back. The search list exists so that a name relative to the cluster resolves without its full suffix.

    Play from step 3
  4. The name api.example.com has two dots, so it is relative too, and no cluster suffix matches it. Three round trips end in NXDOMAIN before the name as written is asked and answered. That makes four names and eight queries for a single external lookup, the cost of ndots:5, paid again on every call, since the resolver inside the Pod keeps no cache.

    Play from step 4
  5. A trailing dot makes a name absolute whatever ndots says, as in api.example.com., so the resolver skips every search domain and asks it once as written: one name, two queries. Fully qualifying hot external names, or lowering ndots through the Pod dnsConfig, is the usual fix for noisy cluster DNS.

    Play from step 5

Sources