Kubernetes Tutorial · Configuration and Resources · Lesson 17 of 26
ConfigMaps
Keep configuration out of images: environment variables and files from ConfigMaps.
ConfigMaps
A ConfigMap stores non-secret configuration as key-value pairs or whole files. Pods consume it as
environment variables (envFrom, valueFrom) or as files in a mounted volume.
- Environment variables are read at container start: changing the ConfigMap needs a restart
(
kubectl rollout restart deployment/web). - Mounted files are updated in place after a short delay (except
subPathmounts); the app must re-read them. - A common trick: put a hash of the config in a pod annotation so a config change triggers a rollout.
kubectl create namespace demo-config > /dev/null
trap 'kubectl delete namespace demo-config --wait=false > /dev/null' EXIT
kubectl -n demo-config create configmap app-config --from-literal=LOG_LEVEL=info --from-literal=FEATURE_SEARCH=on > /dev/null
cat > pod.yaml <<'EOF'
apiVersion: v1
kind: Pod
metadata: {name: app}
spec:
restartPolicy: Never
containers:
- name: app
image: busybox:1.37
command: ["sh", "-c", "echo LOG_LEVEL=$LOG_LEVEL; echo file says: $(cat /etc/app/FEATURE_SEARCH)"]
envFrom: [{configMapRef: {name: app-config}}]
volumeMounts: [{name: config, mountPath: /etc/app}]
volumes:
- name: config
configMap: {name: app-config}
EOF
kubectl -n demo-config apply -f pod.yaml > /dev/null
kubectl -n demo-config wait --for=jsonpath='{.status.phase}'=Succeeded pod/app --timeout=120s > /dev/null
kubectl -n demo-config logs app
Output
LOG_LEVEL=info file says: on
Run this on your computer — see “Get Started” for setup.
See ConfigMaps.
Test yourself
You change a ConfigMap used through envFrom. When do running pods see the new value?
ImmediatelyAfter they restart; environment variables are fixed at container startWithin 60 secondsNever
Show answer
Answer: After they restart; environment variables are fixed at container start
Mounted files update in place; environment variables don't.