What Is a Kubernetes ConfigMap? Inject Environment Variables and Files, Then Apply Updates
A ConfigMap is a Kubernetes API resource for storing non-confidential settings as key-value pairs, separate from an application image and its code. A Pod can receive values as environment variables or command arguments, or mount them as files in a volume. The delivery method affects refresh behavior: environment variables do not update inside a running container, while projected volume files may update after a delay, so each method needs a different restart plan.
What belongs in a ConfigMap, and what can it protect?
A ConfigMap stores UTF-8 strings in data and, when needed, non-UTF-8 bytes as base64-encoded strings in binaryData. Keys cannot appear in both fields. The name must be a valid DNS subdomain, and keys may contain letters, digits, -, _, and .. The data in a ConfigMap cannot exceed 1 MiB. For larger files or data sets, consider a volume, database, or file service instead of putting everything in a ConfigMap.
The key security boundary is simple: a ConfigMap does not keep data secret or encrypt it. Store confidential values such as passwords, tokens, and private keys in a Secret or a separate secrets-management tool. A subject that can read a ConfigMap can read its settings.
A Pod and the ConfigMap it references must be in the same namespace. A static Pod cannot refer to a ConfigMap or other Kubernetes API resource. An application that calls the Kubernetes API directly can read a ConfigMap in another namespace, but that is a separate design requiring API access and application code.
Should you use environment variables or mount a file?
Choose a delivery method based on how the application reads configuration and how it can apply changes while running.
| Delivery method | What the container sees | What happens after a ConfigMap change | When it fits and what to watch |
|---|---|---|---|
env[].valueFrom.configMapKeyRef or envFrom.configMapRef | Container environment variables | Environment variables in an already running process are not refreshed automatically. Recreate the Pod to load new values. | Simple when the app reads environment variables at startup. You can select individual keys or import all keys. |
| ConfigMap volume | A file for each key under the mount path | Ordinary volume projections eventually update after kubelet synchronization and cache propagation. The app must reread or reload the file to apply the new setting. | Fits apps that read a configuration file or can reload it. A file mounted with subPath does not receive updates. |
| Command or arguments | Command-line arguments set when the container starts |
For example, an app might read its log level from an environment variable and a multi-line app.yaml from a file. Connect each ConfigMap key according to the way the app reads it. Updating a mounted file and reloading the application's configuration are separate steps. Do not assume that the app rereads a file just because the file changed.
Example: use one ConfigMap as an environment variable and a read-only file
The manifest below stores environment-specific settings and a file-like setting in demo-config; a Deployment in the same namespace consumes both. APP_ENV and LOG_LEVEL become environment variables when the container starts. app.yaml is mounted read-only at /etc/app-config/app.yaml. The illustrative image is BusyBox with a shell, cat, printenv, and sleep. The manifest and commands are examples and have not been run on a cluster.
apiVersion: v1
kind: ConfigMap
metadata:
name: demo-config
namespace: demo
data:
APP_ENV: "staging"
LOG_LEVEL: "info"
app.yaml: |
server:
port: 8080
featureFlags:
checkoutV2: false
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: config-demo
namespace: demo
spec:
replicas: 1
selector:
matchLabels:
app: config-demo
template:
metadata:
labels:
app: config-demo
spec:
containers:
- name: app
image: busybox:1.36.1
command:
- sh
- -c
- |
printf '%s\n' "$APP_ENV" "$LOG_LEVEL"
cat /etc/app-config/app.yaml
sleep 3600
env:
- name: APP_ENV
valueFrom:
configMapKeyRef:
name: demo-config
key: APP_ENV
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: demo-config
key: LOG_LEVEL
volumeMounts:
- name: app-config
mountPath: /etc/app-config
readOnly: true
volumes:
- name: app-config
configMap:
name: demo-config
items:
- key: app.yaml
path: app.yamlThe demo namespace must already exist. Apply both resources, then check rollout status and the values visible inside the container. These commands are examples and have not been run.
kubectl apply -f configmap-demo.yaml
kubectl get configmap demo-config -n demo
kubectl rollout status deployment/config-demo -n demo
kubectl exec deployment/config-demo -n demo -- printenv APP_ENV
kubectl exec deployment/config-demo -n demo -- cat /etc/app-config/app.yamlThe expected result is staging for APP_ENV, and readable server.port and featureFlags settings in the file. kubectl get only confirms that the ConfigMap exists in the API server; it does not prove that the container read the values or that the application applied them. Check the environment variable with exec, inspect the file at the mount path, and confirm behavior through the app's own logs or health check.
How do you apply a changed value?
Applying an edited ConfigMap manifest changes the API object. Then check how the application consumes it:
**Environment variable:** A running container keeps the old value. In a Deployment, run `kubectl rollout restart deployment/config-demo -n demo` to create a new Pod. Check `kubectl rollout status deployment/config-demo -n demo`, then inspect the environment variable in the new Pod. The Deployment's Pod template changes and a new ReplicaSet is created.
**Volume file:** An ordinary ConfigMap volume eventually projects the new content after kubelet synchronization and cache propagation. It is not immediate; delay depends on the kubelet sync period and cache strategy. If the app does not keep reading the file or detect and reload changes, its process can continue using the old settings even after the file changes.
**`subPath` volume:** A ConfigMap file mounted this way does not receive updates. If settings must change without replacing the Pod, avoid `subPath` and mount a directory, or use a procedure that recreates the Pod.
When changing a ConfigMap in production, save the previous manifest first. After applying the change, inspect the environment variables and files inside the actual Pod and check application behavior. If the change causes a problem, restore the previous settings and apply them again; environment-variable consumers also need another rollout. Rolling back a Deployment restores its Pod template, not the old ConfigMap data. Giving ConfigMaps versioned or hashed names and having the Deployment refer to the chosen name can make the configuration change part of the Pod-template history and easier to roll back.
An immutable ConfigMap can prevent accidental data edits, but after setting immutable: true, you cannot make it mutable again or change its data or binaryData. To replace it, delete and recreate the ConfigMap, and recreate existing Pods so they refer to the new object. Consider these operational effects before marking configuration immutable when automatic reload is required.
When should you use a ConfigMap or a Secret?
Use a ConfigMap for non-confidential values such as ordinary runtime settings, feature flags, log levels, and public service addresses. Use a Secret or a dedicated secrets manager for passwords, authentication tokens, and private keys. Kubernetes Secrets are stored unencrypted in etcd by default, so configure encryption at rest and least-privilege RBAC separately. A resource named Secret is not automatically safe. If configuration exceeds 1 MiB or the app needs a complex set of files, choose an appropriate volume, configuration-delivery system, or file service instead of a ConfigMap.
References
Kubernetes ConfigMap concepts, use, update behavior, and immutability
Configuration examples for using a ConfigMap in a Pod
ConfigMap v1 API fields and validation rules
Purpose and use of Kubernetes Secrets
Deployment Pod-template changes and rollout behavior
kubectl rollout restart command
kubectl exec for running a command in a Deployment Pod
Scope: ConfigMap v1 string and binary data, Pod references, environment-variable, volume, and command-argument consumption, update propagation, and immutability, based on Kubernetes documentation checked on 2026-11-23. The namespace, settings, and Deployment are illustrative and were not applied or run on a cluster. Do not treat a ConfigMap as confidential storage; check the documentation for the deployment tooling and application-specific reload behavior in your environment.