CKAD — Using ConfigMaps

less than 1 minute read

Using ConfigMaps as Environment Variables

All Keys as Env Vars

apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
  - name: app
    image: busybox:1.36
    command: ["/bin/sh", "-c", "env | sort && sleep 3600"]
    envFrom:
    - configMapRef:
        name: app-config

Specific Keys

apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
  - name: app
    image: busybox:1.36
    command: ["/bin/sh", "-c", "echo $DB_HOST:$DB_PORT && sleep 3600"]
    env:
    - name: DB_HOST
      valueFrom:
        configMapKeyRef:
          name: app-config
          key: DATABASE_HOST
    - name: DB_PORT
      valueFrom:
        configMapKeyRef:
          name: app-config
          key: DATABASE_PORT

Mounting ConfigMaps as Volumes

apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  containers:
  - name: nginx
    image: nginx:1.27
    volumeMounts:
    - name: config-volume
      mountPath: /etc/nginx/conf.d
      readOnly: true
  volumes:
  - name: config-volume
    configMap:
      name: nginx-config

Each key in the ConfigMap becomes a file in the mounted directory. The key is the filename and the value is the file content.

Mount Specific Keys

volumes:
- name: config-volume
  configMap:
    name: app-config
    items:
    - key: nginx.conf
      path: default.conf

This mounts only the nginx.conf key as a file named default.conf.


Next: Secrets →

Updated: