0
0
Kubernetesdevops~10 mins

Using ConfigMaps as environment variables in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Using ConfigMaps as environment variables
Create ConfigMap with key-value pairs
Define Pod spec referencing ConfigMap
Pod starts, Kubernetes injects env vars
Container reads env vars from ConfigMap
Application uses env vars at runtime
This flow shows how a ConfigMap is created and then used to inject environment variables into a Kubernetes Pod, which the containerized app can access.
Execution Sample
Kubernetes
kubectl create configmap app-config --from-literal=APP_MODE=production

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: app
    image: busybox
    envFrom:
    - configMapRef:
        name: app-config
Creates a ConfigMap with APP_MODE=production and a Pod that uses this ConfigMap to set environment variables in its container.
Process Table
StepActionResource AffectedResultNotes
1Create ConfigMap 'app-config' with APP_MODE=productionConfigMapConfigMap 'app-config' createdConfigMap stores key-value pairs
2Define Pod spec referencing ConfigMap 'app-config'Pod specPod spec includes envFrom with configMapRefPod will use ConfigMap for env vars
3Apply Pod spec to clusterPodPod 'example-pod' created and scheduledPod starts running
4Kubernetes injects env vars from ConfigMap into containerContainer environmentContainer env var APP_MODE=production setEnv vars available inside container
5Container application reads APP_MODE env varApplication runtimeAPP_MODE=production accessibleApp can use config values dynamically
6Pod runs normally with env varsPod statusPod RunningExecution complete
💡 Pod is running with environment variables injected from ConfigMap
Status Tracker
VariableStartAfter Step 1After Step 4Final
ConfigMap 'app-config'Not createdCreated with APP_MODE=productionUnchangedExists with APP_MODE=production
Pod environment variable APP_MODENot setNot setSet to 'production'Set to 'production'
Key Moments - 3 Insights
Why doesn't the environment variable appear in the container before the Pod starts?
Environment variables from ConfigMaps are injected only when the Pod starts and the container is created, as shown in step 4 of the execution_table.
What happens if the ConfigMap is changed after the Pod is running?
The running Pod's environment variables do not update automatically; the Pod must be restarted to pick up ConfigMap changes, as environment variables are set at container start.
Can multiple ConfigMaps be used to set environment variables in one Pod?
Yes, multiple ConfigMaps can be referenced using multiple envFrom entries or individual env entries, but each must be defined explicitly in the Pod spec.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the environment variable APP_MODE set inside the container?
AStep 2
BStep 4
CStep 1
DStep 6
💡 Hint
Check the 'Result' column in step 4 where container env var APP_MODE=production is set.
According to variable_tracker, what is the value of APP_MODE after step 1?
Aproduction
Bundefined
CNot set
Dnull
💡 Hint
Step 1 creates the ConfigMap but environment variables are injected later, see variable_tracker row for APP_MODE.
If the Pod is restarted after changing the ConfigMap, what will happen to the environment variable APP_MODE?
AIt updates to the new ConfigMap value
BIt remains the old value
CIt becomes empty
DPod fails to start
💡 Hint
Refer to key_moments about ConfigMap changes requiring Pod restart to update env vars.
Concept Snapshot
Create a ConfigMap with key-value pairs
Reference it in Pod spec using envFrom with configMapRef
When Pod starts, env vars from ConfigMap are injected into container
App reads env vars at runtime
Changes to ConfigMap require Pod restart to take effect
Full Transcript
This visual execution shows how to use Kubernetes ConfigMaps as environment variables in Pods. First, a ConfigMap named 'app-config' is created with a key APP_MODE set to 'production'. Then, a Pod specification is defined that references this ConfigMap under the envFrom field. When the Pod is applied and starts running, Kubernetes injects the environment variables from the ConfigMap into the container. The containerized application can then read these environment variables at runtime. The environment variables are only set when the Pod starts, so changes to the ConfigMap after the Pod is running require restarting the Pod to update the environment variables. This method allows configuration to be managed separately from container images and easily updated.