Skip to main content

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.

flowchart TD I["Container image
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

Bash
kubectl create configmap app-config \
--from-literal=APP_ENV=production \
--from-literal=LOG_LEVEL=info \
-n production
YAML
containers:
- 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.conf
volumes:
- name: nginx-conf
configMap:
name: nginx-config

Update 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
Bash
kubectl rollout restart deployment/api-server -n production

Setting immutable: true on a ConfigMap prevents edits and reduces API server load for large fleets.

Security

Never 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 Mistake

Using envFrom everywhere so nobody knows which variables a pod expects. Prefer explicit configMapKeyRef entries for critical values.

Tip

Manage 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.

Found a mistake, an outdated step or a broken link?