Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Kubernetes ConfigMaps and settings

After this lesson: I can change a setting without rebuilding the program.

55 min lesson13 commandsKubernetes lab: a one-node k3s cluster for each student on a private Ubuntu VM

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

WordWhat it meansExample
ConfigMapa set of named settings kept in the cluster
Keythe name of one settingCURRENCY
Environment variablea named value a program reads when it starts
Imagethe packed program a pod starts from
Replace a poddelete 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

MomentConfigMap saysPod A (started first)Pod B (started later)
Pod A startsGBPGBPnot started yet
The ConfigMap changesEURGBP, unchangednot started yet
Pod B startsEURGBP, unchangedEUR

The cells students fill in are left blank in this preview.

Make a namespace for settings

Command
kubectl create namespace settings

Shown 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

Command
kubectl create configmap shop-config -n settings --from-literal=CURRENCY=GBP --from-literal=MODE=test

Shown 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

Command
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

Command
kubectl set env deployment/printer -n settings --from=configmap/shop-config

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Start one pod

Command
kubectl scale deployment printer -n settings --replicas=1

Shown 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

Command
kubectl rollout status deployment/printer -n settings

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read what the program printed

Command
kubectl logs deployment/printer -n settings

Shown 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

Command
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

Command
kubectl logs deployment/printer -n settings

Shown 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

Command
kubectl delete pods -n settings -l app=printer --grace-period=1

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Wait for the new pod

Command
kubectl wait --for=condition=Ready pod -l app=printer -n settings --timeout=60s

Shown 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

Command
kubectl logs deployment/printer -n settings

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Remove the namespace

Command
kubectl delete namespace settings

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

About this lab and its source: Kubernetes lab: a one-node k3s cluster for each student on a private Ubuntu VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.