Course contentsShow
← Cloud and Infrastructure
Lesson 1251 of 2,98925. Kubernetes Configuration and SecretsFree lesson

Mounting ConfigMaps as Volumes

Mount entire ConfigMaps or specific keys as files inside containers using volume mounts.

Mounting ConfigMaps as Volumes

What you'll learn: How to mount ConfigMap data as files inside your containers using volume mounts, making configuration accessible through the filesystem.

The Core Idea

While environment variables work well for simple key-value pairs, sometimes your applications need configuration as actual files—like nginx.conf, application.properties, or JSON config files. Kubernetes lets you mount an entire ConfigMap (or just specific keys from it) as a volume, placing each key as a separate file in a directory of your choice.

Think of it like attaching a USB drive full of config files to your container. The ConfigMap keys become filenames, and their values become the file contents.

How It Works

You define a volume in your Pod spec that references the ConfigMap, then mount that volume to a path inside your container. Each key in the ConfigMap appears as a file at that path.

Mounting the entire ConfigMap:

apiVersion: v1
kind: Pod
metadata:
  name: config-volume-pod
spec:
  containers:
  - name: app
    image: nginx
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: my-configmap

Inside the container, if your ConfigMap has keys database.conf and app.json, you'll find them as /etc/config/database.conf and /etc/config/app.json.

Mounting specific keys only:

  volumes:
  - name: config-volume
    configMap:
      name: my-configmap
      items:
      - key: database.conf
        path: db.conf

This mounts only the database.conf key as /etc/config/db.conf, letting you rename or select specific configuration pieces.

Key Takeaway: ConfigMap volumes transform configuration data into files inside your containers, perfect for applications that read settings from configuration files rather than environment variables. Each ConfigMap key becomes a readable file at your specified mount path.