Kubernetes Cheatsheet

Volumes

Use this Kubernetes reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

Volume Types Quick Reference

TypeLifecycleUse case
emptyDirPod lifetimeScratch space, cache, inter-container sharing
hostPathNode lifetimeNode-local files, DaemonSets
configMap / secretPod lifetimeInject config files
persistentVolumeClaimIndependentDurable storage (databases, uploads)
projectedPod lifetimeCombine multiple sources into one mount
downwardAPIPod lifetimeExpose Pod metadata as files
nfsExternalShared read-write across Pods
csiExternalCloud block/file storage via CSI drivers

emptyDir

Created when Pod is scheduled; deleted when Pod is removed.

spec:
  volumes:
  - name: cache
    emptyDir:
      medium: ""          # "" = disk, "Memory" = tmpfs
      sizeLimit: 500Mi
  containers:
  - name: app
    volumeMounts:
    - name: cache
      mountPath: /tmp/cache

hostPath

Mounts a path from the Node's filesystem. Use sparingly; breaks Pod portability.

spec:
  volumes:
  - name: docker-sock
    hostPath:
      path: /var/run/docker.sock
      type: Socket    # File | Directory | Socket | CharDevice | BlockDevice

Persistent Volume (PV) and Persistent Volume Claim (PVC)

Admin creates PV  ──►  Developer creates PVC  ──►  Pod uses PVC

PersistentVolume (admin-managed)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-data
spec:
  capacity:
    storage: 20Gi
  accessModes:
  - ReadWriteOnce
  reclaimPolicy: Retain       # Retain | Recycle | Delete
  storageClassName: standard
  hostPath:                   # use cloud-specific driver in production
    path: /mnt/data

PersistentVolumeClaim (developer-facing)

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  storageClassName: standard  # must match PV or StorageClass

Use PVC in a Pod

spec:
  volumes:
  - name: data
    persistentVolumeClaim:
      claimName: my-pvc
  containers:
  - name: db
    image: postgres:16
    volumeMounts:
    - name: data
      mountPath: /var/lib/postgresql/data

Access Modes

ModeShortMeaning
ReadWriteOnceRWOR/W by a single Node
ReadOnlyManyROXRead-only by many Nodes
ReadWriteManyRWXR/W by many Nodes (NFS, EFS, GCS)
ReadWriteOncePodRWOPR/W by a single Pod (k8s 1.22+)

StorageClass (dynamic provisioning)

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: fast
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"
provisioner: ebs.csi.aws.com
parameters:
  type: gp3
  iops: "3000"
  throughput: "125"
reclaimPolicy: Delete
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer

When a PVC references a StorageClass, a PV is automatically provisioned.

Expand a PVC

# StorageClass must have allowVolumeExpansion: true
kubectl patch pvc my-pvc -p '{"spec":{"resources":{"requests":{"storage":"20Gi"}}}}'
kubectl get pvc my-pvc   # watch status.capacity

Projected Volume

Combine ConfigMap, Secret, ServiceAccountToken, and DownwardAPI into one mount:

spec:
  volumes:
  - name: combined
    projected:
      sources:
      - configMap:
          name: app-config
      - secret:
          name: db-secret
      - serviceAccountToken:
          path: token
          expirationSeconds: 3600

DownwardAPI

Expose Pod/container metadata as files:

spec:
  volumes:
  - name: podinfo
    downwardAPI:
      items:
      - path: "namespace"
        fieldRef:
          fieldPath: metadata.namespace
      - path: "cpu_limit"
        resourceFieldRef:
          containerName: app
          resource: limits.cpu

kubectl Volume Commands

CommandWhat it does
kubectl get pvList PersistentVolumes
kubectl get pvcList PersistentVolumeClaims
kubectl get scList StorageClasses
kubectl describe pvc my-pvcShow binding status, events
kubectl delete pvc my-pvcDelete PVC (PV fate = reclaimPolicy)
kubectl patch pvc my-pvc ...Resize PVC