Skip to content
LF Cert Trainer

Lesson 116 · Section 9: Networking

Lesson 116: CoreDNS in Kubernetes: the server behind every cluster name

How cluster DNS is put together: the CoreDNS Deployment, its Corefile in a ConfigMap and the kube-dns Service, how the kubelet points every Pod at it through resolv.conf, what each line of the Corefile does, and how to change it safely or repair it when lookups fail. · 22 min read · 3 min video

1/9CoreDNS in Kubernetes
0:00 / 2:50

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why you need to know the server

The previous lesson treated cluster DNS as a black box that turns web.apps into an IP. That is enough until something goes wrong, and DNS problems are loud: when name resolution fails, every application in the cluster that calls another by name fails at once, with errors that mention the application and not DNS.

The server in that box is CoreDNS. The CoreDNS lesson earlier in this section showed it as a standalone program configured with a Corefile. Here it runs inside the cluster, as ordinary Kubernetes objects that you can list, edit, scale and break. An administrator has to know which objects those are, how Pods are pointed at them, and what the configuration says.

What it is made of

The four objects of cluster DNSselectsselectsrunsmounted as /etc/corednswatchesService kube-dns10.96.0.10:53Deployment coredns2 replicasConfigMap corednskey CorefilePod coredns-…k8s-app=kube-dnsPod coredns-…k8s-app=kube-dnsAPI serverServices, Pods
The four objects of cluster DNS. All of them live in kube-system. The Service keeps its old name kube-dns, and the Pods keep the label k8s-app=kube-dns, so that nothing had to change when CoreDNS replaced kube-dns.

Parts:

  • Service kube-dns (10.96.0.10:53)
  • Deployment coredns (2 replicas)
  • ConfigMap coredns (key Corefile)
  • Pod coredns-… (k8s-app=kube-dns)
  • Pod coredns-… (k8s-app=kube-dns)
  • API server (Services, Pods)

Connections:

  • Service kube-dns to Pod coredns-…: selects
  • Service kube-dns to Pod coredns-…: selects
  • Deployment coredns to Pod coredns-…: runs
  • ConfigMap coredns to Pod coredns-…: mounted as /etc/coredns
  • Pod coredns-… to API server: watches
Object (in kube-system)Role
Deployment corednsRuns the CoreDNS Pods, two replicas with kubeadm, spread over nodes where possible
ConfigMap corednsHolds one key, Corefile: the whole configuration. Mounted into the Pods at /etc/coredns/Corefile
Service kube-dnsThe stable address of DNS: 10.96.0.10 on a kubeadm cluster, ports 53/UDP, 53/TCP and 9153 for metrics
ServiceAccount coredns with ClusterRole system:corednsLets CoreDNS list and watch Services, EndpointSlices, Pods and namespaces

Before CoreDNS, the cluster DNS server was a program called kube-dns. When CoreDNS replaced it, the Service name kube-dns and the Pod label k8s-app=kube-dns were kept so that nothing depending on them broke. So you look for the Deployment by coredns and for the Pods and the Service by kube-dns.

The Deployment starts CoreDNS with one argument that ties it to the ConfigMap:

kubectl -n kube-system get deploy coredns -o yaml | grep -B 1 -A 8 'args:'
      - args:
        - -conf
        - /etc/coredns/Corefile
        image: registry.k8s.io/coredns/coredns:v1.12.1
        ...
      volumes:
      - configMap:
          items:
          - key: Corefile
            path: Corefile
          name: coredns
        name: config-volume

How it works

How Pods are pointed at CoreDNS

How a Pod learns where DNS isread bywritesused byUDP 53to a Podkubelet configclusterDNSkubeletstarts the Pod/etc/resolv.confnameserver 10.96.0.10App containerlooks up web.appsService kube-dns10.96.0.10CoreDNS Podanswers
How a Pod learns where DNS is. Nobody configures DNS in a Pod by hand. The kubelet knows the address of the kube-dns Service from its own configuration and writes it into every Pod it starts.

Parts:

  • kubelet config (clusterDNS)
  • kubelet (starts the Pod)
  • /etc/resolv.conf (nameserver 10.96.0.10)
  • App container (looks up web.apps)
  • Service kube-dns (10.96.0.10)
  • CoreDNS Pod (answers)

Connections:

  • kubelet config to kubelet: read by
  • kubelet to /etc/resolv.conf: writes
  • /etc/resolv.conf to App container: used by
  • App container to Service kube-dns: UDP 53
  • Service kube-dns to CoreDNS Pod: to a Pod

The link between a Pod and the DNS Service is the kubelet. Its configuration file, /var/lib/kubelet/config.yaml, contains:

clusterDNS:
  - 10.96.0.10
clusterDomain: cluster.local

For every Pod it starts, the kubelet generates /etc/resolv.conf from those two values and the Pod's namespace:

kubectl exec web -- cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5
LineComes fromMeaning
nameserverkubelet clusterDNSSend every lookup to the kube-dns Service
searchPod namespace + kubelet clusterDomainSuffixes that complete short names (previous lesson)
options ndots:5fixedA name with fewer than five dots is tried with each search suffix first, and as written last

The address in clusterDNS and the ClusterIP of the kube-dns Service must be the same number. kubeadm arranges that: it gives the Service the tenth address of the Service range and writes the same address to every kubelet. If the two ever differ, Pods send their queries to an address where nobody answers.

Reading the Corefile

kubectl -n kube-system get configmap coredns -o yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        health {
           lameduck 5s
        }
        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 {
           max_concurrent 1000
        }
        cache 30
        loop
        reload
        loadbalance
    }

.:53 { ... } is a server block: it handles every name (. is the root of DNS) on port 53. Each line inside is a plugin.

PluginWhat it does
errorsWrites errors to the Pod's log
healthLiveness endpoint on port 8080. lameduck 5s keeps answering for five seconds during shutdown
readyReadiness endpoint on port 8181, OK once the plugins are ready
kubernetes cluster.local in-addr.arpa ip6.arpaAnswers names in the cluster domain and reverse lookups, from the Services and EndpointSlices it watches. The cluster domain is written here
pods insecureAnswer <ip-with-dashes>.<ns>.pod.cluster.local for any IP without checking that such a Pod exists
fallthrough in-addr.arpa ip6.arpaReverse lookups for addresses that are not in the cluster are passed on to the next plugin
ttl 30Clients may cache cluster answers for 30 seconds
prometheus :9153Metrics
forward . /etc/resolv.confEvery name the plugins above did not answer goes to the DNS servers in the CoreDNS Pod's /etc/resolv.conf, which the kubelet copied from the node
cache 30Keeps answers for up to 30 seconds
loopStops CoreDNS if it detects that it is forwarding to itself
reloadRe-reads the Corefile when the ConfigMap changes
loadbalanceShuffles the order of multiple addresses in an answer
One query through the Corefilearrivesmissweb.apps.svc.cluster.localnot mineasksQueryfrom a Podcacheseen it before?kubernetescluster.local namesService or Pod IPanswerforwardevery other nameUpstream DNSnode's resolv.conf
One query through the Corefile. Cluster names are answered by the kubernetes plugin from what CoreDNS has watched. Every other name falls through to forward, which asks the node's DNS servers.

Parts:

  • Query (from a Pod)
  • cache (seen it before?)
  • kubernetes (cluster.local names)
  • Service or Pod IP (answer)
  • forward (every other name)
  • Upstream DNS (node's resolv.conf)

Connections:

  • Query to cache: arrives
  • cache to kubernetes: miss
  • kubernetes to Service or Pod IP: web.apps.svc.cluster.local
  • kubernetes to forward: not mine
  • forward to Upstream DNS: asks

dnsPolicy: which resolv.conf a Pod gets

A Pod can choose how its /etc/resolv.conf is built with spec.dnsPolicy:

dnsPolicyResult
ClusterFirst (the default)Cluster DNS, as described above. Names outside the cluster are forwarded upstream
DefaultThe Pod inherits the node's resolver settings. No cluster names. Despite the name, it is not the default
ClusterFirstWithHostNetFor Pods with hostNetwork: true that still need cluster names. Without it, a host network Pod behaves as Default
NoneNothing is generated. You supply everything in spec.dnsConfig

spec.dnsConfig (nameservers, searches, options) can also be added to the other policies to extend what the kubelet writes. CoreDNS itself runs with dnsPolicy: Default, which is exactly why its own /etc/resolv.conf is the node's and forward reaches the outside world.

Changing the configuration

Edit the ConfigMap. Common changes:

GoalChange in the Corefile
Use specific upstream serversforward . 8.8.8.8 1.1.1.1
Send one company domain to its own DNS serverAdd a second server block corp.example:53 with its own forward . 10.150.0.1 (example below)
Add a few fixed namesA hosts block before kubernetes, holding 10.0.0.5 legacy.corp.example and fallthrough
See every query while debuggingAdd the line log
Turn Pod records off or make them strictpods disabled or pods verified

The reload plugin notices the new ConfigMap after the kubelet has synced it into the Pods, which can take a minute or two. To apply it now, restart the Deployment: kubectl -n kube-system rollout restart deployment coredns.

Under the hood · Details worth knowing
  • Why ndots is 5. A name like web.apps.svc has two dots, fewer than five, so the suffixes are tried and it resolves. The cost is that api.github.com is first tried as api.github.com.default.svc.cluster.local and two more variants before the real name. A trailing dot (api.github.com.) marks a name as complete and skips the search list.
  • The loop on Ubuntu nodes. With systemd-resolved, the node's /etc/resolv.conf contains 127.0.0.53. Copied into the CoreDNS Pod, that address means CoreDNS itself, and loop stops the server with Loop ... detected for zone ".". The kubelet setting resolvConf: /run/systemd/resolve/resolv.conf hands Pods the real upstream servers instead. kubeadm sets it automatically on such nodes.
  • Changing the cluster domain takes two places that must agree: the kubernetes line of the Corefile and clusterDomain on every kubelet. Existing Pods keep their old search list until they are recreated.
  • kubeadm and the ConfigMap. kubeadm upgrade keeps your Corefile and migrates it to the new CoreDNS version.
  • NodeLocal DNSCache is an optional add-on: a small cache on every node at 169.254.20.10 that Pods ask instead of the Service. On such clusters the Pods' nameserver is that address.
  • Scaling. CoreDNS is an ordinary Deployment: kubectl -n kube-system scale deploy coredns --replicas=3.

Using it

Example · Take inventory of cluster DNS

The questions of a typical lab, each with its command.

kubectl -n kube-system get deploy,pods,svc,cm | grep -E 'dns|NAME'
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/coredns   2/2     2            2           12d
NAME                           READY   STATUS    RESTARTS   AGE
pod/coredns-6f6b679f8f-4kq2n   1/1     Running   0          12d
pod/coredns-6f6b679f8f-x9wlb   1/1     Running   0          12d
NAME               TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
service/kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   12d
NAME                DATA   AGE
configmap/coredns   1      12d
QuestionAnswer and where it is
Which DNS solution, how many Pods?CoreDNS, 2 (the Deployment)
Which IP do Pods use for DNS?10.96.0.10 (ClusterIP of kube-dns)
Where is the configuration file inside the Pod?/etc/coredns/Corefile (the -conf argument)
How does the file get there?ConfigMap coredns, mounted as a volume
What is the cluster's root domain?cluster.local (the kubernetes line of the Corefile)

Finish by checking that the Service really has the Pods behind it:

kubectl -n kube-system get endpointslices -l k8s-app=kube-dns
NAME             ADDRESSTYPE   PORTS        ENDPOINTS                 AGE
kube-dns-7xv9k   IPv4          9153,53,53   10.244.0.2,10.244.0.3     12d

Example · Forward a company domain to its own DNS server

Task: "Names under corp.example must be resolved by the server 10.150.0.1."

kubectl -n kube-system get cm coredns -o yaml > /root/coredns-backup.yaml
kubectl -n kube-system edit cm coredns

Add a second server block after the closing brace of the first, at the same indentation:

    corp.example:53 {
        errors
        cache 30
        forward . 10.150.0.1
    }

CoreDNS picks the block with the longest matching name, so intranet.corp.example goes to the new block and everything else to .:53. Apply and verify:

kubectl -n kube-system rollout restart deploy coredns
kubectl -n kube-system rollout status deploy coredns
kubectl -n kube-system logs -l k8s-app=kube-dns --tail=5
kubectl run dns-test --rm -it --restart=Never --image=busybox:1.28 -- nslookup intranet.corp.example
deployment "coredns" successfully rolled out
.:53
corp.example.:53
[INFO] plugin/reload: Running configuration SHA512 = 5f1c...
CoreDNS-1.12.1
linux/amd64, go1.24.1
Server:    10.96.0.10
Name:      intranet.corp.example
Address 1: 10.150.3.20

The log lists both server blocks, which proves that the new Corefile was parsed.

Example · Pods send their queries to the wrong address

Symptom: new Pods on node01 cannot resolve anything, Pods on other nodes can.

kubectl run t --rm -it --restart=Never --image=busybox:1.28 \
  --overrides='{"spec":{"nodeName":"node01"}}' -- cat /etc/resolv.conf
kubectl -n kube-system get svc kube-dns
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.100
options ndots:5
NAME       TYPE        CLUSTER-IP   ...
kube-dns   ClusterIP   10.96.0.10   ...

10.96.0.100 is not 10.96.0.10. The kubelet of that node writes the file, so the mistake is in its configuration:

ssh node01
grep -A 1 clusterDNS /var/lib/kubelet/config.yaml
clusterDNS:
- 10.96.0.100

Correct the address, then restart the kubelet. Only Pods started afterwards get the new file, so recreate the affected ones:

sed -i 's/10.96.0.100/10.96.0.10/' /var/lib/kubelet/config.yaml
systemctl restart kubelet
exit
kubectl rollout restart deploy webapp

Example · A Pod with its own DNS settings

Task: "Pod resolver-test must use only the DNS server 1.1.1.1 and the search domain corp.example."

apiVersion: v1
kind: Pod
metadata:
  name: resolver-test
spec:
  dnsPolicy: None            # generate nothing
  dnsConfig:                 # use exactly this
    nameservers:
      - 1.1.1.1
    searches:
      - corp.example
    options:
      - name: ndots
        value: "2"
  containers:
    - name: main
      image: busybox:1.37
      command: ["sleep", "3600"]
kubectl exec resolver-test -- cat /etc/resolv.conf
search corp.example
nameserver 1.1.1.1
options ndots:2

This Pod can no longer resolve web.apps: it does not ask CoreDNS at all.

Tricks

Common mistakes

  • Searching for a Service called coredns. The Service is kube-dns. So is the label.
  • Editing the Corefile inside the Pod. It is a read-only mount of the ConfigMap. Edit the ConfigMap.
  • Breaking the indentation of the ConfigMap. The Corefile is one multi-line string. Every line needs the same leading spaces as its neighbours.
  • Expecting running Pods to pick up a kubelet DNS change. /etc/resolv.conf is written when the Pod starts.
  • Using dnsPolicy: Default and expecting cluster names. Default means the node's DNS. The default policy is ClusterFirst.
  • Forgetting host network Pods. They need ClusterFirstWithHostNet to resolve Services.
  • Deleting CoreDNS Pods that are Pending on a new cluster. They wait for the network plugin, as lesson "CNI in Kubernetes" showed.

Recap

  • Cluster DNS is the Deployment coredns, the ConfigMap coredns (key Corefile) and the Service kube-dns, in kube-system. Pods carry the label k8s-app=kube-dns.
  • The kubelet writes clusterDNS and the search list into each Pod's /etc/resolv.conf at start.
  • In the Corefile, kubernetes cluster.local answers cluster names and forward . /etc/resolv.conf sends the rest to the node's DNS servers.
  • Change DNS by editing the ConfigMap, then restart the Deployment and read the logs.
  • dnsPolicy and dnsConfig change the resolver of a single Pod.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

2 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.

  • Find the DNS server every pod uses, then change CoreDNS so that names under a company domain are answered by the company's own DNS server.

    Build · Level 2 · standard · target 15 min

  • Section capstone. Repair a Service and a Pod's name resolution, then move the shop from an Ingress to a Gateway with routes that keep its paths, strip a prefix and redirect an old host name.

    Build · Level 3 · challenge · target 35 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.