Disclaimer

Forwarding DNS requests with CoreDNS

dns kubernetes

Intro

In this post I’ll briefly explain setting up DNS forwarding using CoreDNS - deployed in Kubernetes using a Helm chart.

Value file can be found here https://github.com/serbangilvitu/coredns-example

Helm repo and value file

Add Helm repo

helm repo add coredns https://coredns.github.io/helm

Get value file

helm show values coredns/coredns --version 1.14.0 > values.yaml

Configuring forwarder

Following configuration (in values.yaml) will forward requests for example.org to 1.1.1.1, and requests for wikipedia.org to 8.8.8.8. This is already included in the example values.yaml

servers:
- zones:
  - zone: example.org.
  port: 53
  plugins:
  - name: errors
  # Serves a /health endpoint on :8080, required for livenessProbe
  - name: health
    configBlock: |-
      lameduck 5s
  # Serves a /ready endpoint on :8181, required for readinessProbe
  - name: ready
  - name: forward
    parameters: . 1.1.1.1:53
- zones:
  - zone: wikipedia.org.
  port: 53
  plugins:
  - name: errors
  - name: health
    configBlock: |-
      lameduck 5s
  - name: ready
  - name: forward
    parameters: . 8.8.8.8:53

This will generate the following Corefile

    example.org.:53 {
        errors
        health {
            lameduck 5s
        }
        ready
        forward . 1.1.1.1:53
    }
    wikipedia.org.:53 {
        errors
        health {
            lameduck 5s
        }
        ready
        forward . 8.8.8.8:53
    }

Install the chart

helm upgrade -i dns-forwarder coredns/coredns --version 1.14.0 -f values.yaml

Test

Start a test pod

kubectl run -it --rm --restart=Never --image=alpine:3.12 connect -- ash

You should be able to resolve example.org and wikipedia.org using the newly deployed CoreDNS

nslookup example.org dns-forwarder-coredns.dns.svc.cluster.local
nslookup wikipedia.org dns-forwarder-coredns.dns.svc.cluster.local
nslookup en.wikipedia.org dns-forwarder-coredns.dns.svc.cluster.local