Using ConfigMaps with SubPath Mounts
What you'll learn: How to mount individual ConfigMap keys as specific files without replacing an entire directory's contents.
When you mount a ConfigMap as a volume (as covered in earlier lessons), Kubernetes replaces everything in the target directory with the ConfigMap's contents. This can be problematic if you only want to inject one configuration file into a directory that already contains other important files.
SubPath mounts solve this by letting you mount a single key from a ConfigMap to a specific file path, preserving everything else in the directory.
Why Use SubPath?
Think of it like placing a single book on an existing bookshelf without removing the other books. Without subPath, mounting a ConfigMap is like replacing the entire bookshelf with just your new book.
How It Works
In your Pod spec, you reference the ConfigMap as a volume, then use the subPath field in the container's volumeMount to target a specific key:
volumeMounts:
- name: config-volume
mountPath: /etc/myapp/app.conf
subPath: app.conf
volumes:
- name: config-volume
configMap:
name: my-config
Here, only the app.conf key from my-config gets mounted to /etc/myapp/app.conf. Any other files already in /etc/myapp/ remain untouched.
Important Limitation
ConfigMaps mounted with subPath do NOT auto-update. Unlike full volume mounts (which you learned update automatically), subPath mounts are static. If the ConfigMap changes, you must restart the Pod to pick up new values.
Key Takeaway: Use subPath in volumeMounts to inject individual ConfigMap keys as specific files without overwriting existing directory contents, but remember they won't auto-update when the ConfigMap changes.