DEV Community

Cover image for 🎯 Scenario #12 β€” Mount a ConfigMap as a Volume and Update It Dynamically in Kubernetes
Latchu@DevOps
Latchu@DevOps

Posted on

🎯 Scenario #12 β€” Mount a ConfigMap as a Volume and Update It Dynamically in Kubernetes

In this scenario you will:

βœ… Create a ConfigMap
βœ… Mount it as a volume inside a Pod
βœ… Update the ConfigMap
βœ… Watch the Pod automatically see the updated file (no restart needed!)
βœ… Test real-time behavior

This is one of the most important Kubernetes features for configuration-driven apps.


βœ… Step 1 β€” Create the ConfigMap

Create a file:

# config-volume-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  welcome.txt: |
    Welcome version 1
    This is the first config.
Enter fullscreen mode Exit fullscreen mode

Apply it:

kubectl apply -f config-volume-cm.yaml
Enter fullscreen mode Exit fullscreen mode

Verify:

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

1


βœ… Step 2 β€” Create a Pod That Mounts the ConfigMap as a Volume

Create file:

# config-volume-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: config-volume-demo
spec:
  containers:
  - name: nginx
    image: nginx:1.25
    volumeMounts:
    - name: config-volume
      mountPath: /usr/share/nginx/html/config    # Config file available here
  volumes:
  - name: config-volume
    configMap:
      name: app-config
Enter fullscreen mode Exit fullscreen mode

Apply:

kubectl apply -f config-volume-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Check pod:

kubectl get pod config-volume-demo
Enter fullscreen mode Exit fullscreen mode

Wait until Running.

2


βœ… Step 3 β€” Verify the Mounted Config File

Exec into the container:

kubectl exec -it config-volume-demo -- bash
Enter fullscreen mode Exit fullscreen mode

Then check:

cat /usr/share/nginx/html/config/welcome.txt
Enter fullscreen mode Exit fullscreen mode

You will see:

Welcome version 1
This is the first config.
Enter fullscreen mode Exit fullscreen mode

βœ… Perfect.

Exit:

exit
Enter fullscreen mode Exit fullscreen mode

3


βœ… Step 4 β€” Update the ConfigMap (without restarting Pod)

Edit: config-volume-cm.yaml

# update configmap
data:
  welcome.txt: |
    Welcome version 2
    This config was updated dynamically!
Enter fullscreen mode Exit fullscreen mode

Apply:

kubectl apply -f config-volume-cm.yaml
Enter fullscreen mode Exit fullscreen mode

βœ… Step 5 β€” Verify Update in the Running Pod (AUTO-UPDATE)

Exec again:

kubectl exec -it config-volume-demo -- bash
Enter fullscreen mode Exit fullscreen mode

Then:

cat /usr/share/nginx/html/config/welcome.txt
Enter fullscreen mode Exit fullscreen mode

EXPECTED OUTPUT:

Welcome version 2
This config was updated dynamically!
Enter fullscreen mode Exit fullscreen mode

βœ… ConfigMap volume updates automatically (no restart required).

4


βœ… Important Note

βœ… Volume-mounted ConfigMaps auto-update every ~1–2 seconds, thanks to Kubernetes file projections.
❌ But config from environment variables does NOT auto-update β†’ requires pod restart.

This is why many apps prefer mounting ConfigMaps as volumes.


βœ… Step 6 β€” Clean Up

kubectl delete pod config-volume-demo
kubectl delete configmap app-config
Enter fullscreen mode Exit fullscreen mode

🌟 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)