How do I update the volumes in a running Kubernetes Pod?

11/15/2017

I have a Kubernetes pod that is already running on a Minikube (v0.23.0) node.

Some context: it's the coredns pod that is created from enabling the plugin. I'm working through this blog post, in an attempt to set up custom DNS entries for my Kubernetes cluster: https://coredns.io/2017/05/08/custom-dns-entries-for-kubernetes/

I'm able to replace the config map without a problem; I altered the template spec from the blog post to suit my needs and ran:

kubectl create -f configmap.yml -o yaml --dry-run | kubectl replace -f -

That seemed to work; I inspected the configmap using kubectl and everything looked fine.

However I'm not sure how to update the volumes. I tried putting something like this in a file:

apiVersion: v1
kind: Pod
metadata:
  labels:
    k8s-app: coredns
  namespace: kube-system
spec:
  containers:
  - name: coredns
  volumes:
  - configMap:
      items:
      - key: Corefile
        path: Corefile
      - key: cluster.db
        path: cluster.db
      name: coredns
    name: config-volume

Then I tried applying it with:

kubectl create -f k8s_config/coredns/volumes.yml -o yaml --dry-run | kubectl apply -f -

But that gave this error message:

error: error when retrieving current configuration of:
&{0xc4218be840 0xc420213500 kube-system  STDIN 0xc421a9e5e0 0xc421a9e5e0  false}
from server for: "STDIN": resource name may not be empty

I also tried dumping the contents of the coredns pod into a file, altering the volume section accordingly, then replacing the pod, but got this message:

The Pod "coredns-6b4fd7784-xhb5s" is invalid: spec: Forbidden: pod updates may not change fields other than `spec.containers[*].image`, `spec.initContainers[*].image`, `spec.activeDeadlineSeconds` or `spec.tolerations` (only additions to existing tolerations)

Short of deleting the pod and bringing it back up, how should I go about updating the volumes in a Pod? Is it even possible?

I suppose another overarching question would be: what's the correct way to update/configure the coredns pod/plugin? I can't seem to find a guide anywhere. In the blog post it says the following:

To create the new zone, we need to modify the coredns.yaml we have been using to create an additional file in the pod.

However this must have come from when coredns was manually set up in Kubernetes, and now that it "just works" as a plugin I'm not sure how to go about configuring it.

-- 3cheesewheel
coredns
kubernetes
minikube

1 Answer

11/16/2017

Looking at the Pod name coredns-6b4fd7784-xhb5s, it looks like your CoreDNS Pod is actually being managed by a Deployment controller.

Do not modify Pods managed by a deployment controller directly. To edit Pods managed by a deployment, edit the deployment object itself (kubectl -n kube-system edit deployment coredns) and modify the spec.template property. After editing the deployment, the controller will delete the old pod and create a new one with the updated specification.

If you simply updated a ConfigMap object and want the CoreDNS Pod to receive the new contents, its also sufficient to simply delete the old Pod; the deployment controller will create a new one with the most recent version of the ConfigMap.

If you're worried about a potential service interruption by deleting your CoreDNS Pod, consider increasing the deployment's spec.replica value to a value higher than 1.

-- helmbert
Source: StackOverflow