Script Valley
Kubernetes: From Containers to Clusters
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

Kubernetes ConfigMap injection methods diagram

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: 300

Injecting 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_LEVEL

Mounting 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.yaml

When 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.

Up next

Kubernetes Secrets: storing sensitive data securely in a cluster

Sign in to track progress

Kubernetes ConfigMaps: how to inject configuration into pods โ€” Configuration and Storage โ€” Kubernetes: From Containers to Clusters โ€” Script Valley โ€” Script Valley