How to Use ConfigMap as Environment Variable in Kubernetes
In Kubernetes, you can use a
ConfigMap as environment variables by referencing it in the pod's envFrom or env section. This injects the key-value pairs from the ConfigMap directly into your container's environment variables.Syntax
To use a ConfigMap as environment variables in a pod, you add the envFrom field under the container spec. This field points to the ConfigMap by name and imports all its keys as environment variables.
Alternatively, you can use env with valueFrom.configMapKeyRef to import specific keys.
- envFrom: Imports all keys from the ConfigMap as environment variables.
- env: Imports specific keys as environment variables.
yaml
containers:
- name: my-container
image: my-image
envFrom:
- configMapRef:
name: my-configmap
# OR for specific keys
containers:
- name: my-container
image: my-image
env:
- name: MY_VAR
valueFrom:
configMapKeyRef:
name: my-configmap
key: my-keyExample
This example shows a ConfigMap with two keys and a pod that uses envFrom to load them as environment variables.
yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: example-config
data:
APP_MODE: production
LOG_LEVEL: info
---
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: busybox
command: ["sh", "-c", "echo APP_MODE=$APP_MODE; echo LOG_LEVEL=$LOG_LEVEL; sleep 3600"]
envFrom:
- configMapRef:
name: example-configOutput
APP_MODE=production
LOG_LEVEL=info
Common Pitfalls
- Not creating the ConfigMap before the pod tries to use it causes pod startup failure.
- Using
envFromimports all keys, which may unintentionally expose sensitive data. - Referencing a non-existent key with
configMapKeyRefcauses the pod to fail. - Changes to ConfigMap do not update environment variables in running pods automatically; pods must be restarted.
yaml
containers:
- name: bad-container
image: busybox
env:
- name: MISSING_VAR
valueFrom:
configMapKeyRef:
name: example-config
key: missing-key # This key does not exist
# Correct way:
containers:
- name: good-container
image: busybox
env:
- name: APP_MODE
valueFrom:
configMapKeyRef:
name: example-config
key: APP_MODEQuick Reference
| Field | Description | Usage |
|---|---|---|
| envFrom | Imports all keys from a ConfigMap as environment variables | envFrom: - configMapRef: name: my-configmap |
| env with configMapKeyRef | Imports a specific key from a ConfigMap as an environment variable | env: - name: VAR_NAME valueFrom: configMapKeyRef: name: my-configmap key: key-name |
| ConfigMap | Stores key-value pairs for configuration | apiVersion: v1 kind: ConfigMap metadata: name: my-configmap data: key1: value1 key2: value2 |
| Pod restart | Required to update env vars after ConfigMap changes | kubectl delete pod |
Key Takeaways
Use envFrom to load all ConfigMap keys as environment variables in a container.
Use env with configMapKeyRef to load specific ConfigMap keys as environment variables.
Ensure the ConfigMap exists before creating pods that reference it to avoid errors.
Pods must be restarted to pick up ConfigMap changes in environment variables.
Avoid exposing sensitive data by carefully selecting which ConfigMap keys to import.