img1

CoreDNS is a DNS server maintained by the Cloud Native Computing Foundation. While many people prefer DNS servers like dnsmasq, unbound or BIND, I like CoreDNS due to its simplicity. Although it is commonly used with Kubernetes, you can deploy it almost anywhere.

Some considerations

I would strongly advise that you use 2 servers running on different systems. For my personal setup I run CoreDNS in LXC to ensure redundancy and flexibility. This is very important when making changes as chances are you will accidentally push a breaking change at some point.

Installing

To install CoreDNS, you have a few options. The first option is to use native packages. While this might sound great in theory, it can come with few drawbacks since currently Alpine is one of the only mainstream distros packaging CoreDNS. I personally find Alpine Linux to be challenging, but that ultimately is up to you.

The second option is Docker. While this would work for the vast majority of people, I personally like systemd therefore I went with Podman quadlets instead.

To get started with CoreDNS, first create a container file called coredns.container with the following:

[Unit]

[Container]
ContainerName=CoreDNS
Image=docker.io/coredns/coredns
PublishPort=53:53/udp
PublishPort=53:53/tcp
PublishPort=443:443/tcp
Volume=REPLACE_ME_WITH_FULL_PATH_TO_COREDNS_DIR:/etc/coredns:ro,Z
AutoUpdate=registry
Exec=-conf /etc/coredns/Corefile

[Install]
WantedBy=default.target

[Service]
Restart=always

Then install the service with sudo podman quadlet install coredns.container

To start it run sudo systemctl start coredns

Configuring CoreDNS

To start, create a directory called coredns. I would also highly recommend initializing a git repo in the same directory since git will allow you to rollback changes. You can initialize the new repo with git init . Once you have your directory setup, cd into that directory as we will need to create several files.

Once you are in the coredns directory, create the following files inside your coredns directory:

Corefile:

example.com {
  hosts /etc/coredns/example.com
}

. {
  forward . 9.9.9.9 149.112.112.112
}

example.com:

127.0.0.55  boot.example.com othersite.example.com
127.4.5.65  example.com

2006:db8::7 example.com
2006:db8::9 git.example.com othersite.example.com

In the above example, we are using Quad9 as our DNS resolver and configuring IP4 and IP6 records for some domains under example.com. CoreDNS supports hostfile like file syntax so creating records is a breeze. Additionally, you can add many different domains as chances are you don’t want to use example.com.

Further reading