CodeBlanks Logo Icon CodeBlanks

Mounting as a volume

Learn to mount a ConfigMap as a volume.

Mounting as a volume

When a ConfigMap is mounted as a volume, each key becomes a file in the mount directory. It's ideal for applications expecting a config file (nginx, Prometheus):

    spec:
      volumes:
        - name: ayar
          configMap:
            name: nginx-ayar
      containers:
        - name: nginx
          image: nginx:1.27
          volumeMounts:
            - name: ayar
              mountPath: /etc/nginx/conf.d
              readOnly: true

When a ConfigMap mounted as a volume is updated, the files refresh by themselves within a few minutes (unlike environment variables).

Mounting a single file

Mounting at a directory hides the existing files there. To place only a single file, subPath is used; however, a file mounted with subPath isn't refreshed when the ConfigMap is updated:

          volumeMounts:
            - name: ayar
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf

With items, only specific keys of the ConfigMap can be mounted, under the wanted file names.

Practice Mount the config file

Write a pod's spec section (starting with spec:): an ayar volume from the nginx-ayar ConfigMap under volumes; container named nginx, image nginx:1.27; mount ayar at /etc/nginx/conf.d with readOnly: true.

Starter code

terminal
# buraya yaz

Sample solution

terminal
spec:
  volumes:
    - name: ayar
      configMap:
        name: nginx-ayar
  containers:
    - name: nginx
      image: nginx:1.27
      volumeMounts:
        - name: ayar
          mountPath: /etc/nginx/conf.d
          readOnly: true

Expected output

pod/web configured
$ kubectl exec web -- ls /etc/nginx/conf.d
default.conf

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If the nginx-ayar ConfigMap has a default.conf key, which file appears in the container?

  • /etc/nginx/conf.d/default.conf Correct answer
  • /etc/nginx/nginx-ayar
  • /configmap/default.conf
  • None

Explanation: The key becomes the file name, the value its content.

2 Mount to a single file, not a directory.

Question
            - name: ayar
              mountPath: /etc/nginx/nginx.conf
              ____: nginx.conf
Answer
            - name: ayar
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf

Explanation: subPath selects a single path inside the volume.

3 Match.

Term Meaning
volumes.configMap ConfigMap source
volumeMounts Mount location
subPath Single file, not refreshed
readOnly: true Read-only

All lessons in Intermediate