Sub topic 4
Using ConfigMaps as environment variables
This chapter assumes you can write Pod manifests, use kubectl apply, and know that a ConfigMap stores non-secret key-value data. It covers how a container reads ConfigMap keys as environment variables, and the errors you will meet.
How injection works
Kubernetes resolves environment variables when the container starts. The kubelet (the node agent that runs Pods) reads the ConfigMap, copies the values into the container's environment, and then starts the process. After that, the values are fixed for the life of that container.
There are two ways to inject values:
envwithconfigMapKeyRefpicks one key and lets you choose the variable name.envFromwithconfigMapRefimports every key in the ConfigMap.
Injecting a single key
Create the ConfigMap first.
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
LOG_LEVEL: "info"
app.properties: |
cache.ttl=60
cache.size=100Then reference one key from a Pod.
apiVersion: v1
kind: Pod
metadata:
name: app-env
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "echo LOG_LEVEL=$LOG_LEVEL && sleep 3600"]
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVELThe name under env is the variable inside the container. The name under configMapKeyRef is the ConfigMap. They do not have to match, so you can map LOG_LEVEL to APP_LOG_LEVEL if the application expects that.
Check the result with kubectl logs app-env. It prints LOG_LEVEL=info.
Importing all keys with envFrom
envFrom saves you from listing every key.
apiVersion: v1
kind: Pod
metadata:
name: app-envfrom
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "env | sort && sleep 3600"]
envFrom:
- configMapRef:
name: app-config
prefix: CFG_The optional prefix is added to each variable name, so LOG_LEVEL becomes CFG_LOG_LEVEL. This helps avoid collisions when you import several ConfigMaps.
Keys that are not valid environment variable names are skipped. In app-config, the key app.properties contains a dot. Recent Kubernetes versions accept most printable characters except =, but older shells and images may not handle them. A key such as app.properties is better mounted as a file, which the next chapter covers.
If two sources define the same variable, an entry in env wins over envFrom. Among several envFrom sources, the last one listed wins.
Common errors
| Symptom | Likely cause |
|---|---|
| Pod stuck in `CreateContainerConfigError` | The ConfigMap or the referenced key does not exist. |
| Variable is empty or missing | Wrong key name. Keys are case-sensitive. |
| Pod starts without the variable | The reference has `optional: true` and the key was absent. |
| Value is stale after editing the ConfigMap | Environment variables are fixed at container start. |
Run kubectl describe pod app-env and read the Events section. A missing key produces a message such as couldn't find key LOG_LEVEL in ConfigMap default/app-config.
Set optional: true under configMapKeyRef or configMapRef only when the application has a safe default. Otherwise the Pod hides a configuration mistake.
Exercise
You edit the ConfigMap app-config and change LOG_LEVEL from info to debug. A running Pod injects it with configMapKeyRef. What does the container see, and what is the simplest way to make it see debug?
Show answer
The container still sees info, because environment variables are set once at container start. Restart the Pod, for example by deleting it so its controller recreates it, or run kubectl rollout restart on the Deployment that owns it.