Sub topic 6
What happens when a ConfigMap changes
This chapter assumes you can write Pod manifests, use kubectl, and have created a ConfigMap and consumed it as an environment variable and as a mounted volume. The question here is which running Pods see an edit and which do not.
The short answer
Kubernetes treats the two consumption methods differently:
| Consumption method | Sees updates in a running Pod? |
|---|---|
| Environment variable (`configMapKeyRef`, `envFrom`) | No. The value is fixed when the container starts. |
| Mounted volume (`configMap` volume) | Yes, after a delay. |
| Volume with `subPath` | No. The file is never refreshed. |
An environment variable is copied into the container's process environment at start. A restart of the container reads the current value, but the live process never sees an edit.
How volume updates work
The kubelet is the agent on each node that runs Pods. It periodically syncs mounted ConfigMaps. When the ConfigMap changes, the kubelet writes the new content and swaps a symbolic link, so the files change as one step. A reader never sees half-written content.
The delay can be a minute or more. It depends on the kubelet sync period and on how the kubelet caches ConfigMaps. Do not expect an instant change.
Try it
Use the ConfigMap app-config and the Pod app from the earlier chapters. This Pod prints the file once, then sleeps. To watch it change, run a loop instead:
kubectl exec app -- sh -c 'cat /etc/app/app.properties'
kubectl patch configmap app-config --type merge \
-p '{"data":{"app.properties":"cache.ttl=120\ncache.size=100\n"}}'
# Wait one to two minutes, then read the file again
kubectl exec app -- sh -c 'cat /etc/app/app.properties'
# The environment variable is unchanged
kubectl exec app -- sh -c 'echo $LOG_LEVEL'The file shows cache.ttl=120 after the delay. The LOG_LEVEL variable keeps its old value even if you also change that key.
Common errors
- Expecting an environment variable to change. Restart the Pod, or trigger a rollout, to pick up the new value.
- Using
subPathand expecting updates. AsubPathmount copies one file into place and never refreshes it. Mount the whole directory if you need updates. - Assuming the application reloads the file. The file changes on disk, but many programs read their config once at startup. The application must watch the file or reload on a signal.
- Checking too soon. The delay is normal. Wait before concluding the update failed.
What to do in practice
If your application cannot reload files, restarting Pods is the reliable way to apply new configuration. If it can reload, a mounted volume avoids restarts, but you must verify the program actually does it. Because behavior differs by method, many teams choose one approach per application and test it.
Exercise
A Pod reads LOG_LEVEL from a ConfigMap through an environment variable and mounts app.properties from the same ConfigMap as a directory. You edit both keys. Ten minutes later, which values does the running container see, and what would change the answer for the file?
Show answer
The environment variable still has the old value, because it is set only at container start. The mounted file has the new content, because the kubelet refreshes volumes. The file would not update if it were mounted with subPath or if the ConfigMap were immutable. Even with a refreshed file, the application sees the new settings only if it rereads the file.