Module 5 ยท Lesson 5.1
ConfigMaps
Config doesn't belong inside the image
Baking config into a container image means rebuilding the image every time a setting changes โ the same mistake as hardcoding a database hostname into a compiled binary. A ConfigMap is a plain key-value object you create separately from your Pods, so the same image can run in dev, staging, and prod with different config, unchanged.
Three ways to consume one
- A single env var, via
valueFrom.configMapKeyRefโ pick one key out of the ConfigMap and expose it as one environment variable. Good for a handful of simple values. envFromโ dump every key in the ConfigMap into env vars at once, no per-key wiring. Convenient, but less explicit about what a Pod actually depends on.- A mounted volume โ the whole ConfigMap appears as a directory, one
file per key, its contents as the file's content. This is the only
option that fits large config (an actual
nginx.conf, a JSON blob) or files an app expects to literally read from disk.
The option that matters most in practice: env vars are frozen at pod start. Change the ConfigMap, and existing pods keep their old env vars until they're recreated. A mounted volume, by contrast, does get updated in the running pod (kubelet syncs it periodically) โ but most apps don't watch their config files for changes, so you still usually need to restart the pod to actually pick it up. Either way: updating a ConfigMap is not the same as updating what your app is doing right now.
Linux aside: environment variables and files
If you've ever set export FOO=bar in a shell, that's exactly what
valueFrom/envFrom are doing to the container's single process before it
starts โ same mechanism, just set by the kubelet instead of your terminal.
A volume mount, meanwhile, is Kubernetes doing the same thing your OS does
when you mount a USB drive at /mnt/usb: an existing directory path
suddenly has new file contents behind it, fully transparent to whatever
reads from that path.
The infrastructure analogy
Think of a ConfigMap as the externalized .env file or /etc/app/config
you'd manage with Ansible/Puppet in a traditional deploy โ except here it's
a first-class, versioned API object the Pod spec references directly,
instead of a file pushed out-of-band and hoped to be in sync.
In the lab, you'll diagnose a Pod whose environment doesn't match what its ConfigMap actually contains โ the same "which part has the stale value" debugging you've likely done with config-management drift before.
๐งช Lab: lab-09-configmaps
Preview onlyGoal
Wire a ConfigMap into a Deployment, then diagnose a container that never starts because of a key name mismatch.
Tasks
- Apply the starting manifests:
kubectl apply -f manifests/ - Check the pod:
kubectl get pods -n lab-09-configmaps. It's notRunningโ note the status. kubectl describe pod -n lab-09-configmaps -l app=appโ read the Events at the bottom. It names the exact problem.- Compare against the ConfigMap's real keys:
kubectl get configmap app-config -n lab-09-configmaps -o yaml - Fix it: edit
manifests/deployment.yamlso theconfigMapKeyRef.keyforDATABASE_HOSTmatches a key that actually exists in the ConfigMap, thenkubectl apply -f manifests/deployment.yamlagain. (Editing a Deployment's env and reapplying is fine โ unlike a bare Pod, a Deployment's container spec is meant to be changed; it just rolls a new pod out.) - Confirm:
kubectl get pods -n lab-09-configmapsshowsRunning, andkubectl exec -n lab-09-configmaps deploy/app -- printenv DATABASE_HOSTprints the ConfigMap's value.
Check
Run the check once the pod is Running and DATABASE_HOST resolves
correctly inside the container.
This lab runs against a real local Kubernetes cluster with an automated grader โ clone the repo and run make start to do it for real.
๐ Quiz
1. Why use a ConfigMap instead of baking config into the container image?
2. Which ConfigMap consumption method is the only one that fits a large config file like a full nginx.conf?
3. You update a ConfigMap that's consumed via a normal env var (configMapKeyRef). An already-running Pod using it does NOT see the new value. Why?scenario
4. What's the practical difference between envFrom and a single configMapKeyRef env var?
5. A Pod's env references `configMapKeyRef` with key DATABASE_HOST, but the ConfigMap's actual key is DB_HOST. What happens when the Pod starts?scenario
Progress isn't saved in this preview โ run the course locally to track completion and grade labs for real.