How to mount volumes into Pods?
· Category: Kubernetes
Short answer
Volumes in Kubernetes are defined in the Pod spec and mounted into containers via volumeMounts. Supported volume types include emptyDir, hostPath, configMap, secret, and persistentVolumeClaim.
Steps
- Define the volume in the Pod's
volumesarray. - Reference the volume in the container's
volumeMounts. - Specify the mount path and optionally
readOnly.
Example
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: app
image: myapp
volumeMounts:
- name: data
mountPath: /data
- name: config
mountPath: /etc/config
readOnly: true
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: app-config
Tips
- Use
subPathto mount a single file instead of a directory. - Ensure the mount path does not overwrite critical container files.
- Use
projectedvolumes to combine multiple sources into one mount.
Common issues
- Permission mismatches between volume and container user cause access denied errors.
- Mounting over existing directories hides the original contents.
hostPathvolumes are tied to a specific node and break Pod portability.