Snapshots & Cloning

Volume snapshots capture the state of a PersistentVolume at a point in time, allowing you to create backups, restore data, or clone volumes for testing. Volume cloning creates a new volume from an existing volume or snapshot, useful for quickly provisioning similar storage environments.

Volume Snapshots

A volume snapshot represents a point-in-time copy of the data in a PersistentVolume. Snapshots are created using the VolumeSnapshot API and can be used to restore the original volume or create new volumes from the snapshot.

graph TB A[PersistentVolume] --> B[Create Snapshot] B --> C[VolumeSnapshot] C --> D[Snapshot Storage] C --> E[Restore to Original Volume] C --> F[Clone to New Volume] style A fill:#e1f5ff style C fill:#fff4e1 style D fill:#e8f5e9 style E fill:#f3e5f5 style F fill:#ffe1e1

VolumeSnapshot API

Volume snapshots use Kubernetes’ VolumeSnapshot API, which is part of the snapshot.storage.k8s.io API group. This API requires a CSI driver that supports snapshots and a snapshot controller running in your cluster.

Key resources:

  • VolumeSnapshot - Represents a snapshot of a volume
  • VolumeSnapshotClass - Defines snapshot parameters (similar to StorageClass)
  • VolumeSnapshotContent - Represents the actual snapshot in the storage system

Creating a Snapshot

To create a snapshot, you need:

  1. A VolumeSnapshotClass (defines how snapshots are created)
  2. A VolumeSnapshot (references a PVC and VolumeSnapshotClass)

Example VolumeSnapshotClass:

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
  name: csi-snapshot-class
driver: pd.csi.storage.gke.io  # CSI driver name
deletionPolicy: Delete  # Delete snapshot when VolumeSnapshot is deleted

Example VolumeSnapshot:

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: db-snapshot
spec:
  volumeSnapshotClassName: csi-snapshot-class
  source:
    persistentVolumeClaimName: database-pvc  # Snapshot this PVC

Snapshot Lifecycle

sequenceDiagram participant User participant K8s as Kubernetes participant SC as Snapshot Controller participant Driver as CSI Driver participant Storage as Storage System User->>K8s: Create VolumeSnapshot K8s->>SC: Create snapshot SC->>Driver: CreateSnapshot Driver->>Storage: Create snapshot Storage->>Driver: Snapshot created Driver->>SC: Snapshot ready SC->>K8s: VolumeSnapshot ready K8s->>User: Snapshot available

Restoring from a Snapshot

You can restore a volume from a snapshot by creating a new PVC that references the snapshot:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: restored-db-pvc
spec:
  dataSource:
    name: db-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
    - ReadWriteOnce
  storageClassName: fast-ssd
  resources:
    requests:
      storage: 100Gi  # Must be >= original volume size

The restored volume contains the data from the snapshot at the time it was created.

Volume Cloning

Volume cloning creates a new volume from an existing volume or snapshot. Cloning is faster than creating a snapshot and restoring, but requires both volumes to exist simultaneously.

Clone from Volume

Create a new PVC that references an existing PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: cloned-pvc
spec:
  dataSource:
    name: source-pvc  # Clone from this PVC
    kind: PersistentVolumeClaim
  accessModes:
    - ReadWriteOnce
  storageClassName: fast-ssd
  resources:
    requests:
      storage: 100Gi  # Must be >= source volume size

Important: The source PVC must be bound and in use for cloning to work. Some CSI drivers support cloning from idle PVCs.

Clone from Snapshot

Create a new PVC from a VolumeSnapshot:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: cloned-from-snapshot
spec:
  dataSource:
    name: db-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
    - ReadWriteOnce
  storageClassName: fast-ssd
  resources:
    requests:
      storage: 100Gi

Snapshot vs Cloning Comparison

graph TB subgraph snapshot[Snapshot] A[Create Snapshot] --> B[Snapshot Stored] B --> C[Original Volume Unchanged] B --> D[Can Restore Later] B --> E[Can Clone Multiple Times] end subgraph cloning[Direct Cloning] F[Clone from PVC] --> G[New Volume Created] G --> H[Original Volume Must Exist] G --> I[Faster Than Snapshot+Restore] end style B fill:#fff4e1 style G fill:#e8f5e9

Snapshots:

  • Point-in-time copies
  • Original volume unchanged
  • Can be stored separately
  • Useful for backups
  • Can create multiple clones

Cloning:

  • Direct copy of current state
  • Faster than snapshot + restore
  • Requires source volume to exist
  • Useful for quick provisioning
  • Single clone operation

Complete Example: Database Backup and Restore

Here’s a complete example showing how to backup and restore a database:

1. Create a snapshot:

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
  name: db-snapshot-class
driver: pd.csi.storage.gke.io
deletionPolicy: Retain  # Keep snapshot even if VolumeSnapshot is deleted
---
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: db-backup-2024-01-15
spec:
  volumeSnapshotClassName: db-snapshot-class
  source:
    persistentVolumeClaimName: postgres-data

2. Restore from snapshot:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-data-restored
spec:
  dataSource:
    name: db-backup-2024-01-15
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
    - ReadWriteOnce
  storageClassName: fast-ssd
  resources:
    requests:
      storage: 200Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-restored
spec:
  template:
    spec:
      containers:
      - name: postgres
        image: postgres:14
        volumeMounts:
        - name: data
          mountPath: /var/lib/postgresql/data
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: postgres-data-restored

Use Cases

Backup and Recovery

Create regular snapshots for backup purposes:

apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
  name: daily-backup-$(date +%Y%m%d)
spec:
  volumeSnapshotClassName: backup-snapshot-class
  source:
    persistentVolumeClaimName: critical-data

Testing and Development

Clone production data for testing:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-db-data
spec:
  dataSource:
    name: prod-db-snapshot
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
  accessModes:
    - ReadWriteOnce
  storageClassName: standard
  resources:
    requests:
      storage: 100Gi

Data Migration

Use snapshots to migrate data between clusters or storage systems:

  1. Create snapshot in source cluster
  2. Export snapshot data
  3. Import snapshot in destination cluster
  4. Create PVC from snapshot

Snapshot Deletion Policy

VolumeSnapshotClass has a deletionPolicy field that controls what happens when a VolumeSnapshot is deleted:

  • Delete - The snapshot in the storage system is deleted when the VolumeSnapshot is deleted
  • Retain - The snapshot in the storage system is retained when the VolumeSnapshot is deleted (manual cleanup required)
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
  name: backup-snapshot-class
driver: pd.csi.storage.gke.io
deletionPolicy: Retain  # Keep snapshot data even if VolumeSnapshot deleted

Checking Snapshot Status

Check snapshot status and details:

# List snapshots
kubectl get volumesnapshot

# Get snapshot details
kubectl describe volumesnapshot db-snapshot

# Check snapshot ready status
kubectl get volumesnapshot db-snapshot -o jsonpath='{.status.readyToUse}'

Best Practices

  1. Regular snapshots - Create snapshots on a schedule for backups
  2. Test restores - Regularly test restoring from snapshots
  3. Use appropriate deletion policy - Retain for backups, Delete for temporary snapshots
  4. Monitor snapshot storage - Snapshots consume storage space
  5. Document snapshot procedures - Document how to create and restore snapshots
  6. Use cloning for quick provisioning - Use cloning when you need copies quickly
  7. Verify CSI driver support - Ensure your CSI driver supports snapshots/cloning
  8. Size cloned volumes correctly - Cloned volumes must be >= source volume size

Limitations and Considerations

  • CSI driver support - Not all CSI drivers support snapshots and cloning
  • Storage costs - Snapshots consume storage space
  • Performance impact - Creating snapshots may have a performance impact
  • Size requirements - Cloned volumes must be at least as large as the source
  • Consistency - Snapshots capture data at a point in time; application consistency may require coordination

Troubleshooting

Snapshot Stuck in Pending

If a snapshot is stuck in Pending:

  1. Check VolumeSnapshotClass exists: kubectl get volumesnapshotclass
  2. Verify CSI driver supports snapshots
  3. Check snapshot controller is running: kubectl get pods -n kube-system | grep snapshot
  4. Review VolumeSnapshot events: kubectl describe volumesnapshot <name>

Clone Fails

If cloning fails:

  1. Verify source PVC is bound: kubectl get pvc <source-pvc>
  2. Check if CSI driver supports cloning
  3. Ensure cloned volume size >= source volume size
  4. Review PVC events: kubectl describe pvc <cloned-pvc>

See Also