Cluster DNS and service discovery

Intermediate · 15 min

How Pods find Services by name: CoreDNS, the records Kubernetes publishes, the search path and ndots:5 in every Pod's resolv.conf, and dnsPolicy.

Before thisServices: ClusterIP, NodePort and LoadBalancerEndpointSlices and kube-proxy

CNCF [Cloud Native Computing Foundation] · 16 min · from 1:18

Cluster DNS and service discovery

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CNCF [Cloud Native Computing Foundation]Report a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Cluster DNS (CoreDNS in almost every cluster) watches Services and EndpointSlices and answers for names under cluster.local, at the kube-dns Service's IP that the kubelet writes into every Pod.
  • A Service gets <svc>.<ns>.svc.cluster.local. A ClusterIP Service resolves to its virtual IP, a headless one to each ready Pod's IP, and an ExternalName to a CNAME. Named ports get SRV records.
  • A Pod's resolv.conf searches <ns>.svc.cluster.local, svc.cluster.local and cluster.local, so web and web.other work as short names.
  • With ndots:5, any name with fewer than five dots is tried against every search domain first. An external lookup like api.stripe.com can cost several wasted queries; a trailing dot or a lower ndots avoids it.
  • dnsPolicy chooses the setup: ClusterFirst (default), Default (the node's resolver), ClusterFirstWithHostNet, or None with your own dnsConfig.

Nobody wants to type an IP

Services give you a stable virtual IP. Stable isn’t the same as known, though. The IP is allocated when the Service is created, differs between clusters and environments, and changes if someone recreates the Service. A config file that says DB_HOST=10.96.143.7 is wrong in staging.

Kubernetes’s first answer was environment variables. Each container still gets WEB_SERVICE_HOST and WEB_SERVICE_PORT for every Service in its namespace that existed when it started. That’s fragile in an obvious way: Services created later are missing, and a namespace with hundreds of Services floods every process’s environment. Set enableServiceLinks: false on the Pod to turn them off. (The kubernetes Service in default is always injected.)

The real answer is DNS. Every cluster runs a DNS server that knows every Service, and every Pod is configured to ask it first. You write postgres or postgres.data, and the name works in every environment.

The DNS server

Kubernetes defines what the records must look like (the DNS spec in deeper) and leaves the server to an add-on. In practice that’s CoreDNS, run as a Deployment in kube-system behind a Service that, for historical reasons, is still called kube-dns, usually at the tenth address of the service range, 10.96.0.10. CoreDNS’s kubernetes plugin watches Services and EndpointSlices through the API, like kube-proxy does, and answers from memory:

.:53 {
    errors
    health
    ready
    kubernetes cluster.local in-addr.arpa ip6.arpa {
        pods insecure
        fallthrough in-addr.arpa ip6.arpa
        ttl 30
    }
    prometheus :9153
    forward . /etc/resolv.conf
    cache 30
    loop
    reload
}

That’s the shape of the default Corefile. Names under cluster.local come from the API. Everything else is forwarded to the node’s upstream resolvers and cached.

What gets a record

For a Service web in namespace shop, with cluster domain cluster.local:

Name Answer
web.shop.svc.cluster.local A/AAAA: the cluster IP
_http._tcp.web.shop.svc.cluster.local SRV: port 80 and the Service name, for each named port
db.shop.svc.cluster.local (headless) A/AAAA: every ready Pod’s IP
db-0.db.shop.svc.cluster.local A: that one Pod, when it sets hostname and subdomain
payments-api.shop.svc.cluster.local (ExternalName) CNAME api.payments.example.com

The per-Pod names behind a headless Service come from the EndpointSlice. In the lab we created two Pods with hostname: db-0 and db-1 and subdomain: db, behind the headless db Service from earlier. The EndpointSlice controller copied each hostname into the slice’s endpoints, next to the address, and that’s what a DNS server reads to serve db-0.db.shop.svc.cluster.local. StatefulSets set hostname and subdomain for you, which is how their Pods get stable names.

Inside a Pod: resolv.conf

The kubelet writes each Pod’s /etc/resolv.conf. For a Pod in shop with the default dnsPolicy: ClusterFirst, the kubelet’s dns.go produces:

nameserver 10.96.0.10
search shop.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

The search list (namespace first, then svc, then the bare cluster domain, then any search domains from the node) is why short names work. web finds web.shop.svc.cluster.local. web.billing finds web.billing.svc.cluster.local in another namespace. Think of it like dialling an extension inside an office: the phone system tries your department’s prefix first, then the company’s.

The ndots:5 tax

ndots:5 tells the resolver: if a name has fewer than five dots, try it with each search domain before trying it as written. It’s set that high so that names like db-0.db.shop.svc still go through the search list. The cost lands on external names. A Pod calling api.stripe.com (two dots) has its resolver ask for:

api.stripe.com.shop.svc.cluster.local   NXDOMAIN
api.stripe.com.svc.cluster.local        NXDOMAIN
api.stripe.com.cluster.local            NXDOMAIN
api.stripe.com                          answer

Often twice over, for A and AAAA, plus any node search domains. That’s several times the DNS traffic for every outbound call that isn’t cached. Fixes, from cheapest: write external names fully qualified with a trailing dot (api.stripe.com.), which skips the search list; lower ndots for chatty Pods; and run NodeLocal DNSCache, a caching agent on each node that also avoids conntrack races on UDP that cause the infamous five-second DNS timeouts.

dnsPolicy and dnsConfig

spec:
  dnsPolicy: ClusterFirst      # default
  dnsConfig:
    options:
    - {name: ndots, value: "2"}

The policies:

  • ClusterFirst: the cluster DNS, as above. For a hostNetwork Pod it silently falls back to Default.
  • ClusterFirstWithHostNet: cluster DNS even for hostNetwork Pods. Use it for node agents that must resolve Services.
  • Default: inherit the node’s resolver, which knows nothing about Services. Not actually the default.
  • None: ignore everything and use only dnsConfig.

dnsConfig adds nameservers (at most 3), search domains (at most 32 and 2,048 characters, per the API validation) and options, merged on top of whatever the policy produced.

About the video

The embed is a 16-minute clip, 1:18 to 17:34, of Jingming Guo’s KubeCon talk “DNS Deep Dive in Kubernetes with CoreDNS”, from the CNCF channel in November 2024. The clip is the talk’s “Kubernetes DNS fundamentals” section, which per its abstract follows the lifecycle of a DNS request in a cluster for service discovery and debugging. The rest of the talk is Airbnb’s CoreDNS migration and multi-cluster DNS. The talk has only coarse chapters, so this was judged from them and the abstract, since transcripts can’t be fetched here. It carries no audio-track flag. It is a KubeCon talk with an English caption track. Piyush’s CKA episode in deeper is a hands-on look at the CoreDNS Deployment, Corefile and debugging.

What was run: on a local v1.37.3 control plane with no kubelet, we created the headless Service’s Pods with hostname and subdomain, wrote their IPs into status, and saw the EndpointSlice controller record the hostnames. We built CoreDNS v1.15.0 to query it against the lab’s API server, but running it was not permitted here, so no DNS answers were seen. The record table, resolv.conf and query sequence are from the Kubernetes DNS specification, the docs, the CoreDNS plugin docs and the kubelet’s dns.go (search list, ndots:5) and API validation limits in the v1.37.3 source, checked 2026-11-28.

Where this fits

DNS and Services get traffic around inside the cluster. Getting HTTP in from outside, by hostname and path, is the next problem. Next: Ingress and ingress controllers.

Check yourself

  1. 01A Pod in namespace shop runs curl http://web. Which names does its resolver try, in order?

    web has no dots, fewer than ndots:5, so it walks the search list first: web.shop.svc.cluster.local, which exists and answers. If it didn't, web.svc.cluster.local, web.cluster.local, any node search domains, then web as is.

  2. 02Why do Pods sometimes send five or more DNS queries for api.stripe.com, and how do you stop it?

    It has two dots, under ndots:5, so the resolver tries api.stripe.com.shop.svc.cluster.local, .svc.cluster.local and .cluster.local (each NXDOMAIN, often for both A and AAAA) before the real name. Use the FQDN with a trailing dot, or set dnsConfig options ndots to 1 or 2 for that Pod.

  3. 03What does db.shop.svc.cluster.local return for a headless Service with two ready Pods, and what does db-0.db.shop.svc.cluster.local return?

    The Service name returns both Pod IPs as A records, no virtual IP. db-0.db... returns just that Pod's IP, because the Pod set hostname db-0 and subdomain db (as StatefulSet Pods do), and the EndpointSlice carries that hostname.

  4. 04A hostNetwork Pod can't resolve Service names. Why, and what's the fix?

    With dnsPolicy ClusterFirst, a hostNetwork Pod falls back to the node's resolver, which knows nothing about cluster.local. Set dnsPolicy ClusterFirstWithHostNet.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark