
- Reduce duplication of environment variables across many Pod manifests.
- Separate configuration from container images and deployment logic.
- Make configuration updates without rebuilding container images (depending on consumption method).
- Create a ConfigMap that contains key/value configuration data.
- Inject the ConfigMap into one or more Pods as environment variables or mounted files.
kubectl and declaratively with YAML manifests.
Imperative creation (from literals)
- Use
--from-literalto add key/value pairs directly.
--from-literal for additional keys. Good for a small number of items; less practical for many entries.
Imperative creation (from files)
- Use
--from-fileto create keys from file names. The file content becomes the value.
app.properties contains arbitrary content, the ConfigMap stores it under the key app.properties.
Declarative creation (YAML manifest)
- Preferred for repeatability and GitOps workflows. Create
app-config.yaml:
mysql-config, redis-config) and name them to reflect their purpose.
View and inspect ConfigMaps
- List ConfigMaps:
- Describe a specific ConfigMap to inspect keys and values:
- Import all keys into environment variables (envFrom)
- Use
envFromto import all key-value pairs from the named ConfigMap into the container environment:
APP_COLOR=blue and APP_MODE=prod in the container environment.
- Map a single key to a single environment variable
- Use
valueFrom.configMapKeyRefwhen you only need one specific key:
- Mount ConfigMap keys as files in a volume
- Mount the ConfigMap as a volume so that each key appears as a file under the mount path:
/etc/config/APP_COLOR and /etc/config/APP_MODE, with file contents equal to the values.
Best practices and important notes
ConfigMaps are not intended for sensitive data (such as passwords). Use Secrets for sensitive configuration. Also prefer declarative manifests (
kubectl apply -f) for repeatability and version control.- Use clear, descriptive ConfigMap names because Pods reference them by name.
- Prefer declarative YAML for production and CI/CD pipelines.
- When mounting ConfigMaps as files, applications that read filesystem-based configs can pick up changes if they re-read files (watching/reload behavior varies by app).
- To update a ConfigMap and ensure Pods pick up changes, you may need to restart or roll the Pods (depending on how the app consumes the data).
- Why separate configuration from Pod manifests.
- How to create ConfigMaps imperatively (
--from-literal,--from-file) and declaratively (YAML). - How to view and describe ConfigMaps (
kubectl get/kubectl describe). - How to inject ConfigMap data into Pods using
envFrom,valueFrom.configMapKeyRef, and volume mounts.
- Kubernetes ConfigMap documentation: https://kubernetes.io/docs/concepts/configuration/configmap/
- Kubernetes API reference: https://kubernetes.io/docs/reference/generated/kubernetes-api/
- Create a ConfigMap using the YAML manifest above.
- Create the example Pod that consumes the ConfigMap.
- Inspect the Pod environment or mounted files to confirm the ConfigMap values are available.