Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Configuration Management  ›  Lesson

ConfigMaps as Environment Variables

Injecting Pod Config with ConfigMaps 12 minA ConfigMap stores non-secret settings as key-value strings that pods can load as environment variables, which are fixed when the container starts.

ConfigMaps as Environment Variables

notes-web needs to know its log level, the page size for note lists and the database host. Baking those into the image means rebuilding the image for every environment. Kubernetes lets one image take different settings in development and production.

The rule

Settings live beside the image, not inside it — and env is read once, at start.

A ConfigMap is an object that holds plain, non-secret settings as key-value pairs:

apiVersion: v1
kind: ConfigMap
metadata:
  name: notes-web-config
  namespace: notes
data:
  LOG_LEVEL: info
  PAGE_SIZE: "20"
  DB_HOST: notes-db

Values are always strings, so numbers are quoted.

A pod can load every key as an environment variable:

containers:
  - name: web
    image: ghcr.io/example/notes-web:1.2
    envFrom:
      - configMapRef:
          name: notes-web-config

Or pick one key and give it a name of your choice:

    env:
      - name: NOTES_LOG_LEVEL
        valueFrom:
          configMapKeyRef:
            name: notes-web-config
            key: LOG_LEVEL

You can combine both. If the same variable name comes from envFrom and from env, the env entry wins, which is handy for overriding one value in one container.

The same image now runs with LOG_LEVEL: debug on a laptop and info in production — only the ConfigMap differs.

A quick way to create one from the command line:

kubectl create configmap notes-web-config --from-literal=LOG_LEVEL=info --from-literal=PAGE_SIZE=20 -n notes

The shortcut

Remember "env is a photograph". Environment variables are copied into the process when the container starts. Editing the ConfigMap later does not change a photo already taken.

Changing the log levelStep 1 of 4
  1. Edit the ConfigMap

    You change LOG_LEVEL from info to debug and apply it.

A second trap: if the ConfigMap named in configMapRef does not exist, the pod will not start. It waits with a CreateContainerConfigError status until the ConfigMap appears.

Drills

Answer fast — trust the rule.

quick drill

What kind of data belongs in a ConfigMap?

quick drill

Which field loads every key of a ConfigMap as environment variables?

quick drill

You change PAGE_SIZE in the ConfigMap. What do the running notes-web pods see?

quick drill

Why is PAGE_SIZE: "20" written with quotes?

Remember

  • Settings live beside the image in a ConfigMap, not inside it.
  • Env is a photograph: restart pods to pick up changes.
  • Trap: a missing ConfigMap stops the pod from starting.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy didn't it change?

You changed LOG_LEVEL from info to debug in the notes-web ConfigMap, but the running pods still log at info. Explain why, and how to make the change take effect.