Kubernetes: Restart pods when config map values change

9/8/2021

I have a pod with the following specs

kind: Pod
metadata:
  name: busybox
  namespace: default
spec:
  containers:
  - image: busybox
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
    name: busybox
    env:
      - name: WATCH_NAMESPACE
        valueFrom:
          configMapKeyRef:
            name: watch-namespace-config
            key: WATCH_NAMESPACE
  restartPolicy: Always

I also created a ConfigMap

kubectl create configmap watch-namespace-config \
        --from-literal=WATCH_NAMESPACE=dev

The pod looks for values in the watch-namespace-config configmap.
When I manually change the configmap values, I want the pod to restart automatically to reflect this change. Checking if that is possible in any way.

-- CSUNNY
configmap
kubernetes

2 Answers

12/26/2021

This is currently a feature in progress https://github.com/kubernetes/kubernetes/issues/22368

For now, use Reloader - https://github.com/stakater/Reloader

It watches if some change happens in ConfigMap and/or Secret; then performs a rolling upgrade on relevant DeploymentConfig, Deployment, Daemonset, Statefulset and Rollout

How to use it - https://github.com/stakater/Reloader#how-to-use-reloader

-- sridhar
Source: StackOverflow

9/8/2021

As you mentioned correctly once you update a ConfigMap or Secret the Deployment/Pod/Stateful set is not updated.

An optional solution for this scenario is to use Kustomization. Kustomization generates a unique name every time you update the ConfigMap/Secret with a generated hash, for example: ConfigMap-xxxxxx.

If you will will use:

kubectl kustomize . | kubectl apply -f - 

kubectl will "update" the changes with the new config map values.

Working Example(s) using Kustomization:

https://github.com/nirgeier/KubernetesLabs/tree/master/Labs/08-Kustomization

-- CodeWizard
Source: StackOverflow