Configuration and Secrets
Give one image different settings and see which changes a running program can pick up.
What you will understand
- Choose ConfigMap or Secret based on the kind of input being supplied.
- Compare environment variables with files mounted from configuration objects.
- Explain what a Secret protects and which access controls still matter.
Builds on: Init containers and sidecars
How an app gets settings and credentials
You run the same app image in a training cluster and a public service. The training app needs a different color on its page and a different API token. Rebuilding the image just to change those values would mix software releases with environment settings. Kubernetes can supply the values when the app runs.
A ConfigMap is an API object for non-confidential configuration, such as a mode flag or the contents of a small settings file. A Secret is an API object intended for sensitive data such as credentials. Pods can refer to either kind of object by name and read selected values. References generally need the object in the same namespace.
Using a Secret identifies the data as sensitive so you can put access rules and operational controls around it. The data format represents values using base64, a reversible encoding. Anyone authorized to read the Secret can decode it. Protect reads and workload creation permissions with RBAC, manage encryption at rest for stored data, and keep real credentials out of source files, command logs, and screenshots.
Follow a credential to the container that needs it
A Secret stores sensitive API data. RBAC, configured encryption at rest, and a narrow mount protect different parts of its path.
Store a dummy key
- Dummy plaintext tokenSecret API objectwrite stringData
- Secret API objectBase64 representationencode bytes
stringData accepts convenient plain strings. The resulting Secret data is encoded for API representation, not made secret by the encoding.
Protect access and storage
- Authorized readerAPI authorizationread if permitted
- API authorizationetcd protection configuredpersist/access
Confidentiality requires least-privilege access and appropriate storage protections. A permitted reader can decode the bytes.
Mount the needed key
- Namespaced SecretRead-only token mountproject bytes
- Read-only token mountSelected app containerminimum necessary access
The kubelet delivers the Secret to the selected container volume. The application must avoid logging or exposing its contents.
A fake Opaque token is shown; encryption at rest and external stores depend on cluster configuration.
Explore another case
Mount a ConfigMap and a Secret
In this example, MODE is passed as an environment variable and settings.ini is projected as a file. A Secret key is mounted as /run/token/token. The training value is fake. The process checks that the token file exists and has content while keeping the value out of its logs.
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.37.0
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.Save the manifest as configuration-demo.yaml and run the following in a disposable namespace. Expect the app logs to show its mode and the file read to show color=blue. If the container cannot start, read its events for a missing object, missing key, or volume setup error. Changing an image will not create a missing configuration object.
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.iniWhen does a ConfigMap change reach the app?
Environment variables are captured at startup. A regular volume projection can refresh, and the app has to reread the file to use the new value.
Read at process startup
- ConfigMap: blue / trainingMODE=trainingstartup snapshot
- ConfigMap: blue / trainingMounted settings.iniprojection
- MODE=trainingApplicationcontainer environment
The kubelet reads the namespaced ConfigMap while preparing the container. MODE becomes an environment value and settings.ini becomes a mounted file.
Refresh the mounted files
- ConfigMap: green / stagingProjection updatingeventual refresh
- Still MODE=trainingSame running processunchanged snapshot
The API now stores new values. The existing process environment is unchanged, while normal mounted projections refresh eventually.
Reload the application settings
- File now color=greenApp reloads configurationreread/reload
- New container: MODE=stagingApp reloads configurationafter deliberate restart
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.
A normal whole-volume ConfigMap mount is used; subPath and application API watches are excluded.
Inspect the ConfigMap update
One container gets its mode from an environment variable and its color from a ConfigMap volume. Edit both values and compare when the container sees them.
From KubeletTo App environment
Read the original configuration
When the container starts, the kubelet resolves MODE from the ConfigMap. It also projects settings.ini into the volume. Both reads reflect the original values.
- Before
- Container starting
- After
- MODE=training / color=blue
When the container starts, the kubelet resolves MODE from the ConfigMap. It also projects settings.ini into the volume. Both reads reflect the original values.
The projection refresh delay is compressed. The file is mounted as part of a ConfigMap volume. An app that caches the file still needs to reread it.
Reveal prediction answer
Reopening the file can pick up the projection. The process environment is a separate value set when the container starts.
Compare printenv MODE and cat settings.ini before and after projection refresh, then on the replacement Pod.
Observe both values in kindExplore another case
Open Inspect the ConfigMap update to compare the two values inside the same container after editing its ConfigMap.
Observe what happens after a config change
Now change the ConfigMap’s MODE and settings.ini while the Pod keeps running. The environment variable was set when the container started, so the current process keeps the old value. A normal mounted ConfigMap volume can see updated files after the kubelet refreshes the projection. An application may need to re-open the file or reload to use those changes. A single file mounted with subPath does not receive those automatic ConfigMap or Secret updates.
With configuration-demo.yaml saved, source lab-env.sh in the kind lab first. Run the example in a dedicated namespace, then patch the ConfigMap.
kubectl --context kind-kv-lab create namespace kv-config-trace
kubectl --context kind-kv-lab -n kv-config-trace apply -f configuration-demo.yaml
kubectl --context kind-kv-lab -n kv-config-trace wait --for=condition=Ready pod/configuration-demo --timeout=90s
kubectl --context kind-kv-lab -n kv-config-trace patch configmap application-settings --type=merge \
-p '{"data":{"MODE":"review","settings.ini":"color=green\n"}}'Run the next command until the file says color=green. Projection refresh can take a minute or more. The environment still says training.
kubectl --context kind-kv-lab -n kv-config-trace exec configuration-demo -- \
sh -c 'printenv MODE; cat /etc/application/settings.ini'Replace the standalone Pod to see the new environment value. Apply only the Pod document from the file above to keep the updated ConfigMap in place.
kubectl --context kind-kv-lab -n kv-config-trace delete pod configuration-demo
sed -n '/^kind: Pod/,$p' configuration-demo.yaml | \
(printf 'apiVersion: v1\n'; cat) | kubectl --context kind-kv-lab -n kv-config-trace apply -f -
kubectl --context kind-kv-lab -n kv-config-trace wait --for=condition=Ready pod/configuration-demo --timeout=90s
kubectl --context kind-kv-lab -n kv-config-trace exec configuration-demo -- printenv MODE
kubectl --context kind-kv-lab delete namespace kv-config-traceControl who can read the credential
Plan how your app will adopt updates. For an environment variable, change or restart the workload’s Pods in a controlled way. For a file, check whether the app rereads it and confirm the observed value after the projection updates. A Secret rotation also needs the application and upstream service to agree on when the new credential works. Practice how these inputs appear in a Pod with the module lab.
Check your understanding
You update a ConfigMap and the file changes, but the app keeps printing its old mode. Why?
The file and environment variable are separate delivery paths. The kubelet can update the projected file. The running program still has the environment it inherited at startup. Replace the running container through its workload if the change needs to take effect through that variable.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise