Configuration and Secrets
Inject configuration with explicit update semantics and protect secret material beyond base64.
What you will understand
- Use ConfigMaps and Secrets through explicit Pod references
- Predict environment and mounted-file updates
- Explain why base64 does not provide confidentiality
Builds on: Init containers and sidecars
Mental model: configuration is a separate input
An image packages executable code and defaults. A ConfigMap supplies non-confidential deployment configuration without rebuilding that image. A Secret supplies sensitive values through a resource type designed for secret handling. Separating these inputs lets the same executable run in different environments, but it also creates dependencies: the referenced object and key must exist in the correct namespace before an ordinary required reference can be prepared successfully.
A ConfigMap stores string data under data and binary content under binaryData. It is not an encrypted credential store. A Secret commonly uses data containing base64-encoded bytes, with stringData available as a write-time convenience for plain strings. Base64 is reversible encoding, not encryption. Anyone permitted to read a Secret’s data can decode it. Putting a password into a YAML file under stringData leaves plaintext in that file and any repository or terminal history that records it.
Security depends on the full access path: least-privilege API authorization, restricted workload creation, protection of node access, encryption at rest where configured, and careful application handling. Permission to create a Pod in a namespace can indirectly expose that namespace’s Secrets by mounting them. Do not assess Secret safety solely by whether a user can run get secret.
Encoding is not confidentiality
Trace Secret bytes through API access and a scoped mount.
stringData accepts convenient plain strings. The resulting Secret data is encoded for API representation, not made secret by the encoding.
- Dummy plaintext token to Secret API objectwrite stringData
- Secret API object to Base64 representationencode bytes
Read the complete visual sequence
- Write the dummy Secret
stringData accepts convenient plain strings. The resulting Secret data is encoded for API representation, not made secret by the encoding.
- Authorize access and protect storage
Confidentiality requires least-privilege access and appropriate storage protections. A permitted reader can decode the bytes.
- Mount only where needed
The kubelet delivers the Secret to the selected container volume. The application must avoid logging or exposing its contents.
Failure: a Pod creator can expose a Secret indirectly
Failure: a Pod creator can expose a Secret indirectly
- Direct Secret read is denied
A user cannot get Secrets directly but can create arbitrary Pods in the same namespace.
- Workload creation provides another path
A created Pod can mount that namespace’s Secret and run code that reads it unless other controls prevent this configuration.
- Protect the workload boundary too
Restrict workload creation and admission along with Secret API access. Base64 does not stop an authorized execution path from reading bytes.
About this model
A fake Opaque token is shown; encryption at rest and external stores depend on cluster configuration.
Walkthrough: project ordinary settings and a dummy credential
This complete training manifest uses an intentionally fake token. Never replace it with a real credential in a committed example. The Pod prints only the non-sensitive setting and checks that the Secret file exists; it does not print Secret content.
apiVersion: v1
kind: ConfigMap
metadata:
name: application-settings
data:
MODE: "training"
settings.ini: |
color=blue
---
apiVersion: v1
kind: Secret
metadata:
name: application-token
type: Opaque
stringData:
token: "dummy-training-value"
---
apiVersion: v1
kind: Pod
metadata:
name: configuration-demo
spec:
containers:
- name: app
image: busybox:1.36.1
command: ["sh", "-c", "echo mode=$MODE; test -s /run/token/token && sleep 3600"]
env:
- name: MODE
valueFrom:
configMapKeyRef:
name: application-settings
key: MODE
volumeMounts:
- name: settings
mountPath: /etc/application
readOnly: true
- name: token
mountPath: /run/token
readOnly: true
volumes:
- name: settings
configMap:
name: application-settings
- name: token
secret:
secretName: application-token
defaultMode: 0444 # Training default; choose access for the actual app user.
Apply the three documents together in one dedicated lab namespace. Because object processing is asynchronous, a Pod may temporarily report missing configuration while the other documents are being created. Required references are retried as preparation proceeds. Inspect the concrete key and namespace if the condition persists.
kubectl apply -f configuration-demo.yaml
kubectl describe pod configuration-demo
kubectl logs configuration-demo -c app
# Inspect non-secret projected configuration only.
kubectl exec configuration-demo -c app -- cat /etc/application/settings.ini
Expect mode=training in logs and color=blue in the non-secret file. The test command checks the dummy token file without echoing it. The && chain keeps the application waiting only after that file check succeeds; if the check fails, this shell exits instead. This makes the required input explicit without placing its sensitive content in logs. Validate failure behavior as well as the successful startup path.
Environment snapshots and projected files
Contrast startup-time environment injection with eventual projected-file refresh.
The kubelet reads the namespaced ConfigMap while preparing the container. MODE becomes an environment value and settings.ini becomes a mounted file.
- ConfigMap: blue / training to MODE=trainingstartup snapshot
- ConfigMap: blue / training to Mounted settings.iniprojection
- MODE=training to Applicationcontainer environment
Read the complete visual sequence
- Prepare two consumption paths
The kubelet reads the namespaced ConfigMap while preparing the container. MODE becomes an environment value and settings.ini becomes a mounted file.
- Update the ConfigMap
The API now stores new values. The existing process environment is unchanged, while normal mounted projections refresh eventually.
- Adopt the new values deliberately
The file can contain green while the app still caches blue. Reload logic or a deliberate rollout is needed for consumer adoption and new environment values.
Failure: subPath freezes the mounted projection
Failure: subPath freezes the mounted projection
- Mount one key with subPath
A container uses a subPath mount for one ConfigMap file. This differs from a normal directory projection.
- The source object changes
The ConfigMap update does not automatically refresh this subPath-mounted file. Waiting alone does not establish the intended update path.
- Choose an explicit rollout or mount design
Replace the Pod through a deliberate workload rollout, or use a normal projected directory mount with application reload behavior. Waiting for this existing subPath mount alone will not deliver updates.
About this model
A normal whole-volume ConfigMap mount is used; subPath and application API watches are excluded.
Practice: predict configuration refresh
Environment variables are established when a container starts. Updating the ConfigMap does not rewrite the environment of an already running process. A projected ConfigMap or Secret volume can refresh eventually, with propagation delay governed by node update behavior. A subPath mount does not receive these automatic updates. Even when the file changes, an application that read it once at startup will retain its old in-memory settings until it reloads or restarts.
Change only settings.ini to color=green, apply, and inspect the mounted file after allowing for propagation. Then change MODE to another value and observe that the existing process environment does not automatically update. A deliberate workload rollout can create containers with the new environment. Versioned configuration names can make release identity clearer, provided you manage references and cleanup consciously.
Pitfalls
Updating a Secret is not a complete credential rotation. Consumers must reload it, old credentials may need revocation, and dependent systems must accept the new value during the transition. Logs, crash dumps, shell history, and debug output can disclose values after successful injection. Minimize what each container mounts, and use fake credentials during demonstrations.
Check your understanding
The Secret object changed, but the application still uses its old password. Did base64 prevent the update?
No. Encoding does not control refresh. Determine whether the value was injected as an environment variable, mounted as a normal projected volume, or mounted with subPath. Then determine whether the application rereads files or caches credentials in memory. Rotation requires both delivery of new bytes and consumer adoption, plus revocation policy outside this Pod.
Practice both update paths with dummy values in the module lab, then delete the dedicated training resources.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise