Kubernetes cookbook · Intermediate
Restart pods automatically when configuration changes
Edited ConfigMaps don't reach running pods; make configuration changes roll out like code.
Approach
With Kustomize, generate the ConfigMap so its name includes a content hash; references are rewritten and the Deployment rolls out on every change. With Helm, put a checksum of the ConfigMap in a pod annotation.
Solution
# kustomization.yaml
resources: [deployment.yaml]
configMapGenerator:
- name: api-config
literals:
- LOG_LEVEL=info
- FEATURE_SEARCH=on
# Helm: templates/deployment.yaml (pod template)
template:
metadata:
annotations:
checksum/config: '{{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}'
Pitfalls
kubectl edit configmapin production: running pods keep old environment values.- Disabling the generator's hash suffix (
disableNameSuffixHash), which removes the rollout trigger.
Variations
kubectl rollout restart deployment/apias a manual trigger.- Applications that watch mounted config files and reload them (with validation).
Further reading: configMapGenerator
Reference: https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/#configmapgenerator
More Kubernetes recipes
- Autoscale a Deployment on CPU and memory
- Debug a distroless pod with an ephemeral container
- Default-deny network policies that keep DNS working
- Enforce the restricted Pod Security Standard
- Expose a service over HTTPS with automatic certificates
- Give a CI pipeline least-privilege deploy access
- Handle SIGTERM in your application for Kubernetes
- Manage dev, staging and prod with Kustomize overlays