Skip to main content
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.

Checking the Running Pod

First, check how many pods are currently running. In this example, we’re working with a single pod named “webapp-color”:
Output:
Next, inspect the pod details to identify the configured environment variables. Look for the APP_COLOR variable, which in this case is set to pink:
When you access the web application via the provided link, you’ll notice that the background color is pink.

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:
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:
You should see the following confirmation:
After the pod is recreated, verify that the change has taken effect:
The output will now reflect that the environment variable APP_COLOR is set to green, and the web application’s background should display green.

Working with ConfigMaps

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.

Listing Existing ConfigMaps

To see the current ConfigMaps in the default namespace, run:
Output:

Inspecting a Specific ConfigMap

To check the database host specified in the db-config ConfigMap, describe it:
The description will include:
This shows that the DB_HOST is set to SQL01.example.com.

Creating a New ConfigMap for the Web Application

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:
This command creates a ConfigMap named webapp-config-map containing the desired environment variable.

Updating the Pod to Use the New ConfigMap

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:
Save your updated manifest and force replace the pod by running:
You will see confirmation messages:
Finally, verify the update:
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.

Watch Video