This procedure shows how to create Persistent Volume Snapshots. Read more about this feature in the official Kubernetes documentation.
Before you begin
The following items and configurations are required for this procedure:
-
Kubernetes version 1.19.x or higher
-
Konvoy version 1.7.x or higher
-
CSI driver that supports volume snapshots
Limitations
- Only some CSI drivers support snapshots if underlying platform supports it
Setup Volume Snapshot Class
As StorageClass
provides a way for administrators to describe the “classes” of storage they offer when provisioning a volume, VolumeSnapshotClass
provides a way to describe the “classes” of storage when provisioning a volume snapshot.
You can read more about the different options in this document.
The VolumeSnapshotClass
depends on the infrastructure provider you are using. Refer to the sections below for the appropriate one.
AWS
Refer to the AWS EBS CSI documentation.
cat << EOF | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshotClass
metadata:
name: csi-vsc
driver: ebs.csi.aws.com
deletionPolicy: Delete
EOF
Azure
Refer to the Azure CSI documentation.
cat << EOF | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshotClass
metadata:
name: csi-vsc
driver: disk.csi.azure.com
deletionPolicy: Delete
parameters:
incremental: "true" # available values: "true", "false" ("true" by default)
EOF
GCP
Refer to the GCP CSI documentation.
cat << EOF | kubectl apply -f -
apiVersion: snapshot.storage.k8s.io/v1beta1
kind: VolumeSnapshotClass
metadata:
name: csi-vsc
driver: pd.csi.storage.gke.io
deletionPolicy: Delete
EOF
Test Volume Snapshots
-
Create a new
PersistentVolumeClaim
:cat << EOF | kubectl apply -f - apiVersion: v1 kind: PersistentVolumeClaim metadata: name: snapshot-claim spec: accessModes: - ReadWriteOnce resources: requests: storage: 4Gi EOF
-
Create a sample app using the
persistentVolumeClaim
:cat << EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: app spec: containers: - name: app image: centos command: ["/bin/sh"] args: ["-c", "while true; do echo \$(date -u) >> /data/out.txt; sleep 5; done"] volumeMounts: - name: persistent-storage mountPath: /data volumes: - name: persistent-storage persistentVolumeClaim: claimName: snapshot-claim EOF
-
Validate the pod successfully wrote data to the volume. Note the timestamp of the first entry:
kubectl exec -it app -- cat /data/out.txt
The output should look similar to:
Tue Aug 18 15:00:58 UTC 2020 Tue Aug 18 15:01:03 UTC 2020 Tue Aug 18 15:01:08 UTC 2020
-
Create a
VolumeSnapshot
referencing thePersistentVolumeClaim
name:cat << EOF | kubectl apply -f - apiVersion: snapshot.storage.k8s.io/v1beta1 kind: VolumeSnapshot metadata: name: volume-snapshot spec: volumeSnapshotClassName: csi-vsc source: persistentVolumeClaimName: snapshot-claim EOF
-
Wait for the
READYTOUSE: true
attribute of theVolumeSnapshot
:kubectl get volumesnapshot volume-snapshot -w
-
Delete the existing app and
PersistentVolumeClaim
:kubectl delete pod app && kubectl delete pvc snapshot-claim
-
Restore a volume from the snapshot with a
PersistentVolumeClaim
referencing theVolumeSnapshot
in itsdataSource
:cat << EOF | kubectl apply -f - apiVersion: v1 kind: PersistentVolumeClaim metadata: name: snapshot-restored-claim spec: accessModes: - ReadWriteOnce resources: requests: storage: 4Gi dataSource: name: volume-snapshot kind: VolumeSnapshot apiGroup: snapshot.storage.k8s.io EOF
-
Create another sample app using the restored
persistentVolumeClaim
:cat << EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: app spec: containers: - name: app image: centos command: ["/bin/sh"] args: ["-c", "while true; do echo \$(date -u) >> /data/out.txt; sleep 5; done"] volumeMounts: - name: persistent-storage mountPath: /data volumes: - name: persistent-storage persistentVolumeClaim: claimName: snapshot-restored-claim EOF
-
Validate the new pod has the restored data by comparing the timestamp of the first entry to that of in step 3:
kubectl exec -it app -- cat /data/out.txt
The output should look similar to:
Tue Aug 18 15:00:58 UTC 2020 Tue Aug 18 15:01:03 UTC 2020 Tue Aug 18 15:01:08 UTC 2020 ... Tue Aug 18 15:05:40 UTC 2020 Tue Aug 18 15:05:45 UTC 2020
-
Delete the app,
PersistentVolumeClaim
andVolumeSnapshot
:kubectl delete pod app && kubectl delete pvc snapshot-restored-claim && kubectl delete volumesnapshot volume-snapshot
-
Delete the
VolumeSnapshotClass
:kubectl delete volumesnapshotclass csi-vsc