ConfigMaps as Environment Variables
notes-web needs to know its log level, the page size for note lists and the database host. Baking those into the image means rebuilding the image for every environment. Kubernetes lets one image take different settings in development and production.
The rule
Settings live beside the image, not inside it — and env is read once, at start.
A ConfigMap is an object that holds plain, non-secret settings as key-value pairs:
apiVersion: v1
kind: ConfigMap
metadata:
name: notes-web-config
namespace: notes
data:
LOG_LEVEL: info
PAGE_SIZE: "20"
DB_HOST: notes-dbValues are always strings, so numbers are quoted.
A pod can load every key as an environment variable:
containers:
- name: web
image: ghcr.io/example/notes-web:1.2
envFrom:
- configMapRef:
name: notes-web-configOr pick one key and give it a name of your choice:
env:
- name: NOTES_LOG_LEVEL
valueFrom:
configMapKeyRef:
name: notes-web-config
key: LOG_LEVELYou can combine both. If the same variable name comes from envFrom and from env, the env entry wins, which is handy for overriding one value in one container.
The same image now runs with LOG_LEVEL: debug on a laptop and info in production — only the ConfigMap differs.
A quick way to create one from the command line:
kubectl create configmap notes-web-config --from-literal=LOG_LEVEL=info --from-literal=PAGE_SIZE=20 -n notesThe shortcut
Remember "env is a photograph". Environment variables are copied into the process when the container starts. Editing the ConfigMap later does not change a photo already taken.
Edit the ConfigMap
You change
LOG_LEVELfrominfotodebugand apply it.
A second trap: if the ConfigMap named in configMapRef does not exist, the pod will not start. It waits with a CreateContainerConfigError status until the ConfigMap appears.
Drills
Answer fast — trust the rule.
What kind of data belongs in a ConfigMap?
Which field loads every key of a ConfigMap as environment variables?
You change PAGE_SIZE in the ConfigMap. What do the running notes-web pods see?
Why is PAGE_SIZE: "20" written with quotes?
Remember
- Settings live beside the image in a ConfigMap, not inside it.
- Env is a photograph: restart pods to pick up changes.
- Trap: a missing ConfigMap stops the pod from starting.
