Consuming ConfigMaps as Environment Variables
What you'll learn: How to inject ConfigMap data directly into your container's environment so applications can read configuration values as standard environment variables.
The Core Idea
Once you've created a ConfigMap with your configuration data, you need to get it into your running containers. One of the simplest ways is to expose ConfigMap key-value pairs as environment variables—just like setting DATABASE_URL or LOG_LEVEL in a traditional application.
Think of it like unpacking a suitcase: your ConfigMap is the suitcase holding all your config items, and environment variables are how you make those items accessible to your application code.
Two Injection Methods
Individual Keys with env
You can selectively inject specific ConfigMap keys using the env field in your Pod spec:
env:
- name: DATABASE_HOST
valueFrom:
configMapKeyRef:
name: app-config
key: db.host
This pulls the db.host key from the app-config ConfigMap and makes it available as DATABASE_HOST inside the container.
Bulk Import with envFrom
To inject all keys from a ConfigMap at once, use envFrom:
envFrom:
- configMapRef:
name: app-config
Every key in app-config becomes an environment variable automatically. If your ConfigMap has keys API_KEY and TIMEOUT, both appear as environment variables with those exact names.
When to Use Which
Use env when you need precise control—renaming keys or pulling from multiple ConfigMaps. Use envFrom for simplicity when your ConfigMap keys already match your desired environment variable names.
Key Takeaway: ConfigMaps become accessible to containers through env (selective key injection) or envFrom (bulk injection), transforming configuration data into standard environment variables your application can read.