NodeLocal DNSCache

Under load, DNS lookups from Pods can stall for seconds, and some of them end in a timeout. What does a DNS cache on every Node change about the path a lookup takes, and what still goes to CoreDNS?

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

NodeLocal DNSCache diagram: NodeLocal DNSCache in kube-proxy iptables mode

NodeLocal DNSCache, step by step

  1. Without a local cache, each lookup from a ClusterFirst Pod is a UDP query to the kube-dns ClusterIP 10.96.0.10. The kube-proxy rules on Node-1 DNAT it to a CoreDNS Pod, here on Node-2, and the flow opens a conntrack entry that must time out. When its A and AAAA queries race on that entry, one can be dropped, and the resolver waits out its five second timeout.

    Play from step 1
  2. A DaemonSet runs node-local-dns, a caching CoreDNS, on the host network of every Node. In iptables mode it binds a link-local address, here 169.254.20.10, and the kube-dns ClusterIP with NOTRACK rules, so the same query stops at the agent: no DNAT, no conntrack entry, no change to Pods. In the deprecated IPVS mode the agent binds only the first address and needs the Kubelet clusterDNS changed.

    Play from step 2
  3. The cache is empty, so the agent forwards the cluster-domain name to CoreDNS. The agent already owns 10.96.0.10, so it dials a second Service, kube-dns-upstream, over TCP. That leg is DNAT-ed and tracked, but a TCP entry is removed when the connection closes. The answer is cached, then returned to the Pod.

    Play from step 3
  4. The next lookup for that name is answered from the cache and never leaves Node-1. A positive answer is kept for its record TTL, held between 5 and 30 seconds, and a negative one for 5. Once it expires, the next lookup goes to CoreDNS again.

    Play from step 4
  5. By default only the cluster domain and the reverse zones go to CoreDNS. A name such as example.com matches the catch-all zone of the agent, which forwards to the resolvers in the resolv.conf of the Node, and the answer is still cached on Node-1. Of that lookup only the ndots:5 search-list tries in the cluster domain reach CoreDNS.

    Play from step 5

Sources