The same image can run in dev, test, and prod with different configuration values.
Interactive guide to storing non-sensitive configuration outside images and consuming it through environment variables or mounted files.
ConfigMaps separate runtime configuration from application images. The key design question is not whether to use ConfigMaps, but how the container should consume the data.
k8s/labs/workloads/configmap.yaml — ConfigMap resource holding key/value configuration data.k8s/labs/workloads/configpod.yaml — Pod consuming a ConfigMap as env vars or volumes.k8s/labs/workloads/config-svc.yaml — Pod that injects a ConfigMap key into an environment variable via configMapKeyRef.The same image can run in dev, test, and prod with different configuration values.
ConfigMaps are for non-confidential data. Sensitive values should use Secrets.
Use all keys as env vars, selected keys as env vars, or project data as files in a volume.
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap
data:
database: mongodb
database_uri: mongodb://localhost:27017
envFrom:
- configMapRef:
name: example-configmap
volumes:
- name: config-volume
configMap:
name: example-configmap
volumeMounts:
- name: config-volume
mountPath: /config
| Pattern | How it works | Best fit |
|---|---|---|
| envFrom | Loads every key as an environment variable | Simple apps that want all config at once |
| valueFrom | Maps one specific key to one variable | Explicit control over naming and scope |
| Volume mount | Creates files from ConfigMap keys | Apps that expect config files |
Move database URLs, feature flags, and environment names out of the image.
Mount web server configs, properties files, or startup scripts from ConfigMaps.
Share a common non-secret configuration source across multiple Pods.