Using Snapshots

A virtual machine snapshot saves the current state of the virtual machine, and can be used to restore the virtual machine to that state in the event of an unexpected failure.

Prerequisites

  • The Volume Snapshot has been deployed by the administrator in the platform management.

  • Virtual machine snapshots are based on volume snapshots. Ensure that at least one disk is bound to a storage class that supports volume snapshots, such as CephFS built-in storage.

  • Snapshots can be created while the virtual machine is running (online) or stopped (offline). For an application-consistent snapshot, install the QEMU guest agent in the guest so the filesystems can be quiesced; without it, an online snapshot is crash-consistent (the snapshot's status.indications reports Online and NoGuestAgent). Rolling back a snapshot requires the virtual machine to be stopped first — see stop the virtual machine.

Notes

If there are multiple storage types of the same kind in the cluster, for example, attaching multiple different sources of Ceph RBD storage, the disk snapshot functionality may not work properly when the virtual machine is using such storage.

Snapshot consistency

Check the snapshot's status.indications to know how trustworthy it is:

  • GuestAgent — the QEMU guest agent froze the guest filesystems before the snapshot, so it is application-consistent (the safest to restore from).
  • NoGuestAgent — no guest agent was available, so an online snapshot is crash-consistent (equivalent to pulling the power and rebooting). Install the guest agent for application-consistent snapshots.
  • QuiesceFailed — the freeze was attempted but failed; treat the snapshot as crash-consistent.
  • Online — the snapshot was taken while the virtual machine was running.

The persistent state of a virtual TPM (vTPM) is not captured in a snapshot, so a virtual machine that relies on a vTPM (for example for BitLocker) may need manual re-provisioning after a restore.

Creating a Snapshot

The contents included in a virtual machine snapshot: virtual machine settings and the state of the disks that support volume snapshots.

Procedures

  1. Access Container Platform.

  2. In the left navigation bar, click Virtualization > Virtual Machines.

  3. Locate the virtual machine and click ⋮ > Create Snapshot.

  4. Fill in the snapshot description. The description can help you document the current state of the virtual machine, such as Initial Installation, Before Application Upgrade.

  5. Click Create. The time taken for the snapshot depends on network conditions and workload, please be patient.

  6. Check the snapshot status.

    • When the snapshot changes to Ready, it indicates that the creation was successful.

    • If the snapshot remains in Not Ready status for a long time, click expand > View the reasons and troubleshoot, then recreate the snapshot.

Using the API

Create a VirtualMachineSnapshot that references the virtual machine:

apiVersion: snapshot.kubevirt.io/v1beta1
kind: VirtualMachineSnapshot
metadata:
  name: web-01-snap-1
  namespace: demo
spec:
  source:
    apiGroup: kubevirt.io
    kind: VirtualMachine
    name: web-01
kubectl apply -f snapshot.yaml
kubectl get virtualmachinesnapshot web-01-snap-1 -n demo \
  -o jsonpath='phase={.status.phase} readyToUse={.status.readyToUse} {.status.indications}{"\n"}'

When status.readyToUse is true, the snapshot is complete. For a snapshot of a running virtual machine, status.indications includes Online (and NoGuestAgent when the guest agent is absent — the snapshot is then crash-consistent).

Rolling Back a Snapshot

Roll back the virtual machine settings and the disks that support volume snapshots to the state at the time the snapshot was created. For example, disks added after the snapshot creation will be removed; modified disk data will be restored.

Notes

If there are disks bound to a storage class that supports the LVM mechanism (for example, TopoLVM), please confirm with the administrator that the reclamation policy for that storage class is set to Retain (reclaimPolicy: Retain) to use the snapshot rollback feature correctly.

Procedures

  1. Access Container Platform.

  2. In the left navigation bar, click Virtualization > Virtual Machines.

  3. Click on Virtual Machine Name.

  4. In the Snapshots tab, locate the snapshot and click ⋮ > Rollback.

  5. Read the prompt information on the interface, and click Rollback after confirming everything is correct.

    Note: The rollback operation cannot be aborted or undone, please proceed with caution.

  6. Click on the snapshot name to check in the “Snapshot Rollback Records” if the rollback has been completed. The time required for the rollback depends on network conditions and workload, please be patient.

Description

  • If the rollback fails, the virtual machine state remains unchanged. You can start the virtual machine normally or attempt to roll back the snapshot again.

  • If the virtual machine is started during the rollback process, it will revert to the state before it was stopped, and upon stopping the virtual machine again, it will continue rolling back to the state at the time of snapshot creation.

  • To avoid operational conflicts, please ensure that the most recent rollback record has been completed before performing other operations on that virtual machine.

Using the API

Roll back by creating a VirtualMachineRestore that targets the virtual machine and references the snapshot. Stop the target virtual machine first — restore requires it to be stopped:

apiVersion: snapshot.kubevirt.io/v1beta1
kind: VirtualMachineRestore
metadata:
  name: web-01-restore-1
  namespace: demo
spec:
  target:
    apiGroup: kubevirt.io
    kind: VirtualMachine
    name: web-01
  virtualMachineSnapshotName: web-01-snap-1
kubectl apply -f restore.yaml
kubectl get virtualmachinerestore web-01-restore-1 -n demo \
  -o jsonpath='complete={.status.complete}{"\n"}'

The rollback is finished when status.complete is true.

Deleting a Snapshot

Delete unnecessary virtual machine snapshots to free up disk resources.

Notes

When deleting a rolled-back virtual machine snapshot, if the virtual machine disk needs to copy data based on the snapshot (for example, TopoLVM), you must wait until a virtual machine based on the rollback version has been started before deleting, otherwise the virtual machine will fail to start.

Procedures

  1. Access Container Platform.

  2. In the left navigation bar, click Virtualization > Virtual Machines.

  3. Click on Virtual Machine Name.

  4. In the Snapshots tab, locate the target snapshot and click ⋮ > Delete.

  5. Read the prompt information and click Delete after confirming everything is correct.

Using the API

kubectl delete virtualmachinesnapshot web-01-snap-1 -n demo