envFrom to import all keys from a ConfigMap.
1) Inspect the existing Pod and its environment variable
List pods in the current namespace:- The environment variable
APP_COLORis set topink. - Visiting the web application confirms the UI background is pink.
2) Update the Pod environment variable directly (immutable fields limitation)
Most Pod fields are immutable. Changing certain fields (including container environment values) via an in-place patch is not allowed by the API. The usual workflow is to fetch the Pod manifest, edit the desired fields locally, and then replace the Pod by deleting and recreating it from the edited manifest. Fetch and edit the Pod manifest with kubectl:spec.containers[*].env[*].value in-place, the API will reject the update with an error like:
/tmp/kubectl-edit-XXXX.yaml). In that file, change the APP_COLOR value to green. Then replace the Pod by forcing a delete and recreate:
green).
Pods are immutable for many fields. Using
kubectl replace --force -f <file> deletes and recreates the pod from the provided manifest—this is a common technique to apply changes that cannot be done in-place.3) Inspect ConfigMaps to find DB_HOST
List ConfigMaps in thedefault namespace:
db-config ConfigMap to inspect its data:
- The database host is
SQL01.example.com(case-insensitive for answers).
4) Create a ConfigMap to provide APP_COLOR for the Pod
Create a ConfigMap namedwebapp-config-map with the key APP_COLOR and value dark blue:
5) Update the Pod to consume environment variables from the ConfigMap
Rather than setting each environment variable individually, you can import all keys from a ConfigMap usingenvFrom with configMapRef. Update the container spec to include:
envFrom block under the container spec, save the temporary file created by kubectl (e.g. /tmp/kubectl-edit-YYYY.yaml), and then replace the Pod:
- The Pod now loads
APP_COLORfrom thewebapp-config-mapConfigMap and the UI should displaydark blue.
Quick reference — commands used in this lab
Summary
- You inspected the Pod and found
APP_COLOR=pink. - Direct in-place edits to change certain Pod fields are blocked by immutability rules; editing and then using
kubectl replace --force -f <file>is the recommended approach to apply such changes. - You retrieved
DB_HOSTfrom thedb-configConfigMap (SQL01.example.com). - You created a ConfigMap
webapp-config-mapwithAPP_COLOR="dark blue". - You updated the Pod to import environment variables from the ConfigMap using
envFrom: - configMapRef: name: webapp-config-map, then recreated the Pod so it picks updark blue.