Last modified October 30, 2018

Advanced CoreDNS Configuration

The CoreDNS addon running inside your cluster has additional configuration options and features that can be customized.

You can customize two of these configuration options on a per cluster basis through a ConfigMap inside your clusters. The ConfigMap is named coredns-user-values and is located in the kube-system namespace.

Note: This feature is only available in more recent cluster versions. To check if your cluster version supports customization through the ConfigMap, you can check if the above-mentioned ConfigMap is present.

$ kubectl -n kube-system get cm coredns-user-values
NAME                                   DATA      AGE
coredns-user-values                    0         11m

On cluster creation the ConfigMap is empty and below-mentioned defaults will be applied to the final CoreDNS deployment. To customize any of the configuration options, you just need to add the respective line(s) in the data field of the user ConfigMap.

Warning: Please do not edit any of the other CoreDNS related resources. Only the user ConfigMap is safe to edit.

Cache settings

By default we set the cache TTL for CoreDNS to 30 seconds. You can customize the cache settings of CoreDNS by setting the value of the cache field in the user ConfigMap like following.

data:
  cache: 60

Above setting increases the TTL to 60 seconds.

The cache plugin also supports much more detailed configuration which is documented in the upstream documentation.

Additional forwards (formerly known as proxy)

In CoreDNS version 1.4.0 the proxy plugin has been deprecated. The same behaviour can be achieved now with forward although the syntax can be a bit different. The forward plugin has better performance because it reuses opened upstream connections.

The default forward entry we set in CoreDNS is

forward . /etc/resolv.conf

You can add additional forward entries by adding a each as a line to the forward field of the user ConfigMap. They will be selected in sequential order.

You can use a simple line or multiple lines to define the upstreams of the default server block.

data:
  forward: 1.1.1.1
data:
  forward: |
    1.1.1.1
    8.8.8.8

Warning: The number of forward upstreams is limited to 15.

Above example would result in the following additional forward entries in the CoreDNS configuration:

forward . 1.1.1.1 /etc/resolv.conf
forward . 1.1.1.1 8.8.8.8 /etc/resolv.conf

This setting would forward all requests to 1.1.1.1 which is Cloudflare’s DNS. If the first upstream fails the second IP (8.8.8.8) will be used as resolver. In case it fails too, all requests will be resolved by the default DNS provider set for your cluster.

The forward plugin also supports much more detailed configuration which is documented in the upstream documentation.

Advanced configuration

In case you need to have a finer granularity you can define custom server blocks with all desired configuration. They will be parsed after the catch-all block in the Corefile. As an example, let’s define a block for a example.com with some custom configuration:

data:
  custom: |
    example.com {
      forward . 9.9.9.9
      cache 2000
    }

This custom configuration allows CoreDNS resolve all example.com requests to a different upstream DNS resolver (9.9.9.9) than generic one. At the same time we use a different cache TTL(2000) setting.

Warning: Please make sure you test the final Corefile carefully. We do not take responsibility for incorrect custom configuration that could break workload communication.

Further reading