A ConfigMap keeps settings outside the program
A ConfigMap is a Kubernetes object that holds settings as named values, kept outside the program. It matters because you can change a currency, a mode or a greeting without building the program again.
One trap catches almost everyone: a program that is already running does not notice the change.
By the end of this lesson
Note
I can change a setting without rebuilding the program.
Key words
| Word | What it means | Example |
|---|---|---|
| ConfigMap | a set of named settings kept in the cluster | |
| Key | the name of one setting | CURRENCY |
| Environment variable | a named value a program reads when it starts | |
| Image | the packed program a pod starts from | |
| Replace a pod | delete it so a new one starts |
The cells students fill in are left blank in this preview.
Settings live in a ConfigMap
A pod starts from an image, the packed program. Putting a setting inside the image means building a new image for every change.
A ConfigMap holds the settings instead. Each setting has a key and a value. The Deployment hands them to the program as environment variables, which the program reads once, when it starts.
Worked example: who sees which value
| Moment | ConfigMap says | Pod A (started first) | Pod B (started later) |
|---|---|---|---|
| Pod A starts | GBP | GBP | not started yet |
| The ConfigMap changes | EUR | GBP, unchanged | not started yet |
| Pod B starts | EUR | GBP, unchanged | EUR |
The cells students fill in are left blank in this preview.
Make a namespace for settings
kubectl create namespace settingsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make a ConfigMap with two settings
kubectl create configmap shop-config -n settings --from-literal=CURRENCY=GBP --from-literal=MODE=testShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Make a Deployment with no pods yet
kubectl create deployment printer -n settings --image=busybox:1.37.0 --replicas=0 -- sh -c 'echo "currency is $CURRENCY, mode is $MODE"; trap "exit 0" TERM; sleep 3600 & wait'Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Give it the ConfigMap as variables
kubectl set env deployment/printer -n settings --from=configmap/shop-configShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Start one pod
kubectl scale deployment printer -n settings --replicas=1Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Wait until the pod is ready
kubectl rollout status deployment/printer -n settingsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read what the program printed
kubectl logs deployment/printer -n settingsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A running pod does not see the change
Environment variables are read once, when the program starts. Changing the ConfigMap afterwards changes nothing inside a pod that is already running.
The next Runs change CURRENCY to EUR, then read the log of the same pod again. It still says GBP.
Change CURRENCY to EUR
kubectl patch configmap shop-config -n settings --type merge -p '{"data":{"CURRENCY":"EUR"}}'Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the same pod's log again
kubectl logs deployment/printer -n settingsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think that editing a ConfigMap changes every pod at once. It does not. A pod reads its settings when it starts. To use the new value, replace the pod: delete it, and the Deployment starts a new one that reads the ConfigMap again.
Delete the pod so a new one starts
kubectl delete pods -n settings -l app=printer --grace-period=1Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Wait for the new pod
kubectl wait --for=condition=Ready pod -l app=printer -n settings --timeout=60sShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Read the new pod's log
kubectl logs deployment/printer -n settingsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Remove the namespace
kubectl delete namespace settingsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.