CoreDNS on Alpine

Partheeban Kandasamy (PK)
3 min readMar 30, 2020

I’ve recently decided to get my hands dirty w/ my own homelab setup. Once I had my first host up an running ESXi, I wanted to setup a DNS service for the homelab, did somequick research and decided to go with CoreDNS. My lab consists of Dell PowerEdge rack servers but only had 32Gb memomy per host so i had to use my resources wisely and decided to run coreDNS on a lightweight VM. Why did i choose not to run this on a container? I might, later..

Another writeup will cover why a homelab makes sense for all IT professionals working on some sort of cloud related technologies. I’ll also cover the decisions during the buying process as well as provide my BOM. 

I couldn’t find detailed documentation from one source on how to setup CoreDNS on Alpine, so i’ve put together the steps that i’ve had to go through below.

Installing Alpine

I have a minimal VM (oversized for Alpine) with 1 core, 512mb memory and 2gb diskspace. I downloaded the Alpine extended ISO from the link below

Setup was easily completed following the steps below —

  1. Boot from Alpine ISO prompts you to run a script that will install to disk
  2. Run the setup-alpine script that guides you through the setup process

Installing CoreDNS

CoreDNS is a simple setup and the download link is provided below.

Once you download the archive, extract to find the coredns executable which you can run to start your DNS server. Let’s see how to configure CoreDNS below as it doesn’t do much without a configuration.

Configuration

Configure CoreDNS

To get started with you need a CoreFile located in the directory as the coredns executable. This file specifies what port to bind on and what zones to configure CoreDNS with. Here is what my CoreFile and the referenced db files that specify the necessary information that tells CoreDNS how to serve the names look like..

#All the coreDNS files were moved to /etc/coredns/
domainctrl:~# cat /etc/coredns/CoreFile

.:53 {
forward . 8.8.8.8 9.9.9.9 #fwd other queries to known DNS
log
errors
whoami
}
pdotk.example:53 {
file /etc/coredns/pdotk.db #COMMENT: see file content below
log
errors
}
10.0.0.0/8:53 {
file /etc/coredns/db.10 #COMMENT: see file content below
log
errors
}
domainctrl:~# cat /etc/coredns/pdotk.db
$ORIGIN pdotk.example.
@ IN SOA domainctrl.pdotk.example. pdotk.example. (
2017042745 ; serial
7200 ; refresh (2 hours)
3600 ; retry (1 hour)
1209600 ; expire (2 weeks)
3600 ; minimum (1 hour)
)
@ 3600 IN NS domainctrl.pdotk.example.
edgerouter.pdotk.example IN A 10.1.0.1
domainctrl.pdotk.example. IN A 10.1.0.2
esxi01.pdotk.example. IN A 10.2.10.10
esxi02.pdotk.example. IN A 10.2.10.20
vcsa.pdotk.example. IN A 10.3.10.90
domainctrl:~# cat /etc/coredns/db.10
$TTL 604800
@ IN SOA domainctrl.pdotk.example. pdotk.example. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
; name servers - NS records
IN NS domainctrl.pdotk.example.
;
; PTR Records
1.0.1 IN PTR edgerouter.pdotk.example ; 10.1.0.1
1.0.2 IN PTR domainctrl.pdotk.example. ; 10.1.0.2
2.10.10 IN PTR esxi01.pdotk.example. ; 10.2.10.10
2.10.20 IN PTR esxi02.pdotk.example. ; 10.2.10.20
3.10.90 IN PTR vcsa.pdotk.example. ; 10.3.10.90

Start the CoreDNS process using the command

domainctrl:~# /etc/coredns/coredns -conf /etc/coredns/CoreFile

Test your setup from another machine using the command

homepc ~ ❯ dig A vcsa.pdotk.example @10.1.0.2

CoreDNS on Alpine Startup

Now that we have a working DNS server, i wanted to take it a step further and have Alpine run CoreDNS on startup and for that , i created two files with execute permissions under /etc/local.d/dns.start (executes on startup) and dns.stop (executes on shutdown). This is what they look like..

domainctrl:~# ls -al /etc/local.d/
-rwxr-xr-x 1 root root 114 Mar 29 13:20 dns.start
-rwxr-xr-x 1 root root 85 Mar 28 15:31 dns.stop
domainctrl:~# cat /etc/local.d/dns.start
#!/bin/sh
echo "Starting CoreDNS"
/etc/coredns/coredns -dns.port=53 -conf /etc/coredns/CoreFile -quiet & disown
domainctrl:~# cat /etc/local.d/dns.stop
#!/bin/sh
echo "Stopping CoreDNS"
export pid_dns=$(pgrep coredns)
kill -9 $pid_dns

With these, you should have a worry free DNS setup on a tiny VM ready to go. Hopefully this helps you with your home network or homelab setup.

--

--