Lesson 104 · Section 9: Networking
Lesson 104: DNS: how Linux turns a name into an address
Name resolution on a plain Linux machine, before Kubernetes adds its own names: /etc/hosts, /etc/resolv.conf, the lookup order in nsswitch.conf, search domains, record types, and the difference between what nslookup, dig and your application see. · 14 min read · 3 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why names
In the last lesson, machines found each other by IP address. That works for computers and fails for people. Nobody
wants to remember 192.168.1.11, and an address changes when a machine is replaced, while the name db can stay
the same for years.
Name resolution is the step that turns a name into an address before any connection is made. It is worth being
exact about that order: an application first resolves db to 192.168.1.11, and only then opens a connection to
that address. If resolution fails, no packet is ever sent to the target, and no route or firewall is to blame.
This lesson stays on a plain Linux machine. That is on purpose. A container has the same three files as a server, and a Pod resolves a Service name with exactly the mechanism described here. The lesson DNS in Kubernetes then only has to add which names exist.
The hosts file
The oldest and simplest form of name resolution is a text file on each machine.
# /etc/hosts
127.0.0.1 localhost
192.168.1.11 db
192.168.1.20 web web.example.com
Each line is an address followed by one or more names. After adding the second line on host A, ping db and
ssh db reach 192.168.1.11.
Two properties of this file are useful to know:
- The machine believes it without checking. If you write
192.168.1.11 www.google.com, this machine will send traffic for Google to your database server. The file says what the address of a name is, as far as this host is concerned. That makes it a handy tool for tests. - It is local. The entry exists on this machine only. With three hosts you can keep the files in step by hand. With three hundred, every new server means editing three hundred files.
A DNS server
The fix for the second problem is to keep the list in one place and let everyone ask. That place is a DNS server: a program that listens on port 53 (UDP, and TCP for large answers) and answers the question "what is the address of this name?".
A machine learns where its DNS server is from /etc/resolv.conf:
# /etc/resolv.conf
nameserver 192.168.1.100
Now a new host is added once, on the server, and every machine can resolve it. The hosts file does not go away. It stays useful for a private name on one machine, and it is still read first.
Line in resolv.conf | Meaning |
|---|---|
nameserver 192.168.1.100 | A DNS server to ask. Up to three lines, tried in order: the second is used only when the first does not answer |
search example.com prod.example.com | Domains to append to short names, tried in order |
options ndots:1 | How many dots a name needs before it is tried as it is, ahead of the search list (default 1) |
options timeout:2 attempts:2 | How long to wait for a server and how often to retry |
The lookup order
Parts:
- Application (curl http://db)
- /etc/hosts (local list of names)
- DNS server (192.168.1.100:53)
- nsswitch.conf (hosts: files dns)
- /etc/resolv.conf (nameserver, search)
Connections:
- Application to /etc/hosts: files
- /etc/hosts to /etc/resolv.conf: not found
- /etc/resolv.conf to DNS server: asks the nameserver
- nsswitch.conf to Application: sets the order
What happens when a name is in both places with different addresses? A third file decides,
/etc/nsswitch.conf:
# /etc/nsswitch.conf
hosts: files dns
files means /etc/hosts, and dns means the servers in /etc/resolv.conf. With this usual order the hosts file
wins, and DNS is asked only when the file has no entry. Swap the two words and DNS is asked first.
Under the hood · Why does my resolv.conf say 127.0.0.53?
On Ubuntu and other systemd distributions, /etc/resolv.conf often contains nameserver 127.0.0.53. That is not
an error. It is systemd-resolved, a small local resolver that listens on the machine itself, caches answers and
forwards to the real DNS servers. resolvectl status shows which servers it really uses, and the file
/run/systemd/resolve/resolv.conf lists them in the classic format.
This matters for Kubernetes in one place: a Pod must never be given 127.0.0.53 as its upstream DNS server,
because inside the Pod that address is the Pod itself. kubeadm detects systemd-resolved and points the kubelet at
the real file with the resolvConf setting. If CoreDNS reports a forwarding loop, this is one of the first things
to check.
Domain names and search domains
Names in DNS are built from right to left, with dots between the parts:
www.example.com.
| | | └── the root, usually not written
| | └───── top-level domain
| └─────────── the domain
└───────────────── a host or subdomain inside it
A name written out to the top is a fully qualified domain name (FQDN). A DNS server stores and answers full
names only. Inside a company you still want to type web instead of web.example.com, and the search line
makes that possible.
Parts:
- ping web (a short name)
- /etc/resolv.conf (search example.com)
- DNS server (knows web.example.com)
- 192.168.1.20 (the A record)
Connections:
- ping web to /etc/resolv.conf: web
- /etc/resolv.conf to DNS server: full
- DNS server to 192.168.1.20: answers
# /etc/resolv.conf
nameserver 192.168.1.100
search example.com prod.example.com
With this file, a lookup of web tries web.example.com, then web.prod.example.com, and stops at the first that
exists.
The ndots option decides what happens with names that already contain dots. The rule: if the name has fewer
dots than ndots, the search domains are tried first and the name as typed is tried last. With the default
ndots:1, a name with at least one dot, such as web.prod, is tried as it is first.
A name that ends in a dot is treated as complete, and the search list is skipped entirely: web.example.com.
is looked up exactly once.
Record types
A DNS server does not only map names to IPv4 addresses. Each entry is a record with a type.
| Type | Maps | Example | Where you meet it in Kubernetes |
|---|---|---|---|
| A | A name to an IPv4 address | web.example.com → 192.168.1.20 | Every Service name resolves to its ClusterIP |
| AAAA | A name to an IPv6 address | web.example.com → 2001:db8::20 | Services in IPv6 and dual-stack clusters |
| CNAME | A name to another name (an alias) | shop.example.com → web.example.com | A Service of type ExternalName |
| SRV | A service name to a host and port | _http._tcp.web… → 80 web… | Named ports of Services |
| PTR | An address back to a name (reverse lookup) | 192.168.1.20 → web.example.com | Reverse lookups of Service and Pod IPs |
| NS | A domain to the servers responsible for it | example.com → ns1.example.com | Rarely |
| TXT | A name to free text | verification strings | Rarely |
Every record also has a TTL (time to live), in seconds. It tells whoever received the answer how long it may be kept in a cache before asking again.
How a public name is found
Parts:
- Your machine (www.example.com?)
- Your DNS server (resolves and caches)
- Root servers (know who runs .com)
- .com servers (know example.com)
- example.com servers (hold the records)
Connections:
- Your machine to Your DNS server: asks once
- Your DNS server to Root servers: 1. who has .com?
- Your DNS server to .com servers: 2.
- Your DNS server to example.com servers: 3. www.example.com?
No single server knows every name on the internet. Responsibility is split along the dots. When your DNS server is
asked for www.example.com and has no cached answer, it walks down the tree: a root server says who runs
.com, a .com server says who runs example.com, and one of those servers finally holds the record.
Your machine does none of that walking. It asks its configured server once and waits for the final answer. Then both keep it for as long as the TTL allows. Caching is why the second lookup is instant, and also why a changed record can take a while to be seen everywhere.
The same idea runs through a cluster. A Pod asks CoreDNS. CoreDNS answers cluster names itself, and forwards
everything else to the DNS servers of the node. So a Pod can resolve both web.shop.svc.cluster.local and
www.example.com through one nameserver.
The tools, and what each one really asks
This table is the most practical part of the lesson. The tools do not all resolve names in the same way.
| Tool | Reads /etc/hosts? | Uses search domains? | Use it to |
|---|---|---|---|
ping, curl, ssh, your application | Yes | Yes | Do the real thing |
getent hosts <name> | Yes | Yes | See what an application would get, without connecting |
nslookup <name> | No | Yes | Ask the DNS server directly |
dig <name> | No | No, unless you add +search | Read the full DNS answer in detail |
host <name> | No | Yes | A short form of nslookup |
So if ping db works and nslookup db says NXDOMAIN, nothing is broken: the name is in the hosts file and not
on the DNS server.
Two answers from a DNS tool are worth telling apart:
NXDOMAIN: the server answered, and the name does not exist. The server is reachable. The name is wrong, or the search list did not complete it the way you expected.connection timed out; no servers could be reached: nobody answered on port 53. This is a network problem or a wrongnameserveraddress, not a naming problem.
Using it
Example · Give a machine a private name for a test
You want curl http://shop.example.com on this host to reach a test server at 192.168.1.50, without touching
the DNS server.
echo "192.168.1.50 shop.example.com" | sudo tee -a /etc/hosts
getent hosts shop.example.com
192.168.1.50 shop.example.com
getent confirms what applications will now see. nslookup shop.example.com would still show the public address,
because it does not read the file. Remove the line when you are done: forgotten hosts entries are a classic source
of "it only fails on this machine".
Example · Point a machine at a DNS server and add a search domain
cat /etc/resolv.conf
nameserver 192.168.1.100
search example.com
nslookup web
Server: 192.168.1.100
Address: 192.168.1.100#53
Name: web.example.com
Address: 192.168.1.20
The Server line tells you who answered, and the Name line shows the full name that was found, with the
search domain added. Read both every time: they tell you which server and which name were really used.
Example · Read a full answer with dig
dig web.example.com
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41873
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; QUESTION SECTION:
;web.example.com. IN A
;; ANSWER SECTION:
web.example.com. 3600 IN A 192.168.1.20
;; Query time: 1 msec
;; SERVER: 192.168.1.100#53(192.168.1.100) (UDP)
Four things to read: status (NOERROR or NXDOMAIN), the question (the exact name and type that was asked),
the answer (name, TTL, type, value), and the server that replied. Useful variations:
dig +short web.example.com # only the address
dig @8.8.8.8 www.example.com # ask a specific server
dig web.example.com AAAA # ask for another record type
dig -x 192.168.1.20 # reverse lookup (PTR)
dig +search web # apply the search list, like other tools
Example · Look at name resolution from inside a Pod
The same files exist in every container. Three commands show the whole setup of a Pod:
kubectl exec -it cart -- cat /etc/resolv.conf
kubectl exec -it cart -- cat /etc/hosts
kubectl exec -it cart -- nslookup web
search shop.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
# Kubernetes-managed hosts file.
127.0.0.1 localhost
10.244.1.7 cart
Server: 10.96.0.10
Address: 10.96.0.10:53
Name: web.shop.svc.cluster.local
Address: 10.96.14.20
The nameserver is the cluster's DNS Service, the search list starts with the Pod's own namespace, and the hosts file is written by the kubelet with the Pod's own name. Everything you learned on this page applies unchanged.
Tricks
Common mistakes
- Expecting
nslookupordigto see/etc/hosts. They ask the DNS server only. Usegetent hoststo test what applications see. - Listing two nameservers that know different names. The second is only used when the first does not answer at
all. An
NXDOMAINfrom the first is final. - Reading
NXDOMAINas "DNS is down". It means the opposite: a server answered. The name, or the search list, is wrong. - Editing
/etc/resolv.confby hand on a machine that generates it. With systemd-resolved, NetworkManager or DHCP, the file is rewritten and your change disappears. Change the tool's configuration instead. - Forgetting old entries in
/etc/hosts. They win over DNS, silently, long after the test they were added for. - Blaming the network when a name fails. Resolution happens before the connection. Test the IP address directly: if that works, the problem is naming and nothing else.
Recap
- Resolution comes before the connection: name to address first, then packets.
/etc/hostsis a local list that is believed without checks./etc/resolv.confnames the DNSnameserverand thesearchdomains./etc/nsswitch.confsets the order, usuallyfiles dns.- Search domains complete short names.
ndotsdecides when they are tried first. A trailing dot skips them. - Records have types: A and AAAA for addresses, CNAME for aliases, SRV for name and port, PTR for reverse lookups.
nslookupanddigask the DNS server and ignore the hosts file.getent hostsresolves like an application.- A Pod resolves names the same way, with a
resolv.confwritten by the kubelet that points at CoreDNS.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.