ConfigMaps and Secrets
Mid-level Frequently asked 5 min
On this page
The question
How do you inject configuration into an application running on Kubernetes?
30-second answer
Keep it out of the image and put it in ConfigMaps, for plain settings, and Secrets, for credentials. A Pod consumes them in two ways: as environment variables, one key with valueFrom or all keys with envFrom, read once when the container starts; or as a volume, where each key becomes a file that the kubelet updates within about a minute of an edit. Spring Boot binds both: SPRING_DATASOURCE_URL by relaxed binding, files through spring.config.import=configtree:.
Understand it
Environment variables vs mounted files
CompareAs environment variables, values are copied into the process when the container starts. Edit the ConfigMap to on and the running app still sees off, until its Pod restarts.
View as text
Three facts above a ConfigMap, whose FEATURE_X was just edited from off to on, and a Secret holding a database password. Both feed a Pod, which shows what the running app reads for each.
Environment variables: Read: once, when the container starts. Edit: seen only after a restart. Secret: in env dumps and crash logs. $FEATURE_X reads off, stale until restart; $DB_PASSWORD reads s3cret, shows in env dumps. As environment variables, values are copied into the process when the container starts. Edit the ConfigMap to on and the running app still sees off, until its Pod restarts.
Mounted files: Read: from files, at any time. Edit: files update within a minute. Secret: in memory, tmpfs, read-only. /config/FEATURE_X reads on, updated in place; /secrets/password reads s3cret, tmpfs, not on disk. As a volume, each key becomes a file. The kubelet updates them within about a minute of the edit, with no restart, but only an app that rereads them notices.
Three facts above a ConfigMap, whose FEATURE_X was just edited from off to on, and a Secret holding a database password. Both feed a Pod, which shows what the running app reads for each.
Environment variables: Read: once, when the container starts. Edit: seen only after a restart. Secret: in env dumps and crash logs. $FEATURE_X reads off, stale until restart; $DB_PASSWORD reads s3cret, shows in env dumps. As environment variables, values are copied into the process when the container starts. Edit the ConfigMap to on and the running app still sees off, until its Pod restarts.
Mounted files: Read: from files, at any time. Edit: files update within a minute. Secret: in memory, tmpfs, read-only. /config/FEATURE_X reads on, updated in place; /secrets/password reads s3cret, tmpfs, not on disk. As a volume, each key becomes a file. The kubelet updates them within about a minute of the edit, with no restart, but only an app that rereads them notices.
Takeaway: ConfigMaps and Secrets reach a Pod as environment variables, read once at start, or as files, which the kubelet keeps updated. Either way the app decides when it rereads, and Secrets are only base64-encoded.
In code
apiVersion: v1
kind: ConfigMap
metadata:
name: orders-config
data:
FEATURE_X: "off"
SPRING_PROFILES_ACTIVE: prod
---
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
type: Opaque
stringData: # stored base64-encoded under data:, not encrypted
password: s3cret spec:
containers:
- name: orders
image: registry.example.com/orders:1.4.2
envFrom:
- configMapRef: { name: orders-config } # every key becomes a variable
volumeMounts:
- name: creds
mountPath: /etc/secrets/db
readOnly: true
volumes:
- name: creds
secret: { secretName: db-credentials } # every key becomes a file, on tmpfs# /etc/secrets/db/password becomes the property db.password
spring.config.import=optional:configtree:/etc/secrets/
spring.datasource.password=${db.password}kubectl get secret db-credentials -o jsonpath='{.data.password}' # czNjcmV0
kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 -d # s3cret
kubectl auth can-i get secrets --as=system:serviceaccount:shop:orders # who can read themenv:
# Visible to everyone with the repository or kubectl get deployment
Wrong - name: DB_PASSWORD
Wrong value: s3cret
# The value stays in the Secret; the Deployment only names it
Right - name: DB_PASSWORD
Right valueFrom:
Right secretKeyRef: { name: db-credentials, key: password }Solve it
A feature flag changed in the ConfigMap, but the service ignores it
Product asks to turn on FEATURE_X. You run kubectl edit configmap orders-config, set it to on, and wait. An hour
later the service still behaves as if it were off, and kubectl get configmap clearly shows on. The Deployment uses
envFrom with that ConfigMap. Explain and fix it, and make the next change reliable.
- Find how the value arrives. With
envFrom, the kubelet copies the keys into the container’s environment when it starts. A process’s environment never changes afterwards, so the running Pods keep off forever. - Apply the change now.
kubectl rollout restart deployment ordersreplaces the Pods gradually; each new Pod reads on when it starts, with no downtime if readiness probes are in place. - Make changes roll by themselves. Name ConfigMaps by version,
orders-config-v8, mark them immutable and point the Deployment at the new name. The template changes, so a rollout starts, and a rollback restores the old flag. - Know the alternative. Mounting as files updates the files within a minute, but the app must reread them, for example with Spring Cloud Kubernetes’ reload. More moving parts than a rollout, so use it only for frequent changes.
- Check.
kubectl exec deploy/orders -- printenv FEATURE_Xprints on, and the rollout history shows the change.
kubectl exec deploy/orders -- printenv FEATURE_X # off: copied when the container started
kubectl rollout restart deployment orders
kubectl rollout status deployment orders
kubectl exec deploy/orders -- printenv FEATURE_X # on
kubectl create configmap orders-config-v8 --from-env-file=orders.env
kubectl patch configmap orders-config-v8 -p '{"immutable":true}'
kubectl patch deployment orders --type=json -p='[{"op": "replace",
"path": "/spec/template/spec/containers/0/envFrom/0/configMapRef/name", "value": "orders-config-v8"}]'
kubectl rollout undo deployment orders # back to the previous ConfigMap, and its valueHow to answer
- One image for every environment; only the ConfigMaps and Secrets differ.
- Variables need a rollout to change; files update in place, but the app must reread them.
- Files mounted with subPath never update.
- Command-line arguments in the Pod spec can also reference variables, as $(VAR).
Common mistakes
- Baking environment-specific configuration or passwords into the image.
- Expecting a running Pod to see an edited ConfigMap through its environment variables.
Follow-up questions
Are Kubernetes Secrets encrypted?
Not by default. A Secret's values are base64-encoded, which anyone can decode, and stored in etcd as they are. Protection comes from elsewhere: enable encryption at rest in the API server, ideally with a KMS (key management service) provider, restrict who can get Secrets with RBAC, and keep them out of Git. Many teams keep the real values in an external store, such as Vault or a cloud secret manager, synced by the External Secrets Operator or mounted by the Secrets Store CSI driver.
- Anyone allowed to create a Pod in a namespace can mount its Secrets.
- Secret volumes are tmpfs, so values are not written to the node's disk.
- Managed clusters often encrypt etcd at rest, but base64 is still all the API returns.
See it in the infographic: Environment variables vs mounted files
You changed a ConfigMap, but the running service still uses the old value. Why, and what do you do?
If the service reads it from environment variables, they were copied when its containers started and never change, so it needs new Pods: kubectl rollout restart. To make changes roll out by themselves, version the ConfigMap, orders-config-v8, mark it immutable and reference the new name from the Deployment, or add a checksum of the config as a Pod template annotation, as Helm charts do. Either changes the template, so a rollout starts, and rollback restores the old config.
- Immutable ConfigMaps and Secrets also reduce load on the API server.
- Mounted files do update, but most apps read configuration once at startup.
- A change through a rollout is gradual, and a bad value fails readiness instead of the whole fleet.
See it in the infographic: Environment variables vs mounted files
What is the difference between a ConfigMap and a Secret?
Both are key-value objects that Pods consume as variables or files. A Secret is meant for sensitive data: it is base64-encoded, can be encrypted at rest, is kept in memory, on tmpfs, when mounted, has typed variants for TLS (Transport Layer Security) certificates and registry credentials, and is usually locked down by RBAC more tightly. A ConfigMap holds plain settings and is readable by anyone with access to the namespace. Each is limited to 1 MiB.
- kubernetes.io/tls and kubernetes.io/dockerconfigjson are common Secret types.
- stringData accepts plain text and the API stores it base64-encoded.
- Neither is meant for large files or binaries; use a volume or object storage.
See it in the infographic: Environment variables vs mounted files
How does Spring Boot read a Secret mounted as files?
With a config tree: spring.config.import=optional:configtree:/etc/secrets/ turns each file below that folder into a property named after its path, so /etc/secrets/db/password becomes db.password. Spring Cloud Kubernetes can also read ConfigMaps and Secrets from the API and reload on change, which needs RBAC (role-based access control) permission to read them.
Check yourself
Show answers
- off, until the Pods restart: Correct. Variables are copied into the process when the container starts.
- on, within about a minute: Incorrect. That happens for files from a ConfigMap volume, not for variables.
- on, because Kubernetes restarts them: Incorrect. Editing a ConfigMap restarts nothing; only a template change triggers a rollout.
Show answers
- The password, base64-encoded; base64 -d gives s3cret: Correct. base64 is an encoding, not encryption; anyone allowed to read the Secret can decode it.
- The password encrypted with the cluster's key: Incorrect. Encryption at rest happens in etcd; the API returns plain base64.
- A hash of the password, which cannot be reversed: Incorrect. Secrets store the value itself, so the Pod can use it.