Sub topic 8
Discovering Services with cluster DNS
This chapter assumes you can apply manifests with kubectl, know what a ClusterIP Service is, and understand basic DNS. It uses the web Service from the earlier chapters.
How Service names resolve
A ClusterIP is stable, but nobody wants to hard-code it. Kubernetes publishes a DNS record for every Service. Most clusters run CoreDNS to answer these queries. CoreDNS watches the API server and creates records as Services appear and disappear.
The full name of a Service has this form:
<service>.<namespace>.svc.<cluster-domain>The default cluster domain is cluster.local. The web Service in the default namespace is therefore web.default.svc.cluster.local. For a normal ClusterIP Service, this name resolves to the Service's virtual IP.
Why short names work
The kubelet writes a /etc/resolv.conf into each Pod that points at the cluster DNS Service and lists search domains. A Pod in default typically gets this:
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5The resolver appends each search domain in turn to any name with fewer than five dots. This gives you these forms:
| Name you use | Works from | Resolves via |
|---|---|---|
| `web` | Same namespace | `default.svc.cluster.local` search entry |
| `web.default` | Any namespace | `svc.cluster.local` search entry |
| `web.default.svc.cluster.local` | Any namespace | Fully qualified name |
Testing resolution
Run a temporary Pod to check a name from inside the cluster:
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- nslookup web.default.svc.cluster.localA healthy answer shows the cluster DNS server and the Service's ClusterIP.
Other records
- Named ports produce SRV records, such as
_http._tcp.web.default.svc.cluster.local. - Headless Services return the IPs of the ready Pods instead of one virtual IP. The next chapters cover them.
- Environment variables like
WEB_SERVICE_HOSTalso exist. They are set only for Services that existed when the Pod started, so prefer DNS.
Common errors
- Wrong namespace. From namespace
shop,weblooks forweb.shop. Useweb.defaultto reach the other namespace. - NXDOMAIN for an existing Service. Check the name for typos, and check that the Service is in the namespace you expect.
- Slow lookups for external names. With
ndots:5, a name likeapi.example.comhas two dots and is tried against every search domain first. A trailing dot (api.example.com.) skips the search list. - Timeouts for all names. CoreDNS may be unhealthy, or a NetworkPolicy may block egress to port 53 (UDP and TCP). Check with
kubectl get pods -n kube-system -l k8s-app=kube-dns.
Exercise
A Pod in namespace shop runs curl http://web and gets "could not resolve host". The Service web exists in namespace default. What name should it use, and why does the short name fail?
Show answer
Use web.default (or the full web.default.svc.cluster.local). The short name web is expanded with the Pod's own namespace first, so it looks up web.shop.svc.cluster.local, which does not exist.