Stage 8, Part 2 of 9, Storage Deep Dive

Text

PART 2: ADVANCED CONCEPTS

7. Volume Snapshots

A VolumeSnapshot is a point-in-time copy of a volume. Snapshots are the foundation for backup, disaster recovery, and cloning.

The Three Objects

ObjectPurpose
VolumeSnapshotClassSnapshot policy (driver, parameters)
VolumeSnapshotThe snapshot itself
VolumeSnapshotContentThe actual snapshot resource

Creating a Snapshot

yaml
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: my-snapshot
spec:
  volumeSnapshotClassName: csi-snapclass
  source:
    persistentVolumeClaimName: my-claim

Restoring from a Snapshot

yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: restored-claim
spec:
  dataSource:
    name: my-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

Snapshot Requirements

Snapshots vs Backups: The Critical Distinction

Snapshots are not backups. Snapshots are based on Copy-on-Write and are stored in the same storage cluster as the original volume.

AspectSnapshotBackup
LocationSame storage clusterDifferent location
ProtectionAgainst accidental deletionAgainst full storage failure
RetentionShort-termLong-term
ToolCSI SnapshotVelero, Restic

Best practice: Use snapshots for fast recovery and backups for data protection.


8. Volume Group Snapshots

Volume Group Snapshots (GA in Kubernetes 1.36) enable atomic snapshots of multiple volumes that need to be consistent.

Why Volume Group Snapshots

For applications with multiple volumes (e.g., a database with data and log volumes), individual snapshots can be inconsistent. Volume Group Snapshots capture all volumes at the same point in time.

Group Snapshot Example

yaml
apiVersion: groupsnapshot.storage.k8s.io/v1
kind: VolumeGroupSnapshot
metadata:
  name: db-group-snapshot
spec:
  volumeGroupSnapshotClassName: csi-group-snapclass
  source:
    selector:
      matchLabels:
        app: database

Use Cases

ApplicationVolumes
PostgreSQLData volume + WAL volume
MySQLData volume + Binlog volume
MongoDBData volume + Journal volume
ElasticsearchData volumes across nodes

9. Volume Expansion

Volume expansion allows you to increase PVC capacity without downtime when the CSI driver and filesystem support it.

Prerequisites

  1. Kubernetes 1.24+
  2. StorageClass with allowVolumeExpansion: true
  3. CSI driver that supports expansion
  4. Supported filesystem (XFS, ext3, ext4)

Enabling Expansion

yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: expandable-storage
provisioner: ebs.csi.aws.com
allowVolumeExpansion: true

Expanding a PVC

bash
# Patch the PVC to request more storage
kubectl patch pvc my-data -p '{"spec":{"resources":{"requests":{"storage":"50Gi"}}}}'

The Expansion Flow

text
PVC Request (Increase Size)
    │
    ▼
Controller Expands Backend Volume
    │
    ▼
CSI Driver Resizes Volume
    │
    ▼
Kubelet Resizes Filesystem
    │
    ▼
Volume Ready with New Size

Online vs Offline Expansion

TypeRequires Pod Restart?Support
Online❌ NoMost modern CSI drivers
Offline✅ YesOlder drivers

Production recommendation: Verify online expansion support before relying on it.


10. Ephemeral Volumes

Ephemeral volumes are temporary volumes that exist only for the lifetime of a Pod. They are not persistent.

The Three Types

TypeManaged ByUse Case
emptyDirKubernetesScratch space, caching
Generic ephemeralKubernetesPer-Pod storage
CSI ephemeralCSI driverInline volumes

emptyDir

yaml
spec:
  containers:
  - name: app
    image: myapp:1.0
    volumeMounts:
    - name: cache
      mountPath: /cache
  volumes:
  - name: cache
    emptyDir:
      sizeLimit: 1Gi

Important: emptyDir is deleted when the Pod is deleted. If the Pod is restarted, emptyDir may or may not be preserved depending on the node.

Generic Ephemeral

yaml
spec:
  volumes:
  - name: scratch
    ephemeral:
      volumeClaimTemplate:
        spec:
          accessModes: ["ReadWriteOnce"]
          storageClassName: fast-ssd
          resources:
            requests:
              storage: 10Gi

The PVC is created automatically and deleted when the Pod is deleted.

CSI Ephemeral

yaml
spec:
  volumes:
  - name: secrets
    csi:
      driver: secrets-store.csi.k8s.io
      readOnly: true
      volumeAttributes:
        secretProviderClass: my-secrets

Use case: Secret injection, configuration injection.


11. Local Volumes

Local Persistent Volumes use node-local storage (SSD, NVMe) for high performance and low latency.

When to Use Local Volumes

ScenarioLocal PV?
High IOPS database✅ Yes
AI/ML model loading✅ Yes
Real-time analytics✅ Yes
Highly available app❌ No
Shared data❌ No

Local PV Example

yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv
spec:
  capacity:
    storage: 100Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /mnt/nvme/data
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node-1

The Node Affinity Requirement

Local PVs must have nodeAffinity to ensure the Pod is scheduled to the correct node.

Local Path Provisioner

For dynamic local PVs, use local-path-provisioner:

yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-path
provisioner: rancher.io/local-path
volumeBindingMode: WaitForFirstConsumer

Local Volume Limitations

LimitationImpact
No HANode failure = data loss
No replicationManual backup required
Node affinityPods must schedule to the right node
No expansionCapacity fixed

12. Volume Cloning

Volume cloning creates a new volume from an existing PVC.

yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: cloned-claim
spec:
  dataSource:
    name: original-claim
    kind: PersistentVolumeClaim
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

Use cases: Testing, development, data analysis on a copy of production.


13. OCI VolumeSource

OCI VolumeSource (GA in Kubernetes 1.36) mounts OCI registry artifacts directly as volumes.

Why OCI VolumeSource

Traditionally, pulling artifacts (models, datasets, configs) required init containers or custom logic. OCI VolumeSource mounts them directly as volumes.

Example

yaml
spec:
  volumes:
  - name: model
    oci:
      image: "registry.example.com/ml-models/bert:latest"
      pullPolicy: IfNotPresent

Use Cases

Use CaseBenefit
AI/ML modelsPull models without init containers
Configuration bundlesVersioned configs via OCI
Static assetsMount frontend builds
WebAssembly modulesDistribute WASM via OCI

14. Dynamic Resource Allocation (DRA)

DRA (GA in Kubernetes 1.36) is a new API for requesting arbitrary resources (GPUs, FPGAs, specialized hardware) with more flexibility than device plugins.

DRA for Storage

DRA can be used for:

DRA Example

yaml
apiVersion: resource.k8s.io/v1
kind: ResourceClaim
metadata:
  name: my-claim
spec:
  devices:
    requests:
    - name: nvme
      deviceClassName: high-iops-nvme
      count: 1

15. Mount Options and Performance Tuning

Mount options significantly impact storage performance.

Common Mount Options

OptionEffectUse Case
noatimeDon't update access timeRead-heavy workloads
nodiratimeDon't update directory access timeSame as noatime
nconnectMultiple TCP connections (NFS)High throughput
rsize / wsizeRead/write size (NFS)Performance tuning
actimeoAttribute cache timeoutMetadata-heavy workloads
nodiratimeNo directory atimePerformance

StorageClass with Mount Options

yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: high-perf
provisioner: file.csi.azure.com
parameters:
  protocol: nfs
mountOptions:
- nconnect=4
- rsize=262144
- wsize=262144
- actimeo=30

Performance Tuning by Workload

WorkloadMount Options
Databasenoatime, nodiratime
NFS file sharenconnect=4, rsize=256K, wsize=256K
Read-only contentnoatime, ro
High-throughputnconnect=8

1:1 copy of the class Google Doc, code in code blocks.