ConfigMaps

Beginner · 23 min

Keep configuration out of the image: creating ConfigMaps, consuming them as env vars or files, how updates propagate, and immutable ConfigMaps.

Before thisPods in depth

That DevOps Guy · 23 min

ConfigMaps

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by That DevOps GuyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A ConfigMap holds non-secret configuration as string keys (data) or base64 bytes (binaryData), so one image can run unchanged in every environment.
  • Pods consume it three ways: single env vars with configMapKeyRef, every key at once with envFrom, or files in a configMap volume.
  • Env vars are read once at container start and never change. Volume files are updated in place by the kubelet, but not when mounted with subPath.
  • The values in one ConfigMap may total at most 1 MiB, and client-side kubectl apply fails much earlier, at about 256 KiB, because of its last-applied annotation.
  • immutable: true locks data and binaryData for good and lets the kubelet stop watching the object. Roll out a change by creating a new name.

One image, many environments

You build an image once and want to run it in dev, staging and production. The code is the same. The database host, log level, feature flags and the nginx config are not. Baking those into the image means a rebuild per environment, and the image you tested is no longer the image you ship.

The old answer outside Kubernetes was environment variables and config files laid down by a deploy script. Kubernetes turns that into an API object: a ConfigMap, a named bag of keys and values that lives in a namespace next to the Pods that read it. The core objects lesson introduced it in a sentence. This lesson is about how it actually behaves, which is where people get surprised.

A ConfigMap is for configuration that isn’t secret. Passwords and keys go in a Secret, which looks almost identical and is the next lesson.

What one looks like

apiVersion: v1
kind: ConfigMap
metadata:
  name: web
data:
  MODE: prod
  nginx.conf: |
    server {
      listen 8080;
    }

data holds UTF-8 strings. binaryData holds base64-encoded bytes for anything that isn’t text. Keys may use letters, digits, -, _ and ., and a key can’t appear in both maps. There is no schema: a ConfigMap doesn’t know whether a value is a number, a flag or a whole file, and it doesn’t care.

You rarely type that by hand. kubectl create configmap builds it from literals, files or env files:

kubectl create configmap web --from-file=nginx.conf --from-literal=MODE=prod
kubectl create configmap app --from-env-file=app.env

--from-file uses the file name as the key and the contents as the value. --from-env-file reads KEY=value lines and skips comments, but it doesn’t strip quotes: a line FEATURE_X="on" became the value "on", quotes included, when we tried it. Write env files without quotes.

Three ways in

A Pod can read a ConfigMap as one variable, as all of its keys at once, or as files:

spec:
  containers:
  - name: app
    image: nginx:1.29
    env:
    - name: LOG_LEVEL
      valueFrom:
        configMapKeyRef: {name: app, key: LOG_LEVEL}
    envFrom:
    - configMapRef: {name: app}
      prefix: APP_
    volumeMounts:
    - {name: conf, mountPath: /etc/nginx/conf.d}
  volumes:
  - name: conf
    configMap:
      name: web
      items:
      - {key: nginx.conf, path: default.conf}

configMapKeyRef picks one key and names the variable. envFrom turns every key in data into a variable, with an optional prefix, and an explicit env entry wins over an envFrom one with the same name. Older docs say envFrom skips keys that aren’t valid variable names. Since relaxed variable-name validation went GA in 1.34, the v1.37 kubelet copies every data key across as-is; binaryData keys are never used. The volume writes each key as a file, or only the keys listed in items, under the path you choose.

The ConfigMap must exist in the Pod’s namespace. If it doesn’t, the API server still accepts the Pod, because references are checked by the kubelet at start-up, not at admission. The container then sits in CreateContainerConfigError until the ConfigMap appears. Mark a reference optional: true when the config really is optional, and the container starts without it.

Updates: the part everyone gets wrong

Think of the three paths like a printed handout, a whiteboard and a photo of the whiteboard.

Environment variables are the handout. They are copied into the process when the container starts and never change. Edit the ConfigMap and running Pods keep the old values until they restart.

A volume is the whiteboard. The kubelet notices the change and rewrites the files in place. It does this atomically: the files live in a timestamped directory, a symlink called ..data points at it, and each visible file is a symlink through ..data. To update, the kubelet writes a new directory and swaps the symlink with a rename, so a reader never sees half-written config. By default the kubelet watches the ConfigMaps its Pods use, and the docs put the worst-case delay at the kubelet sync period (one minute by default) plus that propagation delay. Your app still has to reread the file. nginx won’t, unless you send it a reload.

A subPath mount is the photo. Mounting one file with subPath binds the file the symlink pointed at when the container started. The swap happens one level up, so the photo never changes. That’s why subPath config “stops updating”.

Because env vars never refresh and most apps don’t watch files, the usual practice is to treat a config change as a rollout. Helm charts put a checksum of the rendered ConfigMap in a Pod template annotation, so any change alters the template and the Deployment rolls. Kustomize’s configMapGenerator appends a content hash to the name, which has the same effect. Or a controller such as Stakater’s Reloader watches ConfigMaps and restarts the workloads that use them.

Limits and immutability

The values in one ConfigMap may total 1 MiB. The check in the API server’s validation sums the lengths of the values in data and binaryData. A 1,048,577-byte file was refused with Too long: may not be more than 1048576 bytes, and one 8 bytes under went in.

The limit you hit first is subtler. Client-side kubectl apply stores the whole object in a last-applied-configuration annotation, and annotations are capped at 256 KiB in total. A 300 KiB ConfigMap failed with metadata.annotations: Too long, and the same file went in with kubectl apply --server-side, which tracks ownership in managedFields instead (see server-side apply).

Setting immutable: true (GA since 1.21) locks the contents:

$ kubectl patch cm app-config-v3 --type=merge -p '{"data":{"LOG_LEVEL":"debug"}}'
The ConfigMap "app-config-v3" is invalid: data: Forbidden: field is immutable when `immutable` is set

You can’t flip it back to false either, though labels can still change. Two things make this worth doing. An accidental edit can’t hit every Pod at once. And the kubelet’s watch-based cache marks immutable objects and stops watching them, which matters when thousands of Pods each reference config. The trade is that every change is a new object, app-config-v4, and a rollout to point at it. That’s the generator-hash pattern again, made explicit.

About the video

The embed is That DevOps Guy (Marcel Dempers), “Kubernetes Configmaps Explained for beginners”, July 2025, 23 minutes. Its chapters run from fundamentals and creating ConfigMaps through consuming them in Deployments, mounting files as volumes, a “propagation and gotchas” chapter, and Helm checksums and Reloader. That matches this lesson’s arc closely, including the update behaviour most tutorials skip. It was judged from the chapter list and description, since transcripts can’t be fetched here, and from its “English (US) original” audio track. The video doesn’t cover immutable ConfigMaps or the apply annotation limit, which the lesson adds. Piyush’s CKA episode in deeper is a shorter, exam-style alternative.

What was run: against a local v1.37.3 API server whose nodes run no containers, we created ConfigMaps from literals, files and an env file, hit the invalid-key, 1 MiB and annotation-size errors, applied the large one server-side, created an immutable ConfigMap and saw the patch refused, and server-side dry-ran the Pod above. The volume update mechanism is from the kubelet’s atomic_writer.go and watch_based_manager.go in the v1.37.3 source, and the propagation timing from the ConfigMap docs, checked 2026-11-28. Nothing mounted a volume or ran a container.

Where this fits

Secrets look like ConfigMaps with a different name, and that resemblance is the problem. Next: Secrets, and what they do not protect.

Check yourself

  1. 01You edit a ConfigMap that a running Deployment reads through envFrom. When do the Pods see the new values?

    Not until each container restarts. Environment variables are fixed when the process starts. A rollout (kubectl rollout restart, or a changed checksum annotation in the Pod template) makes new Pods that read the new values.

  2. 02A ConfigMap is mounted as a volume, and one file is also mounted into another path with subPath. You update the ConfigMap. Which copy changes?

    Only the full volume mount. The kubelet swaps the ..data symlink inside the volume directory, and a subPath mount is bound to the file the symlink pointed at when the container started, so it keeps the old content.

  3. 03kubectl apply -f config.yaml fails with metadata.annotations Too long, but the ConfigMap's data is only 300 KiB. Why, and what are the fixes?

    Client-side apply copies the whole object into the kubectl.kubernetes.io/last-applied-configuration annotation, and all annotations together are limited to 256 KiB. Use kubectl apply --server-side, which tracks ownership in managedFields instead, or kubectl create/replace.

  4. 04Why would you mark a ConfigMap immutable when nothing would edit it anyway?

    It protects against an accidental edit that would reach every Pod at once, and it lets each kubelet stop watching that object, which lowers API server load in large clusters. The cost is that a change means a new ConfigMap name and a rollout.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark