ConfigMaps: settings outside the image

One image, many environments.

Beginner20 min · lesson 11 of 21
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (4 files, 1 KB): configmaps.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/configmaps.tar.gz && tar -xzf configmaps.tar.gz, which creates ~/lab/configmaps/. SHA-256: 356e76534b669b6dd13c2e44e2241e63bf0ed82481b22ed827ad295d67274a09

Say your app reads one setting, the colour of its banner, and you want blue in testing and green in production. If that value is baked into the image, you need two images, and changing the colour means a new build and a new rollout. Settings like that belong outside the image.

A ConfigMap is a small Kubernetes object that holds keys and values. A Pod can read them as environment variables or as files, so one image runs everywhere and only the ConfigMap differs. What needs care is what a running Pod sees when you edit the ConfigMap, because variables and files behave differently.

Create a ConfigMap

One key comes from the command line and one from a file. With --from-file, the file name becomes the key and the file contents become the value. The file greeting.txt holds one line:

greeting.txt
Hello from version 1
ubuntu@secopslog-k8s:~/lab/configmaps · Kubernetes 1.37
$ kubectl create configmap app-config --from-literal=COLOR=blue --from-file=greeting.txt
configmap/app-config created
$ kubectl describe configmap app-config
Name: app-config Namespace: default Labels: <none> Annotations: <none> Data ==== COLOR: ---- blue greeting.txt: ---- Hello from version 1 BinaryData ==== Events: <none>

Under Data there are two keys: COLOR with the value blue, and greeting.txt with the file's single line. ConfigMaps are for plain text settings. They are not meant for passwords, which get their own object in lesson 12, Secrets.

Read it as a variable and as files

This Deployment reads the ConfigMap three ways. COLOR becomes an environment variable. The whole ConfigMap is mounted as a directory, one file per key. And greeting.txt is also mounted on its own at a single path with subPath, which is how people usually drop one config file into a directory that already has other files in it:

app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
spec:
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
image: busybox:1.38.0
# stand-in for a real app: wait, and exit at once when Kubernetes asks it to stop
command: ["sh", "-c", "trap 'exit 0' TERM; while true; do sleep 1; done"]
env:
- name: COLOR # one key, as an environment variable
valueFrom:
configMapKeyRef:
name: app-config
key: COLOR
volumeMounts:
- name: config # every key as a file in /etc/app
mountPath: /etc/app
- name: config # one key mounted over a single file path
mountPath: /etc/greeting.txt
subPath: greeting.txt
volumes:
- name: config
configMap:
name: app-config
ubuntu@secopslog-k8s:~/lab/configmaps · Kubernetes 1.37
$ kubectl apply -f app.yaml && kubectl rollout status deployment/app
deployment.apps/app created Waiting for deployment "app" rollout to finish: 0 of 1 updated replicas are available... deployment "app" successfully rolled out
$ kubectl exec deploy/app -- sh -c 'echo "COLOR=$COLOR"; cat /etc/app/greeting.txt /etc/greeting.txt; ls /etc/app'
COLOR=blue Hello from version 1 Hello from version 1 COLOR greeting.txt

All three agree: the variable says blue, both greeting files say version 1, and /etc/app holds one file per key.

Change it while the Pod runs

kubectl patch changes a few keys of an existing object in place. Here COLOR becomes green and the greeting gets a new line. Nothing tells the Pod:

ubuntu@secopslog-k8s:~/lab/configmaps · Kubernetes 1.37
$ kubectl patch configmap app-config --type merge -p '{"data":{"COLOR":"green","greeting.txt":"Hello from version 2\n"}}'
configmap/app-config patched

Now run kubectl exec deploy/app -- cat /etc/app/greeting.txt every few seconds until it prints version 2. It does not change at once; here it took about a minute and a half. The kubelet on each node refreshes mounted ConfigMaps on its own schedule. Its periodic sync runs about once a minute, and it may also be reading from a cache, so expect a delay of a minute or two. Then look at all three again:

ubuntu@secopslog-k8s:~/lab/configmaps · Kubernetes 1.37
$ kubectl exec deploy/app -- sh -c 'echo "COLOR=$COLOR"; cat /etc/app/greeting.txt /etc/greeting.txt; cat /etc/app/COLOR; echo'
COLOR=blue Hello from version 2 Hello from version 1 green

Three different answers from one ConfigMap. The mounted directory has the new values: version 2, and /etc/app/COLOR says green. The environment variable still says blue, because variables are set once when the container starts and never change after that. The subPath file still says version 1. A subPath mount is fixed to the file as it was when the container started, and it is never updated, even though it looks like any other mounted file.

The mounted directory can update safely because of how the kubelet builds it:

ubuntu@secopslog-k8s:~/lab/configmaps · Kubernetes 1.37
$ kubectl exec deploy/app -- ls -la /etc/app
total 12 drwxrwxrwx 3 root root 4096 Nov 11 08:28 . drwxr-xr-x 1 root root 4096 Nov 11 08:27 .. drwxr-xr-x 2 root root 4096 Nov 11 08:28 ..2026_11_11_08_28_41.1634933233 lrwxrwxrwx 1 root root 32 Nov 11 08:28 ..data -> ..2026_11_11_08_28_41.1634933233 lrwxrwxrwx 1 root root 12 Nov 11 08:27 COLOR -> ..data/COLOR lrwxrwxrwx 1 root root 19 Nov 11 08:27 greeting.txt -> ..data/greeting.txt

The real files sit in a hidden folder named after a timestamp. ..data is a link to that folder, and COLOR and greeting.txt are links through ..data. On an update the kubelet writes a new timestamped folder and then swaps the ..data link in one step, so an app never reads half an old file and half a new one. An app that watches its config file has to follow the link to see this.

Restart to pick up everything

To make variables and subPath files pick up the change, the containers have to start again. kubectl rollout restart does that the same way a new version would roll out: new Pods start, then the old ones stop.

ubuntu@secopslog-k8s:~/lab/configmaps · Kubernetes 1.37
$ kubectl rollout restart deployment/app && kubectl rollout status deployment/app
deployment.apps/app restarted Waiting for deployment "app" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "app" rollout to finish: 1 old replicas are pending termination... deployment "app" successfully rolled out
$ kubectl get pods -l app=app && kubectl exec "$(kubectl get pods -l app=app -o name --sort-by=.metadata.creationTimestamp | tail -1)" -- sh -c 'echo "COLOR=$COLOR"; cat /etc/app/greeting.txt /etc/greeting.txt'
NAME READY STATUS RESTARTS AGE app-655dbd677b-6vw28 1/1 Running 0 1s app-84797bb5d4-vm95q 1/1 Terminating 0 99s COLOR=green Hello from version 2 Hello from version 2

The listing caught the old Pod while it was still Terminating, next to the new one, a second old. That is why the exec names the newest Pod instead of deploy/app: while an old Pod is still shutting down, kubectl exec deploy/app may pick it and show you the old values. --sort-by=.metadata.creationTimestamp sorts the Pods by age and tail -1 keeps the newest. The new Pod reads green and version 2 everywhere. Many teams skip the waiting entirely and restart on every config change, so all three ways always agree.

Immutable ConfigMaps

A ConfigMap can be marked immutable. Then nobody can change its data, by accident or otherwise, and the kubelet stops watching it for changes, which takes load off the API server in large clusters:

flags.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: flags
immutable: true
data:
NEW_CHECKOUT: "off"
ubuntu@secopslog-k8s:~/lab/configmaps · Kubernetes 1.37
$ kubectl apply -f flags.yaml
configmap/flags created
$ kubectl patch configmap flags --type merge -p '{"data":{"NEW_CHECKOUT":"on"}}'
The ConfigMap "flags" is invalid: data: Forbidden: field is immutable when `immutable` is set
$ kubectl delete configmap flags && kubectl create configmap flags --from-literal=NEW_CHECKOUT=on && kubectl get configmap flags -o jsonpath='{.data}{"\n"}'
configmap "flags" deleted from default namespace configmap/flags created {"NEW_CHECKOUT":"on"}

The patch was refused: Forbidden: field is immutable. The only way to change it is to delete it and create it again, as the last command did. Pods that already mounted the old one keep the old data until they restart, so the usual practice is to create a new ConfigMap with a new name, such as flags-v2, and point the Deployment at it.

Hands-on

1. Change the colour to red: kubectl patch configmap app-config --type merge -p '{"data":{"COLOR":"red"}}'.

2. Print the variable straight away: kubectl exec deploy/app -- sh -c 'echo "env: $COLOR"'.

3. Restart the Deployment with kubectl rollout restart deployment/app and wait with kubectl rollout status deployment/app.

4. Print the variable in the newest Pod: kubectl exec "$(kubectl get pods -l app=app -o name --sort-by=.metadata.creationTimestamp | tail -1)" -- sh -c 'echo "env: $COLOR"'.

5. Clean up with kubectl delete deployment app and kubectl delete configmap app-config flags, then list what is left with kubectl get configmaps.

What you should see: step 2 still prints env: green, since the running container has not restarted. Step 3 ends with successfully rolled out, and step 4 prints env: red. After step 5 one ConfigMap is left, kube-root-ca.crt. Kubernetes puts that one into every namespace so Pods can check the API server's certificate. Leave it alone.

Remember

Mounted ConfigMap files follow a change after a minute or so. Environment variables and subPath files keep the old value until the container restarts, so when in doubt, run kubectl rollout restart.

Quick check
01You change a key in a ConfigMap that a running Pod uses as an environment variable. What does the app see?
Incorrect — That is how mounted files behave. The variable still said blue after the file had changed.
Incorrect — Nothing in the Pod changes at once. Even the mounted file took over a minute.
Incorrect — A running process keeps the variables it started with. Nothing clears them.
Correct — Variables are set once, when the container starts. In the lab only the restarted Pod saw red.
02A config file is mounted with subPath from a ConfigMap. You update the ConfigMap. What happens to the file?
Incorrect — That is a normal ConfigMap volume. The subPath file stayed on version 1.
Correct — A subPath mount is fixed to the file as it was at container start.
Incorrect — Kubernetes never restarts Pods because a ConfigMap changed.
Incorrect — Read-only or not, a subPath mount is never refreshed.
03You try to change an immutable ConfigMap with kubectl patch. What happens?
Incorrect — No data changed. The API server refused the request.
Incorrect — There is no queue. The request failed straight away.
Correct — The error said Forbidden: field is immutable. Delete and recreate it, or use a new name.
Incorrect — Immutable applies to everyone, cluster admins included.

Related