Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Configuration Management  ›  Lesson

ConfigMaps as Files

Injecting Pod Config with ConfigMaps 12 minMounting a ConfigMap as a volume turns each key into a file, and those files are refreshed after edits unless mounted with subPath.

ConfigMaps as Files

Some apps do not read environment variables at all. They expect a configuration file, such as an nginx.conf or an app.yaml. A ConfigMap can deliver whole files too — and unlike env, those files can change while the pod runs.

The rule

Mount a ConfigMap and every key becomes a file.

Put a whole file's text under one key using a YAML block (|):

apiVersion: v1
kind: ConfigMap
metadata:
  name: notes-web-files
  namespace: notes
data:
  app.yaml: |
    theme: dark
    max_note_length: 5000

Then mount it as a volume:

spec:
  containers:
    - name: web
      image: ghcr.io/example/notes-web:1.2
      volumeMounts:
        - name: config
          mountPath: /etc/notes
  volumes:
    - name: config
      configMap:
        name: notes-web-files

Inside the container, the file /etc/notes/app.yaml now exists with that text. Every key becomes one file named after the key.

A mounted directory replaces whatever the image had at that path. Mount into an empty or dedicated folder.

When the ConfigMap is edited, mounted files are refreshed automatically after a short delay (usually within a minute or two). Two exceptions:

  • files mounted with subPath are never refreshed;
  • the app itself must notice and reread the file — many only read config at start.

You can also build a ConfigMap straight from files on disk:

kubectl create configmap notes-web-files --from-file=app.yaml -n notes

ConfigMaps are meant to be small: the total is limited to 1 MiB.

The shortcut

Choose by how the app reads settings: individual variables → env; a whole file → a volume. Then ask "does it need live changes?" — only a mounted volume without subPath can give them.

Giving notes-web a theme fileStep 1 of 4
  1. How does it read?

    notes-web loads /etc/notes/app.yaml. That is a file, so use a volume.

The trap is mounting over a folder the app needs, such as /etc, which hides every other file the image put there.

Drills

Answer fast — trust the rule.

quick drill

A ConfigMap with keys app.yaml and banner.txt is mounted at /etc/notes. What appears there?

quick drill

The ConfigMap is edited. Which mounted file is never refreshed?

quick drill

The image ships files in /etc/notes. You mount a ConfigMap at /etc/notes. What happens to the image's files?

quick drill

An app only reads its config file at startup. The mounted file changes. What else is needed?

Remember

  • Mount a ConfigMap and every key becomes a file.
  • Variables → env; whole files or live changes → volume.
  • Trap: mounts hide the image's folder, and subPath mounts never refresh.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainEnv or volume?

notes-web reads a theme from /etc/notes/app.yaml and the team wants to change the theme without redeploying. Explain how you would deliver the file, what happens when the ConfigMap changes, and one pitfall to avoid.