Aziz
Aziz

Reputation: 953

Kubernetes deployments: Editing the 'spec' of a pod's YAML file fails

The env element added in spec.containers of a pod using K8 dashboard's Edit doesn't get saved. Does anyone know what the problem is?

Is there any other way to add environment variables to pods/containers?

I get this error when doing the same by editing the file using nano:

# pods "EXAMPLE" was not valid:
# * spec: Forbidden: pod updates may not change fields other than `containers[*].image` or `spec.activeDeadlineSeconds`

Thanks.

Upvotes: 15

Views: 16323

Answers (3)

Indrajeet Gour
Indrajeet Gour

Reputation: 4490

Not sure about others but when I edited the pod YAML from google Kubernetes Engine workloads page, the same error came to me. But if I retry after some time it worked.

feels like some update was going on at the same time earlier, so I try to edit YAML fast and apply the changes and it worked.

Upvotes: 1

kishorK
kishorK

Reputation: 543

We cannot edit env variables, resource limit, service account of a pod that is running live. But definitely, we can edit/update image name, toleration and active deadline seconds,, etc. However, the "deployment" can be easily edited because "pod" is a child template of deployment specification. In order to "edit" the running pod with desired changes, the following approach can be used.

Extract the pod definition to a file, Make necessary changes, Delete the existing pod, and Create a new pod from the edited file:

kubectl get pod my-pod -o yaml > my-new-pod.yaml
vi my-new-pod.yaml
kubectl delete pod my-pod
kubectl create -f my-new-pod.yaml

Upvotes: 10

rwehner
rwehner

Reputation: 4668

Not all fields can be updated. This fact is sometimes mentioned in the kubectl explain output for the object (and the error you got lists the fields that can be changed, so the others probably cannot).:

$ kubectl explain pod.spec.containers.env
RESOURCE: env <[]Object>

DESCRIPTION:
     List of environment variables to set in the container. Cannot be updated.

    EnvVar represents an environment variable present in a Container.

If you deploy your Pods using a Deployment object, then you can change the environment variables in that object with kubectl edit since the Deployment will roll out updated versions of the Pod(s) that have the variable changes and kill the older Pods that do not. Obviously, that method is not changing the Pod in place, but it is one way to get what you need.

Another option for you may be to use ConfigMaps. If you use the volume plugin method for mounting the ConfigMap and your application is written to be aware of changes to the volume and reload itself with new settings on change, it may be an option (or at least give you other ideas that may work for you).

Upvotes: 19

Related Questions