Skip to content
Visual explainer 2h to learn 3 steps

CoreDNS & service discovery

Debug DNS resolution failures inside the cluster.

By Invicly Tech, , in Kubernetes Administrator (CKA). About 9 minutes to read.

Topic 22 of 33

In short

CoreDNS is the cluster DNS server that turns Service names into cluster IPs. Every Pod's resolv.conf points at the kube-dns Service and includes search domains for its namespace. Its configuration is the Corefile stored in the coredns ConfigMap in kube-system.

Explained simply

Remembering IP addresses is hard, and in Kubernetes they change all the time. So the cluster runs its own phone book: a DNS server called CoreDNS. You ask for a name like db, and it answers with the Service IP.

Every Service gets a name in the form name.namespace.svc.cluster.local. You rarely type all of that. Inside the same namespace, just db works. From another namespace, db.prod is enough. The rest is added automatically.

That automatic part comes from each Pod's /etc/resolv.conf file. The kubelet writes it when the Pod starts. It points at the cluster DNS Service (called kube-dns for historical reasons) and lists search domains to try after short names.

When DNS breaks, almost everything looks broken: apps time out connecting to each other by name. Testing DNS from a small test Pod is one of the first steps in any network troubleshooting.

Press Play on a diagram to walk through it one step at a time.

SequenceResolving a Service name
Client Pod
CoreDNS
API server
  1. 1Watch Services and EndpointSlices
    API server to CoreDNS: Watch Services and EndpointSlices
  2. 2Query db.prod
    Client Pod to CoreDNS: Query db.prod
  3. 3Expand to db.prod.svc.cluster.local
    CoreDNS to CoreDNS: Expand to db.prod.svc.cluster.local
  4. 4Answer with ClusterIP
    CoreDNS to Client Pod: Answer with ClusterIP
  5. 5Connect to ClusterIP
    Client Pod to Client Pod: Connect to ClusterIP
FlowHow the short name db is resolved from namespace shop
  1. 1
    App asks for db

    Fewer than 5 dots, so use the search list

  2. 2
    Try db.shop.svc.cluster.local

    Sent to nameserver 10.96.0.10

  3. 3
    kube-proxy forwards

    To a CoreDNS Pod on port 53

  4. 4
    kubernetes plugin

    Finds Service db in namespace shop

  5. 5
    Answer

    A record with the ClusterIP, cached 30s

In depth

What a Pod's resolv.conf contains

A Pod in namespace shop usually gets: nameserver 10.96.0.10 (the kube-dns Service IP from the kubelet's clusterDNS setting), search shop.svc.cluster.local svc.cluster.local cluster.local, and options ndots:5.

ndots:5 means any name with fewer than five dots is tried with each search domain first. So db becomes db.shop.svc.cluster.local on the first try, which is fast. But an outside name like api.github.com (two dots) is first tried as api.github.com.shop.svc.cluster.local and so on, and only then as itself, which adds extra lookups. Writing it with a trailing dot (api.github.com.) skips the search list. The Pod's dnsPolicy controls all this: ClusterFirst is the default, Default copies the node's resolv.conf, ClusterFirstWithHostNet is for hostNetwork Pods, and None uses only your dnsConfig.

  • kubectl exec POD -- cat /etc/resolv.conf
  • kubelet config fields: clusterDNS and clusterDomain
  • dnsConfig can add nameservers, searches and options

Records CoreDNS serves

A normal Service gets an A record (and AAAA for IPv6) pointing to its ClusterIP. A headless Service returns one A record per Ready Pod instead. StatefulSet Pods behind a headless Service also get their own names, such as web-0.web.shop.svc.cluster.local, which is how databases find their peers.

Named Service ports get SRV records, for example _http._tcp.web.shop.svc.cluster.local, which return the port number and target. Pods get records too, in the form 10-244-1-5.shop.pod.cluster.local (the IP with dashes), if the Corefile enables pods. An ExternalName Service returns a CNAME to the outside host. CoreDNS also serves reverse lookups for cluster IPs in in-addr.arpa.

MapDNS names in namespace shop
cluster.local
  • Service web
    • web.shop.svc.cluster.local
    • A record: ClusterIP
  • Headless Service db
    • db.shop.svc.cluster.local
    • A records: each Ready Pod IP
    • db-0.db.shop.svc.cluster.local
  • Named port http on web
    • _http._tcp.web.shop.svc.cluster.local
    • SRV: port and target
  • Pod 10.244.1.5
    • 10-244-1-5.shop.pod.cluster.local

Reading the Corefile

The Corefile in the coredns ConfigMap lists server blocks. The default kubeadm block for .:53 includes errors (log errors), health and ready (probes), kubernetes cluster.local in-addr.arpa ip6.arpa (answer cluster names from the API), prometheus :9153 (metrics), forward . /etc/resolv.conf (send other names to the node's upstream DNS), cache 30, loop (detect forwarding loops), reload (pick up Corefile changes) and loadbalance (rotate answers).

To send a company domain to its own DNS server, add a second block such as corp.example:53 { forward . 10.0.0.53 }. With reload, CoreDNS picks up edits on its own after the ConfigMap reaches the Pods, which can take a minute or two; kubectl -n kube-system rollout restart deployment coredns is faster. Always check the CoreDNS logs afterwards, because a syntax error keeps the old config or crashes the Pods.

A step by step DNS debug routine

First, test from a Pod: nslookup kubernetes.default should return the API Service IP. If it does, cluster DNS works and the problem is the specific name or namespace. If not, check that the Pod's resolv.conf points at the kube-dns Service IP, that the CoreDNS Pods are Running and Ready, and that the kube-dns Service has endpoints.

If CoreDNS is in CrashLoopBackOff with a log line saying a loop was detected, the node's resolv.conf points at a local stub resolver like 127.0.0.53, so CoreDNS forwards to itself. The fix is pointing the kubelet's resolvConf at the real upstream file, usually /run/systemd/resolve/resolv.conf. If internal names work but outside names fail, look at the forward line and the node's upstream DNS. Also remember that a NetworkPolicy blocking port 53 looks exactly like broken DNS.

  • kubectl -n kube-system get endpointslices -l kubernetes.io/service-name=kube-dns
  • kubectl -n kube-system logs deploy/coredns
  • busybox:1.28 is often used for nslookup; some newer builds behave oddly

Learn it in 3 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Explain Service DNS names~40m
  2. 2
    Inspect the CoreDNS ConfigMap~40m
  3. 3
    Debug resolution from a test Pod~40m

Key points

  • Service FQDN format is NAME.NAMESPACE.svc.cluster.local.
  • Within the same namespace, the short Service name is enough.
  • The kube-dns Service fronts the CoreDNS Pods in kube-system.
  • Debug with nslookup from a temporary busybox Pod.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
kubectl run dnstest --image=busybox:1.36 --rm -it --restart=Never \
  -- nslookup kubernetes.default
kubectl exec web-0 -- cat /etc/resolv.conf
kubectl -n kube-system get pods -l k8s-app=kube-dns
kubectl -n kube-system get cm coredns -o yaml
kubectl -n kube-system logs -l k8s-app=kube-dns

Name works in one namespace, not in another

A Pod in namespace web calls http://db and gets a name error, while it works from Pods in data. Step 1: look up the short name from web; it fails. Step 2: look up db.data; it works. Step 3: the Service lives in data, and the short name only searches the caller's own namespace. Step 4: change the app config to db.data or the full name.

bash
kubectl -n web run t --rm -it --image=busybox:1.28 --restart=Never -- nslookup db
# fails: can't resolve 'db'
kubectl -n web run t --rm -it --image=busybox:1.28 --restart=Never -- nslookup db.data
# resolves to db.data.svc.cluster.local and its ClusterIP
kubectl get svc -A | grep ' db '

Forward a private domain to a company DNS server

Pods must resolve names under corp.example through the DNS server 10.0.0.53. Step 1: edit the coredns ConfigMap and add a new server block after the main one. Step 2: restart CoreDNS (or wait for reload). Step 3: check the logs for errors. Step 4: test a corp.example name from a Pod.

text
# kubectl -n kube-system edit configmap coredns
# add below the existing .:53 { ... } block:
corp.example:53 {
    errors
    cache 30
    forward . 10.0.0.53
}
# kubectl -n kube-system rollout restart deployment coredns
# kubectl -n kube-system logs deploy/coredns | tail

Quick reference

  • Service: NAME.NAMESPACE.svc.cluster.local; StatefulSet Pod: POD.SVC.NAMESPACE.svc.cluster.local
  • Pod resolv.conf: nameserver = kube-dns IP, search NS.svc.cluster.local svc.cluster.local cluster.local
  • ndots:5 makes short names try search domains first
  • Corefile: kubectl -n kube-system get cm coredns -o yaml
  • kubectl -n kube-system rollout restart deploy coredns applies Corefile changes now
  • CoreDNS loop detected: point kubelet resolvConf to /run/systemd/resolve/resolv.conf
  • nslookup kubernetes.default from a Pod is the first DNS health check

Common mistakes

  • Using the short name from a different namespace; add the namespace.
  • Editing the Corefile and not checking CoreDNS logs for reload errors.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A Pod in namespace dev runs nslookup api. Which name is tried first?

Question 2

2What does a DNS lookup of a headless Service return?

Question 3

3CoreDNS Pods crash with a log about a detected loop. What is the usual cause?

Question 4

4Which dnsPolicy makes a Pod use the node's DNS settings instead of CoreDNS?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    From a test Pod, resolve the kubernetes Service and write the returned IP to /opt/k8s-ip.txt.

  2. Exercise 2medium

    Find the cluster domain and the kube-dns Service IP from the node configuration, and check they match a Pod's resolv.conf.

  3. Exercise 3hard

    Create a headless Service for a 2-replica StatefulSet web and resolve both the Service name and the name of web-0 from a test Pod.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Why is the CoreDNS Service called kube-dns?
kube-dns was the original cluster DNS server. CoreDNS replaced it but kept the Service name and the k8s-app=kube-dns label so existing setups keep working.
Why are external DNS lookups slow in my Pods?
ndots:5 makes short external names try every cluster search domain first. Use a trailing dot on fully qualified names, or lower ndots with dnsConfig for that Pod.
How do I add a custom hostname for all Pods?
Add a hosts block or a forward to your own DNS server in the Corefile. For a single Pod, use hostAliases in the Pod spec, which writes to its /etc/hosts.
ShareXLinkedInWhatsApp