Configure an app with ConfigMaps and Secrets
In this task you'll keep settings and passwords out of the image and change them without rebuilding.
A ConfigMap holds plain settings and a Secret holds sensitive ones (base64-encoded, which is not encryption). A pod can read either as environment variables or as files in a mounted volume. Environment variables are fixed when the container starts; mounted files update in place a minute or two after the ConfigMap changes, if the app re-reads them.
So a settings change needs a plan: either the app watches its files, or you restart the deployment (kubectl rollout restart), or you put a hash of the config in the pod template so any change rolls the pods. Real secrets are better kept in a vault (such as Azure Key Vault with the CSI driver) and secrets at rest should be encrypted.
Example. SS Kaiser's shop front reads its feature flags from a mounted ConfigMap and reloads them every minute, so marketing can switch a banner on without a release. Its payment key comes from Key Vault, not from a Secret in Git.
Before you start
You need: Docker Desktop, kind and kubectl on your computer (all free), and a cluster from Run a practice cluster on your computer. Time: 25 minutes.
Where to look
- Terminal
- kubectl
Steps
- Create a ConfigMap
shop-configwith LOG_LEVEL=info and BANNER=off, and a Secretshop-secretwith API_KEY=example-only. - Run a deployment that loads the ConfigMap with envFrom and mounts it as files at /config.
- Check both with
kubectl exec(env, and cat /config/BANNER). - Change BANNER to on; within a minute or two the file shows on, but the environment variable still says off.Env is read once at start.
- Run
kubectl rollout restart deployment shopand check the variable again. - Decode the Secret with
kubectl get secret shop-secret -o jsonpath='{.data.API_KEY}' | base64 -dto see why it isn't encryption.
Commands
kubectl create configmap shop-config --from-literal=LOG_LEVEL=info --from-literal=BANNER=off
kubectl create secret generic shop-secret --from-literal=API_KEY=example-only
kubectl create deployment shop --image=nginx:1.27
kubectl set env deployment/shop --from=configmap/shop-config
kubectl rollout restart deployment shop
kubectl get secret shop-secret -o jsonpath='{.data.API_KEY}' | base64 -d
Watch out for
- Cost
- Free.
- Security
- Anyone who can read Secrets in a namespace can read every password there. Limit get on secrets, encrypt etcd, and prefer a vault for real credentials.
- Common mistakes
- Committing Secret YAML to Git, expecting env vars to update without a restart, and using subPath mounts (they never update).
Clean up
Delete the deployment, ConfigMap and Secret.
Words to know
- ConfigMap
- A Kubernetes object holding plain settings for pods.
- Secret
- Like a ConfigMap, for sensitive values; encoded, not encrypted, by default.
- envFrom
- Loads every key of a ConfigMap or Secret as environment variables.
- rollout restart
- Replaces a deployment's pods gradually so they pick up new settings.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On