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
| Object | Purpose |
| VolumeSnapshotClass | Snapshot policy (driver, parameters) |
| VolumeSnapshot | The snapshot itself |
| VolumeSnapshotContent | The 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
- Kubernetes 1.28+
- Snapshot Controller installed
- Snapshot CRDs installed
- CSI driver with snapshot support
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.
| Aspect | Snapshot | Backup |
| Location | Same storage cluster | Different location |
| Protection | Against accidental deletion | Against full storage failure |
| Retention | Short-term | Long-term |
| Tool | CSI Snapshot | Velero, 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
| Application | Volumes |
| PostgreSQL | Data volume + WAL volume |
| MySQL | Data volume + Binlog volume |
| MongoDB | Data volume + Journal volume |
| Elasticsearch | Data 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
- Kubernetes 1.24+
- StorageClass with
allowVolumeExpansion: true - CSI driver that supports expansion
- 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
| Type | Requires Pod Restart? | Support |
| Online | ❌ No | Most modern CSI drivers |
| Offline | ✅ Yes | Older 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
| Type | Managed By | Use Case |
| emptyDir | Kubernetes | Scratch space, caching |
| Generic ephemeral | Kubernetes | Per-Pod storage |
| CSI ephemeral | CSI driver | Inline 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
| Scenario | Local 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
| Limitation | Impact |
| No HA | Node failure = data loss |
| No replication | Manual backup required |
| Node affinity | Pods must schedule to the right node |
| No expansion | Capacity 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 Case | Benefit |
| AI/ML models | Pull models without init containers |
| Configuration bundles | Versioned configs via OCI |
| Static assets | Mount frontend builds |
| WebAssembly modules | Distribute 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:
- NVMe devices with specific performance characteristics
- Persistent memory (PMEM)
- Hardware accelerators with storage requirements
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
| Option | Effect | Use Case |
| noatime | Don't update access time | Read-heavy workloads |
| nodiratime | Don't update directory access time | Same as noatime |
| nconnect | Multiple TCP connections (NFS) | High throughput |
| rsize / wsize | Read/write size (NFS) | Performance tuning |
| actimeo | Attribute cache timeout | Metadata-heavy workloads |
| nodiratime | No directory atime | Performance |
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
| Workload | Mount Options |
| Database | noatime, nodiratime |
| NFS file share | nconnect=4, rsize=256K, wsize=256K |
| Read-only content | noatime, ro |
| High-throughput | nconnect=8 |
1:1 copy of the class Google Doc, code in code blocks.