Skip to main content

Persistent volume snapshots in a Managed Kubernetes cluster

Last update:

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

  1. 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.yaml

    The response will contain a list of the created objects. For example:

    deployment.apps/snapshot-controller created
    serviceaccount/snapshot-controller created
    clusterrole.rbac.authorization.k8s.io/snapshot-controller-runner created
    clusterrolebinding.rbac.authorization.k8s.io/snapshot-controller-role created
    role.rbac.authorization.k8s.io/snapshot-controller-leaderelection created
    rolebinding.rbac.authorization.k8s.io/snapshot-controller-leaderelection created
    customresourcedefinition.apiextensions.k8s.io/volumesnapshots.snapshot.storage.k8s.io created
    customresourcedefinition.apiextensions.k8s.io/volumesnapshotcontents.snapshot.storage.k8s.io created
    customresourcedefinition.apiextensions.k8s.io/volumesnapshotclasses.snapshot.storage.k8s.io created
  2. Make sure the pods with the Volume Snapshot Controller have been created:

    kubectl get pod -l app=snapshot-controller --namespace=kube-system

    The response will contain a list of pods. For example:

    NAME READY STATUS RESTARTS AGE
    snapshot-controller-7d7f5775d4-cz9j4 1/1 Running 0 3m37s
    snapshot-controller-7d7f5775d4-tc42c 1/1 Running 0 3m37s
  3. создайте yaml-файл with манифестом для объектов VolumeSnapshotClass and VolumeSnapshot:

    Manifest example:

    ---
    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshotClass
    metadata:
    name: csi-hostpath-snapclass-v1
    driver: cinder.csi.openstack.org
    deletionPolicy: Delete
    parameters:
    force-create: "true"
    ---
    apiVersion: snapshot.storage.k8s.io/v1
    kind: VolumeSnapshot
    metadata:
    name: new-snapshot-demo
    spec:
    volumeSnapshotClassName: csi-hostpath-snapclass-v1
    source:
    persistentVolumeClaimName: my-pv-claim

    Where:

    • 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.
  4. 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 created
    volumesnapshot.snapshot.storage.k8s.io/new-snapshot-demo created
  5. Make sure the VolumeSnapshotContent object has been created:

    kubectl get volumesnapshotcontents.snapshot.storage.k8s.io

    The response will contain the created VolumeSnapshotContent object. For example:

    NAME READYTOUSE RESTORESIZE DELETIONPOLICY DRIVER VOLUMESNAPSHOTCLASS VOLUMESNAPSHOT VOLUMESNAPSHOTNAMESPACE AGE
    snapcontent-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

  1. создайте yaml-файл with манифестом для создания нового PersistentVolumeClaim из снапшота.

    Manifest example:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: snapshot-demo-restore
    spec:
    storageClassName: fast.ru-3b
    dataSource:
    name: new-snapshot-demo
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io
    accessModes:
    - ReadWriteOnce
    resources:
    requests:
    storage: 10Gi

    Where:

    • 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.
  2. Apply the manifest:

    kubectl apply -f <file_name>

    укажите <file_name> — имя yaml-файла with манифестом для создания нового PersistentVolumeClaim из снапшота. например, snapshot-restore.yaml.

  3. Make sure the new PersistentVolumeClaim has been created:

    kubectl get pvc

    The response will contain the new PersistentVolumeClaim. For example:

    NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
    snapshot-demo-restore Bound pvc-62394055-3780-4324-8871-1a56a447d70f 10Gi RWO fast.ru-3b 22m