The snapshot.kubevirt.io
API Group defines resources for snapshotting and restoring KubeVirt VirtualMachines
KubeVirt leverages the VolumeSnapshot
functionality of Kubernetes CSI drivers for capturing persistent VirtualMachine
state. So, you should make sure that your VirtuallMachine
uses DataVolumes
or PersistentVolumeClaims
backed by a StorageClass
that supports VolumeSnapshots
and a VolumeSnapshotClass
is properly configured for that StorageClass
.
To list VolumeSnapshotClasses
:
kubectl get volumesnapshotclass
Make sure that the provisioner
property of your StorageClass
matches the driver
property of the VolumeSnapshotClass
Even if you have no VolumeSnapshotClasses
in your cluster, VirtualMachineSnapshots
are not totally useless. They will still backup your VirtualMachine
configuration.
Snapshot/Restore are currently considered alpha features and are disabled by default.
kubectl patch -n kubevirt kubevirt kubevirt -p '{"spec": {"configuration": { "developerConfiguration": { "featureGates": [ "Snapshot" ] }}}}' -o json --type merge
To snapshot a VirtualMachine
named larry
, apply the following yaml.
* It is currently a requirement that VirtualMachines
are shut down before taking a snapshot
(One way to achieve this is via virtctl - cluster-up/virtctl.sh stop larry
and when appropriate, start again: cluster-up/virtctl.sh start larry
).
apiVersion: snapshot.kubevirt.io/v1alpha1
kind: VirtualMachineSnapshot
metadata:
name: snap-larry
spec:
source:
apiGroup: kubevirt.io
kind: VirtualMachine
name: larry
To wait for a snapshot to complete, execute:
kubectl wait vmsnapshot snap-larry --for condition=Ready
To restore the VirtualMachine
larry
from VirtualMachineSnapshot
snap-larry
, apply the following yaml.
apiVersion: snapshot.kubevirt.io/v1alpha1
kind: VirtualMachineRestore
metadata:
name: restore-larry
spec:
target:
apiGroup: kubevirt.io
kind: VirtualMachine
name: larry
virtualMachineSnapshotName: snap-larry
To wait for a restore to complete, execute:
kubectl wait vmrestore restore-larry --for condition=Ready
Keep VirtualMachineSnapshots
(and their corresponding VirtualMachineSnapshotContents
) around as long as you may want to restore from them again.
Feel free to delete larry-restore
as it is not needed once the restore is complete.