Idempotently set upstream nameservers in dns addon

I’m running an old version of microk8s (1.18), and need update the DNS nameserver to Azure’s one so that my Private Link connection resolves correctly. Newer versions support enable dns: but alas I’m one version behind and am not in a place where I can upgrade.

Changing the DNS after the fact is easy enough to do via microk8s kubectl -n kube-system edit configmap/coredns and editting out the,

apiVersion: v1
  Corefile: |
    .:53 {
        health {
          lameduck 5s
        log . {
          class error
        kubernetes cluster.local {
          pods insecure
        prometheus :9153
        forward .
        cache 30
kind: ConfigMap

However I don’t see how I could do this programmatically (i.e. via Ansible). Can I create this ConfigMap file somewhere and tell microk8s to use it? I can’t find any configuration/yaml in /var/snap that relates to this change.

I’m very new to Kubernetes so apologies in advance if I’m asking something obvious. I need to be able to do this programmatically so I can apply the same steps across multiple environments.

Hi you can try some of the techniques diacussed here, such as exporting the yaml using kubectl get cm | sed. ... | kubectl apply -f - ....

Awesome, thanks for the pointers!

I’ve now created a simple Ansible playbook to do this for me, sharing below in case it’s useful for others:

- name: Update coredns nameservers
  hosts: jupyter
    nameservers: # Azure

  - name: Read coredns configmap
    command: microk8s kubectl get configmap/coredns -n kube-system -o yaml
    register: coredns_config_stdout
    changed_when: no
    become: yes
    become_user: "{{ app_user }}"

  - name: Parse configmap yaml into usable variable
      coredns_config: "{{ coredns_config_stdout.stdout | from_yaml }}"

  - name: Replace 'forward' nameservers with our config
          Corefile: "{{ | regex_replace('forward \\. ([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+ *)+\\n', 'forward . ' + nameservers + ' \\n' ) }}"

  - name: Create coredns configmap file
      content: "{{ coredns_config | combine(new_coredns_config) | to_yaml }}"
      dest: "/home/{{ app_user }}/coredns.configmap.yaml"
    become: yes
    when: !=

  - name: Load in new coredns configmap
    command: microk8s kubectl apply -f "/home/{{ app_user }}/coredns.configmap.yaml"
    when: !=
    become: yes
    become_user: "{{ app_user }}"
1 Like