In Google Kubernetes Engine (GKE), you can use the Kubernetes volume snapshot feature for persistent volumes in your GKE clusters.
Volume snapshots let you create a copy of your volume at a specific point in time. You can use this copy to bring a volume back to a prior state or to provision a new volume.
From version 1.17+, you can provision and attach volume snapshots with the following components:
Requirements
Using volume snapshots on GKE, has the following requirements:
You must install a CSI driver that supports snapshots. Snapshots are not supported by the in-tree persistent disk driver. If you are using the Compute Engine persistent disk CSI driver, we recommend automatically deploying the driver to reduce your management overhead. For a list of all CSI drivers that support snapshots, see the "Other features" column in Drivers in the Kubernetes documentation.
You must use control plane (master) versions that are GKE versions 1.17 or later. To use the Compute Engine persistent disk CSI Driver, in a VolumeSnapshot, use GKE versions 1.17.6-gke.4 or later.
You must have an existing PersistentVolumeClaim to take a snapshot of and the PersistentVolume used for a snapshot source must be managed by a CSI driver. You can verify that you're using a CSI driver by checking that the PersistentVolume spec has a
csi
section withdriver: pd.csi.storage.gke.io
. If the PersistentVolume is dynamically provisioned by the CSI driver as described in the following sections, it's managed by the CSI driver.
Creating a VolumeSnapshotClass
Create a VolumeSnapshotClass to determine the CSI driver and deletionPolicy
for your VolumeSnapshot. You can reference the VolumeSnapshotClass in your
VolumeSnapshot.
The following manifest named snapshot-class-example.yaml
is an example of a
VolumeSnapshotClass:
# snapshot-class-example.yaml
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshotClass
metadata:
name: snapshot-class
driver: csi-driver
deletionPolicy: Delete
Replace the following:
snapshot-class
is the name of your VolumeSnapshotClass.csi-driver
specifies which CSI driver to use to provision the snapshot. For example, you could addpd.csi.storage.gke.io
to use the Compute Engine persistent disk CSI Driver.Delete
ensures the VolumeSnapshotContent object is deleted when the VolumeSnapshot it's bound to is deleted. It also deletes the underlying storage system's snapshot. Set this value toRetain
if you want to keep the VolumeSnapshotContent.
To create the VolumeSnapshotClass, run the following command after creating the file.
kubectl apply -f snapshot-class-example.yaml
Creating a VolumeSnapshot
A VolumeSnapshot is a request for a snapshot of a pre-existing PersistentVolumeClaim.
The following manifest named snapshot-example.yaml
is an example of a
VolumeSnapshot:
#snapshot-example.yaml
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshot
metadata:
name: snapshot
spec:
volumeSnapshotClassName: snapshot-class
source:
persistentVolumeClaimName: pvc
Replace the following:
snapshot
is the name of your VolumeSnapshot.snapshot-class
specifies the name of the VolumeSnapshotClass you want to reference. If you do not specify a value, the default VolumeSnapshotClass is used if it is available.pvc
is the name of the existing PersistentVolumeClaim you want to take a snapshot of.
To create the VolumeSnapshot, run the following command after creating the file:
kubectl apply -f snapshot-example.yaml
Verifying VolumeSnapshot creation
After you have created a volume snapshot, a corresponding VolumeSnapshotContent object is created in the cluster. This object stores the snapshot and bindings of VolumeSnapshot objects. You do not interact with VolumeSnapshotContents objects directly.
To confirm if VolumeSnapshotContents has been created, run the following command:
kubectl get volumesnapshotcontents
The output is similar to:
NAME AGE
snapcontent-cee5fb1f-5427-11ea-a53c-42010a1000da 55s
After the volume snapshot content is created, the CSI driver you specified in
the VolumeSnapshotClass
creates a snapshot on the corresponding storage
system. Once GKE creates a snapshot on the storage system and
binds it to a volume snapshot object on the cluster, the snapshot is ready to
use. You can check the status by running the following command:
kubectl get volumesnapshot \
-o custom-columns='NAME:.metadata.name,READY:.status.readyToUse'
If it's ready to use, the output is similar to:
NAME READY
snapshot-example true
Provisioning and restoring volumes using a VolumeSnapshot
Referencing a VolumeSnapshot in a PersistentVolumeClaim lets you provision a new volume with data from an existing volume or restore a volume to a state that you captured in a snapshot.
To reference a VolumeSnapshot in a PersistentVolumeClaim, add the dataSource
field to your PersistentVolumeClaim.
The following manifest named pvc-demo.yaml
is an example of how to add the
dataSource
field:
# pvc-demo.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-demo
spec:
dataSource:
name: snapshot-name
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 30Gi
Ensure dataSource
has the following fields:
snapshot-name
is the name of the VolumeSnapshot you want to use.kind
must beVolumeSnapshot
.apiGroup
must besnapshot.storage.k8s.io
.
To apply the change to the PersistentVolumeClaim, run the following command:
kubectl apply -f pvc-demo.yaml
Deleting a VolumeSnapshot
To delete a VolumeSnapshot, run the following command:
kubectl delete volumesnapshot snapshot-example
If a volume snapshot's VolumeSnapshotClass
has deletionPolicy:Delete
, the
corresponding VolumeSnapshotContent
and the physical snapshot on the storage
system are also deleted. If it has a deletionPolicy:Retain
, GKE
retains the physical snapshot.
What's next
- Read the Kubernetes Volume Snapshot documentation.
- Learn about volume expansion.
- Learn how to manually install a CSI driver.