Persistent volume snapshots in a Managed Kubernetes cluster
Persistent volume snapshots (VolumeSnapshots) are point-in-time copies of persistent volumes (PersistentVolume) in a Managed Kubernetes cluster.
Snapshots can be used for backing up data or transferring data to another resource without having to create a new volume.
In Managed Kubernetes, snapshots work on cloud platform resources.
Create a snapshot
-
Configure the Volume Snapshot Controller — one of the CSI Snapshotter components:
kubectl apply -f https://raw.githubusercontent.com/selectel/mks-csi-snapshotter/master/deploy/setup-snapshot-controller.yamlThe response will contain a list of the created objects. For example:
deployment.apps/snapshot-controller createdserviceaccount/snapshot-controller createdclusterrole.rbac.authorization.k8s.io/snapshot-controller-runner createdclusterrolebinding.rbac.authorization.k8s.io/snapshot-controller-role createdrole.rbac.authorization.k8s.io/snapshot-controller-leaderelection createdrolebinding.rbac.authorization.k8s.io/snapshot-controller-leaderelection createdcustomresourcedefinition.apiextensions.k8s.io/volumesnapshots.snapshot.storage.k8s.io createdcustomresourcedefinition.apiextensions.k8s.io/volumesnapshotcontents.snapshot.storage.k8s.io createdcustomresourcedefinition.apiextensions.k8s.io/volumesnapshotclasses.snapshot.storage.k8s.io created -
Make sure the pods with the Volume Snapshot Controller have been created:
kubectl get pod -l app=snapshot-controller --namespace=kube-systemThe response will contain a list of pods. For example:
NAME READY STATUS RESTARTS AGEsnapshot-controller-7d7f5775d4-cz9j4 1/1 Running 0 3m37ssnapshot-controller-7d7f5775d4-tc42c 1/1 Running 0 3m37s -
создайте yaml-файл with манифестом для объектов VolumeSnapshotClass and VolumeSnapshot:
Manifest example:
---apiVersion: snapshot.storage.k8s.io/v1kind: VolumeSnapshotClassmetadata:name: csi-hostpath-snapclass-v1driver: cinder.csi.openstack.orgdeletionPolicy: Deleteparameters:force-create: "true"---apiVersion: snapshot.storage.k8s.io/v1kind: VolumeSnapshotmetadata:name: new-snapshot-demospec:volumeSnapshotClassName: csi-hostpath-snapclass-v1source:persistentVolumeClaimName: my-pv-claimWhere:
csi-hostpath-snapclass-v1— name of the SnapshotClass object;new-snapshot-demo— name of the VolumeSnapshot object;my-pv-claim— name of the PersistentVolumeClaim object from which you are creating a snapshot.
-
Apply the manifest:
kubectl apply -f <file_name>укажите
<file_name>— имя yaml-файла with манифестом для объектов VolumeSnapshotClass and VolumeSnapshot. например,snapshot.yaml.The response will contain a message stating that the objects have been created. For example:
volumesnapshotclass.snapshot.storage.k8s.io/csi-hostpath-snapclass-v1 createdvolumesnapshot.snapshot.storage.k8s.io/new-snapshot-demo created -
Make sure the VolumeSnapshotContent object has been created:
kubectl get volumesnapshotcontents.snapshot.storage.k8s.ioThe response will contain the created VolumeSnapshotContent object. For example:
NAME READYTOUSE RESTORESIZE DELETIONPOLICY DRIVER VOLUMESNAPSHOTCLASS VOLUMESNAPSHOT VOLUMESNAPSHOTNAMESPACE AGEsnapcontent-acc83be5-065f-42c1-a465-9f26497de1b1 Delete cinder.csi.openstack.org csi-hostpath-snapclass-v1 new-snapshot-demo default 9m7s
Restore a persistent volume from a snapshot
-
создайте yaml-файл with манифестом для создания нового PersistentVolumeClaim из снапшота.
Manifest example:
apiVersion: v1kind: PersistentVolumeClaimmetadata:name: snapshot-demo-restorespec:storageClassName: fast.ru-3bdataSource:name: new-snapshot-demokind: VolumeSnapshotapiGroup: snapshot.storage.k8s.ioaccessModes:- ReadWriteOnceresources:requests:storage: 10GiWhere:
snapshot-demo-restore— the name of the new PersistentVolumeClaim object where data from the snapshot will be copied;new-snapshot-demo— the name of the snapshot from which you will restore the object.
-
Apply the manifest:
kubectl apply -f <file_name>укажите
<file_name>— имя yaml-файла with манифестом для создания нового PersistentVolumeClaim из снапшота. например,snapshot-restore.yaml. -
Make sure the new PersistentVolumeClaim has been created:
kubectl get pvcThe response will contain the new PersistentVolumeClaim. For example:
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEsnapshot-demo-restore Bound pvc-62394055-3780-4324-8871-1a56a447d70f 10Gi RWO fast.ru-3b 22m