CoreDNS retention on server restart

Earlier we used to edit the coredns configmap and put the hostfile entries there. But whenever there was a server restart it was lost and had to be redone. Also if we make any mistakes while editing the default configmap, it will result in the coredns not working. Later we discovered that coredns allows us to add customization as a separate configmap named coredns-custom in the namespace kube-system.

Below are the steps:

  1. In the master node create hostfile-cm.yaml file and add the below content

apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns-custom
  namespace: kube-system
data:
  test.server: |
    test.ai:53 {
        errors
        hosts {
            10.1.2.3            prod.test.ai
            103.45.67.89        stage.test.ai
        }
        reload
        loadbalance
    }


  1. Apply the configuration

kubectl apply -f hostfile-cm.yaml


  1. Get the pod name of coredns

kubectl -n kube-system get po


  1. Delete the pod so that the coredns deployment recreates it with loading the coredns-custom configmap in the process.

kubectl -n kube-system delete po <coredns-pod-name>


  1. Execute the below command to ssh into any pod other than coredns pod

kubectl -n kube-system exec --stdin --tty <traefik_pod_name> -- /bin/sh


  1. Then try pinging the domain names


  1. If ping is not there then install it using the below commands

apt update


apt install -y iputils-ping