Lesson 3 of 8 in Networking for DevOps
DNS: how names become addresses
DNS is the distributed system that turns names such as example.com into addresses and other records. A resolver asks a chain of servers for the answer on your behalf and caches it for a time set by the record's TTL.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: IP addresses, CIDR and subnets
The problem this solves
People and configuration files use names, while the network only moves packets between addresses. Hard-coding addresses breaks the moment a server moves, so every service needs a lookup layer.
DNS is also the first thing that fails and the last thing people check. A wrong record, a stale cache or a missing entry produces errors that look like the service is down.
How it works
- People and clients
- Network
- Control
- Data
Your app asks a resolver, and the resolver asks the root, the top-level domain and the authoritative servers in turn before returning the answer.
- Your app to Resolver: 1 ask, 5 answer back
- Resolver to Root servers: 2 who runs .com?
- Resolver to .com servers: 3 who runs example?
- Resolver to Authoritative: 4 what is the address?
Your program does not talk to the root servers. It asks a recursive resolver, usually one run by your network, your cloud or a public provider. If the resolver already has an unexpired answer, it replies from its cache. Otherwise it does the walk in the diagram.
Each step narrows the question. The root servers say which servers handle .com. The .com servers say which servers are authoritative for example.com. The authoritative servers hold the actual records and give the final answer, which the resolver caches and passes back.
Common record types are A (name to IPv4 address), AAAA (IPv6), CNAME (this name is an alias for another name), MX (mail), TXT (free text, used for verification) and NS (which servers are authoritative). Every record has a TTL, the number of seconds a cache may keep it.
Before DNS is asked, the system normally checks local sources such as /etc/hosts. So the address your application sees can differ from the one dig shows, because dig queries DNS directly and skips those local sources.
Try it yourself
These commands only read DNS. dig ships in the dnsutils or bind-utils package on Linux and is present on macOS.
- 1See which resolver your machine uses
cat /etc/resolv.conf
A
nameserverline names the resolver. On many Ubuntu systems it shows a local address such as127.0.0.53, which is a local forwarder. On macOS usescutil --dns. - 2Look up a record
dig example.com A +noall +answer
The answer line has the name, the TTL in seconds,
IN, the record type and the address. Run it twice and watch the TTL count down when your resolver serves a cached answer. - 3Watch the whole walk from the root
dig +trace example.com
This performs the lookup itself, step by step. You will see the root servers, then the
.comservers, then the servers for the domain. - 4Trigger a name that does not exist
dig this-name-does-not-exist.invalid
Find the header line with
status: NXDOMAIN, which means the name does not exist. The.invalidending is reserved and never resolves. - 5Compare two resolvers
dig @1.1.1.1 example.com +short dig example.com +short
@1.1.1.1sends the query to Cloudflare's public resolver instead of your default. Differing answers point at caching or split-horizon DNS. - 6See what applications see
getent hosts example.com
getentuses the same lookup path as most programs, including/etc/hosts. On macOS usedscacheutil -q host -a name example.com.
What breaks, and where to look
curl: (6) Could not resolve host: api.example.internal, ordigshowsstatus: NXDOMAIN.- WHY IT HAPPENS
- The name has no record in the zone the resolver consults. Common reasons are a typo, a record never created, or a private name asked of a public resolver that cannot see it.
- CHECK FIRST
- Run
dig <name>and read the status line. Then rundig @<authoritative-or-internal-resolver> <name>to see whether the record exists at the source.
You changed a record and some machines still go to the old address.
- WHY IT HAPPENS
- Resolvers cache the old answer until its TTL runs out. A long TTL set before the change means the old value can linger for that long.
- CHECK FIRST
- Run
dig <name>and read the TTL in the answer. Lower the TTL well before a planned move, then change the record after the old TTL has passed.
digreturns the right address and your application connects somewhere else, or fails.- WHY IT HAPPENS
- The application uses the system lookup path, which can include
/etc/hosts, search domains or a different resolver, anddigbypasses them. - CHECK FIRST
- Compare
dig <name> +shortwithgetent hosts <name>, then read/etc/hostsand/etc/resolv.conf.
When not to use this
- DNS is a poor tool for instant failover. Caches obey the TTL in their own time, and some clients ignore it, so use a load balancer or health-checked routing when a switch must happen at once.
/etc/hostsis fine for a quick local test and a hazard in production, because the override is invisible to everyone else and easy to forget.
Check yourself
Why does your app ask a resolver instead of the root servers?
The resolver does the multi-step lookup for you and caches results, so most lookups are answered without repeating the walk.
What does the TTL on a record control?
How many seconds a resolver may reuse the answer before asking again.
What does status: NXDOMAIN mean?
The queried name does not exist in DNS.
Keep going
Lesson 18 of 92 in the DevOps path