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

  1. Define the volume in the Pod's volumes array.
  2. Reference the volume in the container's volumeMounts.
  3. 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 subPath to mount a single file instead of a directory.
  • Ensure the mount path does not overwrite critical container files.
  • Use projected volumes 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.
  • hostPath volumes are tied to a specific node and break Pod portability.