Bookmark this page

Chapter 7.  Advanced Virtual Machine Management

Abstract

Goal

Create a snapshot, clone, and live migrate a virtual machine, and initiate node maintenance.

Objectives
  • Describe VM snapshots and execute snapshot management tasks.

  • Clone an existing VM's PVC as a new VM and as a new data volume to provision additional VMs.

  • Describe and manage VM live migrations.

  • Set a node into maintenance mode and describe how the OpenShift Virtualization update process affects VMs.

Sections
  • Manage Virtual Machine Snapshots (and Guided Exercise)

  • Cloning Virtual Machines (and Guided Exercise)

  • Performing Virtual Machine Live Migrations (and Guided Exercise)

  • Node Maintenance and OpenShift Virtualization Updates (and Guided Exercise)

Lab
  • Advanced Virtual Machine Management

Manage Virtual Machine Snapshots

Objectives

  • Describe and manage VM snapshots.

Take Snapshots of Virtual Machines

A snapshot is a view of a Virtual Machine (VM) that includes the operating system and applications on all the attached disks at a given point in time. A developer might take a snapshot of a VM before changing it. The snapshot can protect against errors that might have unintended consequences. In the event of a problem, the developer can revert to the state of the VM that is recorded in the snapshot.

With Red Hat OpenShift Virtualization, you can take live snapshots of running VMs, as well as snapshots of stopped VMs.

When the VM is running, OpenShift Virtualization communicates with the Quick EMUlator (QEMU) guest agent that is running inside the VM to quiesce the file systems before taking the snapshot. This mechanism ensures that the file systems are in a consistent state. If you do not install the QEMU guest agent, then you can still take live snapshots, but the file systems might not be consistent. The default Red Hat templates come with the QEMU guest agent.

Disk snapshots rely on the back-end storage. For example, Red Hat OpenShift Data Foundation (ODF) provides a storage solution that supports snapshots. Other storage technologies might not provide that feature. The Container Storage Interface (CSI) driver that Kubernetes uses to pilot the back-end storage must also support the snapshot feature. The references section provides a link to the list of drivers and their snapshot support.

If one VM disk relies on back-end storage that does not support snapshots, then OpenShift Virtualization displays a warning message when you take the snapshot. When you revert the snapshot, OpenShift Virtualization does not restore the data for that specific disk, which stays unchanged after reverting.

Red Hat supports taking a live snapshot of a VM with hot-plugged disks. However, the snapshot includes only the hot-plugged disks in the VM specification. Hot-plugged disks are disks that you attach to a running VM.

A VM snapshot also records the VM configuration, such as the number of CPUs or the amount of memory. If you change the VM configuration after taking the snapshot, then OpenShift Virtualization restores the original configuration when you revert the snapshot.

Create Snapshots

Use the OpenShift web console to create a VM snapshot. Navigate to VirtualizationVirtualMachines and then select the VM to snapshot.

If you did not install the QEMU guest agent in the VM, then Red Hat recommends stopping the VM before taking the snapshot. Click ActionsStop and wait for the VM to stop.

Navigate to the Snapshots tab and then click Take snapshot to initiate the snapshot. Complete the form and then click Save. Consider providing a meaningful name and description to help with snapshot identification.

A warning message is displayed if the VM includes disks that do not support snapshots, as shown in the following screen capture. The nfs1 disk uses the nfs-storage storage class, which does not support snapshots.

Figure 7.1: Taking a snapshot of a VM with disks that do not support snapshots

If you stopped the VM before taking the snapshot, then restart it.

You create a snapshot from the command line by declaring a VirtualMachineSnapshot resource in a file. You trigger the snapshot from the file by running the oc apply -f resourcefile.yaml command. The following resource file prepares a snapshot named mariadb-server-2024-3-11 for the mariadb-server VM.

apiVersion: snapshot.kubevirt.io/v1alpha1
kind: VirtualMachineSnapshot
metadata:
  name: mariadb-server-2024-3-11
spec:
  source:
    apiGroup: kubevirt.io
    kind: VirtualMachine
    name: mariadb-server

Restore Snapshots

Before you can restore a snapshot, you must stop the VM.

After you stop the machine, navigate to the Snapshots tab, and then locate the snapshot to restore. You can click a snapshot name to access the description that you provided at creation time.

To initiate the restoration process from the OpenShift web console, navigate to the Snapshots tab, and then locate the snapshot to restore. Click the vertical ellipsis icon at the right of the snapshot, and then click Restore. You can restart the VM after the restoration completes.

From the command line, use the oc get virtualmachinesnapshot command to list the available snapshots in your project:

[user@host ~]$ oc get virtualmachinesnapshot
NAME                     SOURCEKIND     SOURCENAME     PHASE     READYTOUSE ...
db-before-schema         VirtualMachine mariadb-server Succeeded true       ...
mariadb-server-2024-3-11 VirtualMachine mariadb-server Succeeded true       ...

To restore a snapshot, create a VirtualMachineRestore resource in a file. and then use the oc apply -f resourcefile.yaml command to initiate the process. The following resource file restores the mariadb-server-2024-3-11 snapshot for the mariadb-server VM.

apiVersion: snapshot.kubevirt.io/v1alpha1
kind: VirtualMachineRestore
metadata:
  name: restore-db
spec:
  target:
    apiGroup: kubevirt.io
    kind: VirtualMachine
    name: mariadb-server
  virtualMachineSnapshotName: mariadb-server-2024-3-11

Delete Snapshots

Because snapshots consume disk space on back-end storage, delete the snapshots that you are no longer using.

To delete a snapshot, navigate to the Snapshots tab, click the vertical ellipsis icon at the right of the snapshot to delete, and then click Delete. You do not need to stop the VM to delete its snapshots.

From the command line, use the oc delete command. The following example deletes the mariadb-server-2024-3-11 snapshot:

[user@host ~]$ oc delete virtualmachinesnapshot mariadb-server-2024-3-11
virtualmachinesnapshot.snapshot.kubevirt.io "mariadb-server-2024-3-11" deleted

Update the Virtual Machine Configuration

A VM snapshot includes the VM configuration. If you change the VM disk configuration after taking a snapshot, then review the following considerations before restoring the snapshot:

  • If you removed a disk, then OpenShift Virtualization re-creates the disk. If the initial disk does not support snapshots, then OpenShift Virtualization attaches a new blank disk with the same characteristics as the original disk. With this type of disk, OpenShift Virtualization does not restore the data from the time that you took the snapshot. That data is lost.

  • If you add a disk, then OpenShift Virtualization removes the disk when you restore the snapshot.

Copy Kubernetes Volume Snapshots

In addition to restoring a complete VM from a snapshot, you can also duplicate individual VM disks from their snapshots.

VM snapshots rely on Kubernetes volume snapshots. When you take a snapshot of a VM, OpenShift Virtualization creates a Kubernetes volume snapshot for each VM disk. For example, if the VM has four disks that use a storage class that supports snapshots, then OpenShift Virtualization creates four Kubernetes volume snapshots, one for each disk.

To list the volume snapshots, use the OpenShift web console, and navigate to StorageVolumeSnapshots. You can also run the oc get volumesnapshots command:

[user@host ~]$ oc get volumesnapshots
NAME                                               SOURCEPVC
vmsnapshot-0575...ce32-volume-dbdata          ...  mariadb-server-dbdata-dv01d
vmsnapshot-0575...ce32-volume-dbredo          ...  mariadb-server-dbredo-6zt45
vmsnapshot-0575...ce32-volume-dbundo          ...  mariadb-server-dbundo-etpi5
vmsnapshot-0575...ce32-volume-mariadb-server  ...  mariadb-server

The preceding output lists the Kubernetes volume snapshots and their associated PVC. With this information, you can identify the snapshots that correspond to a VM disk.

Duplicate an Individual Disk and Attach it to a VM

You can restore a Kubernetes volume snapshot as a new PVC. You can then attach the new PVC as a VM disk and access its content.

Duplicating a VM disk snapshot and attaching it to a VM is useful for inspecting the disk's content or for using the same initial data with several VMs.

To restore a Kubernetes volume snapshot and attach it to a VM, use the following steps:

  • Navigate to StorageVolumeSnapshots.

  • Identify the Kubernetes volume snapshot to restore from the associated source PVC, which corresponds to the VM disk to copy.

  • Click the vertical ellipsis icon at the right of the snapshot, and then click Restore as new PVC. Complete the form and then click Restore.

    Figure 7.2: Restoring a volume snapshot as a PVC
  • Navigate to VirtualizationVirtualMachines, select the VM you want to attach the PVC, and then click ActionsStop to stop the VM.

  • Navigate to the Configuration tab and then click Add disk. Select Use an existing PVC for the Source field, and set the PersistentVolumeClaim field to the name of the restored PVC. Click Save to attach the disk.

  • Click ActionsStart to start the VM.

References

For more information about installing the VirtIO driver, refer to the Installing the QEMU Guest Agent and VirtIO Drivers section in the Red Hat OpenShift Container Platform 4.14 documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/virtualization/index#virt-installing-qemu-guest-agent

For more information about managing virtual machine snapshots, refer to the Backup and Restore by Using VM Snapshots section in the Red Hat OpenShift Container Platform 4.14 documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/virtualization/index#backup-and-restore

For more information about volume snapshots, refer to the CSI Volume Snapshots section in the Red Hat OpenShift Container Platform Storage guide at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/storage/index#persistent-storage-csi-snapshots

Live Snapshots in OpenShift Virtualization

CSI Drivers and Their Snapshot Support Status

Revision: do316-4.14-d8a6b80