DNS: finding Services by name

Names instead of addresses.

Beginner20 min · lesson 15 of 21
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (4 files, 1 KB): dns.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/dns.tar.gz && tar -xzf dns.tar.gz, which creates ~/lab/dns/. SHA-256: 9f6c272ee23630b4727fa23dcb8ba04dd39b05c0dec800f788cc8730e3295edc

A Service gives your Pods one stable IP, but nobody wants an address like 10.96.146.156 in a config file. The IP is picked when the Service is created, so it is different in every cluster and changes if someone deletes and re-creates the Service. Apps should say http://web and let the cluster work out the address.

Cluster DNS makes that work. Every Service gets a name, and every Pod is set up to ask the cluster's own DNS server before any other. The DNS server is the first thing to look at.

The DNS server is a Deployment too

ubuntu@secopslog-k8s:~/lab/dns · Kubernetes 1.37
$ kubectl get pods -n kube-system -l k8s-app=kube-dns && kubectl get service kube-dns -n kube-system
NAME READY STATUS RESTARTS AGE coredns-559f6c778d-7bzhb 1/1 Running 0 60s coredns-559f6c778d-b4kvn 1/1 Running 0 60s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 67s

Cluster DNS is CoreDNS, running as two ordinary Pods in kube-system. In front of them sits a Service called kube-dns (the old name stayed for compatibility) with the fixed IP 10.96.0.10, on port 53 for DNS over UDP and TCP. Port 9153 serves CoreDNS metrics. CoreDNS watches the API for Services and answers with their current addresses.

A Pod to look things up from

Two nginx Pods that answer with their own name, a Service called web, and a Pod to run lookups from:

web.yaml
# Two nginx Pods that answer with their own name, and a Service called web in front of them.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.30.6-alpine
command: ["/bin/sh", "-c"]
args: ['echo "answered by $(hostname)" > /usr/share/nginx/html/index.html && exec nginx -g "daemon off;"']
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
dnstest.yaml
# A Pod to run lookups from. agnhost is the Kubernetes project's test image;
# it has nslookup and dig, which follow resolv.conf the way apps do.
apiVersion: v1
kind: Pod
metadata:
name: dnstest
spec:
containers:
- name: tools
image: registry.k8s.io/e2e-test-images/agnhost:2.66.1
args: ["pause"]

The lookup Pod uses agnhost, the Kubernetes project's own test image, because it has the standard nslookup and dig tools. The nslookup in busybox does not follow the search list for names that contain a dot, so a name like api.shop can end up at the internet's .shop domain instead of in the cluster. That makes it a poor teacher here.

ubuntu@secopslog-k8s:~/lab/dns · Kubernetes 1.37
$ kubectl apply -f web.yaml -f dnstest.yaml && kubectl rollout status deployment/web && kubectl wait --for=condition=Ready pod/dnstest --timeout=90s
deployment.apps/web created service/web created pod/dnstest created Waiting for deployment "web" rollout to finish: 0 out of 2 new replicas have been updated... Waiting for deployment "web" rollout to finish: 0 of 2 updated replicas are available... Waiting for deployment "web" rollout to finish: 1 of 2 updated replicas are available... deployment "web" successfully rolled out pod/dnstest condition met
$ kubectl get service web
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE web ClusterIP 10.96.146.156 <none> 80/TCP 2s
$ kubectl exec dnstest -- cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local nameserver 10.96.0.10 options ndots:5

The kubelet wrote this /etc/resolv.conf into the Pod. nameserver 10.96.0.10 is the kube-dns Service, so every lookup goes to CoreDNS. The search line lists suffixes to try: first default.svc.cluster.local (the Pod's own namespace), then svc.cluster.local, then cluster.local. options ndots:5 says: if a name has fewer than five dots, try it with each suffix before trying it as it is.

Three ways to write the same name

ubuntu@secopslog-k8s:~/lab/dns · Kubernetes 1.37
$ kubectl exec dnstest -- nslookup web
;; Got recursion not available from 10.96.0.10 Server: 10.96.0.10 Address: 10.96.0.10#53 Name: web.default.svc.cluster.local Address: 10.96.146.156 ;; Got recursion not available from 10.96.0.10
$ kubectl exec dnstest -- nslookup web.default
;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 Server: 10.96.0.10 Address: 10.96.0.10#53 Name: web.default.svc.cluster.local Address: 10.96.146.156 ;; Got recursion not available from 10.96.0.10
$ kubectl exec dnstest -- nslookup web.default.svc.cluster.local
;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 Server: 10.96.0.10 Address: 10.96.0.10#53 Name: web.default.svc.cluster.local Address: 10.96.146.156 ;; Got recursion not available from 10.96.0.10
$ kubectl exec dnstest -- curl -s http://web
answered by web-7c4f64b8d8-mmgz5

All three lookups found the same thing: web.default.svc.cluster.local with the Service's cluster IP. The full name has four parts: the Service, its namespace, svc, and the cluster domain cluster.local. web worked because the first search suffix turned it into the full name. web.default worked through the second suffix. The full name works from anywhere in the cluster. The curl shows that ordinary programs follow the same rules: http://web reached one of the Pods.

The ;; Got recursion not available lines are nslookup noticing that CoreDNS answers names in cluster.local itself and does not offer to look up other names on your behalf in those answers. They are harmless, and you will see them every time.

Other namespaces need the namespace

A namespace is a named section of a cluster. Everything so far lived in the one called default; lesson 19 (Namespaces) covers them properly. For now you only need kubectl create namespace and the -n flag, which tells a command which namespace to work in. Here is a Service called api in a second namespace, billing:

ubuntu@secopslog-k8s:~/lab/dns · Kubernetes 1.37
$ kubectl create namespace billing && kubectl create deployment api -n billing --image=nginx:1.30.6-alpine && kubectl expose deployment api -n billing --port=80 && kubectl rollout status deployment/api -n billing
namespace/billing created deployment.apps/api created service/api exposed Waiting for deployment "api" rollout to finish: 0 of 1 updated replicas are available... deployment "api" successfully rolled out
$ kubectl exec dnstest -- nslookup api
;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 ;; Got SERVFAIL reply from 10.96.0.10 Server: 10.96.0.10 Address: 10.96.0.10#53 ** server can't find api: SERVFAIL command terminated with exit code 1
$ kubectl exec dnstest -- nslookup api.billing
;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 Server: 10.96.0.10 Address: 10.96.0.10#53 Name: api.billing.svc.cluster.local Address: 10.96.199.124 ;; Got recursion not available from 10.96.0.10
$ kubectl exec dnstest -- curl -s http://api.billing | grep "<title>"
<title>Welcome to nginx!</title>

From the Pod in default, plain api failed. The search list only tried api.default.svc.cluster.local, api.svc.cluster.local and api.cluster.local, none of which exist, and then the bare name api, which CoreDNS passed on to the DNS servers outside the cluster. That last try came back as SERVFAIL. With the namespace added, api.billing was found as api.billing.svc.cluster.local, and curl got the nginx welcome page.

The short form api.billing relies on the search list, and here ndots:5 is a trade-off. Any name with fewer than five dots, example.com included, is first tried with every cluster suffix, which costs a few extra DNS queries per lookup. Apps that call many outside hosts sometimes add a trailing dot (example.com.) to skip the search list. For config files inside the cluster, api.billing.svc is a good form: it names the namespace, and the last search suffix completes it to the full name. The full name also works, but cluster.local is only the default cluster domain, and some clusters use another.

Headless Services

Sometimes a client wants the Pods themselves rather than one shared address. A database client may need to talk to one particular replica, for example. Setting clusterIP: None makes a headless Service: no virtual IP, no kube-proxy load balancing, and DNS answers with the Pod IPs.

web-headless.yaml
# Same Pods, but no virtual IP: DNS answers with the Pod IPs themselves.
apiVersion: v1
kind: Service
metadata:
name: web-headless
spec:
clusterIP: None
selector:
app: web
ports:
- port: 80
ubuntu@secopslog-k8s:~/lab/dns · Kubernetes 1.37
$ kubectl apply -f web-headless.yaml && kubectl get service web-headless
service/web-headless created NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE web-headless ClusterIP None <none> 80/TCP 0s
$ kubectl exec dnstest -- nslookup web-headless
;; Got recursion not available from 10.96.0.10 Server: 10.96.0.10 Address: 10.96.0.10#53 Name: web-headless.default.svc.cluster.local Address: 10.244.1.2 Name: web-headless.default.svc.cluster.local Address: 10.244.2.2 ;; Got recursion not available from 10.96.0.10
$ kubectl get pods -l app=web -o custom-columns=NAME:.metadata.name,IP:.status.podIP
NAME IP web-7c4f64b8d8-7kkwp 10.244.1.2 web-7c4f64b8d8-mmgz5 10.244.2.2

CLUSTER-IP says None. The lookup returned two addresses, and they are exactly the two Pod IPs in the last table. The client now picks a Pod itself. When Pods are replaced, CoreDNS answers with the new IPs, but a client that cached the old answer will still try the old ones, so this suits software that expects to handle that, such as databases and StatefulSets.

Hands-on

Build a headless Service in another namespace and look it up from the dnstest Pod, which is still running in default.

1. Create the namespace team-b, a two-Pod Deployment cache in it and a headless Service in front of it, then wait for the Pods: kubectl create namespace team-b && kubectl create deployment cache -n team-b --image=nginx:1.30.6-alpine --replicas=2 && kubectl expose deployment cache -n team-b --port=80 --cluster-ip=None && kubectl rollout status deployment/cache -n team-b. --cluster-ip=None is what makes it headless.

2. Look up the short name: kubectl exec dnstest -- nslookup cache.

3. Look up the full name: kubectl exec dnstest -- nslookup cache.team-b.svc.cluster.local.

4. List the Pod IPs to compare: kubectl get pods -n team-b -o custom-columns=NAME:.metadata.name,IP:.status.podIP.

5. Clean up: kubectl delete namespace billing team-b && kubectl delete -f web.yaml -f web-headless.yaml -f dnstest.yaml.

What you should see: step 1 ends with deployment "cache" successfully rolled out. Step 2 fails with ** server can't find cache: SERVFAIL and command terminated with exit code 1, because dnstest lives in default. Step 3 returns two Address: lines, one per cache Pod, and in step 4 they match the two IPs kubectl lists. Step 5 reports the two namespaces and the four objects from the files as deleted. The cache Deployment and Service are not listed: they went with their namespace.

Remember

Pods ask CoreDNS, behind the kube-dns Service, and their search list turns a short name into name.namespace.svc.cluster.local. A bare name only finds Services in the Pod's own namespace, so add the namespace for anything else.

Quick check
01A Pod in namespace shop needs the Service orders in namespace backend. Which address works?
Incorrect — Names only have to be unique per namespace, and a bare name is tried with the Pod's own namespace. Plain api failed the same way.
Incorrect — The order is the other way round: Service first, then namespace.
Correct — The last search suffix completes it to orders.backend.svc.cluster.local, just as api.billing was found.
Incorrect — The namespace is part of every Service name. Without it there is nothing to match.
02What does options ndots:5 in a Pod's /etc/resolv.conf mean?
Correct — That is how web became web.default.svc.cluster.local, and why example.com costs a few extra queries.
Incorrect — Retries are set with other options. ndots counts the dots in the name.
Incorrect — Every lookup goes to the nameserver 10.96.0.10, which is CoreDNS.
Incorrect — Caching is set in CoreDNS itself, not in resolv.conf.
03You look up a Service and get back two 10.244.x.x addresses instead of one 10.96.x.x address. Why?
Incorrect — CoreDNS watches the API and knows a cluster IP as soon as the Service exists.
Incorrect — Ports do not change the DNS answer. A normal Service has one IP for all ports.
Incorrect — Both CoreDNS Pods give the same answer, and the client only uses one reply.
Correct — With clusterIP: None there is no virtual IP, and the lookup returned the two Pod IPs directly.

Related