Working with Volume Snapshots
1. Installing VolumeSnapshot CRDs
| Resource | Description | Installation Method | Notes |
|---|---|---|---|
| VolumeSnapshotClass | Defines a class of snapshot storage with parameters for provisioning snapshots | kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/v6.2.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml |
Required for snapshot creation |
| VolumeSnapshot | Represents a snapshot of a volume to be created or restored from | kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/v6.2.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yaml |
Used to request a snapshot of a PersistentVolumeClaim |
| VolumeSnapshotContent | Represents a snapshot that has been created in the storage backend | kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/v6.2.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml |
Created by the CSI driver when a snapshot is taken |
Example: Apply VolumeSnapshot CRDs
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/v6.2.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/v6.2.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/external-snapshotter/v6.2.0/client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml
Note: Ensure that the CSI driver supports volume snapshots before applying these CRDs.
2. Creating VolumeSnapshotClass
| Field | Description | Required | Example Value |
|---|---|---|---|
| apiVersion | API version of the VolumeSnapshotClass resource | Yes | snapshot.storage.k8s.io/v1 |
| kind | Kind of the Kubernetes resource | Yes | VolumeSnapshotClass |
| metadata.name | Name of the VolumeSnapshotClass | Yes | fast-ssd-snapshot-class |
| driver | CSI driver name that handles snapshots for this class | Yes | aws-ebs.csi.aws.com |
| deletionPolicy | Policy for handling snapshots when the VolumeSnapshot is deleted | Yes | Retain or Delete |
| parameters | Driver-specific parameters for snapshot creation | No | { "type": "gp2" } |
Example: VolumeSnapshotClass YAML Definition
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: fast-ssd-snapshot-class
driver: aws-ebs.csi.aws.com
deletionPolicy: Retain
parameters:
type: gp2
Note: The
deletionPolicy field determines whether the actual snapshot in the storage backend is deleted when the Kubernetes VolumeSnapshot object is deleted. Retain keeps the snapshot, while Delete deletes it.
3. Creating VolumeSnapshot
| Field | Description | Required | Example Value |
|---|---|---|---|
| apiVersion | API version of the VolumeSnapshot resource | Yes | snapshot.storage.k8s.io/v1 |
| kind | Kind of the resource (must be VolumeSnapshot) | Yes | VolumeSnapshot |
| metadata.name | Name of the VolumeSnapshot | Yes | my-snapshot |
| spec.source | Source of the VolumeSnapshot (PersistentVolumeClaim or VolumeSnapshotContent) | Yes | |
| spec.volumeSnapshotClassName | Name of the VolumeSnapshotClass to use for creating the snapshot | No | my-snapshot-class |
Example: Creating a VolumeSnapshot from a PVC
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: my-snapshot
spec:
source:
persistentVolumeClaimName: my-pvc
Note: The VolumeSnapshot must reference a valid PersistentVolumeClaim that supports snapshots.
4. Listing VolumeSnapshots
| Field | Description | Example Value |
|---|---|---|
| apiVersion | API version of the VolumeSnapshot resource | snapshot.storage.k8s.io/v1 |
| kind | Kind of the resource (must be VolumeSnapshot) | VolumeSnapshot |
| metadata.name | Name of the VolumeSnapshot | my-snapshot |
| spec.source.persistentVolumeClaimName | Name of the PVC from which the snapshot is created | my-pvc |
| status.readyToUse | Indicates whether the snapshot is ready to use | true |
| status.restoreSize | Size of the volume when restored from this snapshot | 10Gi |
Example: Listing VolumeSnapshots in a specific namespace
kubectl get volumesnapshots -n my-namespace
5. Describing VolumeSnapshot Details
| Field | Description | Example Value |
|---|---|---|
| apiVersion | The API version of the VolumeSnapshot resource | snapshot.storage.k8s.io/v1 |
| kind | The type of the Kubernetes resource | VolumeSnapshot |
| metadata.name | The name of the VolumeSnapshot resource | my-snapshot |
| metadata.namespace | The namespace where the VolumeSnapshot is created | default |
| spec.source.volumeSnapshotContentName | The name of the VolumeSnapshotContent that this snapshot references | snapshot-content-12345 |
| status.boundVolumeSnapshotContentName | The name of the VolumeSnapshotContent that this snapshot is bound to | snapshot-content-12345 |
| status.createtime | The timestamp when the VolumeSnapshot was created | 2025-04-05T10:00:00Z |
| status.readyToUse | Indicates whether the snapshot is ready to be used | true |
| status.restoreSize | The size of the volume that was snapshotted | 10Gi |
Example: Describe VolumeSnapshot in YAML
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: my-snapshot
namespace: default
spec:
source:
volumeSnapshotContentName: snapshot-content-12345
status:
boundVolumeSnapshotContentName: snapshot-content-12345
createtime: "2025-04-05T10:00:00Z"
readyToUse: true
restoreSize: "10Gi"
6. Restoring PVC from Snapshot
| Field | Description | Example Value |
|---|---|---|
| apiVersion | API version of the VolumeSnapshotContent resource | snapshot.storage.k8s.io/v1 |
| kind | Kind of the resource | VolumeSnapshotContent |
| metadata.name | Name of the VolumeSnapshotContent | snapshot-content-1 |
| spec.source.volumeSnapshotContentName | Name of the VolumeSnapshotContent to restore from | snapshot-content-1 |
| spec.volumeSnapshotRef.name | Name of the VolumeSnapshot object | my-snapshot |
Example: Restoring PVC from VolumeSnapshot
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotContent
metadata:
name: snapshot-content-1
spec:
source:
volumeSnapshotContentName: snapshot-content-1
volumeSnapshotRef:
name: my-snapshot
Note: Ensure that the VolumeSnapshotContent is compatible with the storage class used by the PVC.
Warning: Restoring from a snapshot may result in data loss if the original PVC was modified after the snapshot was created.
7. Configuring Snapshot Deletion Policy
| Policy | Description | Configuration | Notes |
|---|---|---|---|
| retention | Number of snapshots to retain | 10 |
Applies to all volumes in the namespace |
| autoDelete | Automatically delete oldest snapshots | true |
When retention limit is exceeded |
| schedule | Cron expression for snapshot creation | 0 0 * * * |
Runs daily at midnight |
Example: Snapshot Deletion Policy Configuration
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: example-snapshot-class
deletionPolicy: Retain
parameters:
retention: "5"
autoDelete: "true"
schedule: "0 0 * * *"
Note: The
deletionPolicy field controls whether snapshots are deleted when the VolumeSnapshot is deleted.Warning: Setting
autoDelete to true can lead to data loss if not properly configured.8. Using Snapshot with CSI Drivers
| Property | Description | Values | Notes |
|---|---|---|---|
| driver | CSI driver name | aws-ebs.csi.aws.com |
Must be registered in the cluster |
| snapshotHandle | Unique identifier for the snapshot | snapshot-1234567890abcdef0 |
Provided by CSI driver |
| creationTime | Timestamp of snapshot creation | 2024-05-15T10:30:00Z |
ISO 8601 format |
| readyToUse | Indicates if snapshot is ready for use | true or false |
Used in volume creation |
Example: Create Volume from Snapshot
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-from-snapshot
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
dataSource:
apiGroup: snapshot.storage.k8s.io
kind: VolumeSnapshot
name: example-snapshot
Note: The
dataSource field must reference a valid VolumeSnapshot resource in the same namespace.
Warning: Ensure that the CSI driver supports volume cloning or snapshotting features before using them.
9. Deleting VolumeSnapshots
| Field | Description | Values |
|---|---|---|
| apiVersion | API version of the VolumeSnapshot resource | snapshot.storage.k8s.io/v1 |
| kind | Kind of the resource | VolumeSnapshot |
| metadata.name | Name of the VolumeSnapshot to delete | snapshot-name |
Note: Deleting a VolumeSnapshot will also delete the underlying snapshot in the storage provider.
Warning: Ensure that no pods are using the volume associated with this snapshot before deletion.
10. Troubleshooting Snapshot Issues
| Issue | Potential Causes | Solution Steps |
|---|---|---|
| Snapshot creation fails | StorageClass does not support snapshots, insufficient permissions, or volume is in use. |
Verify StorageClass supports snapshots. Check permissions for CSI driver and volume. Ensure volume is not mounted in any pod. |
| Snapshot status stuck as "Pending" | Storage backend is slow or unresponsive, missing CSI driver support. | Check logs from CSI driver and storage backend. Confirm snapshot is supported by the volume plugin. |
| Snapshot restore fails | Snapshot is corrupted, incompatible with source volume, or missing from storage backend. | Validate snapshot integrity using kubectl describe volumesnapshot. Ensure source and restore volume types match. |
Example: Describe VolumeSnapshot for diagnostics
kubectl describe volumesnapshot <snapshot-name>
Note: Use
kubectl get volumesnapshot to check the status and kubectl get volumesnapshotcontent for underlying storage details.
Warning: Do not delete a VolumeSnapshotContent if it is still referenced by a VolumeSnapshot, as this can lead to data loss.