DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · DNS · 36 OF 187

How DNS Works: Resolvers, Records, TTL and dig

DNS from the inside: stub resolver, recursive resolver, root, TLD and authoritative servers; the records you will set (A, AAAA, CNAME, MX, TXT, NS) queried with dig against our own lab zone; TTL counting down in a cache (300, then 297 three seconds later), REFUSED versus NXDOMAIN, /etc/resolv.conf, DNS in Kubernetes and the cloud, and safe DNS changes.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Stub → recursive → root → TLD → authoritative
  • A, CNAME, MX, TXT
  • TTL 300 → 297
  • NXDOMAIN vs SERVFAIL
  • dig

Notes

Stub and resolver

Your application does not do much. It calls the system's resolver library, which checks etc hosts first, then asks the nameserver listed in etc resolv dot conf. That server is a recursive resolver: on a laptop, your router or ISP; in AWS, the VPC resolver; in Kubernetes, CoreDNS. Its job is to find the answer, however many servers it has to ask, and cache it.

On the machine
$ cat /etc/resolv.conf
nameserver 10.0.0.2
search svc.cluster.local

app → libc getaddrinfo()
  → /etc/hosts first
  → the nameserver listed
systemd-resolved, cloud VPC
  resolver, CoreDNS in k8s

Recursion

On a cold cache, the resolver walks the tree. It asks a root server, which does not know the answer but says ask the.com servers. The.com servers say shoprung.com is served by these name servers. The authoritative server, run by your DNS provider, gives the real answer with a TTL. The resolver caches every step, so the next lookup is instant. dig plus trace shows this walk.

A cold lookup
  • 1 root: "ask the .com servers"
  • 2 .com: "shoprung.com is served by ns1.provider"
  • 3 authoritative: "A 203.0.113.10, TTL 300"
  • resolver caches every step
  • $ dig +trace shows the path

Records

Here are the records you will set, from our own lab zone served by dnsmasq. An A record maps a name to an IP version four address: ten dot zero dot one dot twenty, with a TTL of three hundred seconds. A CNAME makes www an alias for another name. MX says where email goes. TXT holds text, here an SPF email policy, and is used for domain verification. AAAA is the IP version six address, NS names the authoritative servers, and CAA limits who may issue certificates.

dig @127.0.0.1 -p 5300
shoprung.test  300 A
  10.0.1.20
www  CNAME shoprung.test.
MX   10 mail.shoprung.test.
TXT  "v=spf1 -all"

also: AAAA (IPv6), NS (who
  is authoritative), SRV,
  CAA (who may issue certs)

TTL and caching

Every answer carries a TTL, how long it may be cached. We queried a name through a caching resolver and got three hundred seconds. Three seconds later the same query returned two hundred and ninety seven: the answer came from the cache and the TTL was counting down. This is why DNS changes are not instant: clients keep old answers until their TTL runs out. Before a migration, lower the TTL a day ahead.

Through our caching resolver
$ dig api.shoprung.test
api...  300  IN A  10.0.1.21
  (3 seconds later)
api...  297  IN A  10.0.1.21

TTL counts down in the cache
a change is seen only after
  old TTLs expire
lower TTL before a migration

Error answers

When something is wrong, read dig's status line. NOERROR means it worked, or that the name exists without that record type. NXDOMAIN means the name does not exist: a typo or a missing record. SERVFAIL means the resolver tried and failed, often a broken upstream or DNSSEC problem. REFUSED means the server will not answer you; our lab server refused names outside its zone because we gave it no upstream.

dig status
  • NOERROR answer found (or the name exists with no record of that type)
  • NXDOMAIN name does not exist
  • SERVFAIL resolver could not get an answer (DNSSEC, upstream down)
  • REFUSED server will not answer you (ours: no upstream configured)

Debugging

Debugging DNS means comparing answers. dig plus short gives just the result. dig at a public resolver tells you what the world sees. dig at your authoritative server tells you what you actually published. If those differ, it is caching. dig plus trace walks the delegation. And getent hosts shows what your application really gets, including etc hosts entries that dig ignores, a common source of confusion.

dig habits
$ dig shoprung.com +short
$ dig @1.1.1.1 shoprung.com
$ dig @ns1.provider ...
  ask the authoritative
$ dig +trace shoprung.com
$ getent hosts shoprung.com
  what the app really sees
  (incl. /etc/hosts)

In clusters and clouds

In the cloud, Route fifty three hosts public zones and private zones visible only inside your VPCs, and the VPC resolver sits at the base address plus two. In Kubernetes, every service gets a name like service dot namespace dot svc dot cluster dot local, answered by CoreDNS; its default ndots of five makes external lookups try several search domains first. And cloud load balancers change IPs, so always point at their DNS name.

Where you meet DNS
  • AWS Route 53: public and private hosted zones
  • VPC resolver at base + 2
  • Kubernetes: svc.ns.svc. cluster.local via CoreDNS
  • ndots:5 → extra lookups for external names
  • load balancers change IPs: always use their DNS name

Common mistakes

Mistakes. Keeping a long TTL during a migration. A CNAME at the zone apex, which DNS does not allow; providers offer alias records instead. Hardcoding a load balancer's IP address. Testing with dig while the application reads etc hosts. And forgetting that NXDOMAIN answers are cached too, so querying a name before you create it can delay it.

Avoid these
  • high TTL during a migration
  • CNAME at the zone apex (use ALIAS/ANAME)
  • hardcoding a load balancer's IP
  • testing with dig, while the app reads /etc/hosts
  • forgetting negative caching of NXDOMAIN

Say it like this

In the interview, the DNS part of what happens when you type a URL: the stub resolver checks etc hosts and its cache, then asks the configured recursive resolver. On a miss the resolver asks a root server, the TLD servers, then the domain's authoritative servers, and caches each answer for its TTL. To debug, I compare dig against the resolver and the authoritative server, and check TTLs.

In the interview
  • "The stub resolver checks
  • /etc/hosts, then asks the recursive resolver. On a miss it
  • asks a root, the TLD, then the authoritative server, and caches
  • by TTL. I debug with dig against
  • the resolver and the authoritative, and check TTLs."

Interview questions

Q1What is the difference between a recursive resolver and an authoritative server?

The authoritative server holds a zone's real records; the recursive resolver finds answers for clients by querying root, TLD and authoritative servers, and caches them.

Q2What is the difference between an A record and a CNAME?

An A record maps a name to an IPv4 address; a CNAME makes a name an alias of another name, which is then resolved further.

Q3What is TTL in DNS?

Time to live: how many seconds a resolver may cache an answer. Lower it before migrations so changes propagate quickly.

Q4NXDOMAIN vs SERVFAIL?

NXDOMAIN means the name does not exist; SERVFAIL means the resolver could not get a valid answer, for example an upstream or DNSSEC failure.

Q5How does service discovery work in Kubernetes?

CoreDNS answers names like my-svc.my-namespace.svc.cluster.local with the Service's cluster IP (or pod IPs for headless services).

Q6Why can dig and your application disagree?

dig queries DNS directly, while applications use the system resolver, which also reads /etc/hosts, search domains and local caches; use getent hosts.

Q7Why can't you put a CNAME at the zone apex?

The apex must hold SOA and NS records, and a CNAME cannot coexist with other records; DNS providers offer ALIAS or ANAME records instead.