Sub topic 13
Immutable ConfigMaps and Secrets
This chapter assumes you can write Pod and Deployment manifests, use kubectl, and already know how to consume a ConfigMap or Secret as environment variables or mounted files.
Why make configuration immutable
By default, you can edit a ConfigMap or Secret at any time. That flexibility has two costs.
- A bad edit reaches every Pod that mounts the object. Mounted files refresh within a minute or so, so a typo can spread to running Pods without any rollout.
- Each Pod that mounts the object makes the kubelet watch it for changes. In a large cluster with thousands of mounts, those watches add load on the API server.
Setting immutable: true addresses both. The API server rejects any change to data, binaryData, or stringData. The kubelet stops watching immutable objects, which reduces API server load.
Creating an immutable ConfigMap
Add the immutable field at the top level of the object, next to data.
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config-v1
data:
LOG_LEVEL: "info"
immutable: trueThe v1 suffix in the name is deliberate. Because the object cannot change, a new version of the configuration needs a new object name. Secrets work the same way: set immutable: true at the top level of the Secret.
What you can and cannot do afterwards
Once immutable is true, the rules are strict.
| Action | Allowed? |
|---|---|
| Change `data` or `binaryData` | No |
| Set `immutable` back to `false` | No |
| Delete the object | Yes |
| Create a new object with the same name after deleting | Yes |
The change is one-way. To edit the content, you must delete the object and create it again. Pods that already use the old object keep their current values for environment variables. For a mounted volume, the files stay as they were, but a restarted container will fail to start if the object is missing. For that reason, deleting and recreating under the same name is a poor way to roll out changes.
The versioned-name workflow
The safe pattern is to publish a new object and point the workload at it.
- Create
app-config-v2with the new values andimmutable: true. - Edit the Pod template in your Deployment so it references
app-config-v2. - Let the Deployment roll out new Pods, which start with the new values.
- Delete
app-config-v1once no Pods use it.
kubectl apply -f app-config-v2.yaml
kubectl set env deployment/web --from=configmap/app-config-v2
kubectl rollout status deployment/web
kubectl delete configmap app-config-v1Changing the Pod template is what triggers the rollout. This gives you a normal rolling update, and kubectl rollout undo returns to the previous template, which still names app-config-v1, as long as you have not deleted it yet.
Common errors
- Editing fails with "field is immutable". This is expected. Create a new versioned object instead.
- Trying to turn immutability off. The API server rejects it. Delete and recreate the object if you truly must.
- Deleting the old version too early. A rollback or a rescheduled Pod then fails to find the object and stays in
ContainerCreatingor showsCreateContainerConfigError.
Exercise
A Deployment uses the immutable ConfigMap app-config-v1. You need to change LOG_LEVEL from info to debug. What steps do you take, and why not simply edit the object?
Show answer
The API server rejects edits to an immutable ConfigMap's data. Create app-config-v2 with LOG_LEVEL set to debug and immutable: true, update the Deployment's Pod template to reference app-config-v2, and let the rolling update replace the Pods. Keep app-config-v1 until the rollout succeeds so you can roll back, then delete it.