Sub topic 5
Mounting ConfigMaps as Files
This chapter assumes you can write Pod manifests, use kubectl, and already know how to create a ConfigMap. Environment variables suit simple values. Files suit larger content such as .properties, JSON, or nginx configuration that an application reads from disk.
How a ConfigMap volume works
You declare a volume of type configMap under spec.volumes. You then mount it into a container with volumeMounts. Kubernetes writes each key as a file, and the file contents are the key's value.
Take the ConfigMap app-config with two keys, LOG_LEVEL and app.properties. Mounting the whole ConfigMap at /etc/app creates two files there: LOG_LEVEL and app.properties.
Selecting keys with items
Use items to mount only some keys, or to give a file a different name.
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c", "echo $LOG_LEVEL && cat /etc/app/app.properties && sleep 3600"]
env:
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
name: app-config
key: LOG_LEVEL
volumeMounts:
- name: config
mountPath: /etc/app
readOnly: true
volumes:
- name: config
configMap:
name: app-config
items:
- key: app.properties
path: app.propertiesThis Pod does two things:
- It reads
LOG_LEVELas an environment variable. - It mounts only
app.propertiesas a file at/etc/app/app.properties.
Once items is present, any key you do not list is left out of the volume.
What the mounted directory looks like
Kubernetes does not write the files straight into the mount path. It builds a hidden, timestamped directory and points a symlink named ..data at it. Each visible file is a symlink through ..data. When the ConfigMap changes, the kubelet (the node agent) writes a new directory and swaps the ..data symlink in one step. Readers never see a half-updated set of files.
Mount behavior to know
- The mount hides existing content. If the image already has files in
mountPath, the volume covers them. Mount to a dedicated directory, not/etcor/app. - subPath disables updates. Mounting one key with
subPathlets you drop a single file into an existing directory. AsubPathmount does not receive later ConfigMap updates, because it bypasses the symlink swap. - Permissions default to 0644. Set
defaultModeon the volume, ormodeon an individual item, to change them. In YAML, write octal values with a leading zero, such as0440. - Read-only is sensible. The volume is already managed by the kubelet. Setting
readOnly: truemakes the intent clear. - The ConfigMap must exist. If it is missing, the Pod stays in
ContainerCreatinguntil you create it. Setoptional: trueon the volume source to start without it.
Verifying the mount
kubectl apply -f pod.yaml
kubectl exec app -- ls -la /etc/app
kubectl exec app -- cat /etc/app/app.propertiesThe ls -la output shows app.properties as a symlink to ..data/app.properties.
Exercise
You mount a ConfigMap with subPath: app.properties at /etc/app/app.properties. You then edit the ConfigMap. Will the running container see the new contents, and why?
Show answer
No. A subPath mount bypasses the ..data symlink swap that the kubelet uses to deliver updates, so the file keeps its original contents until the Pod is recreated.