Skip to content

How to customize rke2-coredns

Article Number: 000021179

Environment

A Rancher-provisioned or standalone RKE2 cluster

Situation

RKE2 allows the use of HelmChartConfig resources to customize add-on packages deployed as Helm Charts. This can be used to customize RKE2 packaged compoments, such as Canal, CoreDNS and ingress-nginx.

Resolution

Details on the customization of components via HelmChartConfig resources can be found within the RKE2 documentation

In the example below, we will use a HelmChartConfig to add a custom zonefile to rke2-coredns for the domain example.com

apiVersion: helm.cattle.io/v1
kind: HelmChartConfig
metadata:
  name: rke2-coredns
  namespace: kube-system
spec:
  valuesContent: |-
    zoneFiles:
      - filename: example.com.conf
        domain: example.com
        contents: |
          example.com:53 {
              errors
              cache 30
              forward . 10.0.254.1
          }
    extraConfig:
      import:
        parameters: /etc/coredns/example.com.conf

In the next sample, we'll use a HelmChartConfig to modify the default root '.'  zonefile for rke2-coredns, adding some host entries using the hosts plugin:

apiVersion: helm.cattle.io/v1
kind: HelmChartConfig
metadata:
  name: rke2-coredns
  namespace: kube-system
spec:
  valuesContent: |-
    zoneFiles: 
      - filename: Corefile
        contents: |
          .:53 {
            errors
            health {
                lameduck 10s
            }
            ready
            kubernetes  cluster.local  cluster.local in-addr.arpa ip6.arpa {
                pods insecure
                fallthrough in-addr.arpa ip6.arpa
                ttl 30
            }
            prometheus  0.0.0.0:9153
            forward  . /etc/resolv.conf
            cache  30
            loop
            reload
            loadbalance

            hosts {
            10.1.1.1      web1.local
            10.1.1.2      web2.local
            10.90.3.1     sql.prod.space.net
            fallthrough
            }

Note, the full Corefile content is used in the above example to set the hosts plugin values. This is needed as items can't be appended to a list in helm values, care should be taken when combining this with other helm value changes for coredns

Rancher-provisioned RKE2 cluster

To apply this HelmChartConfig customization in a Rancher-provisioned RKE2 cluster, navigate to the Cluster Management view and click Edit Config for the desired cluster. Click the Add-On Config tab and add the HelmChartConfig manifest into the Additional Manifest section, before clicking Save.

Standalone RKE2 cluster

Create the file /var/lib/rancher/rke2/server/manifests/rke2-coredns-config.yaml, containing the HelmChartConfig manifest content. The file must be created on every server node within the RKE2 cluster, the rke2-server service will detect changes to the files in this directory and apply these to the cluster.

Verify the customization

In this case, with a customization to the rke2-coredns configuration, the configmap rke2-coredns-rke2-coredns can be reviewed to determine if the change was successful:

kubectl -n kube-system get configmap rke2-coredns-rke2-coredns -o json