Skip to content
LF Cert Trainer

Lesson 105 · Section 9: Networking

Lesson 105: CoreDNS: a DNS server built from plugins

CoreDNS on its own, before it runs in a cluster: what it is, how the Corefile is read, how server blocks and plugins handle a query, and how to build, test and debug a small DNS server with hosts, forward, cache and log. · 14 min read · 3 min video

1/9CoreDNS
0:00 / 2:55

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

What CoreDNS is

The last lesson ended with a gap. Every host had a nameserver line that pointed at 192.168.1.100, and we simply assumed that a DNS server was running there. This lesson fills the gap with CoreDNS.

CoreDNS is a DNS server written in Go and shipped as a single program. It is a graduated project of the Cloud Native Computing Foundation, and it is the DNS server that kubeadm installs in every cluster. The CKA curriculum names it directly: "Understand and use CoreDNS".

In a cluster, CoreDNS arrives already configured, inside a Pod, with its settings in a ConfigMap. That makes it easy to use and hard to understand. So this lesson takes it out of the cluster and runs it on a plain Linux host. With the Kubernetes parts removed, three ideas remain, and they are all you need: the Corefile, server blocks, and plugins. The lesson CoreDNS in Kubernetes then puts it back.

One DNS server for the whole networkport 53port 53read at startrestHost Anameserver .1.100Host Bnameserver .1.100CoreDNS192.168.1.100:53Corefilethe configurationUpstream DNS8.8.8.8
One DNS server for the whole network. Every host points its resolv.conf at the CoreDNS host. CoreDNS answers the names it knows and forwards the rest to an upstream server.

Parts:

  • Host A (nameserver .1.100)
  • Host B (nameserver .1.100)
  • CoreDNS (192.168.1.100:53)
  • Corefile (the configuration)
  • Upstream DNS (8.8.8.8)

Connections:

  • Host A to CoreDNS: port 53
  • Host B to CoreDNS: port 53
  • Corefile to CoreDNS: read at start
  • CoreDNS to Upstream DNS: rest

Running it

CoreDNS is one file. Download a release from the project's GitHub page, unpack it, and run it:

./coredns
.:53
CoreDNS-1.12.0
linux/amd64, go1.23.3, 51e11f1

The first line says what it serves: the zone . (every name) on port 53. With no configuration at all, CoreDNS loads only two small plugins, whoami and log, which is enough to prove that it runs and useless as a DNS server. Real behaviour comes from a file named Corefile in the directory where it starts.

FlagMeaning
-conf <file>Read this Corefile instead of ./Corefile
-dns.port <port>Listen on this port when the Corefile names none (default 53)
-pluginsList the plugins compiled into this binary
-versionPrint the version

Port 53 is a privileged port, so the plain command needs root. For experiments, a high port such as 1053 avoids that, and avoids a clash with a resolver that may already use port 53 on the machine.

The Corefile

.:53 {
    errors
    log
    hosts /etc/coredns/hosts {
        fallthrough
    }
    forward . 8.8.8.8
    cache 30
}

The shape is always the same:

ZONE:PORT {
    PLUGIN [arguments]
    PLUGIN [arguments] {
        option
    }
}
  • The part before the brace is the zone this block is responsible for, and optionally the port. . is the root zone, which contains every name. example.com would contain only names that end in example.com.
  • Each line inside enables one plugin. Some take arguments on the same line, and some have their own inner block of options.
  • A line that starts with # is a comment.

Read as a sentence, the file above says: "For every name, on port 53: report errors, log each query, answer from the file /etc/coredns/hosts if the name is there, otherwise ask 8.8.8.8, and remember answers for up to 30 seconds."

Plugins

Everything CoreDNS can do is a plugin. These are the ones worth knowing for the exam: together they make up almost the whole Corefile of a cluster.

PluginWhat it doesTypical line
errorsPrints errors to standard outputerrors
logPrints one line for every querylog
healthServes a liveness endpoint on port 8080 (/health)health
readyServes a readiness endpoint on port 8181 (/ready)ready
hostsAnswers A, AAAA and PTR queries from a file in /etc/hosts formathosts /etc/coredns/hosts
fileServes a zone from a standard zone filefile db.example.com
forwardSends queries to other DNS serversforward . 8.8.8.8 or forward . /etc/resolv.conf
cacheKeeps answers, at most for the given number of secondscache 30
rewriteChanges a query before it is answeredrewrite name shop.example.com web.example.com
loopStops CoreDNS when it detects that it forwards to itselfloop
reloadRe-reads the Corefile when it changes, without a restartreload
loadbalanceShuffles the order of addresses in an answerloadbalance
prometheusServes metricsprometheus :9153
kubernetesAnswers Service and Pod names from the Kubernetes APIkubernetes cluster.local in-addr.arpa ip6.arpa

The order is fixed

A query walks a chain of pluginsquerystarts the chainmissnoneasks upstreamClientdb.example.com?CoreDNSserver block .:53Upstream DNS8.8.8.8cacheseen it before?hostsin the hosts file?forwardask someone else
A query walks a chain of plugins. Inside a server block, plugins are tried in a fixed order. One of them answers, or passes the query to the next. Here cache answers repeats, hosts answers local names, and forward sends everything else upstream.

Parts:

  • Client (db.example.com?)
  • CoreDNS (server block .:53)
  • Upstream DNS (8.8.8.8)
  • cache (seen it before?)
  • hosts (in the hosts file?)
  • forward (ask someone else)

Connections:

  • Client to CoreDNS: query
  • CoreDNS to cache: starts the chain
  • cache to hosts: miss
  • hosts to forward: none
  • forward to Upstream DNS: asks upstream

A query is passed through the enabled plugins like a chain. Each plugin either answers, or does its part and hands the query to the next one.

The surprise is where the order comes from. It is not the order of the lines in your Corefile. The order is compiled into the CoreDNS binary, and for the plugins above it is: ready, health, errors, log, cache, rewrite, hosts, kubernetes, file, loop, forward. You can write forward on the first line and hosts on the last: hosts is still asked before forward.

So a Corefile is a list of which plugins are on, with their settings. Write it in whatever order is easiest to read.

Fallthrough

Some plugins are a final authority for their zone: hosts, file and kubernetes. When such a plugin is asked for a name in its zone that it does not have, it answers with an error itself, and the chain ends there.

Often that is not what you want. A small hosts file should override a few names, and every other name should go on to forward. The option fallthrough says so: "if you have no record, pass the query to the next plugin".

hosts /etc/coredns/hosts {
    fallthrough
}

Without it, this server would answer the three names in the file and fail for the rest of the internet.

Under the hood · Why is the order not configurable?

Fixed ordering looks like a limitation, and it is a deliberate one. A plugin chain is code: each plugin calls the next. If the order followed the Corefile, a harmless reordering of lines could put cache behind forward, so that nothing is ever cached, or put log behind the plugin that answers, so that queries are never logged.

With a fixed order, such mistakes cannot happen, and two Corefiles with the same lines always behave the same. The list lives in a file named plugin.cfg in the CoreDNS source. People who really need another order, or an extra plugin, compile their own CoreDNS. For the exam, one fact is enough: line order in a server block does not matter.

Server blocks and zones

Server blocks: the most specific zone winsmatchrestreadssendsdb.example.coma querywww.cncf.ioa queryexample.com:53hosts.:53forward, cacheHosts file/etc/coredns/hostsUpstream DNS8.8.8.8
Server blocks: the most specific zone wins. A Corefile can hold several server blocks. Each query goes to the block whose zone matches the longest part of the name, and only that block's plugins see it.

Parts:

  • db.example.com (a query)
  • www.cncf.io (a query)
  • example.com:53 (hosts)
  • .:53 (forward, cache)
  • Hosts file (/etc/coredns/hosts)
  • Upstream DNS (8.8.8.8)

Connections:

  • db.example.com to example.com:53: match
  • www.cncf.io to .:53: rest
  • example.com:53 to Hosts file: reads
  • .:53 to Upstream DNS: sends

A Corefile may contain several server blocks. Each query is given to exactly one of them: the block whose zone matches the longest part of the name.

example.com:53 {
    hosts /etc/coredns/hosts
    log
}

.:53 {
    forward . 8.8.8.8
    cache 30
    errors
}

db.example.com matches both example.com and ., and the first is more specific, so only the first block's plugins see the query. www.cncf.io matches only ., and is forwarded.

Two details follow from this:

  • Blocks do not share plugins. The cache and errors lines in the second block do nothing for example.com queries. Each block needs its own.
  • Here hosts has no fallthrough, and that is right: the first block has no forward to fall through to. A name such as nothere.example.com gets an error answer, which is correct for a zone you own.

The same technique is how a cluster sends one company domain to a company DNS server: an extra server block for that zone only.

How this looks in a cluster

You will meet CoreDNS in the exam as a Deployment, not as a binary. This is the Corefile kubeadm gives it, stored in the ConfigMap coredns in kube-system:

.: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
}

After this lesson you can already read most of it. One server block for all names. The kubernetes plugin is the authority for cluster.local, in the same way hosts was for the hosts file. Everything else reaches forward . /etc/resolv.conf, which means "ask the DNS servers of the node this Pod runs on". The lesson CoreDNS in Kubernetes goes through the remaining options and shows how to change the file safely.

Using it

Example · Serve the names from a hosts file

Put the names of your network in a file in the usual hosts format:

# /etc/coredns/hosts
192.168.1.10   web.example.com web
192.168.1.11   db.example.com db
192.168.1.12   cache.example.com

Write a Corefile next to the binary:

.:53 {
    errors
    log
    hosts /etc/coredns/hosts {
        fallthrough
    }
    forward . 8.8.8.8
    cache 30
}

Start CoreDNS and test from the same machine:

sudo ./coredns -conf Corefile &
dig @127.0.0.1 +short db.example.com
dig @127.0.0.1 +short -x 192.168.1.11
192.168.1.11
db.example.com.

The hosts plugin also answers the reverse lookup. It re-reads the file every few seconds, so a new line needs no restart.

Example · Make the other hosts use it

On every other machine, name the CoreDNS host as the DNS server and add a search domain for short names:

# /etc/resolv.conf
nameserver 192.168.1.100
search example.com
nslookup db
Server:         192.168.1.100
Address:        192.168.1.100#53

Name:   db.example.com
Address: 192.168.1.11

The whole network now resolves db, and a new host means one new line in one file. Public names still work, because CoreDNS forwards what it does not know.

Example · Send one domain to a different DNS server

The company has an internal DNS server at 10.0.0.53 for corp.internal. Everything else should still go to the public resolver. Add a server block for that zone. The most specific zone wins, so only names under corp.internal enter it:

corp.internal:53 {
    errors
    cache 30
    forward . 10.0.0.53
}

.:53 {
    errors
    forward . 8.8.8.8
    cache 30
}
dig @127.0.0.1 +short wiki.corp.internal
dig @127.0.0.1 +short kubernetes.io

The first query goes to 10.0.0.53, the second to 8.8.8.8. Inside the new block, forward . means "everything that reached this block". In a cluster, this extra block in the CoreDNS ConfigMap is the standard answer to "Pods must resolve the names of our internal domain".

Example · Watch queries arrive with the log plugin

With log enabled, CoreDNS prints one line per query:

[INFO] 192.168.1.10:41873 - 5210 "A IN db.example.com. udp 55 false 1232" NOERROR qr,aa,rd 62 0.000112s
[INFO] 192.168.1.10:38211 - 911 "A IN nothere.example.com. udp 60 false 1232" NXDOMAIN qr,rd,ra 135 0.019s

Read it from the left: who asked, the record type (A), the exact name that was asked, and the result (NOERROR or NXDOMAIN). The name is the valuable part. When a client appends search domains, you see every attempt here, for example web.shop.svc.cluster.local. followed by web.svc.cluster.local.. No other tool shows so directly what a client really asked for.

Tricks

Common mistakes

  • Expecting plugins to run in Corefile order. The order is compiled in. Moving a line up or down changes nothing.
  • Leaving out fallthrough. A hosts block for the root zone without it answers only the names in the file, and every other lookup fails.
  • Leaving out forward. A server that is the only nameserver of a network must pass on what it does not know, or public names stop resolving.
  • Forwarding to yourself. If the upstream in forward leads back to CoreDNS (for example 127.0.0.53 or the server's own address), queries go round in a circle. The loop plugin detects this and stops CoreDNS on purpose, with the message Loop ... detected for zone ".".
  • Starting on port 53 when something already listens there. On Ubuntu, systemd-resolved holds 127.0.0.53:53. CoreDNS then fails with bind: address already in use. Use another port or address, or stop the other service.
  • Assuming blocks share settings. cache, log and errors apply only inside the server block where they are written.
  • A typo in a plugin name. CoreDNS refuses to start with Unknown directive. In a cluster, that shows as CoreDNS Pods in CrashLoopBackOff right after a ConfigMap edit.

Recap

  • CoreDNS is a single-binary DNS server, and the cluster DNS of Kubernetes. Its configuration is one file, the Corefile.
  • A server block is zone:port { ... }. A query goes to the block with the most specific matching zone.
  • Each line in a block enables a plugin. Plugins form a chain with a fixed, compiled-in order, whatever the order of the lines.
  • hosts (or kubernetes in a cluster) answers local names, fallthrough passes on what it cannot answer, and forward sends the rest to another server.
  • cache, errors and log make it fast and observable. Test with dig @server name, and read the log.

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.

Discussion

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