K8s by Example: ConfigMaps

ConfigMaps store non-sensitive configuration as key-value pairs. They decouple configuration from container images. Inject them as environment variables or mount as files. Never store secrets here.

configmap.yaml

ConfigMaps use the core v1 API. The data field holds string key-value pairs for configuration.

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  DEBUG: "false"
  ENVIRONMENT: "production"
  LOG_LEVEL: "info"
configmap-file.yaml

Store multi-line config files using literal block syntax (|). The key becomes the filename when mounted. Perfect for nginx.conf or application.yaml.

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    server {
      listen 80;
      location / {
        proxy_pass http://backend:8080;
      }
    }
pod-envfrom.yaml

Inject all keys as environment variables with envFrom. Use prefix to namespace them and avoid collisions. DEBUG becomes APP_DEBUG.

spec:
  containers:
    - name: app
      image: my-app:v1
      envFrom:
        - configMapRef:
            name: app-config
          prefix: APP_
pod-env-valueFrom.yaml

Select specific keys with valueFrom. Useful when ConfigMap key names don’t match expected env vars. Use optional: true to allow Pod startup if key is missing.

env:
  - name: DEBUG_MODE
    valueFrom:
      configMapKeyRef:
        name: app-config
        key: DEBUG
        optional: true
pod-volume-mount.yaml

Mount ConfigMap as files in a volume. Each key becomes a file. Mounted ConfigMaps auto-update when changed (within ~1 minute). Use items to select specific keys.

spec:
  containers:
    - name: app
      volumeMounts:
        - name: config
          mountPath: /etc/app
          readOnly: true
  volumes:
    - name: config
      configMap:
        name: app-config
        items:
          - key: nginx.conf
            path: nginx.conf
configmap-immutable.yaml

Immutable ConfigMaps cannot be changed after creation. This improves performance (no watches) and prevents accidental changes. You must delete and recreate to update.

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config-v1
immutable: true
data:
  ENVIRONMENT: "production"
  LOG_LEVEL: "info"
terminal

Create ConfigMaps imperatively from literals, files, directories, or env files. The —dry-run=client -o yaml pattern generates declarative manifests.

$ kubectl create configmap app-config \
    --from-literal=DEBUG=false \
    --from-literal=PORT=8080
configmap/app-config created

$ kubectl create configmap nginx-conf \
    --from-file=nginx.conf
configmap/nginx-conf created

$ kubectl create configmap my-config \
    --from-env-file=.env \
    --dry-run=client -o yaml > configmap.yaml
terminal

ConfigMap updates with mounted volumes propagate automatically. Env vars never update, so a Pod restart is required. Use rollout restart or tools like Reloader for auto-restart.

$ kubectl edit configmap app-config
configmap/app-config edited

$ kubectl rollout restart deployment my-app
deployment.apps/my-app restarted

Index | GitHub | Use arrow keys to navigate |