Kubernetes EnvFrom ConfigMap Environment Variables
Inject all ConfigMap keys as environment variables using envFrom in Kubernetes pods. Configure configMapRef, secretRef, prefix options, and selective key
π‘ Quick Answer:
envFrominjects ALL keys from a ConfigMap (or Secret) as environment variables in one declaration. UseenvFrom[].configMapRef.nameto inject an entire ConfigMap, orenv[].valueFrom.configMapKeyReffor individual keys. Keys become env var names; values become env var values.
The Problem
- Listing every config key individually in
env[]is verbose and error-prone - Adding a new config key requires updating the Deployment spec
- Need to inject dozens of environment variables from a ConfigMap without boilerplate
- Want to separate configuration data from pod definition
- Need to combine variables from multiple ConfigMaps and Secrets
The Solution
envFrom β Inject All Keys
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: production
data:
DATABASE_HOST: "postgres.db.svc"
DATABASE_PORT: "5432"
DATABASE_NAME: "myapp"
LOG_LEVEL: "info"
CACHE_TTL: "300"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
template:
spec:
containers:
- name: app
image: registry.example.com/app:v1
# Inject ALL keys from ConfigMap as env vars
envFrom:
- configMapRef:
name: app-config
# Result: DATABASE_HOST=postgres.db.svc, DATABASE_PORT=5432, etc.envFrom with Prefix
containers:
- name: app
envFrom:
- configMapRef:
name: app-config
prefix: "APP_" # All keys prefixed with APP_
# Result: APP_DATABASE_HOST, APP_DATABASE_PORT, APP_LOG_LEVEL, etc.Combine Multiple Sources
containers:
- name: app
envFrom:
# All keys from ConfigMap
- configMapRef:
name: app-config
# All keys from Secret
- secretRef:
name: app-secrets
# Another ConfigMap with prefix
- configMapRef:
name: feature-flags
prefix: "FF_"
# Individual overrides (higher priority)
env:
- name: DATABASE_HOST
value: "override-host.example.com" # Overrides ConfigMap valueIndividual Keys (configMapKeyRef)
containers:
- name: app
env:
# Single key from ConfigMap
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: app-config
key: DATABASE_HOST
# Single key from Secret
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: password
# Downward API (pod metadata)
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
# Resource field
- name: MEMORY_LIMIT
valueFrom:
resourceFieldRef:
containerName: app
resource: limits.memoryOptional ConfigMaps
containers:
- name: app
envFrom:
- configMapRef:
name: app-config
optional: true # Don't fail if ConfigMap doesn't exist
- secretRef:
name: app-secrets
optional: false # Fail if Secret missing (default)Verify Injected Variables
# Check what env vars a pod has
kubectl exec my-app-xxx -- env | sort
# Or check the resolved pod spec
kubectl get pod my-app-xxx -o jsonpath='{.spec.containers[0].envFrom}'
# Debug: print env in container
kubectl exec my-app-xxx -- printenv DATABASE_HOST
# postgres.db.svcCommon Issues
ConfigMap key with invalid env var characters
- Cause: Key contains
-or.(e.g.,app.config.host) β invalid for env vars - Fix: Use underscores in ConfigMap keys; or use
configMapKeyRefwith explicitnamemapping
envFrom not picking up ConfigMap changes
- Cause: Environment variables are set at pod creation β not updated live
- Fix: Restart pods after ConfigMap update; or use volume mount for live reload
Priority: env overrides envFrom
- Cause: If same key exists in both
env[]andenvFrom[],env[]wins - Fix: Intentional β use
env[]for overrides. Check for unintended conflicts
Secret values visible in kubectl describe pod
- Cause:
envvalues from Secrets shown in pod spec - Fix: Use volume-mounted Secrets for sensitive values; or rely on RBAC to restrict
describe
Best Practices
- Use
envFromfor groups β inject related config as a unit - Use
envfor individual overrides β fine-grained control - Prefix when combining ConfigMaps β avoid key collisions
- Keep keys as valid env var names β uppercase, underscores only (A-Z, 0-9, _)
- Mark optional ConfigMaps β prevent startup failures in dev environments
- Use Secrets for sensitive values β never put passwords in ConfigMaps
- Restart after changes β env vars donβt hot-reload (use volumes for that)
Key Takeaways
envFrom.configMapRefinjects ALL ConfigMap keys as environment variables at onceenvFrom.secretRefdoes the same for Secretsprefixoption adds a string prefix to all injected variable namesenv[].valueFrom.configMapKeyRefinjects a single specific keyenv[]takes priority overenvFrom[]for the same key name- Changes to ConfigMap require pod restart β env vars are set at creation time
- Use
optional: truewhen ConfigMap might not exist yet

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
