DevOps Peach

Docker & Kubernetes · Kubernetes Storage and Configuration – Volumes, PV/PVC, ConfigMaps, Secrets · Lesson 3 of 4

StorageClasses and Volume Snapshots

StorageClasses let PVs be created automatically by storage "type"; reclaimPolicy decides the fate of the real disk; snapshots need a CSI driver.

Practice interactively →

A StorageClass describes a "type" of storage an admin offers (SSD, HDD, backed up…). A PVC only sets storageClassName (or uses the default class), and when no PV exists the provisioner creates a new one — dynamic provisioning. Apps therefore do not depend on a specific cloud.

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gp3
  annotations: { storageclass.kubernetes.io/is-default-class: "true" }
provisioner: ebs.csi.aws.com
parameters: { type: gp3 }
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
flowchart LR
  C["PVC (storageClassName: gp3)"] --> SC["StorageClass gp3"]
  SC -- "provisioner creates" --> PV["New PV"]
  PV --> D[("EBS gp3")]
Field Meaning
provisioner The CSI driver creating volumes (for example ebs.csi.aws.com)
parameters Driver-specific settings (disk type, IOPS…)
reclaimPolicy Delete (default for dynamic): deleting the PVC deletes the real disk; Retain: keep the disk for an admin to handle
volumeBindingMode WaitForFirstConsumer waits until the pod is scheduled to create the disk in the right AZ

Volume Snapshots

VolumeSnapshot (the request, namespaced) and VolumeSnapshotContent (the real snapshot, cluster-scoped) mirror the PVC/PV pair for snapshots. The source is persistentVolumeClaimName. Snapshots only work when the cluster has the snapshot CRDs + snapshot controller and a CSI driver that supports snapshots; old in-tree plugins cannot.

Command Meaning
kubectl get storageclass Which classes exist and which is (default)
kubectl get volumesnapshot -n dev Snapshots of PVCs in dev

Classes you will see in managed clusters

Cloud Example classes
EKS gp2 (older default) and custom gp3 classes with the EBS CSI driver; efs-sc for shared files
AKS managed-csi (Standard SSD), managed-csi-premium, azurefile-csi
GKE standard-rwo, premium-rwo (Persistent Disk), Filestore classes

kubectl get storageclass shows which one is marked (default); a PVC without storageClassName uses it. Only one class should be the default.

Useful extra fields

Taking and restoring a snapshot

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata: { name: data-snap-1, namespace: dev }
spec:
  volumeSnapshotClassName: csi-snapclass
  source: { persistentVolumeClaimName: data }
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata: { name: data-restored, namespace: dev }
spec:
  dataSource: { name: data-snap-1, kind: VolumeSnapshot, apiGroup: snapshot.storage.k8s.io }
  accessModes: [ReadWriteOnce]
  resources: { requests: { storage: 3Gi } }

A VolumeSnapshotClass plays the role of a StorageClass for snapshots and sets the driver and a deletion policy. A PVC with dataSource can also clone another PVC directly.

Snapshots are not a full backup plan

A crash-consistent snapshot may catch a database mid-write; quiesce the app or use the database's own backup tool for consistency. Snapshots usually live in the same cloud account and region as the disk. Tools such as Velero back up Kubernetes objects and volume snapshots together and can restore a whole namespace into another cluster, which covers disaster recovery and migrations.

Practice interactively →