Using volume snapshots

In Google Kubernetes Engine (GKE), you can use the Kubernetes volume snapshot feature for persistent volumes in your GKE clusters.

Overview

Volume snapshots lets 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:

Prerequisites

Using volume snapshots on GKE, has the following prerequisites:

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 add pd.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 to Retain 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 StorageClass is used.
  • 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 be VolumeSnapshot.
  • apiGroup must be snapshot.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