How it works
The explanation, with examples you can run.
Consuming configuration
apiVersion: v1kind: ConfigMapmetadata: name: web-configdata: LOG_LEVEL: info features.json: | { "newCheckout": false }---apiVersion: v1kind: Secretmetadata: name: web-dbtype: OpaquestringData: # plain text in; stored base64-encoded DATABASE_URL: postgres://web:s3cret@db:5432/shop---apiVersion: v1kind: Podmetadata: name: web-config-demospec: containers: - name: app image: ghcr.io/acme/web:1.4.2 envFrom: - configMapRef: { name: web-config } # every key as an env var env: - name: DATABASE_URL valueFrom: secretKeyRef: { name: web-db, key: DATABASE_URL } volumeMounts: - { name: config, mountPath: /etc/web, readOnly: true } volumes: - name: config configMap: name: web-config items: - { key: features.json, path: features.json } # /etc/web/features.jsonUpdates. Environment variables are read once at container start, so changing the ConfigMap does nothing until the pod is replaced. Mounted files are refreshed by the kubelet after a delay (its sync period plus cache propagation), except for subPath mounts, which never update. A common pattern is to roll the Deployment when config changes, for example by putting a hash of the config in a pod-template annotation, which tools such as Helm and Kustomize can generate. Marking a ConfigMap or Secret immutable: true protects it from accidental edits and reduces load on the API server.
What "Secret" does and does not mean
kubectl get secret web-db -o jsonpath='{.data.DATABASE_URL}' | base64 -d# postgres://web:s3cret@db:5432/shopThe Kubernetes documentation is explicit: Secrets are by default stored unencrypted in etcd; anyone with API access to them can read or modify them, so can anyone with access to etcd, and anyone allowed to create a pod in a namespace can use it to read any Secret in that namespace. What Secrets do give you over ConfigMaps is separate RBAC, delivery to pods via tmpfs rather than node disk, and a type that tools know to redact.
To make them actually safe:
- Enable encryption at rest on the API server (an
EncryptionConfiguration, ideally with a KMS provider). Managed clusters often do this for you; check. - Restrict RBAC: few humans need
getorliston Secrets, andlistreturns every value. Remember that create-pod implies secret-read in that namespace. - Keep them out of git in plain form. Use an external secrets manager with the External Secrets Operator or the Secrets Store CSI driver, or encrypt them in git with Sealed Secrets or SOPS.
- Prefer files to environment variables for secrets: environment variables leak into crash dumps, child processes and debug output more easily.
- Rotate: a mounted Secret updates in place, so an app that rereads its credentials file can rotate without a restart.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.