Video in progress

A value in a ConfigMap changed. Does a running Spring Boot application see the new value?

If it was given as an env variable, no, only when the pod is recreated. A file mounted as a volume is updated within a few minutes, but Spring does not re-read it on its own, so you need a reload mechanism or a restart.

📖 Answer

Two ways, two different behaviors.

Through env (envFrom, valueFrom.configMapKeyRef): the value is read when the process starts. If you update the ConfigMap, the pod notices nothing.

Through a volume: the kubelet refreshes the file during its sync cycle (usually around a minute). One catch: a file mounted with subPath is not updated at all, and many people forget this.

Even if the file is updated, Spring's @Value and @ConfigurationProperties values are bound at startup. The options:

  • Spring Cloud Kubernetes' config reload feature, or Spring Cloud Config + /actuator/refresh and @RefreshScope beans;
  • the simplest and most predictable: a rolling restart.

In practice I prefer the restart. To automate it, you put a hash of the ConfigMap into an annotation on the pod template:

template:
  metadata:
    annotations:
      checksum/config: "{{ include (print $.Template.BasePath \"/configmap.yaml\") . | sha256sum }}"

Every time the hash changes with Helm, the pod template changes and the Deployment does a rolling update. As a bonus, a configuration change stays in the history like any ordinary deploy and can be reverted with rollout undo.

The risk of hot reload is that half the pods may end up running with the old value and half with the new one, and nobody notices.