Sub topic 17
ConfigMaps, Secrets, and volumes at a glance
This chapter assumes you know what a Pod is and can read a manifest. It covers how a Pod receives configuration, credentials, and storage without baking them into the container image.
Why separate configuration from the image
An image should be the same in every environment. Settings such as a log level or a database host change between development and production. Kubernetes stores these settings as API objects, saved in etcd like any other object. Pods then consume them at runtime.
ConfigMaps
A ConfigMap holds non-sensitive key-value data. A Pod can read it as environment variables or as files in a mounted volume.
Secrets
A Secret has the same shape as a ConfigMap but is meant for passwords, tokens, and keys. Kubernetes handles Secrets with a little more care. For example, it avoids writing them to node disks when they are mounted as volumes.
Volumes
A volume is a directory made available to the containers in a Pod. Common kinds:
| Volume type | Lifetime | Typical use |
|---|---|---|
| `emptyDir` | Same as the Pod | Scratch space shared by containers |
| `configMap` / `secret` | Follows the object | Config or credentials as files |
| `persistentVolumeClaim` | Independent of the Pod | Data that must survive restarts |
A PersistentVolumeClaim (PVC) is a request for storage. The cluster binds it to a PersistentVolume, which the admin or a storage class provisions. The Pod refers only to the claim, so it does not need to know the storage backend.
Example
This manifest defines a ConfigMap and a Secret, then mounts both in a Pod.
apiVersion: v1
kind: ConfigMap
metadata:
name: web-config
data:
LOG_LEVEL: info
---
apiVersion: v1
kind: Secret
metadata:
name: web-secret
type: Opaque
stringData:
DB_PASSWORD: change-me
---
apiVersion: v1
kind: Pod
metadata:
name: web-config-demo
spec:
containers:
- name: web
image: nginx:1.27
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: web-config
key: LOG_LEVEL
volumeMounts:
- name: creds
mountPath: /etc/creds
readOnly: true
volumes:
- name: creds
secret:
secretName: web-secretLOG_LEVEL arrives as an environment variable. DB_PASSWORD appears as the file /etc/creds/DB_PASSWORD. The stringData field lets you write plain text, and the API server stores it base64-encoded.
Common errors
- Pod stuck in
ContainerCreating. The Pod references a ConfigMap, Secret, or PVC that does not exist in its namespace. Checkkubectl describe podfor events. - Config changes not appearing. Environment variables are fixed when the container starts, so restart the Pod. Mounted files update after a delay, except for
subPathmounts, which never update. - Data lost after a restart. The data was in an
emptyDir. Use a PVC instead.
Exercise
A Pod reads its database password from an environment variable filled from a Secret. You update the Secret. Why does the running container still see the old value, and what fixes it?
Show answer
Environment variables are set once, when the container starts. Updating the Secret does not change them. Restarting the Pod, for example with kubectl rollout restart on its Deployment, makes the new container read the new value.