Bookmark this page

Creating and Deploying Virtual Machines with Templates

Objective

After completing this section, you should be able to create a Linux virtual machine template and deploy new virtual machines using that template.

Rapid Deployments Using Templates

A template is a copy of a preconfigured virtual machine, used to simplify the subsequent, repeated creation of similar virtual machines. Templates capture the installed software, software configuration, and hardware configuration of the original virtual machine. When administrators need to deploy multiple, mostly identical machines, it can be beneficial to use templates instead of doing multiple installations.

A template can be seen as a bit-for-bit copy of the original disk image, and bears many resemblances to imaging a machine with tools like Clonezilla or Ghost. Just like an automated installation, such as Kickstart, this helps maintain consistency across machines, making management and troubleshooting easier.

When working with virtual machines, basing machines on a template can also help reduce memory usage. Since machines based on the same template will be very similar, Kernel Same-page Merging (KSM) has a higher chance of finding duplicate memory pages on those machines that can be merged in physical memory.

Warning

One of the challenges involved in using templates is making sure that no identifying information, such as references to MAC addresses or SSL certificates, are left behind, because issues may occur when multiple machines are deployed from the same template. This process is called sealing the image.

Creating a Template from a Virtual Machine

The first step to create a template is to install a new virtual machine. This can be done manually, or you may automate the installation using a tool such as Kickstart. After the virtual machine finishes installing, log into the virtual machine and perform any additional customization that is needed. Again, you can add customizations manually, or use a tool such as Ansible to do the configuration for you. Your goal is to get the virtual machine to a state where everything works as expected for the desired function when the VM is started.

Once the virtual machine has been configured, remove all information unique to the virtual machine. This is referred to as sealing the image. Unique information includes hardware information specific to the virtual machine, such as MAC addresses; unique system configurations, such as the host name and static IP addresses; and possibly logs and other data. Depending on the operating system of the virtual machine, you may need to perform these steps manually, or there may be tools available to seal the image for you. For example, Linux virtual machines can typically use the virt-sysprep utility on a stopped virtual machine. A running Windows virtual machine typically allows using sysprep.exe to initiate a System Out-of-Box-Experience (OOBE). The process for sealing a Windows virtual machine concludes when you shut down the virtual machine. The RHV-M Administration Portal also provides an option where it will seal the virtual machine image prior to creating a template. Be aware that this option does not work for all operating systems.

One important thing to keep in mind is that many items that were stripped out of the virtual machine during the sealing process are recreated when the virtual machine boots up for the first time. As such, once a virtual machine has been sealed, do not start it until after you have made a template. If you accidentally start the virtual machine before you create the template, you will have to go through the process of sealing the image again.

A new template can be created from the RHV-M Administration Portal by clicking Compute in the menu bar, and then selecting Virtual Machines. Highlight the entire row of the virtual machine that will be used to create the template by clicking an empty cell in that row. With the virtual machine row highlighted, click the three vertical dot menu located to the right of the Create Snapshot button, and select Make Template. You will be prompted to specify details for the template in the New Template window, and then the template is created.

After a template has been created, it is always a good idea to test the template by creating a new virtual machine based on the template.

The following window is presented when creating a new template.

Figure 8.3: New Template window

The settings for the New Template window are described below:

Name

The name of the template. This name is listed on the Templates tab in the Administration Portal. It must be a unique name, and can include any combination of uppercase and lowercase letters, numbers, hyphens, or underscores.

Description

A limited description of the template. This field is recommended but not mandatory.

Comment

A field for adding plain text, human-readable comments about the template. For example, you might use this to elaborate on the purpose or even the usage of a template.

Cluster

The cluster with which the template is associated. By default, this is the same as the original virtual machine cluster. However, any cluster within the same data center can be selected.

CPU Profile

An advanced feature used to limit how much CPU capacity a virtual machine can use on a host. The default setting imposes no limit. Use CPU Profile to define the maximum amount of processing capability a virtual machine can access on its host. This is expressed as a percent of the total processing capability available on a host. See the Administration Guide for more information.

Disks Allocation
  • Alias: An alias for the virtual machine disk used by the template. By default, the alias is set to the same value as that of the source virtual machine.

  • Virtual Size: The current actual size of the virtual disk used by the template. This value cannot be edited, and is provided for reference only.

  • Format: The format of the disks that the template should use. A QCOW2 format always implies that a disk is thin provisioned. The RAW format in file storage implies thin provisioned, while RAW on block storage implies preallocated virtual disks.

  • Target: The storage domain on which the virtual disk used by the template is stored. By default, the storage domain is set to the same value as that of the source virtual machine. Any storage domain in the cluster can be selected.

Allow All Users to access this Template

Specifies whether a template is public or private. A public template can be accessed by all users, whereas a private template can only be accessed by users with the TemplateAdmin or SuperUser roles.

Copy VM Permissions

Copies permissions set on the source virtual machine to the template.

Seal Template

Seals the template using the virt-sysprep command. Information on the actions completed by this command are in the "virt-sysprep Operations" appendix of the Red Hat Virtualization Virtual Machine Management Guide on the Red Hat Customer Portal. This option only works for Linux virtual machines.

If you are not able to use this option, you must manually seal the virtual machine image before using this window to create the template.

Using a Template to Create a New Virtual Machine

Once a template has been created from a virtual machine, administrators can create new virtual machines using that template. Consider this a test to validate that virtual machines created from a template do what you expect them to do. If the virtual machine used to create the template was not sealed, or was not sealed correctly, virtual machines created from the template could experience problems. If the first virtual machine created from the template works as expected, you know that additional virtual machines will also work.

You can create a new virtual machine from a template using the RHV-M Administration Portal. The process for creating a new virtual machine from a template is very similar to the process used for creating any other virtual machine.

There are a few significant differences:

  • From the New Virtual Machine window, select a template from the Template menu. Only templates associated with the current data center, based on the selected cluster, are listed.

  • When you select a template, many fields within the New Virtual Machine window are populated using the template. On the General tab, this includes Operating System, Instance Type, Optimized for, and network interfaces. Additionally, you cannot attach or create a hard disk, as this come from the template.

  • Clicking the Show Advanced Options button reveals additional options. Although much of this information is populated based on the selected template, you can make adjustments as needed.

  • The Resource Allocation tab has a section specifically related to templates. In the Storage Allocation section, you can specify how to provision the virtual disks for the new virtual machine.

    When choosing Thin, an overlay is used on top of the original template. This choice is more efficient for disk space, and the virtual machine is created more quickly, but the virtual machine cannot exist apart from the template. Editing a virtual machine which is still associated with a template will show the name of the template in the Template menu.

    When choosing Clone, the original template is cloned; the Format option becomes available to select either Raw or QCOW2 for each disk. Once created, this type of virtual machine no longer needs the template, and an edit of the virtual machine will show Blank in the Template menu.

  • Finally, after you create the new virtual machine, there is no need to perform an installation using Run Once. The virtual machine is already installed and should run normally. Be aware that depending on the operating system of the virtual machine, the initial boot may take longer than normal as items that were stripped out during the sealing process are recreated.

References

virt-sysprep(1) man page

Further information is available in the "Templates" chapter and the "virt-sysprep Operations" appendix of the Virtual Machine Management Guide for Red Hat Virtualization at https://access.redhat.com/documentation/en-us/red_hat_virtualization/4.3/html-single/virtual_machine_management_guide/index#chap-Templates

Revision: rh318-4.3-c05018e