ConfigMaps

A ConfigMap stores non-secret configuration data as key-value pairs, separate from your image.

Hard-coding settings into a container image is a bad idea — you would rebuild the image for every environment. A ConfigMap keeps configuration outside the image so the same image runs anywhere.

What to store

Any non-sensitive setting: feature flags, URLs, log levels, or whole config files. (For passwords and tokens, use a Secret instead.)

Two ways to consume it

  • As environment variables injected into the container.
  • As files mounted into a volume — great for full config files.

Example

Example · yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  LOG_LEVEL: "info"
  APP_MODE: "production"
  welcome.txt: |
    Hello from a mounted config file!
---
apiVersion: v1
kind: Pod
metadata:
  name: configured-app
spec:
  containers:
    - name: app
      image: myapp:1.0
      envFrom:
        - configMapRef:
            name: app-config

When to use it

  • A team stores database hostnames and feature flags in a ConfigMap so the same container image can be deployed to dev, staging, and production without rebuilding.
  • An operator mounts a ConfigMap as a file to supply an Nginx configuration template to a reverse-proxy container.
  • A developer updates a ConfigMap to change an application's log level without redeploying the container image.

More examples

ConfigMap manifest

Creates a ConfigMap with three key-value pairs that can be injected into pods as environment variables or files.

Example · yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  LOG_LEVEL: info
  DB_HOST: postgres-svc
  DB_PORT: "5432"

Inject ConfigMap as env vars

Uses envFrom to bulk-inject all ConfigMap keys as environment variables into the container.

Example · yaml
spec:
  containers:
    - name: app
      image: myapp:1.0
      envFrom:
        - configMapRef:
            name: app-config

Mount ConfigMap as a file

Mounts each ConfigMap key as a separate file under /etc/nginx/conf.d, allowing full config file management via ConfigMap.

Example · yaml
spec:
  containers:
    - name: nginx
      image: nginx:1.25
      volumeMounts:
        - name: nginx-conf
          mountPath: /etc/nginx/conf.d
  volumes:
    - name: nginx-conf
      configMap:
        name: nginx-config

Discussion

  • Be the first to comment on this lesson.