Working with Volumes

1. Using emptyDir Volumes

volumes:
- name: cache
  emptyDir:
    medium: Memory      # tmpfs (RAM)
    sizeLimit: 1Gi
PropertyDetail
LifetimePod (deleted with pod)
medium"" (disk) or Memory (tmpfs)
UseScratch space, sidecar sharing

2. Using hostPath Volumes

volumes:
- name: docker-sock
  hostPath:
    path: /var/run/docker.sock
    type: Socket
typeCheck
DirectoryOrCreateCreate if missing
DirectoryMust exist
FileOrCreate / FileSame for files
SocketUnix socket
Warning: hostPath bypasses pod isolation; restrict via Pod Security Standards. Avoid in multi-tenant clusters.

3. Configuring PersistentVolumes

apiVersion: v1
kind: PersistentVolume
metadata: { name: pv-data-1 }
spec:
  capacity: { storage: 100Gi }
  accessModes: [ReadWriteOnce]
  persistentVolumeReclaimPolicy: Retain
  storageClassName: gp3
  csi:
    driver: ebs.csi.aws.com
    volumeHandle: vol-0abc1234
SourceExamples
CSIAWS EBS, GCE PD, Azure Disk, Ceph RBD
NFSnfs: { server, path }
Localnode-local SSD

4. Creating PersistentVolumeClaims

apiVersion: v1
kind: PersistentVolumeClaim
metadata: { name: data }
spec:
  accessModes: [ReadWriteOnce]
  storageClassName: gp3
  resources: { requests: { storage: 50Gi } }
FieldDetail
storageClassNameDynamic provisioning class
accessModesSubset of PV's modes
volumeNameBind to specific PV (skip class)

5. Binding PVC to PV

Binding Lifecycle

  1. PVC created → controller searches matching PV (size, class, modes)
  2. No match + StorageClass → dynamic provisioner creates PV
  3. PV bound; claimRef set; status Bound
  4. Pod consumes via persistentVolumeClaim.claimName

6. Listing PersistentVolumes

kubectl get pv
kubectl get pv --sort-by=.spec.capacity.storage
StatusMeaning
AvailableFree for binding
BoundIn use by PVC
ReleasedPVC deleted (waiting reclaim)
FailedReclaim failed

7. Listing PersistentVolumeClaims

kubectl get pvc -A
kubectl get pvc -o wide   # shows bound PV
StatusMeaning
PendingNo PV match / provisioner working
BoundLinked to a PV
LostUnderlying PV gone

8. Understanding Access Modes

ModeMeaning
ReadWriteOnce (RWO)R/W by single node
ReadOnlyMany (ROX)RO by many nodes
ReadWriteMany (RWX)R/W by many nodes (NFS, CephFS, EFS)
ReadWriteOncePod NEWR/W by single pod (1.27+)

9. Setting Reclaim Policies

persistentVolumeReclaimPolicyEffect on PVC delete
RetainPV stays; manual cleanup
DeletePV + underlying volume deleted (dynamic default)
RecycleDEPRECATED

10. Using Dynamic Provisioning

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata: { name: gp3 }
provisioner: ebs.csi.aws.com
parameters: { type: gp3, encrypted: "true" }
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Delete
allowVolumeExpansion: true

11. Expanding PersistentVolumes

kubectl patch pvc data -p '{"spec":{"resources":{"requests":{"storage":"100Gi"}}}}'
RequirementDetail
StorageClassallowVolumeExpansion: true
Online expansionSupported by CSI driver (EBS, GCE PD)
ShrinkingNot supported

12. Deleting PersistentVolumeClaims

kubectl delete pvc data
kubectl delete pv pv-data-1   # only after PVC released & reclaim done
Warning: Default StorageClasses use Delete reclaim — deleting PVC destroys underlying disk. Switch to Retain for production data.