Best Practices for Managing Immutable Pod Configurations.

Aaradhy Srivastava

--

Always Remember, you CANNOT edit specifications of an existing POD other than the below.

spec.containers[*].image
spec.initContainers[*].image
spec.activeDeadlineSeconds
spec.tolerations

For example you cannot edit the environment variables, service accounts, resource limits (all of which we will discuss later) of a running pod.

But if you really want to, you have 2 options:

Editing a Pod (Directly):
Pods can be considered as primarily non-modifiable. Changes can only be made to certain parameters such as: —
1. Container Image(s):
(spec.containers[*].image or spec.initContainers[*].image)
2. Active deadline seconds
3. Tolerations

Any attempts to alter these such as environment variables will result in Kubernetes denying the operation.

Two Options to Change Other Fields:

1. Edit And Replace:

Use:

kubectl edit pod <pod-name>

However, when permission is denied, the changes are retained in the temporarily. So basically it will not applied to the running pod directly. So for this we have the option to delete the pod and recreate it from the temp file where it is placed . How?

Delete the pod:

kubectl delete pod <pod-name>

Create it from temporary file:

kubectl create -f /tmp/<temp-file>.yaml

2. Export And Recreate:

Export the pod yaml content:

kubectl get pod <pod-name> -o yaml > my-new-pod.yaml

Edit an exported pod my-new-pod.yaml.

Delete the pod using the command:

kubectl delete pod <pod-name>

Create it again using the command:

kubectl create -f my-new-pod.yaml

Now if you want to edit any deployment in which pod comes as a child we can do it unlike changing in pod yaml.

  • If you edit its pod template (kubectl edit deployment <deployment-name>), Kubernetes automatically deletes and recreates Pods with the new configuration.
  • This is easier since you don’t have to manually delete/recreate Pods.

Editing a Deployment:

A Deployment is used to control the Pods. You can change pod template configurations using (kubectl edit deployment <deployment-name>) and Kubernetes would take care of deleting the old pods and adding new ones with the updated configurations automatically. This is easier since you don’t have to manually delete/recreate Pods.

Let’s understand through example:

Scenario: Update the container image in a Pod.
Suppose you have a Pod named my-app-pod, and it uses the container image nginx:1.18. You want to update it to nginx:1.19.

Editing the Pod Directly (Allowed Change).

Since updating the container image is allowed, you can use:

  1. Run edit command.
kubectl edit pod my-app-pod

2. Locate the image field under container in a text editor.

spec:
containers:
- name: nginx
image: nginx:1.18 # Change this to nginx:1.19

3. Save and exit. Kubernetes updates the Pod with the new image.

Making Other Changes (Not Allowed Directly)
If you want to change something else, like adding an environment variable, you cannot do this directly. Instead:

Option 1: Edit and Replace (Temporary File).

  1. Run edit commad that is created in cluster.
kubectl edit pod my-app-pod

You are now wondering how can we get the temp file to edit so when you attempt to add the environment variable in the main pod yaml file it will denied to edit and throw some error . When denied, note the saved temporary file location (e.g., /tmp/kubectl-edit-xyz.yaml).

2. Delete the existing pod.

kubectl delete pod my-app-pod

3 . Recreate from temp location.

kubectl create -f /tmp/kubectl-edit-xyz.yaml

Option 2: Export and Recreate.

  1. Export Yaml
kubectl get pod my-app-pod -o yaml > my-new-pod.yaml

2. Edit the my-new-pod.yaml.

spec:
containers:
- name: nginx
image: nginx:1.18
env:
- name: MY_ENV_VAR
value: "my-value" # Adding an environment variable

3. Delete existing pod.

kubectl delete pod my-app-pod

4. Recreate a pod.

kubectl create -f my-new-pod.yaml

Now , we will take example as how to edit deployment.

If my-app-pod is part of a Deployment:

  1. Run.
kubectl edit deployment my-app-deployment

2. Find the Pod template in the YAML, and update the container image or other properties:

spec:
template:
spec:
containers:
- name: nginx
image: nginx:1.19 # Change the image

3. Save and exit. Kubernetes automatically updates the Pods managed by the Deployment.

Thank you for reading! If you found this post helpful, feel free to connect with me on LinkedIn 💼 or follow me on Twitter 🐦 for more insights on Kubernetes and cloud-native technologies. 🚀.

--

--

No responses yet