Networking: Services, DNS, Ingress and policies · 6 MIN READ
Cluster DNS: CoreDNS, service names, search domains and ndots
Resolve Services and StatefulSet pods by short and fully qualified names across namespaces, explain the extra lookups that ndots:5 causes, and debug name resolution from inside a pod.
Pods find Services by name. CoreDNS answers those names from the Kubernetes API, and the kubelet writes every pod's resolv.conf so that short names work. When a call cannot connect, the name has often resolved differently than you expected, or not at all.
kubernetes · A MAP OF THE IDEA
- Same namespace: web
- Other namespace: web.shop
- One pod: db-0.db.shop
- External: trailing dot
Useful when
- Names survive pod and IP churn without client configuration.
- Headless records give stateful members stable identities.
Watch out
- ndots:5 turns one external lookup into four or more queries.
- Short names are namespace-relative and fail across namespaces.
Implementation & operation cost: Two CoreDNS pods per cluster, extra queries for every outside call, and a node-local cache once DNS load matters.
CoreDNS and the kube-dns Service
Cluster DNS is an ordinary Deployment: CoreDNS, two replicas in kube-system labelled k8s-app=kube-dns, behind a Service that is still called kube-dns for compatibility. That Service has a fixed ClusterIP, 10.96.0.10 in kind, and the kubelet writes it as the nameserver of every pod whose dnsPolicy is ClusterFirst, the default. CoreDNS's kubernetes plugin watches Services and EndpointSlices and answers names under cluster.local from memory; everything else is forwarded to the node's upstream resolvers. Its configuration, the Corefile, lives in the coredns ConfigMap in kube-system. Because DNS is just pods behind a Service, it fails like any workload: if both CoreDNS pods are down, or a policy blocks port 53, every name lookup in the cluster times out.
The names a Service gets
A Service web in namespace shop gets an A record, web.shop.svc.cluster.local, that points at its ClusterIP; a headless Service gets one A record per ready pod instead. Named ports also get SRV records such as _http._tcp.web.shop.svc.cluster.local. When a StatefulSet's serviceName is a headless Service, each pod gets its own record, db-0.db.shop.svc.cluster.local, which follows the pod across restarts even though its IP changes. An ExternalName Service is a CNAME. Ordinary pods are not resolvable by name; only pods with hostname and subdomain set, as StatefulSets do. Namespaces matter: a short name resolves only in the caller's own namespace, so a pod in default must say web.shop to reach the Service in shop.
apiVersion: v1
kind: Service
metadata:
name: db
namespace: shop
spec:
clusterIP: None
selector:
app: db
ports:
- name: redis
port: 6379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
namespace: shop
spec:
serviceName: db
replicas: 2
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: redis
image: redis:7
ports:
- name: redis
containerPort: 6379The headless Service db (clusterIP: None) is what the StatefulSet names in serviceName. That pairing gives db-0 and db-1 stable names under db.shop.svc.cluster.local, while db.shop itself resolves to the addresses of both ready pods. Clients that need one specific member, such as a primary, connect to db-0.db.shop; clients that can use any member use db.shop.
Search domains and ndots:5
In a pod in namespace default, /etc/resolv.conf lists three search domains, default.svc.cluster.local, svc.cluster.local and cluster.local, and options ndots:5. A name with fewer than five dots is treated as relative: the resolver first tries it with each search domain appended and only then as written. That is what makes web and web.shop work. It also means that an external name such as api.github.com, with two dots, is first looked up as api.github.com.default.svc.cluster.local, then with .svc.cluster.local, then with .cluster.local: three NXDOMAIN answers before the real one, and resolvers usually send both A and AAAA queries for every try. For a service that calls outside APIs often, that multiplies CoreDNS load and adds latency to every new connection.
kubectl exec client -- cat /etc/resolv.confThe search line starts with the pod's own namespace, which is why a short name is namespace-relative. The nameserver is the kube-dns ClusterIP. ndots:5 is high on purpose, so that names such as web.shop.svc still go through the search list; the price is paid by every external name with fewer than five dots.
Cutting the extra lookups
Write external hosts as fully qualified names with a trailing dot, api.github.com., and the resolver skips the search list; a few HTTP clients and servers mishandle the dot in Host headers or TLS, so test it. For a pod that mostly calls outside services, lower ndots with dnsConfig, as below, so names with two or more dots are tried as written first while short in-cluster names still use the search list. Cluster-wide, NodeLocal DNSCache runs a caching resolver on every node and takes load off CoreDNS. Do not lower ndots to 1 for callers that use names like web.shop: that name would be tried as an absolute name first, and .shop is a real top-level domain. The lab terminal accepts dnsConfig, but its resolver ignores the option; measure the effect on a real cluster.
apiVersion: v1
kind: Pod
metadata:
name: worker
spec:
dnsConfig:
options:
- name: ndots
value: "2"
containers:
- name: worker
image: busybox
command: ["sleep", "3600"]dnsConfig is merged into the resolv.conf the kubelet generates: the search list and the nameserver stay, and ndots becomes 2. Option values are strings, so the 2 is quoted. The same block in a Deployment's pod template applies to every replica.
Debugging DNS from a pod
Test from inside the cluster, in the namespace of the failing client, because the answer depends on the caller's search list. busybox's nslookup covers most checks; for dig, the Kubernetes DNS debugging guide uses a dnsutils image. NXDOMAIN means CoreDNS answered and the name does not exist: check the namespace, the Service name and typos. A timeout means no answer at all: check that the CoreDNS pods are Running and Ready, that kube-dns has endpoints, and that no NetworkPolicy blocks egress to UDP and TCP port 53. If kubernetes.default resolves but your name does not, the problem is the name, not DNS. And a successful lookup proves nothing about the backend: a ClusterIP is returned even when the Service has no endpoints.
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl get endpoints -n kube-system kube-dns
kubectl logs -n kube-system -l k8s-app=kube-dns
kubectl exec client -- nslookup kubernetes.defaultWork from the server side to the client: two coredns pods Running, the kube-dns Service with endpoints on port 53, logs without errors, then a lookup of kubernetes.default, a name that exists in every cluster. If that lookup returns 10.96.0.1, DNS is healthy and the remaining question is the name you asked for.
Try it in the lab terminal
Put a web Service and a Redis StatefulSet with a headless Service in namespace shop, then resolve them from a busybox client in namespace default. Read the client's resolv.conf first, then try a short name that fails, a namespace-qualified name and a fully qualified one. Finish with the headless Service, which returns every pod, and the stable name of one StatefulSet pod. busybox ships nslookup and wget, which is all this needs.
kind create cluster
kubectl create namespace shop
kubectl create deployment web -n shop --image=gopherforge/web:v1 --port=8080
kubectl expose deployment web -n shop --port=80 --target-port=8080
cat > db.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
name: db
namespace: shop
spec:
clusterIP: None
selector:
app: db
ports:
- name: redis
port: 6379
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
namespace: shop
spec:
serviceName: db
replicas: 2
selector:
matchLabels:
app: db
template:
metadata:
labels:
app: db
spec:
containers:
- name: redis
image: redis:7
ports:
- name: redis
containerPort: 6379
EOF
kubectl apply -f db.yaml
kubectl run client --image=busybox -- sleep 3600
kubectl wait --for=condition=Ready pod/client
kubectl exec client -- cat /etc/resolv.conf
kubectl exec client -- nslookup web
kubectl exec client -- nslookup web.shop
kubectl exec client -- wget -qO- http://web.shop.svc.cluster.local/version
kubectl rollout status statefulset/db -n shop
kubectl exec client -- nslookup db.shop
kubectl exec client -- nslookup db-0.db.shop.svc.cluster.localresolv.conf shows "search default.svc.cluster.local svc.cluster.local cluster.local", "nameserver 10.96.0.10" and "options ndots:5". nslookup web fails with "** server can't find web: NXDOMAIN", because the client lives in default and the Service in shop. web.shop resolves to web.shop.svc.cluster.local and its ClusterIP, and wget through the full name prints v1. Once the StatefulSet is ready, db.shop returns two addresses, one per pod, and db-0.db.shop.svc.cluster.local returns only the first pod's IP.
TAKE THIS WITH YOU
Service names resolve as name.namespace.svc.cluster.local, short names only within the caller's namespace, and headless Services expose pods directly. ndots:5 sends external names through three search domains first; use trailing dots, a lower ndots or a node-local cache where that cost matters.
Check your understanding
A pod in namespace default runs nslookup web and gets NXDOMAIN, although kubectl get svc -n shop lists web with endpoints. Which name should the pod use?
Go to the source
Progress stays in this browser. No account needed.