Importing a snapshot into Kubernetes
How to import a CSI VolumeSnapshot in Kubernetes
Using CSI VolumeSnapshots is a powerful tool for managing persistent volumes, creating clones or restoring to a previous state. You can create VolumeSnapshots using Kubernetes (see create snapshot) and use those snapshots within your cluster. However sometimes it is necessary to import an already existing VolumeSnapshot that was created outside your cluster, into your Kubernetes cluster.
This runbook describes how this works and shows examples on how to easily do this using acloud-toolkit snapshot import.
Manually importing VolumeSnapshots
First you need to find the id of your snapshot in your backend storage, such as AWS. This is the ID of the snapshot. For AWS, this looks like snap-027df45af20ee07c6.
Preparing the VolumesnapshotContent
We are going to construct the VolumesnapshotContent resource for Kubernetes.
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshotContent
metadata:
name: snapcontent-<name>
spec:
deletionPolicy: Retain
driver: <your-csi-driver>
source:
snapshotHandle: <snapshot-id>
volumeSnapshotClassName: <your-csi-volumesnapshotclass-name>
volumeSnapshotRef:
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
name: <name-of-snapshot>
namespace: <namespace-of-snapshot>A few important things to note here:
-
You need to enter your CSI Driver information for the snapshots. You can retrieve this by using
kubectl get volumesnapshotclasses. Use theDRIVERvalue forspec.driverand theNAMEvalue forspec.volumeSnapshotClassName. -
The
spec.volumeSnapshotRefis the name and namespace we will be using to create theVolumeSnapshotresource in your cluster, at the next step. Make sure these do not exist yet and are unique within your cluster. -
And finally, the
spec.source.snapshotHandlevalue will contain the ID of the snapshot in your backend storage (i.g.snap-027df45af20ee07c6).
You can validate the VolumeSnapshotContent has been created in your cluster by using:
kubectl get volumesnapshotcontentCreate the VolumeSnapshot resource
Once the VolumeSnapshotContent has been created, we can insert the VolumeSnapshot resource:
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: <name-of-snapshot>
namespace: <namespace-of-snapshot>
spec:
source:
volumeSnapshotContentName: snapcontent-<name>
volumeSnapshotClassName: <your-csi-volumesnapshotclass-name>-
Make sure the
metadata.nameandmetadata.namespaceequal those used in thespec.volumeSnapshotReffrom theVolumeSnapshotContent. -
Use the
nameof theVolumeSnapshotContentforspec.source.volumeSnapshotContentNameand match thespec.volumeSnapshotClassName.
Apply this resource into your cluster and you are now ready to use the VolumeSnapshot to resource persistent volumes within your Kubernetes Cluster!
Using acloud-toolkit
Using the CLI tool acloud-toolkit you are able to quick and easily import a snapshot ID using the above methods, by simply specifying the name and snapshot ID. The CLI will automatically follow the previously detailed steps and create the VolumeSnapshotContent and VolumeSnapshot resources within your Kubernetes cluster.
The acloud-toolkit snapshot import subcommand will by default select your default SnapshotStorageClass and ensure the driver and other settings are configured correctly.
How to use
How to use the snapshot import command:
❯ acloud-toolkit snapshot import -h
This command creates Kubernetes CSI snapshot resources using a snapshot ID from the backend storage, for example AWS EBS, or Ceph RBD.
Usage:
acloud-toolkit snapshot import <snapshot> [flags]
Examples:
acloud-toolkit snapshot import --name example snap-12345
Flags:
-h, --help help for import
--name string name of the snapshot
--namespace string If present, the namespace scope for this CLI request. Otherwise uses the namespace from the current Kubernetes context
--snapshot-storage-class stringExample output
In this example, we will be importing the snapshot snap-027df45af20ee07c6 into a VolumeSnapshot called test within our current namespace context.
❯ acloud-toolkit snapshot import --name test snap-027df45af20ee07c6
Creating snapshotcontent "test-66a656c6-f40c-4a20-86a7-07f2a32c9ba1"..
Creating snapshot "test"..
Completed importing snapshot "snap-027df45af20ee07c6" into CSI snapshot test..You can verify the created resources:
❯ kubectl get volumesnapshot,volumesnapshotcontents.snapshot.storage.k8s.io
NAME READYTOUSE SOURCEPVC SOURCESNAPSHOTCONTENT RESTORESIZE SNAPSHOTCLASS SNAPSHOTCONTENT CREATIONTIME AGE
volumesnapshot.snapshot.storage.k8s.io/test true test-66a656c6-f40c-4a20-86a7-07f2a32c9ba1 8Gi ebs test-66a656c6-f40c-4a20-86a7-07f2a32c9ba1 34m 7s
NAME READYTOUSE RESTORESIZE DELETIONPOLICY DRIVER VOLUMESNAPSHOTCLASS VOLUMESNAPSHOT VOLUMESNAPSHOTNAMESPACE AGE
volumesnapshotcontent.snapshot.storage.k8s.io/test-66a656c6-f40c-4a20-86a7-07f2a32c9ba1 true 8589934592 Retain ebs.csi.aws.com ebs test default 7sLast updated on