Sub topic 10
Reading resolv.conf: search domains and ndots
This chapter assumes you know basic kubectl usage, how Pods and namespaces work, and what a DNS A record is. Earlier chapters covered Service DNS names such as web.default.svc.cluster.local. Here you will read the file that decides how a Pod turns a short name into a lookup.
Where the file comes from
The kubelet writes /etc/resolv.conf into every Pod when it starts. The file points the Pod at the cluster DNS Service and adds search domains based on the Pod's namespace. Your application's resolver library reads this file, not CoreDNS directly.
Read it from a running Pod:
$ kubectl exec tmp-debug -- cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5The nameserver IP varies by cluster. It is the ClusterIP of the kube-dns Service.
The three fields
| Field | Meaning in this example |
|---|---|
| `search` | Suffixes the resolver appends to a name that is not fully qualified. The first one is the Pod's own namespace. |
| `nameserver` | The DNS server that receives every query. In Kubernetes this is the `kube-dns` Service IP. |
| `options ndots:5` | If a name has fewer than 5 dots, try the search suffixes before trying the name as written. |
How ndots changes the lookup
Take the name web. It has zero dots, which is fewer than 5. The resolver tries each suffix in order and stops at the first answer:
web.default.svc.cluster.localreturns the Service address.
That is why a short Service name works inside the same namespace. A Pod in another namespace has a different first suffix, so web fails there. It must use web.default or the full name.
Now take an external name such as api.example.com. It has two dots, still fewer than 5. The resolver tries these in order:
api.example.com.default.svc.cluster.localreturns NXDOMAIN (no such name).api.example.com.svc.cluster.localreturns NXDOMAIN.api.example.com.cluster.localreturns NXDOMAIN.api.example.comreturns the real answer.
Each external lookup can cost several extra queries. Many resolvers also query A and AAAA records separately, which doubles the traffic.
Reducing extra lookups
You have two simple options:
- Add a trailing dot to make the name fully qualified, for example
api.example.com.. The resolver skips the search list. - Set a lower
ndotsfor the Pod withdnsConfig.
apiVersion: v1
kind: Pod
metadata:
name: low-ndots
spec:
containers:
- name: app
image: busybox:1.36
command: ["sleep", "3600"]
dnsConfig:
options:
- name: ndots
value: "2"With ndots:2, a name like web.default (one dot) still uses the search list. A name like api.example.com (two dots) is tried as written first.
Common errors
- Short name fails across namespaces. The search list contains only the Pod's own namespace. Use
web.other-namespace. - Slow or noisy external lookups. Check the DNS logs for repeated NXDOMAIN answers for names ending in
cluster.local. This isndots:5at work, not a broken cluster. - Wrong nameserver. If the
nameserverline does not match thekube-dnsClusterIP, the Pod may use a customdnsPolicy. Checkspec.dnsPolicyandspec.dnsConfig. - Image behavior differs. Some minimal images, including Alpine-based ones, handle search lists differently from glibc. Test with the same image your application uses.
Exercise
A Pod in the namespace shop runs wget http://web:8080 and gets "bad address", but the web Service exists in the namespace default. The Pod's search line starts with shop.svc.cluster.local. Why does it fail, and what name fixes it?
Show answer
The search list contains only the Pod's own namespace, so web expands to web.shop.svc.cluster.local, which does not exist. Use web.default (or web.default.svc.cluster.local) so the lookup reaches the Service in the default namespace.
Exercise
With ndots:5, how many names does the resolver try for api.example.com before it reaches the real answer, using the search list shown in this chapter?
Show answer
Four names in total. It tries the three search-suffixed names first, which return NXDOMAIN, and then api.example.com as written, which succeeds. Three of the four queries are wasted.