Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

kubernetes

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.

like image 719
Aziz Avatar asked Nov 01 '16 11:11

Aziz


People also ask

How do I edit a deployment file in Kubernetes?

You can edit a Deployment by changing the container image from one version to the other, decreasing or increasing the number of instances by changing the ReplicaSet value.

How do I edit a yaml file on a pod?

You can edit pod yaml on the fly using kubectl edit pods <pod-name> . You have to keep in mind that there are fields which will not be allowed to be edited while pod is scheduled, this is mentioned in your error message. I think you should first remove the pod and apply the new yaml file.

What is spec in Kubernetes yaml file?

spec. selector — An optional object that tells the Kubernetes deployment controller to only target pods that match the specified labels. Thus, to only target pods with the labels of "app" and "deployer", you can make the following modification to our deployment YAML.


1 Answers

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).

like image 138
rwehner Avatar answered Nov 15 '22 00:11

rwehner