Skip to content
Back to Deployment

Kubernetes Volumes

Persistent and ephemeral storage for pods — PV, PVC, and storage classes.

ContainersAdvanced

Why

K8s volumes provide storage that survives pod restarts (PersistentVolume) or is ephemeral (emptyDir). PVCs abstract storage requests from storage provisioners.

Docker is used by 59% of professional developers per the Stack Overflow 2024 survey, and Kubernetes runs 60% of containerized workloads in production (CNCF 2024 survey). Container orchestration reduces deployment-related incidents by 35% through declarative rollouts.

When

Use PV/PVC for persistent data (DBs, uploads). Use emptyDir for scratch space.

How

1. StorageClass — defines provisioner (EBS, gp3, etc) 2. PVC — request storage, auto-provisions PV 3. Mount — volumeMounts in pod spec 4. Access — ReadWriteOnce, ReadOnlyMany, ReadWriteMany 5. Resize — allowVolumeExpansion: true 6. Snapshot — VolumeSnapshot for backup 7. ephemeral — emptyDir for scratch

Example

# pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: { name: data } spec: accessModes: [ReadWriteOnce] storageClassName: gp3 resources: { requests: { storage: 10Gi } } # deployment.yaml — mount PVC spec: containers: - name: app volumeMounts: - name: data mountPath: /data volumes: - name: data persistentVolumeClaim: { claimName: data } # ephemeral scratch space volumes: - name: scratch emptyDir: { sizeLimit: 1Gi }

Common Problems

  • ⚠PVC pending — no storage class or insufficient capacity, check events
  • ⚠Data lost on pod delete — using emptyDir not PVC, emptyDir is ephemeral
  • ⚠Configuration drift between environments — use infrastructure-as-code (Terraform/Pulumi) to keep dev/staging/prod identical

Best Practices

  • ✓Use PVC for persistent data — survives pod deletion and rescheduling
  • ✓Set storageClassName to fast SSD — gp3 on AWS, premium SSD on Azure
  • ✓Set up health checks and graceful shutdown — handle SIGTERM to drain in-flight requests before exit

FAQ