ConfigMap
A ConfigMap is a Kubernetes object that stores non-sensitive configuration as key-value pairs, which pods consume as environment variables or mounted files. It keeps configuration separate from container images so one image runs in every environment.
Why use a ConfigMap
Without one, changing a log level or hostname means rebuilding the image. With a ConfigMap, the image stays identical across dev, staging, and production and only the injected configuration differs.
built once"] --> P["Running pod"] C["ConfigMap
per environment"] -->|"env vars or files"| P classDef img fill:#DBEAFE,color:#1E293B classDef cfg fill:#FFEDD5,color:#1E293B classDef pod fill:#D1FAE5,color:#1E293B class I img class C cfg class P pod
Create and consume
kubectl create configmap app-config \ --from-literal=APP_ENV=production \ --from-literal=LOG_LEVEL=info \ -n productioncontainers: - name: api-server image: registry.example.com/api-server:v2.4.1 envFrom: - configMapRef: name: app-config # all keys as env vars env: - name: CACHE_HOST valueFrom: configMapKeyRef: # one explicit key name: app-config key: REDIS_HOST volumeMounts: - name: nginx-conf mountPath: /etc/nginx/nginx.conf subPath: nginx.confvolumes: - name: nginx-conf configMap: name: nginx-configUpdate behaviour
| Consumed as | Picks up changes? |
|---|---|
| Environment variable | No, restart the pod |
| Mounted volume | Yes, after a delay |
| Mounted with subPath | No, restart the pod |
kubectl rollout restart deployment/api-server -n productionSetting immutable: true on a ConfigMap prevents edits and reduces API server load for large fleets.
SecurityNever store passwords or tokens in a ConfigMap. It is plain text and readable by anyone with access to it. Use Secrets or an external secrets manager.
Common MistakeUsing
envFromeverywhere so nobody knows which variables a pod expects. Prefer explicitconfigMapKeyRefentries for critical values.
TipManage ConfigMaps through Helm values or GitOps, so every change is versioned and reviewable.
Frequently Asked Questions
Why not bake configuration into the image?
Every environment would need its own image build just to change a value. A ConfigMap decouples the two so the same tested image is promoted unchanged.
Why did my pod not see the ConfigMap change?
Environment variables are read once at container start, and subPath mounts are not updated. Restart the pod, or mount the whole ConfigMap as a volume with an app that reloads files.
ConfigMap or Secret?
ConfigMaps hold non-sensitive settings. Secrets hold sensitive data, and should also be protected with encryption at rest and RBAC.