ConfigMaps: settings outside the image
One image, many environments.
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: 356e76534b669b6dd13c2e44e2241e63bf0ed82481b22ed827ad295d67274a09Say 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:
Hello from version 1
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:
apiVersion: apps/v1kind: Deploymentmetadata:name: appspec:replicas: 1selector:matchLabels:app: apptemplate:metadata:labels:app: appspec:containers:- name: appimage: busybox:1.38.0# stand-in for a real app: wait, and exit at once when Kubernetes asks it to stopcommand: ["sh", "-c", "trap 'exit 0' TERM; while true; do sleep 1; done"]env:- name: COLOR # one key, as an environment variablevalueFrom:configMapKeyRef:name: app-configkey: COLORvolumeMounts:- name: config # every key as a file in /etc/appmountPath: /etc/app- name: config # one key mounted over a single file pathmountPath: /etc/greeting.txtsubPath: greeting.txtvolumes:- name: configconfigMap:name: app-config
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:
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:
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:
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.
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:
apiVersion: v1kind: ConfigMapmetadata:name: flagsimmutable: truedata:NEW_CHECKOUT: "off"
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.
blue after the file had changed.red.subPath from a ConfigMap. You update the ConfigMap. What happens to the file?kubectl patch. What happens?Forbidden: field is immutable. Delete and recreate it, or use a new name.