DEV Community

Cover image for ⭐ Scenario #5: Configure Environment Variables from ConfigMap in a Pod
Latchu@DevOps
Latchu@DevOps

Posted on

⭐ Scenario #5: Configure Environment Variables from ConfigMap in a Pod

This is one of the most important Kubernetes basics, used in real microservices deployments.

You will learn:

✔ Create a ConfigMap
✔ Inject values into a Pod as environment variables
✔ Verify inside the container
✔ Update ConfigMap and see Pod behavior


🟩 Step 1 — Create a ConfigMap

Create app-config.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: demo-config
data:
  APP_ENV: "development"
  APP_VERSION: "1.0.0"
  WELCOME_MESSAGE: "Hello from ConfigMap!"
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f app-config.yaml
Enter fullscreen mode Exit fullscreen mode

Check it:

kubectl get configmaps demo-config -o yaml
Enter fullscreen mode Exit fullscreen mode

1


🟩 Step 2 — Create a Pod That Uses the ConfigMap

Create pod-configmap-demo.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: configmap-demo-pod
spec:
  containers:
    - name: demo-container
      image: busybox
      command: ["sh", "-c", "sleep 3600"]
      env:
        - name: ENVIRONMENT
          valueFrom:
            configMapKeyRef:
              name: demo-config
              key: APP_ENV

        - name: VERSION
          valueFrom:
            configMapKeyRef:
              name: demo-config
              key: APP_VERSION

        - name: MESSAGE
          valueFrom:
            configMapKeyRef:
              name: demo-config
              key: WELCOME_MESSAGE
Enter fullscreen mode Exit fullscreen mode

Apply:

kubectl apply -f pod-configmap-demo.yaml
Enter fullscreen mode Exit fullscreen mode

Wait until ready:

kubectl wait --for=condition=Ready pod/configmap-demo-pod --timeout=60s
Enter fullscreen mode Exit fullscreen mode

1


🟩 Step 3 — Verify Environment Variables Inside the Pod

Exec inside the Pod:

kubectl exec -it configmap-demo-pod -- sh
Enter fullscreen mode Exit fullscreen mode

Inside container:

echo $ENVIRONMENT
echo $VERSION
echo $MESSAGE
Enter fullscreen mode Exit fullscreen mode

Expected output:

development
1.0.0
Hello from ConfigMap!
Enter fullscreen mode Exit fullscreen mode

Exit:

exit
Enter fullscreen mode Exit fullscreen mode

2


🟩 Step 4 — Update the ConfigMap (Real-Time Demo)

Edit the ConfigMap:

kubectl edit configmap demo-config
Enter fullscreen mode Exit fullscreen mode

Change:

APP_VERSION: "2.0.0"
Enter fullscreen mode Exit fullscreen mode

Save and exit.

Check latest value:

kubectl get configmap demo-config -o yaml
Enter fullscreen mode Exit fullscreen mode

3


🛑 Important Note:

Pods do NOT automatically pick up ConfigMap changes.
You must restart the Pod.

Delete:

kubectl delete pod configmap-demo-pod
Enter fullscreen mode Exit fullscreen mode

Recreate:

kubectl apply -f pod-configmap-demo.yaml
Enter fullscreen mode Exit fullscreen mode

Exec again:

kubectl exec -it configmap-demo-pod -- sh
echo $VERSION
Enter fullscreen mode Exit fullscreen mode

Now output:

2.0.0
Enter fullscreen mode Exit fullscreen mode

4


🌟 Thanks for reading! If this post added value, a like ❤️, follow, or share would encourage me to keep creating more content.


— Latchu | Senior DevOps & Cloud Engineer

☁️ AWS | GCP | ☸️ Kubernetes | 🔐 Security | ⚡ Automation
📌 Sharing hands-on guides, best practices & real-world cloud solutions

Top comments (0)