Bookmark this page

Guided Exercise: Create Custom Templates for Virtual Machines

Create a Red Hat Enterprise Linux template based on a predefined image that contains a MariaDB server instance. You then use the custom template to create a VM that contains a database with preconfigured data.

Outcomes

  • Create a template with the mariadb-server image by defining the attributes.

  • Create a VM from the customized template and access it to confirm that the MariaDB service is up and running.

  • Delete the VM and the template.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command ensures that the cluster API is reachable. It also creates the template-create namespace and ensures that all resources are available for this exercise.

[student@workstation ~]$ lab start template-create

Instructions

  1. As the admin user, clone a template to the template-create namespace and name it template-create. Configure the eviction strategy as Live Migration. Configure the template to use a mariadb-server image that is hosted on a website. Configure the rootdisk to use the ocs-external-storagecluster-ceph-rbd storage class. The template must use the Server workload type. Configure the cloud-init service to create the developer user with developer as the password.

    1. Open a web browser and navigate to https://console-openshift-console.apps.ocp4.example.com.

    2. Click htpasswd_provider and log in as the admin user with redhatocp as the password.

    3. Navigate to VirtualizationTemplates. Select the All Projects project from the Projects list and search for rhel8-server-small. Click the vertical ellipsis icon and select Clone.

      Figure 6.15: Clone template
    4. In the Clone template form, change the Template name to template-create. Change the Template project to template-create. Leave the other form fields with their default values. Click Clone.

      Figure 6.16: Create template
    5. Navigate to VirtualizationTemplates. Select the template-create project from the Projects list and select the template-create template.

      Figure 6.17: Find template
    6. Select the Scheduling tab and click the pencil icon under Eviction strategy. Confirm that the checkbox for Live Migration is selected, and then click Save.

      Figure 6.18: Scheduling
    7. Select the Network interfaces tab and confirm that the default interface is on the Pod Networking network and that it uses the Masquerade type.

      Figure 6.19: Network review
    8. Select the Disks tab and detach the rootdisk disk. Click the vertical ellipsis icon next to the rootdisk line, and then click Detach. Click Detach to confirm.

      Figure 6.20: Detach disk
    9. Create a new rootdisk. Click Add disk. Complete the form by using the following information and then click Save.

      ParametersValue
      Use this disk as a boot source enabled
      Name rootdisk
      Source URL (creates PVC)
      URL http://utility.lab.example.com:8080/openshift4/images/mariadb-server.qcow2
      Size 30 GiB
      Interface virtio
      StorageClass ocs-external-storagecluster-ceph-rbd
    10. Review the storage settings to confirm that the rootdisk volume storage size is 30 GiB and that it uses the ocs-external-storagecluster-ceph-rbd storage class.

      Figure 6.21: Review disks
    11. Use the YAML editor to set the name of the rootdisk data volume to the name of the VM that the template creates. Navigate to the YAML tab and locate the spec.dataVolumeTemplates.metadata.name and spec.template.volumes.datavolume.name objects. Update the object values to use the ${NAME} template variable, and then click Save.

      apiVersion: template.openshift.io/v1
      kind: Template
      ...output omitted...
        spec:
          dataVolumeTemplates:
          - metadata:
              name: '${NAME}'
      ...output omitted...
          template:
            volumes:
            - name: rootdisk
              dataVolume:
                name: '${NAME}'
      ...output omitted...
    12. Configure Cloud-init to create the developer user with developer as the password. Select the Scripts tab and click Edit next to Cloud-init. In the User and Password fields, enter developer and then click Apply.

      Figure 6.22: Cloud-init
  2. Create a VM from the template-create template.

    1. Navigate to VirtualizationCatalog. On the right panel of filter options, under Template project, select the template-create project and select the newly created template Red Hat Enterprise Linux 8 VM.

      Figure 6.23: Use template
    2. Change the VirtualMachine name to template-create-vm and click Quick create VirtualMachine.

      Figure 6.24: Name of VM
    3. Wait for the template-create-vm VM to provision and display the Running status.

      Figure 6.25: VM status
  3. Access the template-create-vm VM.

    1. Navigate to VirtualizationVirtualMachines.

    2. Click the template-create-vm link to access the Overview tab for the VM.

    3. Navigate to the Console tab. Wait for the console to display the login prompt.

      Figure 6.26: VM console
    4. Use the console to log in as the developer user with developer as the password.

      template-create-vm login: developer
      Password: developer
      [developer@template-create-vm ~]$
    5. Confirm that the preconfigured database, sakila, is available. Use the mysql command to log in as the devuser with developer as the password.

      [developer@template-create-vm ~]$ mysql sakila -u devuser -p
      Enter password: developer
      Welcome to the MariaDB monitor. Command end with ; or \g.
      ...output omitted...
      MariaDB [sakila]>
    6. Exit the sakila database and the template-create-vm VM.

      MariaDB [sakila]> EXIT;
      Bye
      [developer@template-create-vm ~]$ exit
  4. Delete the template-create-vm VM and the template-create template.

    1. Navigate to VirtualizationVirtualMachines.

    2. Click the vertical ellipsis icon to the right of the template-create-vm VM and select Delete. Click Delete in the confirmation window.

      Figure 6.27: Delete VM
    3. Navigate to VirtualizationTemplates.

    4. Click the vertical ellipsis icon to the right of the template-create template row, and click Delete Template. Click Delete in the confirmation window.

      Figure 6.28: Delete template

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish template-create

Revision: do316-4.14-d8a6b80