Configuration
ConfigMaps, Secrets, environment variables, and the downward API
The same image runs in every environment; configuration comes from the cluster. The web Deployment gets it three ways:
That's how every response can say which pod and node served it:
curl localhost:8180/api/whoami
{"pod": "web-5c6cf9586b-l5q7x", "node": "lab-worker2", "version": "v1"}Changing a ConfigMap changes nothing (yet)
From rollouts.py, with traffic flowing:
1. ConfigMap VERSION=v2, 5 s later, pods still serve: {'v1': 88}Environment variables are read when the container starts. Updating the ConfigMap doesn't restart anything, so running pods keep the old values until they're replaced, and new pods (from a crash or a scale-up) get the new ones. That's how a cluster ends up running two configurations at once, with nobody having deployed anything.
Make configuration changes roll out like code changes. Either restart
explicitly (kubectl rollout restart deploy/web), or generate the
ConfigMap with a content hash in its name (kustomize's
configMapGenerator does this) so a change alters the pod template and
triggers a normal rolling update, with a rollback path.
ConfigMaps mounted as files (volumes: - configMap:) are updated in
place after a delay, but the app has to notice and reload; most don't. And
subPath mounts never update.
Secrets
Secrets look like ConfigMaps with values base64-encoded:
kubectl -n shop create secret generic api-keys --from-literal=PAYMENT_KEY=sk_live_123
kubectl -n shop get secret api-keys -o jsonpath='{.data.PAYMENT_KEY}' | base64 -d
sk_live_123Base64 is an encoding, not encryption. What actually protects Secrets:
- RBAC: who may
getorlistSecrets in a namespace.liston Secrets is as good as reading all of them. - Encryption at rest in etcd (an
EncryptionConfigurationon the API server; managed clusters offer KMS integration). - Not committing them: manifests in git hold references, and the values come from a secret manager via the External Secrets Operator, Sealed Secrets, or the CSI secrets driver.
Anyone who can create a pod in a namespace can mount any Secret in that namespace and read it, without any permission on Secrets themselves. Namespace boundaries are the real boundary for secrets.
Lab
- Change
VERSIONin the ConfigMap and request/api/whoamifor a minute. Then delete one pod. What versions do you see now? - Switch
web.yamlto a kustomizeconfigMapGeneratorand change a value. What doeskubectl apply -kdo to the Deployment? - Create a Secret, mount it as a file, and
kubectl execto read it.