Create persistent volume snapshot.
When you need to create a back-up of a persistent volume claim in your cluster that makes use of block storage.
Step-by-step guide
In your cluster there is a VolumeSnapshotClass
resource available. On AWS, the default name is csi-aws-vsc
.
You can view all available classes by performing the following command:
kubectl get volumesnapshotclass
If you see no volumesnapshotclasses
, you may need to upgrade your cluster or the cloud provider your cluster runs in does not support snapshot functionality. Please contact Avisi Cloud support.
Similarly to persistent volumes, we use snapshotclass to determine what driver will be used for creating the snapshot.
Given the following persistent volume as an example of a PVC we want to back-up:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-snapshot-claim
namespace: default
spec:
accessModes:
- ReadWriteOnce
storageClassName: ebs
resources:
requests:
storage: 1Gi
This can be any PVC.
Creating a snapshot
To create a new snapshot, apply the following resource to your cluster:
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: test-snapshot-demo
namespace: default
spec:
volumeSnapshotClassName: csi-aws-vsc
source:
persistentVolumeClaimName: test-snapshot-claim
You can view the created snapshots by performing the following command:
$ kubectl get volumesnapshot -A;
NAMESPACE NAME READYTOUSE SOURCEPVC SOURCESNAPSHOTCONTENT RESTORESIZE SNAPSHOTCLASS SNAPSHOTCONTENT CREATIONTIME AGE
default example true data-example-db-0 1Gi rbd-snapshot snapcontent-ca28a4d9-a340-45f0-a9c7-2f1eacd7c42f 49d 49d
The READYTOUSE
column means you can use the snapshot to create a new persistent volume using the snapshot as a source.
Restoring a snapshot
You can restore a snapshot by creating a new Persistent Volume Claim with a datasource configured. Note the spec.dataSource.name, which refers the VolumeSnapshot previously created.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-snapshot-claim-restore
spec:
storageClassName: ebs
dataSource:
name: test-snapshot-demo
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Restoring a volume may take a minute or so, depending on the size of your snapshot and provider used.
Note that on AWS, disk performance will be slow after the initial restore while AWS is completing restore operations. All data can be read immediately after restore.
Related external documentation
- https://kubernetes.io/docs/concepts/storage/volume-snapshots/
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSSnapshots.html
Using acloud-toolkit
When using acloud-toolkit
, you can create a snapshot using the following command:
$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
data-my-release-postgresql-ha-postgresql-0 Bound pvc-6c2f336d-80d5-4298-88f7-bf81b7820800 8Gi RWO gp2 63d
tmp-test-snapshot-claim Bound pvc-1d6cb933-2706-4471-9a89-e895a9cf3439 2Gi RWO gp2-immediate 50d
$ acloud-toolkit storage create-snapshot --pvc tmp-test-snapshot-claim test-snapshot01
PVC with volume pvc-1d6cb933-2706-4471-9a89-e895a9cf3439 found...
Creating snapshot "test-snapshot01" for PVC "tmp-test-snapshot-claim"...
Snapshot "test-snapshot01" created for PVC "tmp-test-snapshot-claim"...
Waiting until "test-snapshot01" is ready for use...
...