Using ConfigMaps via Volume Mounts
Environment variables work well for short strings. They break down for multi-line config files. You cannot inject an Nginx configuration block or a JSON settings file as a single env var. Volume mounts solve this: each ConfigMap key becomes a file in a directory inside the container.
Start with a ConfigMap that holds a config file:
nano nginx-config.yamlapiVersion: v1kind: ConfigMapmetadata: name: nginx-configdata: nginx.conf: | server { listen 80; server_name _; location /health { return 200 'ok'; } } log-format: combinedkubectl apply -f nginx-config.yamlMounting all keys as files
A configMap volume type mounts every key as a separate file in the specified directory. The key name becomes the filename. The value becomes the file content.
nano nginx-pod.yamlapiVersion: v1kind: Podmetadata: name: nginx-podspec: containers: - name: nginx image: busybox:1.36 command: ['sh', '-c', 'ls /etc/nginx-config/ && cat /etc/nginx-config/nginx.conf'] volumeMounts: - name: config-vol mountPath: /etc/nginx-config volumes: - name: config-vol configMap: name: nginx-config restartPolicy: Neverkubectl apply -f nginx-pod.yamlkubectl logs nginx-podThe output lists both files: nginx.conf and log-format. Then it prints the full contents of nginx.conf. Each ConfigMap key became a separate file.
Mounting a specific key
Sometimes you want only one file from the ConfigMap, not all of them. Use the items field to select specific keys and map them to specific filenames:
nano nginx-selective.yamlapiVersion: v1kind: Podmetadata: name: nginx-selectivespec: containers: - name: nginx image: busybox:1.36 command: ['sh', '-c', 'ls /etc/nginx/ && cat /etc/nginx/server.conf'] volumeMounts: - name: config-vol mountPath: /etc/nginx volumes: - name: config-vol configMap: name: nginx-config items: - key: nginx.conf path: server.conf restartPolicy: Neverkubectl apply -f nginx-selective.yamlkubectl logs nginx-selectiveOnly one file appears: server.conf. The log-format key was not listed in items, so it was not mounted. The file is named server.conf instead of nginx.conf because the path field controls the destination filename.
A ConfigMap has 5 keys. A Pod mounts it with an items field that lists only 2 of them. How many files appear in the mounted directory?
Reveal answer
2. The items field acts as a filter and a rename map. Only listed keys are projected into the volume. Unlisted keys are not included. This is useful when you want to mount only the relevant config file for a specific container without exposing the rest.
Live updates without restart
When you update a ConfigMap, volume-mounted files are updated automatically. The kubelet periodically refreshes ConfigMap volumes (typically within a minute). The container sees the new file content without being restarted.
Edit the ConfigMap:
kubectl edit configmap nginx-configChange log-format from combined to short. Save and exit. Wait a moment, then exec into the Pod to see the change:
kubectl exec nginx-pod -- cat /etc/nginx-config/log-formatThe file content reflects the updated value. The container did not restart.
This live update behavior applies only to volume-mounted ConfigMaps. Environment variables set from a ConfigMap are fixed at container startup and do not update when the ConfigMap changes. If your application reads configuration from environment variables, a Pod restart is required to pick up new values. If it reads from files, it may pick up changes automatically if it watches the file. Verify your application's config reload behavior before relying on live updates.
kubectl delete pod nginx-pod nginx-selectivekubectl delete configmap nginx-configYou update a ConfigMap value. A Pod using it via envFrom still shows the old value. A second Pod using a volume mount shows the new value. Why the difference?
Reveal answer
Environment variables are set once at container startup from the ConfigMap values at that moment. Updating the ConfigMap does not change running containers’ environment. Volume-mounted ConfigMaps are refreshed periodically by the kubelet, so file content updates without a restart. To update env-var-based configuration, you must restart the Pod.
Volume mounts handle multi-line config files cleanly and support live updates. The next lesson covers immutable ConfigMaps, a performance optimization for high-scale clusters.