Kubernetes : Volumes

local

Compared to hostPath volumes, local volumes are used in a durable and portable manner without manually scheduling pods to nodes. The system is aware of the volume's node constraints by looking at the node affinity on the PersistentVolume. Both local and hostPath are analogous to Docker bind mount.

Where possible, use local instead of hostPath

local @ PersistentVolume manifest

apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv
spec:
  capacity:
    storage: 100Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  # Any name here okay
  storageClassName: local-storage
    # Advised for local
    volumeBindingMode: WaitForFirstConsumer
  local:
    path: /mnt/disks/ssd1
  # MUST use nodeAffinity
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - example-node

hostPath

HostPath is the Kubenetes equivalent to Docker's bind mount. Must restrict pod to same node (host) as the volume.

hostPath @ Pod manifest

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  nodeName: specific-node-name
  containers:
    - name: my-container
      image: your-container-image
      volumeMounts:
        - name: host-path-volume
          mountPath: /path/in/container  # Mount path inside the container
  volumes:
    - name: host-path-volume
      hostPath:
        path: /path/on/host  # Path on the specific node you want to bind