- How to create ConfigMaps (imperative and declarative)
- How to inject ConfigMaps into Pods (envFrom, single env var, or mounted as files)
- How to inspect ConfigMaps and best practices
Why use ConfigMaps?
- Centralize configuration so many Pods can reuse the same values.
- Keep Pod manifests focused on application runtime rather than configuration.
- Separate code and configuration to simplify updates and rollouts.
ConfigMaps are not a replacement for Secrets. Do not store passwords, tokens, or other sensitive data in ConfigMaps.
Quick example: Pod with inline environment variable (before extracting to a ConfigMap)
Creating ConfigMaps: two approaches
Imperative examples
Create a ConfigMap from literals:
configmap.yaml
app-config, mysql-config, redis-config) — you will reference these names when injecting them into Pods.
View ConfigMaps List ConfigMaps:
Injecting ConfigMaps into Pods Common methods to provide ConfigMap data to containers:
- envFrom — inject all keys as environment variables
- valueFrom (
configMapKeyRef) — inject a single key as an environment variable - Volume mount — expose keys as files inside the container
pod-envfrom.yaml
APP_COLOR=blue and APP_MODE=prod as environment variables.
Method 2: Inject a single environment variable from a ConfigMap key (valueFrom)
Use this when you only need specific keys as environment variables.
/etc/app-config/APP_COLORcontainsblue/etc/app-config/APP_MODEcontainsprod
- envFrom — convenient when many keys map directly to environment variables
- valueFrom — precise control for individual variables
- Volume mount — when the app expects config files or you need to provide structured files
Best practices and tips
- Do not store secrets (passwords, tokens) in ConfigMaps — use Kubernetes Secrets instead.
- Use declarative YAML files for ConfigMaps when you want auditability and GitOps workflows.
- Prefer descriptive ConfigMap names tied to their consumer or service.
- When updating a ConfigMap that a Pod uses as environment variables, you must recreate or restart the Pod to pick up env changes. Volume-mounted ConfigMap updates are reflected automatically in many cases, but behavior can depend on the application and how it reads files.
Summary
- ConfigMaps store non-sensitive configuration as key/value pairs.
- Create them imperatively with
kubectl create configmap(--from-literal,--from-file) or declaratively with a YAML manifest. - Inject them into Pods using:
envFrom(all keys as env vars),env+valueFrom(configMapKeyRef) for a single key, or- mount as files using a
configMapvolume.
- Use Secrets for sensitive data.
kubectl describe, and try each injection method to see how your application reads configuration.
Links and references
- Kubernetes ConfigMap documentation: https://kubernetes.io/docs/concepts/configuration/configmap/
- kubectl cheat sheet: https://kubernetes.io/docs/reference/kubectl/cheatsheet/