Diagnosing Name Resolution
Goal
You check which path name resolution takes, then work through two incidents with the same symptom, "the name resolves to the wrong address", but different causes, and tell them apart.
Why it matters
The application calls glibc's getaddrinfo(), and glibc asks in the order given by /etc/nsswitch.conf, in its hosts: line (usually files dns — that is, /etc/hosts first). dig, on the other hand, skips all of this and asks the nameserver in /etc/resolv.conf directly, and by default it does not attach search domains either. If the answers of the two tools differ, the problem is not the DNS server but the stub resolver segment.
The two incidents at the end have the same symptom: a name resolves to the wrong address. In one, the culprit is a hosts line put in during earlier work; in the other, resolver configuration that should have disappeared. Instead of losing a day waiting for the TTL because "DNS hasn't propagated yet", you practice finishing it in a few minutes just by holding the answers of the two tools up against each other.
Steps
You work in the /root/dns directory.
- Write the address of the DNS server that this Pod's stub resolver asks first to
/root/dns/nameserver.txt. - In
/etc/hosts, register10.99.0.10under two names,app.labhub.localand the short nameapp. - From
/etc/nsswitch.conf, take the value of thehosts:entry (everything after the colon) and write it to/root/dns/order.txt. If the file does not exist, writeunavailable. - Ask for the same name
app.labhub.localwithgetentand withdigrespectively, and write the results to/root/dns/compare.txtas two lines,getent=<result>anddig=<result or none>. - Write three lines to
/root/dns/search.txt:search=<search domains or none>,ndots=<value>, andpay.corp.test=<absolute or search>. - In
/etc/hosts, registerapi.labhub.localon two lines,10.99.0.21(first) and10.99.0.22(after it), and write the IP that actually comes back to/root/dns/first.txt. - Incident
incident ln-dns-moved: the payment server was moved, but only this server's app goes to the old address. Fix it and report the cause. - Incident
incident ln-dns-ghost: even names that do not exist resolve to 10.66.0.1. Fix it while keeping the original resolver configuration alive, and report the cause.
Notes
- Incident tools:
incident listshows the incidents you can raise and a one-line symptom for each, andincident show <name>shows the ticket again. Raising the same incident again returns it to its original broken state. - The report is two lines in
/root/incident/<incident-name>/report.txt:cause: <candidate-code>andevidence: <one output line that shows the cause>. Grading looks not only at the report but also at the actual state after your fix (getent's answers, file contents). - In this Pod,
/etc/hostsand/etc/resolv.confare bind mounts, so appending and writing in place work, but edits that create a new file and swap it in (sed -iand the like) fail. - Common mistake 1: in step 4, you must not judge it a failure because the dig result is empty. For a name that exists only in hosts, it is normal for DNS not to know it.
- Common mistake 2: in the incidents, repairs that only erase the symptom (patching a new line in at the very top, killing only the responder, rewriting resolv.conf from scratch) invite the same fault again next time. The grading catches them too.
Check the resolver address
Write the address of the DNS server that this Pod's stub resolver asks first to /root/dns/nameserver.txt.
A single file decides which server a program asks (/etc/resolv.conf). If there are several server lines, they are used from the top, so write only the address on the first one.
Register a name in the hosts file
In /etc/hosts, register 10.99.0.10 under two names, app.labhub.local and the short name app.
A line in hosts is "an address followed by names". All the names after it become aliases for the same address. After registering, check both names not with dig but with a tool that asks along the same path as the app.
Check the name service order
From /etc/nsswitch.conf, take the value of the hosts: entry (everything after the colon) and write it to /root/dns/order.txt. If the file does not exist, write unavailable.
This value decides "whether /etc/hosts beats DNS". Write only the value after the colon, joined with single spaces.
Compare getent and dig
Ask for the same name app.labhub.local with getent and with dig respectively, and write the results (addresses) to /root/dns/compare.txt as the following two lines. If dig gives no answer at all, write none.
getent=<result>dig=<result or none>
The two tools ask along different paths: one follows the nsswitch order, the other goes straight to the server in resolv.conf. If you make dig wait only briefly, it does not hang for long when there is no answer. Different results do not mean something is wrong.
Read the search domains and ndots
Read /etc/resolv.conf and write three lines to /root/dns/search.txt.
search=<search domains actually used, space-separated, or none>ndots=<ndots value, or the default if not set>pay.corp.test=<absolute or search>: when asking forpay.corp.test, a name with two dots, whether the stub resolver first asks for the name as is (absolute) or for the name with the search domain attached (search)
If there are several search lines, only the last one is used. ndots is on the options line, and if it is absent, glibc's default is used (man resolv.conf). If a name has fewer dots than ndots, the search domain is tried first; if there is no search domain at all, there is nothing to attach.
Two lines for the same name: which one wins
In /etc/hosts, register api.labhub.local on two lines. 10.99.0.21 first, then 10.99.0.22 after it. Then write the IP that actually comes back, when asked along the path the app uses, to /root/dns/first.txt.
The files backend reads the file from the top and stops at the first match. Since the order changes the result, append so that 21 comes on top, and actually ask to confirm before writing it down.
Incident: only this server's app cannot reach the moved server
Running incident ln-dns-moved shows a ticket. Fix it so that this server's app (the path glibc's getaddrinfo uses) finds pay.corp.test at its new address. The internal DNS and the deployment tool's managed block in /etc/hosts (# BEGIN corp-managed to # END corp-managed) are correct, so do not touch them, and other service names and /etc/nsswitch.conf must also be left as they are. In /root/incident/ln-dns-moved/report.txt, write cause: <code> and evidence: <one observed output line>. Candidate codes: dns-record (the record in the internal DNS is stale), hosts-stale (a stale line in /etc/hosts wins), local-resolver (a local resolver gives a wrong answer or does not answer), nsswitch-order (the name service order is flipped), search-ndots (another name with a search domain attached resolves instead).
Direction: first determine whether the answer the app sees and the internal DNS's answer are the same. If they differ, the problem is not the DNS server but the stub segment in front of it; if they are the same, it is the server side. Tools: getent, dig (you can specify which server to ask), grep. Concretely: look at what canonical name the getent output shows, the hosts: order in nsswitch, and every line in /etc/hosts where that name appears, along with their order. /etc/hosts is a bind mount, so sed -i (swapping in a new file) fails; build the fixed content and write it back in place.
Incident: even names that do not exist resolve
After running incident ln-dns-ghost, fix it so that names that do not exist (legacy-ftp.example, pay.corp.tset) no longer resolve to 10.66.0.1 along the path the app uses (getent). This server's original resolver configuration (the nameserver list, search, and options of /etc/resolv.conf before the incident) must stay alive as it was. In /root/incident/ln-dns-ghost/report.txt, write cause: <code> and evidence: <one observed output line>. The candidate codes are the same as in the previous step: dns-record (the record in the internal DNS is stale), hosts-stale (a stale line in /etc/hosts wins), local-resolver (a local resolver gives a wrong answer or does not answer), nsswitch-order (the name service order is flipped), search-ndots (another name with a search domain attached resolves instead).
Direction: if getent and dig give different answers, before suspecting the server, suspect the difference in "how" the two tools ask for the name. Tools: getent (there is a key that shows even the canonical name), dig, cat. Concretely: look at what is attached to the end of the canonical name in the getent output, whether dig uses search domains by default (it does not; there is a separate option to make it), the search line actually in use in resolv.conf (the last one) and the ndots in options, and where the lines vpn-agent left behind start and end.