Kubernetes cookbook · Intermediate

Restart pods automatically when configuration changes

Edited ConfigMaps don't reach running pods; make configuration changes roll out like code.

Create a free account to save this page, take notes and track your progress. Sign in

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 configmap in production: running pods keep old environment values.
  • Disabling the generator's hash suffix (disableNameSuffixHash), which removes the rollout trigger.

Variations

  • kubectl rollout restart deployment/api as 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

Was this page helpful?