Keep config values outside the image
General settings such as the log level go in a ConfigMap; passwords and tokens go in a Secret. Both store key-value pairs.
With values outside the image, changing one address does not require a rebuild, and the same image runs in dev and prod.
Key ideaAll values are stored as strings, and a single ConfigMap holds at most 1 MiB. It is not meant for large files.
A Secret is base64-encoded, not encrypted
Secret values are written in base64, a way of re-spelling text. Anyone can decode the original value with a single command.
What makes a Secret safer than a ConfigMap is that access can be restricted separately with permissions. Encryption at rest has to be turned on separately.
Watch outAnyone who can read a Secret sees its value. Grant get and list only to the subjects that truly need it, and never commit the YAML to Git as-is.
When the Pod starts, the kubelet fetches the values
The Pod spec only holds a reference: "use values from this ConfigMap or Secret". It does not copy the values.
Once the Pod is assigned to a node, that node’s kubelet fetches the referenced values through the API server.
Option 1 — inject as environment variables
When starting the container, the kubelet sets the values as environment variables. LOG_LEVEL comes from the ConfigMap, DB_PASSWORD from the Secret.
The app reads its environment once, at startup. This suits a few short values.
Option 2 — mount as files
Attach the ConfigMap as a volume and each key becomes a file under /etc/config.
The app reads the file when it needs to. This suits whole config files or certificates.
Changing the ConfigMap leaves env vars unchanged
The user changes LOG_LEVEL from info to debug. The stored value changes immediately.
But environment variables are injected only once, when the container starts. Running Pods keep using info.
Watch out"I changed the ConfigMap but the app did not" is the most common confusion in operations. Values injected as env vars only change when the Pod restarts.
What changed in this step
File values change after a short delay
The kubelet notices the mounted ConfigMap changed and, after a short delay, swaps the file content for the new value. No Pod restart is needed.
But the app must re-read the file for the change to apply. If it only reads at startup, the result is the same as env vars.
Restarting the Pod picks up the new env value
Restarting the Deployment creates new Pods, and the kubelet injects env vars from the ConfigMap as it is at that moment.
Restart with kubectl rollout restart deployment/NAME. Check the actual values by printing env with kubectl exec.
What changed in this step
- ~Pod restarted
- ~env var LOG_LEVEL=debug
Storing a value is not the same as the app using it
ConfigMaps and Secrets only store values. When a value reaches the app depends on how it is injected.
Env vars need a Pod restart; files change after a delay but need the app to re-read them. Secrets are protected by permissions and masking.
SummaryChanging a ConfigMap or Secret does not make the app use the new value right away. If it was injected as an env var, a restart is required.