Learn to manage environment variables in Kubernetes pods and update them using direct modifications and ConfigMaps for dynamic configuration.
In this lab, you’ll learn how to manage environment variables in a Kubernetes pod and update them using both direct modifications and ConfigMaps. Follow along to understand how to check running pods, update environment variables, and integrate ConfigMaps for dynamic configuration.
Updating the Pod to Change the Environment Variable
To change the background color of the web application, update the APP_COLOR environment variable. Start by editing the pod manifest using:
Copy
Ask AI
kubectl edit pod webapp-color
Pods are immutable, so while you can edit a pod’s manifest, the changes cannot be applied directly. Instead, save the modified manifest locally and force replace the pod.
Below is an excerpt from the downloaded manifest where the APP_COLOR value is updated to "green":
Save your changes. If you receive an error stating that editing pods is forbidden due to immutability, don’t worry—the manifest is stored (e.g., in /tmp/kubectl-edit-3135302771.yaml). Force replace the existing pod with the updated manifest:
In addition to direct environment variable updates, Kubernetes supports managing configurations with ConfigMaps. This provides a dynamic way to update environment variables for your pods.
For the web application pod, we want to use a ConfigMap to set the environment variable dynamically. Create a new ConfigMap that sets APP_COLOR to dark blue:
Next, update the pod manifest so that the web application retrieves its environment variables from the ConfigMap instead of a static declaration. Modify the manifest to remove the direct env key and add an envFrom reference:
pod "webapp-color" deletedpod/webapp-color replaced
Finally, verify the update:
Copy
Ask AI
kubectl describe pod webapp-color
The pod description now shows that it sources its APP_COLOR environment variable from the webapp-config-map ConfigMap. When you access the web application, the background color will have changed to dark blue.
In this lab, you learned how to update environment variables both directly in a pod manifest and dynamically using ConfigMaps. These skills are essential for managing application configurations in Kubernetes.
Explore more about managing configurations in Kubernetes by visiting the Kubernetes Documentation.