Bookmark this page

Creating Custom Templates for Virtual Machines

Objectives

  • Generate custom templates to provision VMs.

Customized Template Creation

You can create custom templates to provision VMs in Red Hat OpenShift Virtualization. In the OpenShift web console, navigate to the VirtualizationCatalog to browse and select from the catalog of available operating systems for cluster templates, or define new ones by navigating to VirtualizationTemplates and create and manage cluster templates.

Figure 6.11: Virtualization catalog

You cannot access custom templates within a specific project from other projects. To make a custom template available to all projects, create the template in the openshift namespace, where the cluster templates are. Use the oc get templates -n openshift command to view a list of available templates to all projects.

Navigate to VirtualizationTemplates and click Create Template. Customize the parameters in the YAML editor and click Create.

YAML Template Definitions

The following YAML template includes the required parameters for a manifest that you can either paste into the YAML template creation editor in the web console or add by using the oc command-line client.

apiVersion: template.openshift.io/v1
kind: Template
metadata:
  name: ${NAME} 1
  namespace: template-create 2
  labels:
    os.template.kubevirt.io/rhel8: "true" 3
    template.kubevirt.io/type: vm 4
    workload.template.kubevirt.io/server: "true" 5
  annotations:
    description: VM template example
    iconClass: icon-rhel
    name.os.template.kubevirt.io/rhel8: Rhel 8.0 VM
    openshift.io/display-name: Rhel 8.0 VM
...output omitted...
spec:
  dataVolumeTemplates:
    - apiVersion: cdi.kubevirt.io/v1beta1
      kind: DataVolume
      metadata:
        name: ${NAME}
      spec:
        storage:
          resources:
            requests:
              storage: 30Gi
          storageClassName: ocs-external-storagecluster-ceph-rbd
        source: 6
          http:
            url: "http://utility.lab.example.com:8080/openshift4/images/mariadb-server.qcow2"
  running: false
  template:
    ...output omitted...
        devices:
          disks: 7
            - disk:
                bus: virtio
              name: rootdisk
            - disk:
                bus: virtio
              name: cloudinitdisk
          interfaces:
            - masquerade: {}
              name: default
              model: virtio
      ...output omitted...
      hostname: ${NAME}
      networks: 8
        - name: default
          pod: {}
      terminationGracePeriodSeconds: 180
      volumes:
        - dataVolume:
            name: ${NAME}
          name: rootdisk
        - cloudInitNoCloud: 9
            userData: |-
              #cloud-config
              user: ${CLOUD_USER_PASSWORD}
              password: ${CLOUD_USER_PASSWORD}
              chpasswd: { expire: False }
          name: cloudinitdisk
parameters: 10
- name: NAME
  description: VM name
  generate: expression
  from: 'template-create-vm'
- name: CLOUD_USER_PASSWORD
  description: User name and password for the cloud-init user cloud-user
  generate: expression
  from: 'developer'

General parameters found in a template manifest:

1

Template name

2

Project namespace

3

Operating system template

4

Template type

5

Workload

6

Data source

7

Storage volumes

8

Networking interfaces

9

Cloud-init

10

Customize parameters

Customizable Template Parameters

When creating custom templates, many parameters provide the appropriate information, definitions, and supporting descriptions.

Review each category to view the default selections and the customizable options. These categories store the template definitions that define cluster resources:

  • Details: Overview of the template definition.

  • YAML: YAML view of the template definition.

  • Scheduling: Add, edit, or remove node selectors, tolerations, affinity rules, dedicated resources, and eviction strategies.

  • Network interfaces: Add, edit, or remove network interfaces.

  • Disks: Add, edit, or remove disks.

  • Scripts: cloud-init, sysprep, and SSH access configurations.

  • Parameters: Add, edit, or remove additional parameters.

Creating a VM from a Customized Template

After you create a custom template, it is listed among the available virtual machine templates. From the web console, navigate to VirtualizationCatalog and browse through the list of templates. To refine your search, use the search bar or the provided filters.

Figure 6.12: Search for VM template

Select the card of the template to create a VM on. Review the template information, change the CPU | Memory or VM name if needed, and then click Quick create VirtualMachine. If you need additional customizations, then select Customize VirtualMachine and follow the prompts.

Figure 6.13: Create a template

Navigate to VirtualizationVirtualMachines to view the created VM.

Figure 6.14: View VMs

References

For more information, refer to the Creating Virtual Machine from Templates 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-creating-vms-from-templates

Revision: do316-4.14-d8a6b80