How can I regenerate the Corefile in k8s?

3/5/2020

My core dns corefile got corrupted somehow and now I need to regenerate it or reset it to it's default installed value. How do I do that? I've tried copying and pasting a locally-saved version of the file via kubectl edit cm coredns -n kube-system but I get validation errors

error: configmaps "coredns" is invalid                                                                                                                                                                             
A copy of your changes has been stored to "/tmp/kubectl-edit-suzaq.yaml"                                                                                                                                           
error: Edit cancelled, no valid changes were saved.
-- user_78361084
dns
kubernetes

1 Answer

3/5/2020

When you directly edit the setting, it used to give the error.

What can you do?

before you run anything, please take a backup:

kubectl -n kube-system get configmap coredns -o yaml > coredns.yaml

Ways #1, forcely apply it.

kubectl apply --force -f /tmp/kubectl-edit-suzaq.yaml

In most cases, it will apply the latest setting successfully by this way. If failed, go through the error, update the file /tmp/kubectl-edit-suzaq.yaml and forcely apply again.

Ways #2, delete and apply again.

kubectl -n kube-system get configmap coredns -o yaml > coredns.yaml

# do a backup, if you don't 100% sure the change will work
cp coredns.yaml coredns.yaml.orig

# update the change in coredns.yaml

# delete coredns
kubectl delete configmap coredns

# apply new change
kubectl apply -f coredns.yaml

Be careful, above steps will generate outage. if you work on a prod environment, you should think about to backup all kubernetes setting before doing above change.

-- BMW
Source: StackOverflow