Configuration: ConfigMaps and Secrets
Keep configuration out of images: ConfigMaps for settings, Secrets for credentials, how they reach containers, and why a Kubernetes Secret is encoded, not encrypted.
By the end of this lesson you'll be able to
0 of 4 completeVerified lab: the ConfigMap, the Secret, decoding it, loading both into shop-web, and reading them
back from a running Pod, on the course's EKS cluster. The output shown is real.
Code walkthrough: External Secrets Operator and KMS encryption (explained, not run).
Cloud lab: none needed; works on kind.
The same image should run in dev and prod; only the settings change. If the greeting, the colour or the database address is baked into the image, every change means a new build. Kubernetes keeps settings in two objects: ConfigMaps for ordinary settings and Secrets for credentials.
ConfigMaps
# 06-config.yaml (part 1) apiVersion: v1 kind: ConfigMap metadata: name: shop-config data: GREETING: "Namaste from a ConfigMap" APP_COLOR: "#b23f1f"
A ConfigMap is just key-value data. A Pod can use it two ways:
- As environment variables (
envFromorenv.valueFrom): simple, read once when the container starts. - As files in a mounted volume: good for whole config files (an
nginx.conf), and updated in the Pod eventually after you change the ConfigMap.
The lab loaded every key as an environment variable, then called the app:
$ kubectl set env deployment/shop-web --from=configmap/shop-config deployment.apps/shop-web env updated $ kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api {"version": "v1", "pod": "shop-web-55d49ccc4b-5xkmp", "greeting": "Namaste from a ConfigMap", "uptime_seconds": 11}
kubectl set env deployment/shop-web --from=configmap/shop-configkubectltalk to a Kubernetes cluster
kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api--image=busybox:1.37container image
The greeting changed without rebuilding the image. Note uptime_seconds: 11: changing the env meant a new
Pod template, so the Deployment rolled out new Pods. That's the rule to remember: environment variables
are read only at start. Edit a ConfigMap later and running Pods keep the old values until they restart,
for example with kubectl rollout restart deployment/shop-web.
Secrets
A Secret looks almost the same. stringData lets you write the value in plain text, and Kubernetes stores it
base64-encoded:
# 06-config.yaml (part 2) apiVersion: v1 kind: Secret metadata: name: shop-secret type: Opaque stringData: DATABASE_URL: "postgres://shop:S3cr3t-demo@db.internal:5432/shop"
Now look at what anyone with read access to Secrets sees:
$ kubectl get secret shop-secret -o yaml apiVersion: v1 data: DATABASE_URL: cG9zdGdyZXM6Ly9zaG9wOlMzY3IzdC1kZW1vQGRiLmludGVybmFsOjU0MzIvc2hvcA== kind: Secret metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Secret","metadata":{"annotations":{},"name":"shop-secret","namespace":"default"},"stringData":{"DATABASE_URL":"postgres://shop:S3cr3t-demo@db.internal:5432/shop"},"type":"Opaque"} ... type: Opaque $ kubectl get secret shop-secret -o jsonpath="{.data.DATABASE_URL}" | base64 -d postgres://shop:S3cr3t-demo@db.internal:5432/shop
kubectl get secret shop-secret -o yamlkubectltalk to a Kubernetes clustergetlist objects-ooutput format
kubectl get secret shop-secret -o jsonpath="{.data.DATABASE_URL}" | base64 -d|pipe: send output to the next command
Two lessons in one output:
- base64 is not encryption. One command decodes it.
- The password also sits in plain text in an annotation.
kubectl applysaves the manifest it applied inlast-applied-configuration,stringDataand all. Applying real secrets from files leaks them a second time.
Inside the Pod, it's an ordinary environment variable:
$ kubectl set env deployment/shop-web --from=secret/shop-secret deployment.apps/shop-web env updated $ kubectl exec shop-web-55d49ccc4b-5xkmp -- printenv GREETING APP_COLOR DATABASE_URL Namaste from a ConfigMap #b23f1f postgres://shop:S3cr3t-demo@db.internal:5432/shop
kubectl set env deployment/shop-web --from=secret/shop-secretkubectltalk to a Kubernetes cluster
Keeping real secrets safe
So why use Secrets at all? Because Kubernetes treats them differently from ConfigMaps: RBAC can grant
get secrets separately (lesson 8), they're kept in memory on nodes rather than on disk, and the API server can
encrypt them at rest.
For real credentials, go further:
- Encryption at rest with KMS. On EKS, envelope encryption with an AWS KMS key encrypts Secrets in etcd. (The course lab skipped it to save cost; production shouldn't.)
- Least privilege. Very few people and service accounts should be able to read Secrets.
- An external secrets store. Keep the real value in AWS Secrets Manager or Vault, and let the External Secrets Operator create the Kubernetes Secret, or mount it directly with the Secrets Store CSI Driver. The value then never lives in Git or in a manifest.
- Never commit Secret manifests with real values to Git, even private repositories.
Change the app's behaviour without a rebuild
Create a ConfigMap with your own GREETING and APP_COLOR, and a Secret with a fake DATABASE_URL. Load both into shop-web, call /api to see the new greeting, decode the Secret, and find the second place its value appears in the Secret's YAML.
~/config.txtReveal one safe solution
$ kubectl apply -f 06-config.yaml $ kubectl set env deployment/shop-web --from=configmap/shop-config --from=secret/shop-secret $ kubectl rollout status deployment/shop-web $ kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api > ~/config.txt $ kubectl get secret shop-secret -o jsonpath="{.data.DATABASE_URL}" | base64 -d >> ~/config.txt $ kubectl get secret shop-secret -o yaml | grep -c S3cr3t
kubectl apply -f 06-config.yamlkubectltalk to a Kubernetes clusterapplycreate or update from a file-ffrom this file
kubectl rollout status deployment/shop-webrolloutmanage a rollout
kubectl run c --image=busybox:1.37 --restart=Never --rm -i --quiet -- wget -qO- http://shop-web/api > ~/config.txt--image=busybox:1.37container image>write output to a file, replacing it
kubectl get secret shop-secret -o jsonpath="{.data.DATABASE_URL}" | base64 -d >> ~/config.txtgetlist objects-ooutput format|pipe: send output to the next command>>append output to a file
kubectl get secret shop-secret -o yaml | grep -c S3cr3tgrepfind lines that match a pattern-ccount matching lines
The last command finds the plain-text password in the annotation. If the greeting doesn't change, the Pods
weren't restarted: check kubectl rollout status.
Try it yourself
0 of 5 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
What protects the value inside a Kubernetes Secret by default?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.