0

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.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
CSUNNY
  • 414
  • 1
  • 7
  • 23
  • 1
    Have you searched? Like: https://stackoverflow.com/search?q=restart+pod+configmap+change – SYN Sep 08 '21 at 17:27

4 Answers4

3

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
  • 223
  • 2
  • 8
1

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
  • 128,036
  • 21
  • 144
  • 167
  • Thanks. I wanted the deployment to automatically restart the pods. Guess that won't happen. You either have to do what you mentioned or do a `rollout restart` – CSUNNY Sep 09 '21 at 13:28
0

Seems Kyverno also has some solution for it: https://kyverno.io/policies/other/res/restart-deployment-on-secret-change/restart-deployment-on-secret-change/

It is basically a policy to restart whenever some identified resource changed. In the linked example it is a secret, but a configmap should work likewise.

Requires to install Kyverno though.

gnod
  • 407
  • 3
  • 9
-1

kubectl rollout restart deployment deployment-name

for automatic restart : configmap change doesn't reflect automatically on respective pods