---
title: "Persistent volume snapshots in a Managed Kubernetes cluster"
sidebar_label: "Persistent volume snapshots"
sidebar_position: 3
description: "How to create snapshots and restore a persistent volume from a snapshot"
toc_max_heading_level: 2
---

import Formbricks from '@theme/MDXComponents/Formbricks'

# Persistent volume snapshots in a Managed Kubernetes cluster

Persistent volume snapshots ([VolumeSnapshots](https://kubernetes.io/docs/concepts/storage/volume-snapshots/#volumesnapshots)) are point-in-time copies of [persistent volumes](/managed-kubernetes/volumes/persistent-volumes.mdx) (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](/access-control/projects/about-projects.mdx#selectel-products-that-support-projects).

## Create a snapshot \{#create-snapshot}

1. Configure the Volume Snapshot Controller — one of the [CSI Snapshotter components](https://github.com/kubernetes-csi/external-snapshotter#usage):

   ```bash
   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:

   ```bash
   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:

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

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

   ```bash
   NAME                                   READY   STATUS    RESTARTS   AGE
   snapshot-controller-7d7f5775d4-cz9j4   1/1     Running   0          3m37s
   snapshot-controller-7d7f5775d4-tc42c   1/1     Running   0          3m37s
   ```

3. Create a YAML file with the manifest for the VolumeSnapshotClass and VolumeSnapshot objects:

   Manifest example:

   ```yaml
   ---
   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:

   ```bash
   kubectl apply -f <file_name>
   ```

   Specify `<file_name>` — the name of the YAML file with the manifest for the VolumeSnapshotClass and VolumeSnapshot objects. For example, `snapshot.yaml`.

   The response will contain a message stating that the objects have been created. For example:

   ```bash
   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:

   ```bash
   kubectl get volumesnapshotcontents.snapshot.storage.k8s.io
   ```

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

   ```bash
   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 \{#restore-persistent-volume-from-snapshot}

1. Create a YAML file with the manifest to create a new PersistentVolumeClaim from the snapshot.

   Manifest example:

   ```yaml
   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:

   ```yaml
   kubectl apply -f <file_name>
   ```

   Specify `<file_name>` — the name of the YAML file with the manifest to create a new PersistentVolumeClaim from the snapshot. For example, `snapshot-restore.yaml`.

3. Make sure the new PersistentVolumeClaim has been created:

   ```bash
   kubectl get pvc
   ```

   The response will contain the new PersistentVolumeClaim. For example:

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

<Formbricks />
