DNS: finding Services by name
Names instead of addresses.
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: 9f6c272ee23630b4727fa23dcb8ba04dd39b05c0dec800f788cc8730e3295edcA 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
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:
# Two nginx Pods that answer with their own name, and a Service called web in front of them.apiVersion: apps/v1kind: Deploymentmetadata:name: webspec:replicas: 2selector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: nginximage: nginx:1.30.6-alpinecommand: ["/bin/sh", "-c"]args: ['echo "answered by $(hostname)" > /usr/share/nginx/html/index.html && exec nginx -g "daemon off;"']---apiVersion: v1kind: Servicemetadata:name: webspec:selector:app: webports:- port: 80
# 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: v1kind: Podmetadata:name: dnstestspec:containers:- name: toolsimage: registry.k8s.io/e2e-test-images/agnhost:2.66.1args: ["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.
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
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:
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.
# Same Pods, but no virtual IP: DNS answers with the Pod IPs themselves.apiVersion: v1kind: Servicemetadata:name: web-headlessspec:clusterIP: Noneselector:app: webports:- port: 80
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.
shop needs the Service orders in namespace backend. Which address works?api failed the same way.orders.backend.svc.cluster.local, just as api.billing was found.options ndots:5 in a Pod's /etc/resolv.conf mean?web became web.default.svc.cluster.local, and why example.com costs a few extra queries.10.96.0.10, which is CoreDNS.clusterIP: None there is no virtual IP, and the lookup returned the two Pod IPs directly.