Externalizing Configuration
Externalizing Configuration
A ConfigMap stores non-secret configuration as key-value data, keeping settings out of images so the same image promotes from staging to production unchanged:
kubectl create configmap app-config \
--from-literal=LOG_LEVEL=info \
--from-literal=FEATURE_X=true \
--from-file=nginx.conf
Values are limited to about 1 MiB per object (the etcd request cap) and keys must match [-._a-zA-Z0-9]+. ConfigMaps are namespace-scoped, so each environment gets its own copy rather than branching on conditionals inside the app.
They are not containers' filesystem by default — you choose how pods consume them: environment variables, volume mounts, or the Downward API. That choice determines how (or whether) changes reach running processes.
Consuming a ConfigMap
Consuming a ConfigMap
spec:
containers:
- name: app
image: nginx:1.27
envFrom:
- configMapRef: { name: app-config } # every key as env var
env:
- name: LOG_LEVEL
valueFrom: { configMapKeyRef: { name: app-config, key: LOG_LEVEL } }
volumeMounts:
- { name: cfg, mountPath: /etc/nginx/nginx.conf, subPath: nginx.conf }
volumes:
- name: cfg
configMap: { name: app-config }
Mounted files update in place within seconds (kubelet syncs), but apps must re-read or watch them; environment variables are injected only at container start, so a ConfigMap change never reaches an already-running process.
subPath mounts a single key and — important detail — does not receive in-place updates.
Pitfalls and Interview Angles
Pitfalls and Interview Angles
Pitfall 1 — expecting env vars to refresh. They are snapshot at start; changing the ConfigMap requires a restart, typically by annotating the pod template with the ConfigMap's resourceVersion to trigger a new rollout.
Pitfall 2 — secrets in ConfigMaps. Anything sensitive (tokens, passwords) belongs in a Secret — ConfigMaps are stored and displayed as plain text in etcd and in kubectl get -o yaml.
Interview-ready: contrast the three consumption methods with the restart implication, explain the 1 MiB limit and immutable ConfigMaps (immutable: true for stable files, with the tradeoff that changes need a new object and pod restart), and describe your update strategy: bump the template annotation to force recreation.
Quiz
1. Do environment variables from a ConfigMap refresh in a running container?
2. What is the approximate size limit of a single ConfigMap?
3. Why should a password never live in a ConfigMap?
Flashcards
Question
ConfigMap creation sources
Click to reveal answer
Answer
kubectl create configmap --from-literal, --from-file, or a YAML manifest.
Question
env vs mounted file
Click to reveal answer
Answer
env = snapshot at start, needs restart; file mount = kubelet syncs updates, app must re-read (subPath does not update).
Question
ConfigMap size cap
Click to reveal answer
Answer
About 1 MiB per object — the etcd per-request limit.
Question
Triggering a restart on config change
Click to reveal answer
Answer
Annotate the pod template with the ConfigMap resourceVersion so the Deployment rolls.
Revision Notes
Key Takeaways
- 1. ConfigMaps decouple configuration from image content
- 2. Consumption method decides whether changes ever reach running pods
- 3. Secrets belong in Secrets, never in ConfigMaps
Interview Tips
- • Answer "how do you update config" with the annotation-rollout trick, not "it auto-updates"
- • Distinguish env snapshot behavior from volume-mount sync behavior clearly
Cheat Sheet
ConfigMap = non-secret config ≤1MiB. env = start-time only; files sync (not subPath). Restart via template annotation bump. Secrets never here.
Continue Learning
Before You Start
- No prerequisites listed.