How to Manage Volume Snapshots in Kubernetes?
Introduction
Kubernetes volume snapshots allow you to capture the contents of a Persistent Volume at a specific point in time. Snapshots can be useful for recovering volume data after accidental deletion, corruption, or data loss.
In CMP Kubernetes environments, volume snapshots are implemented using Kubernetes Custom Resource Definitions (CRDs) and the snapshot-controller. These components must be installed and configured before volume snapshots can be created.
This guide explains how to install the required snapshot components, create a VolumeSnapshotClass, create a volume snapshot from an existing PersistentVolumeClaim (PVC), verify the snapshot, and delete it when it is no longer required.
Important: Creating a volume snapshot does not modify or move the data stored on the source volume.
Prerequisites
Before managing volume snapshots, ensure the following requirements are met.
Persistent Volume Claim
An existing PersistentVolumeClaim (PVC) must be available in the Kubernetes cluster.
The snapshot is created from the volume associated with the PVC.
CSI Snapshotter Version
The CSI snapshotter version used with this procedure must not be higher than:
release-5.0
Warning: The source documentation states that using a CSI snapshotter version higher than release-5.0 is not supported and may cause the snapshot CRDs or snapshot-controller to behave unexpectedly.
Required Components
- VolumeSnapshotClass CRD
- VolumeSnapshotContent CRD
- VolumeSnapshot CRD
- Snapshot-controller RBAC resources
- ClusterRole
- ServiceAccount
- ClusterRoleBinding
- Role
- RoleBinding
- Snapshot-controller deployment
Install the Volume Snapshot Components
The source procedure uses manifests from the upstream Kubernetes CSI external-snapshotter repository and follows an Infrastructure as Code (IaC) approach.
Phase A: Clone the External Snapshotter Repository
Clone the repository and check out the supported release:
git clone https://github.com/kubernetes-csi/external-snapshotter/
cd ./external-snapshotter
git checkout release-5.0Important: Use the release-5.0 version specified by the source documentation for this procedure.
Phase B: Apply the Snapshot CRDs
Apply the required VolumeSnapshotClass, VolumeSnapshotContent, and VolumeSnapshot CRDs:
kubectl apply -f client/config/crd/snapshot.storage.k8s.io_volumesnapshotclasses.yaml kubectl apply -f client/config/crd/snapshot.storage.k8s.io_volumesnapshotcontents.yaml
kubectl apply -f client/config/crd/snapshot.storage.k8s.io_volumesnapshots.yamlThese commands install the Kubernetes resources required for managing volume snapshots.
Phase C: Deploy the Snapshot Controller
Apply the required RBAC resources and snapshot-controller deployment in the kube-system namespace:
kubectl apply -f deploy/kubernetes/snapshot-controller/rbac-snapshot-controller.yaml -n kube-system
kubectl apply -f deploy/kubernetes/snapshot-controller/setup-snapshot-controller.yaml -n kube-systemThese manifests create the required:
- Snapshot CRDs
- ClusterRole
- ServiceAccount
- ClusterRoleBinding
- Role
- RoleBinding
- snapshot-controller deployment
Create a Volume Snapshot
After the required snapshot components have been installed, you can create a volume snapshot from an existing PVC.
Phase A: Define a VolumeSnapshotClass
Create a file named:
snapshot-class.yaml
Add the following configuration:
vi snapshot-class.yaml
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotClass
metadata:
name: mysnapclass
driver: cinder.csi.openstack.org
deletionPolicy: Delete
parameters:
force-create: “true”This configuration creates a VolumeSnapshotClass named mysnapclass using the Cinder CSI driver.
The configuration uses:
deletionPolicy: Delete
With the Delete policy, deleting the associated VolumeSnapshot also removes the underlying storage snapshot and the associated VolumeSnapshotContent object.
If you want to preserve the underlying snapshot after deleting the VolumeSnapshot object, use:
deletionPolicy: Retain
With Retain, the underlying snapshot and VolumeSnapshotContent are retained.
Apply the VolumeSnapshotClass
Run:
kubectl apply -f snapshot-class.yamlExpected output:
volumesnapshotclass.snapshot.storage.k8s.io/mysnapclass createdDefine a VolumeSnapshot
Create a file named:
snapshot.yaml
Add the following configuration:
vi snapshot.yaml
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: mysnapshot
spec:
volumeSnapshotClassName: mysnapclass
source:
persistentVolumeClaimName: mypvcThis configuration:
- Creates a snapshot named mysnapshot.
- Uses the mysnapclass VolumeSnapshotClass.
- Creates the snapshot from the PVC named mypvc.
Note: Replace mypvc with the name of the PVC that you want to snapshot.
Create the Volume Snapshot
Run:
kubectl create -f snapshot.yamlExpected output:
volumesnapshot.snapshot.storage.k8s.io/mysnapshot createdThe newly created snapshot will appear in the CMP self-service panel under:
Compute → Volumes → [Volume] → Snapshots
Verify the Volume Snapshot
You can verify the available volume snapshots using:
kubectl get vsThis command displays the VolumeSnapshot resources available in the Kubernetes environment.
Delete a Volume Snapshot
When a volume snapshot is no longer required, it can be deleted using the manifest that was used to create it.
Run:
kubectl delete -f snapshot.yamlExpected output:
volumesnapshot.snapshot.storage.k8s.io “mysnapshot” deletedImportant: Whether the underlying storage snapshot is also deleted depends on the deletionPolicy configured in the associated VolumeSnapshotClass.
Delete Policy: Delete
If the VolumeSnapshotClass uses:
deletionPolicy: Delete
the underlying storage snapshot and VolumeSnapshotContent are removed when the VolumeSnapshot is deleted.
Delete Policy: Retain
If the VolumeSnapshotClass uses:
deletionPolicy: Retain
the underlying storage snapshot and VolumeSnapshotContent remain after the VolumeSnapshot is deleted.
Choose the policy according to the required retention and recovery behavior.
Safety and Recovery
Data Integrity
Creating a volume snapshot does not alter, move, or delete the data stored on the source volume.
The snapshot captures the volume state at the time it is created and can be used for recovery purposes later.
Version Compatibility
The source procedure supports a CSI snapshotter version no higher than:
release-5.0
Using a higher version is not supported by this documentation and may cause unexpected behavior with the snapshot CRDs or snapshot-controller.
Snapshot Best Practices
For critical workloads:
- Take snapshots regularly.
- Consider creating a snapshot before major workload changes or upgrades.
- Use the Retain deletion policy for critical volumes when you need the underlying snapshot to survive accidental deletion of the VolumeSnapshot object.
Warning: A snapshot should be treated as part of your recovery strategy. Ensure that the selected deletion policy matches your intended snapshot-retention requirements.
Troubleshooting and Important Considerations
Snapshot Creation Fails
If a VolumeSnapshot cannot be created, verify:
- The required snapshot CRDs are installed.
- The snapshot-controller is deployed.
- The required RBAC resources are available.
- A valid VolumeSnapshotClass exists.
- The referenced PVC exists.
- The CSI snapshotter version is supported by this procedure.
The VolumeSnapshot Is Not Listed
If the snapshot does not appear when running:
kubectl get vsverify that the VolumeSnapshot resource was created successfully and that the Kubernetes snapshot components have been installed.
Snapshot Deletion Behavior Is Unexpected
Check the deletionPolicy configured in the associated VolumeSnapshotClass.
For example:
deletionPolicy: Delete
and:
deletionPolicy: Retain
have different effects on the underlying storage snapshot.
Summary
Kubernetes volume snapshots provide a way to capture the state of a Persistent Volume at a specific point in time.
The CMP procedure requires:
- An existing PersistentVolumeClaim.
- Supported CSI snapshotter components.
- The required Kubernetes snapshot CRDs.
- The snapshot-controller and its RBAC resources.
- A VolumeSnapshotClass.
- A VolumeSnapshot referencing the required PVC.
Snapshots can be verified using:
kubectl get vsWhen a snapshot is no longer required, it can be deleted using:
kubectl delete -f snapshot.yamlThe effect of deleting the snapshot depends on whether the associated VolumeSnapshotClass uses the Delete or Retain deletion policy.
Conclusion
Managing volume snapshots in Kubernetes provides an effective way to capture PVC data at a specific point in time for recovery purposes. Before creating snapshots, ensure that the required CRDs, snapshot-controller, RBAC resources, and supported CSI snapshotter components are installed.
When creating snapshots, carefully select the VolumeSnapshotClass and its deletion policy. For critical volumes where the underlying snapshot should survive deletion of the Kubernetes snapshot object, the source documentation recommends using the Retain policy.
Following these steps allows Kubernetes volume snapshots to be created, verified, and deleted in a controlled manner within CMP.
FAQ’s
What is a Kubernetes volume snapshot?
A Kubernetes volume snapshot captures the contents of a Persistent Volume at a specific point in time. It can be used for recovery purposes if volume data is accidentally deleted, corrupted, or lost.
Does creating a snapshot modify the source volume?
No. Creating a volume snapshot does not modify or move the data on the source volume.
What is required before creating a VolumeSnapshot?
An existing PVC, the required volume snapshot CRDs, snapshot-controller, RBAC resources, a VolumeSnapshotClass, and a supported CSI snapshotter are required.
Which CSI snapshotter version is supported by this procedure?
The source documentation specifies that the CSI snapshotter version must not be higher than release-5.0.
How do I create a Kubernetes volume snapshot?
Create a VolumeSnapshot manifest referencing the required PVC and VolumeSnapshotClass, then run:
kubectl create -f snapshot.yamlHow can I verify existing volume snapshots?
Run:
kubectl get vsThis lists the VolumeSnapshot resources available in the cluster.
What is the difference between Delete and Retain?
With Delete, deleting the VolumeSnapshot also removes the underlying storage snapshot and VolumeSnapshotContent. With Retain, the underlying snapshot and VolumeSnapshotContent are preserved.
How do I delete a volume snapshot?
Run:
kubectl delete -f snapshot.yamlThe effect on the underlying storage snapshot depends on the configured deletionPolicy.