Pod inspection and environment variables
- Check how many pods exist in the cluster:
webapp-color.
- Inspect the Pod to view container details and environment variables:
describe output:
- Environment variable name:
APP_COLOR - Current value:
pink
Change the Pod color to green
Goal: update the environment variable so the web app displays a green background. Important: Many Pod spec fields are immutable. Attempting to edit and save certain fields for a running Pod will produce an error. Example error when trying to update immutable fields:Direct edits to immutable Pod fields will be rejected. Use
kubectl edit to create a local copy of your changes (kubectl saves the attempted edit to a temp file), then replace the Pod by deleting and recreating it from that edited manifest.-
Run
kubectl edit pod webapp-colorand change theenvvalue forAPP_COLORfrompinktogreen. When you save, the API will reject the edit, but kubectl keeps your changed manifest in a temporary file (for example:/tmp/kubectl-edit-3135302771.yaml). - Replace the Pod using the saved file:
- After the Pod is recreated and Ready, open the web UI again and verify the background color is now green.
Pod specs have many immutable fields. If you need to change an immutable field for a Pod, recreate it with the updated manifest or use higher-level controllers (for example, a Deployment) which support rolling updates.
ConfigMaps: list and inspect
- List ConfigMaps in the
defaultnamespace:
default namespace.
- Inspect the
db-configConfigMap to find the database host:
SQL01.example.com.
Create a ConfigMap for the webapp
Create a ConfigMap that providesAPP_COLOR='dark blue' for the webapp:
APP_COLOR: "dark blue" under data:.
Update the Pod to consume the ConfigMap
There are two common ways to expose ConfigMap data to a container as environment variables:- Set individual env vars using
valueFrom.configMapKeyReffor specific keys. - Import all keys from a ConfigMap using
envFromwithconfigMapRef.
envFrom to import every key from webapp-config-map.
Because the Pod spec is immutable for many fields, follow the same replace workflow:
- Run
kubectl edit pod webapp-colorand modify the container spec to includeenvFrom. The container section should resemble:
- Save the edit (kubectl typically stores it at a path like
/tmp/kubectl-edit-1925296777.yaml) and then force-replace the Pod:
- Verify the Pod now consumes the ConfigMap:
dark blue as supplied by the webapp-config-map.
Quick reference: common ConfigMap-to-env methods
Additional examples and references
- Kubernetes docs on ConfigMaps: Configure a Pod to use a ConfigMap
valueFrom.configMapKeyRef:
envFrom to import all keys:
Summary
- We inspected a Pod’s environment variables and identified
APP_COLOR. - We attempted to change the Pod env var (immutable fields prevent direct updates), used
kubectl editto create a local manifest, then replaced the Pod withkubectl replace --force. - We created a ConfigMap (
webapp-config-map) and updated the Pod to import that ConfigMap usingenvFrom. - Finally, we verified the application picked up the new color provided by the ConfigMap.