Kubernetes custom DNS with CoreDNS

I had some DNS trouble with Kubernetes (k3s) on Oracle Cloud. Unfortunately, the iptables didn't work reliably, and since the Oracle Infrastructure is just my test environment I didn't mind patching the Kubernetes DNS Server CoreDNS which is used by k3s.

Export the CoreDNS ConfigMap as YAML

To get the CoreDNS Configuration as a YAML, you can run this command:

kubectl get configmap -o yaml -n kube-system coredns > coredns-configmap.yaml

now, you can either modify the config yourself, or take the example below to use Cloudflare’s DNS Server (see the line with forward . tls://)

Create a file coredns-patch.yaml with the following contents:

apiVersion: v1
data:
Corefile: |
.:53 {
errors
health
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
hosts /etc/coredns/NodeHosts {
ttl 60
reload 15s
fallthrough
}
prometheus :9153
forward . tls://1.1.1.1 tls://1.0.0.1 {
tls_servername cloudflare-dns.com
health_check 5s
}
cache 30
loop
reload
loadbalance
}
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system

… and apply it:

kubectl apply -f coredns-patch.yaml