Sub topic 14
Rolling out Pods after a configuration change
This chapter assumes you know Deployments, the ConfigMap and Secret consumption methods from earlier chapters, and basic kubectl rollout commands.
Why editing a ConfigMap does not roll Pods
A Deployment starts a rollout only when its Pod template changes. A ConfigMap or Secret lives outside that template. If you edit the object, the Deployment sees no change, and its Pods keep running as they were:
- Environment variables are fixed when the container starts. They never update.
- Mounted volumes update after a delay, but most applications do not re-read their files.
- Volumes mounted with
subPathnever update.
So you need a deliberate way to replace the Pods. There are three common approaches.
Approach 1: restart the Deployment
The quickest option is a forced rollout:
kubectl rollout restart deployment/app
kubectl rollout status deployment/appThis adds a kubectl.kubernetes.io/restartedAt annotation to the Pod template. The changed template starts a normal rolling update, so new Pods read the current configuration.
The weakness is that nothing in your manifests records the change. The rollout history does not show which configuration each revision used, and rolling back does not restore the old values.
Approach 2: version the object name
Create a new ConfigMap for each change and point the Deployment at it. Combine this with immutable: true from the previous chapter.
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config-v2
data:
LOG_LEVEL: "debug"
immutable: true
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
spec:
replicas: 3
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "echo $LOG_LEVEL && sleep 3600"]
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config-v2
key: LOG_LEVELChanging the name from app-config-v1 to app-config-v2 changes the Pod template, so Kubernetes starts a rolling update. The old ConfigMap still exists, so kubectl rollout undo deployment/app restores the old Pods and the old configuration together.
Approach 3: hash the content into an annotation
Tools such as Helm compute a checksum of the rendered ConfigMap and place it in a Pod template annotation:
template:
metadata:
annotations:
checksum/config: "3f7a9c0d51e2"When the data changes, the checksum changes, and so does the template. The rollout then follows automatically. You keep one stable object name, but you need tooling to compute the hash.
Choosing an approach
| Approach | Automatic | Rollback restores config | Needs extra tooling |
|---|---|---|---|
| `rollout restart` | No | No | No |
| Versioned name | Yes, when you apply | Yes | No |
| Checksum annotation | Yes | Only if the old data still exists | Yes, such as Helm |
Common errors
- Editing a ConfigMap and waiting. Nothing happens, because the template did not change.
- Deleting the old versioned ConfigMap too early. A rollback or a Pod restart then fails with
CreateContainerConfigError. Keep the previous version until the new rollout is healthy. - Updating a Secret the same way as a ConfigMap. The same rules apply, so version Secret names too.
Exercise
You edit the data in a mutable ConfigMap app-config that a Deployment reads through env. No Pods restart. Name two ways to make the Pods use the new value.
Show answer
Run kubectl rollout restart deployment/app, which changes the Pod template annotation. Alternatively, publish the data as a new object such as app-config-v2 and update the Deployment to reference it. Adding a checksum annotation that changes with the data also works.