App
ConfigMaps and Secrets
Getting config into a Pod
Environment variables versus mounted files, and what actually happens when you update one. The web app takes its log level from an env var and its feature flags from a file. Same two objects - a ConfigMap and a Secret - but the two delivery paths have opposite update semantics, and knowing which is which explains most config surprises.
- Objects
- ConfigMap web-config · Secret stripe-key
- Path 1
- env vars · fixed at container start
- Path 2
- mounted files · refreshed in place
- Secret = base64
- encoding, not encryption
Read the whole lesson as text
1. Two objects
kubectl applies both objects in one file. The ConfigMap is plain key-value pairs - a log level and a settings file. The Secret looks the same with one twist: values are base64. That's an encoding requirement from the API (it stores bytes, not strings), not protection - anyone with read access decodes it instantly. Real encryption for secrets is a separate, cluster-level decision.
- kubectl posts the ConfigMap and the Secret in one apply.
2. As env vars
The Pod spec wires LOG_LEVEL through valueFrom, so at container-create time the kubelet expands it into the process environment. runc sets env on the exec - from then on it belongs to the process, a copy. Patch the ConfigMap now and nothing happens to this container: env cannot be edited under a running process. The next container picks up the new value - a restart or a new Pod; this process carries the old one until it exits.
- The kubelet resolves the key reference into a literal value before creating the container.
- The environment is fixed into the process when it starts - and never changes again.
3. As files
The settings file rides a different road: the Pod mounts web-config as a volume, and the kubelet materializes it as real files under the Pod's directory, bind-mounted into the container at /etc/web. To the app it's an ordinary file - but it's a projection. The kubelet owns the directory and can change what's projected into it, which is the hook that makes updates possible.
- The kubelet writes each key of the ConfigMap as a file in the Pod's volume directory.
- The app reads its feature flags from what looks like a local file.
4. The update
You flip cart_ab off and patch web-config. The kubelet's configmap watcher notices on its sync - up to a minute by default - and updates the volume atomically: it writes a fresh timestamped directory and re-points a ..data symlink at it. The file's content changes; its path doesn't. Whether the app notices is the app's business - a process that read the file at boot sees nothing until it re-reads.
- You flip one feature flag in the ConfigMap.
- On its next sync the kubelet writes a fresh directory and re-points ..data at it.
- A watcher or periodic re-read picks up the new flags with no restart; a boot-time reader never does.
5. Secrets
The stripe key travels the exact same two roads - env or file - from a Secret instead of a ConfigMap. Differences are policy, not plumbing: mounted secrets never hit the node's disk (they live on tmpfs) and they refresh like ConfigMaps. File permissions are not tighter by default - both default to 0644 - so set defaultMode: 0400 yourself. The real boundary is RBAC: whoever can read the Secret object has the key, base64 or not.
- The app charges a card using the projected key file - reading it the way it read settings.
- The key's first and only legitimate trip out of the Pod: the API call it authenticates.
Summary
One ConfigMap, two delivery paths, opposite contracts: environment is a snapshot taken at container start, a mounted file is a live projection refreshed by the kubelet. Secrets use both roads unchanged - the differences are RBAC, file modes, and staying off node disk. Choose per config: safe-to-snapshot values as env, anything that changes as files.
Next lesson: StatefulSet with storage