Kubernetes · Chapter 9 of 28
ConfigMaps
By the end of this chapter you can keep an app's settings out of its image in a ConfigMap, hand them to a container as environment variables or as files, change them without building a new image, and say when a running pod sees the change and when it does not. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Running apps
- 4 tasks
- Kubernetes
- In the all labs plan
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.
Why you need this
Jhatpat changes its words and numbers far more often than its code. For Dhanteras, marketing wants the storefront's banner changed four times in three days (the sale, free delivery, delivery till midnight, thank you). The catalog's settings list which of the 14 dark stores sell in which area and how prices are rounded, and Bhola changes them when a store closes for cleaning. The same catalog image will soon run in Kanpur with a different city.
So far every value lived in the image or in the Deployment's YAML. A new banner baked into the image means a new build, a new tag and a rollout for one sentence; writing value: in each Deployment means the same setting copied into every manifest that needs it, and Bhola editing Deployments. A ConfigMap keeps settings as their own object in the cluster: one place to change, read by as many pods as need it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
storefront.yaml: the banner from a ConfigMap
Move the storefront's banner out of the image and into a ConfigMap.
1. Make a ConfigMap named storefront-config with one key, BANNER. Its value is exactly: Diwali sale: free delivery above Rs 199
2. Open storefront.yaml with nano. Give the web container an env var named BANNER.
3. Take its value from the key BANNER of storefront-config, with valueFrom (or envFrom). Do not write the text in the file.
4. Apply storefront.yaml. Each new storefront pod logs the banner when it starts.
To pass: storefront-config has BANNER with that exact text (spaces and capitals count), the Deployment takes BANNER from it, and a ready storefront pod logs the banner. Task 3 changes the text later, and this task accepts both texts.
- Task 2
catalog-deploy.yaml: catalog.yaml as a mounted file
Give the catalog its settings file from a ConfigMap.
1. Make a ConfigMap named catalog-config from the file catalog.yaml in the chapter folder. Its key must be catalog.yaml.
2. Open catalog-deploy.yaml with nano. Add a volume from that ConfigMap to the pod.
3. Mount the volume in the catalog container, so the file is at /etc/catalog/catalog.yaml.
4. Apply catalog-deploy.yaml, and read a new catalog pod's logs.
To pass: catalog-config has the key catalog.yaml with the file from the chapter folder, the Deployment mounts it, and every ready catalog pod logs config /etc/catalog/catalog.yaml with the same number of lines as that file.
- Task 3
Change the banner and roll it out
Change the banner, and make every storefront pod show the new one.
1. Change BANNER in the ConfigMap storefront-config to exactly: Dhanteras: delivery till midnight
2. Keep the storefront's BANNER env var coming from the ConfigMap.
3. Read a running pod's logs. It still shows the old banner.
4. Make the Deployment start new pods, and wait for the rollout to finish.
To pass: storefront-config has the new text, and every ready storefront pod logs the new banner. No pod from before the change may be left.
- Task 4 · interview question
Interview: env or a file
Interview question: you changed the ConfigMap, so why does the pod still have the old value?
1. Say when a container reads its env vars.
2. Say what makes the pod see the new value.
3. Say what a mounted file does differently when the ConfigMap changes.
4. Write one or two sentences of your own below the notes in answer.txt.
To pass: answer.txt says when an env var is read and what brings the new value, and what a mounted file does.
What you will learn
- ConfigMaps: keys and values (or whole files) stored by the API server.
- Making one: kubectl create configmap --from-literal and --from-file, or YAML.
- As env vars: one key with valueFrom, or all keys with envFrom.
- As files: a volume from the ConfigMap, with items and subPath to pick and place files.
- What updates and when: files follow changes, env vars do not until the pod is replaced (kubectl rollout restart).
- A missing ConfigMap or key: CreateContainerConfigError and FailedMount.
More in Running apps
Build "ConfigMaps" yourself
Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.