VM Snapshot & Restore

A virtual machine (VM) snapshot captures the state and data of a virtual machine at the specific time when the snapshot was taken. It acts pretty much like the usual operating system snapshots. A VM snapshot is an exact copy of the VM and can be used for VM migration or for creating multiple instances of the same VM.

The snapshot.kubevirt.io API Group defines resources for snapshotting and restoring KubeVirt Virtual Machines.

Pre-requisites:

  • CSI Persistent Storage with support for VolumeSnapshot API should be installed with CRDs required for VolumeSnapshot API such as VolumeSnapshot, VolumeSnapshotClass etc.

    • KubeVirt leverages the VolumeSnapshot functionality of Kubernetes CSI drivers for capturing persistent VirtualMachine state. It is required that VMs use DataVolumes or PersistentVolumeClaims backed by a StorageClass that supports VolumeSnapshots and a VolumeSnapshotClass is properly configured for that StorageClass.

  • KubeVirt looks for Kubernetes Volume Snapshot-related APIs/resources in the v1 version. To make sure that KubeVirt's snapshot controller is able to snapshot the VirtualMachine and referenced volumes as expected, Kubernetes Volume Snapshot APIs must be served from v1 version.

  • Snapshot/Restore support must be enabled in the feature gates to be supported. Platform9 KubeVirt has it enabled by default.

Limitation

If the persistent TPM feature is used with KubeVirt VM, that VM cannot be snapshotted.

Snapshot a Virtual Machine

To snapshot a VirtualMachine named fedora-vm-1, apply the following yaml on the cluster:

apiVersion: snapshot.kubevirt.io/v1alpha1 kind: VirtualMachineSnapshot metadata: name: snap-fedora-1 spec: source: apiGroup: kubevirt.io kind: VirtualMachine name: fedora-vm-1

Checking the snapshot progress:

$ kubectl get vmsnapshot -w NAME SOURCEKIND SOURCENAME PHASE READYTOUSE CREATIONTIME ERROR snap-fedora-1 VirtualMachine fedora-vm-1 snap-fedora-1 VirtualMachine fedora-vm-1 InProgress false snap-fedora-1 VirtualMachine fedora-vm-1 InProgress false snap-fedora-1 VirtualMachine fedora-vm-1 InProgress false snap-fedora-1 VirtualMachine fedora-vm-1 Succeeded true 0s $ kubectl get volumesnapshot -w NAME READYTOUSE SOURCEPVC SOURCESNAPSHOTCONTENT RESTORESIZE SNAPSHOTCLASS SNAPSHOTCONTENT CREATIONTIME AGE vmsnapshot-786c1511-9372-431b-b095-cb9bc494ed44-volume-rootfs fedora-dv-1 csi-cephfsplugin-snapclass 0s vmsnapshot-786c1511-9372-431b-b095-cb9bc494ed44-volume-rootfs fedora-dv-1 csi-cephfsplugin-snapclass 0s vmsnapshot-786c1511-9372-431b-b095-cb9bc494ed44-volume-rootfs false fedora-dv-1 csi-cephfsplugin-snapclass snapcontent-65f9b955-1b69-4e04-bbc7-edbbd17e9fea 0s vmsnapshot-786c1511-9372-431b-b095-cb9bc494ed44-volume-rootfs false fedora-dv-1 csi-cephfsplugin-snapclass snapcontent-65f9b955-1b69-4e04-bbc7-edbbd17e9fea 0s vmsnapshot-786c1511-9372-431b-b095-cb9bc494ed44-volume-rootfs true fedora-dv-1 5Gi csi-cephfsplugin-snapclass snapcontent-65f9b955-1b69-4e04-bbc7-edbbd17e9fea 2s 2s

Restore a Virtual Machine

To restore the VM fedora-vm-1 from VirtualMachineSnapshot snap-fedora-1, Stop the VM, wait for it to be stopped and then apply the following yaml.

apiVersion: snapshot.kubevirt.io/v1alpha1 kind: VirtualMachineRestore metadata: name: restore-fedora-1 spec: target: apiGroup: kubevirt.io kind: VirtualMachine name: fedora-vm-1 virtualMachineSnapshotName: snap-fedora-1
Note

The used csi storageclasses must support volume expansion in order to be able to restore the snapshots. Storageclasses that do not, will be able to make snapshots, but not restore them.

Clean Snapshots

VirtualMachineSnapshots (and their corresponding VirtualMachineSnapshotContents) objects can be deleted when they are no longer required for the restoring.