Configuration and StorageLesson 4.1
Kubernetes ConfigMaps: how to inject configuration into pods
ConfigMap definition, key-value data, multi-line config files, mounting as volume, injecting as env vars, envFrom, immutable ConfigMaps, config change propagation
Separate Config from Code
Hardcoding configuration in container images means rebuilding and redeploying for every config change. ConfigMaps let you store configuration separately and inject it at runtime — either as environment variables or mounted files.
Creating a ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
LOG_LEVEL: "info"
DATABASE_HOST: "postgres-svc"
app.yaml: |
server:
port: 8080
timeout: 30s
cache:
ttl: 300Injecting as Env Vars
containers:
- name: api
envFrom:
- configMapRef:
name: app-config # injects ALL keys as env vars
# OR specific keys:
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVELMounting as a File
volumes:
- name: config-vol
configMap:
name: app-config
containers:
- name: api
volumeMounts:
- name: config-vol
mountPath: /etc/config # app.yaml available at /etc/config/app.yamlWhen a ConfigMap mounted as a volume is updated, the file inside the Pod updates automatically (within ~60s). Env var injection does NOT update live — Pods must restart to see changes.
