Skip to content
BytePatterns

ConfigMaps, Secrets & Env

Docker & Kubernetes for Interviews: lesson 8 of 12

One image everywhere; the settings and the passwords live beside it, not in it.

Lesson 8 of 12 · 6 min

ConfigMaps, Secrets & Env

Step 1 of 11

One image, every environment. The settings live beside it, in objects the Pod reads when it starts.

The Idea

Keep configuration out of the image. A ConfigMap holds non-confidential key-value data; a Secret holds small sensitive values such as passwords and tokens. A Pod consumes either as environment variables or as files in a mounted volume.

Real-World Example

The same api image runs in staging and production. LOG_LEVEL comes from a ConfigMap; the database password comes from a Secret, mounted read-only as a file. Promoting a release changes config objects, never the image.

The Tradeoff

Secret values are base64-encoded, not encrypted, and by default sit unencrypted in etcd: enable encryption at rest and restrict access with RBAC. Environment variables are fixed when the container starts; mounted files update eventually, except through subPath.

Hands-On

# illustrative — a ConfigMap and a Secret from the command line
kubectl create configmap api-config --from-literal=LOG_LEVEL=info
kubectl create secret generic api-db --from-literal=password=change-me
# illustrative — in the Pod template
containers:
  - name: api
    envFrom: [{ configMapRef: { name: api-config } }]
    volumeMounts: [{ name: db, mountPath: /etc/db, readOnly: true }]
volumes:
  - { name: db, secret: { secretName: api-db } }

Your turn

Put the steps in the right order.

  1. Reference them from the Pod template: envFrom and a secret volume
  2. The kubelet starts the container with the variables and the mounted file
  3. Create the ConfigMap and the Secret
  4. To change LOG_LEVEL, update the ConfigMap and roll the Deployment

Mini quiz

1 / 3

A Secret's data field is:

Sources

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.