Bookmark this page

Guided Exercise: Use a Virtual Machine Template

Define the attributes of a product template based on a predefined image to create a medium-sized VM that contains a web server.

Outcomes

  • Create a VM by using a product template.

  • Define the attributes of a product template. Define the boot source as a predefined image and define the size, the workload type, and the custom access credentials.

  • Access the VM to confirm that the web server is up and running.

  • Delete the VM.

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-intro namespace and exercise resources.

The OpenShift Virtualization operator auto-imports default boot images for Red Hat provided templates. To enable custom boot sources for these templates, the lab command ensures that the enableCommonBootImageImport feature is disabled at the start of the exercise.

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

Instructions

  1. As the admin user, navigate to the Red Hat OpenShift web console, and confirm that the web-server-image data volume is available in the vm-images namespace.

    1. From the command line, log in to your Red Hat OpenShift cluster as the admin user with redhatocp as the password.

      [student@workstation ~]$ oc login -u admin -p redhatocp \
        https://api.ocp4.example.com:6443
      Login Successful
      ...output omitted...
    2. Confirm that the web-server-image data volume is accessible in the vm-images namespace, and that the corresponding PVC has a 10 GiB capacity and the Bound status.

      [student@workstation ~]$ oc get datavolume -n vm-images
      NAME               PHASE       PROGRESS   RESTARTS   AGE
      web-server-image   Succeeded   100.0%                69s
      [student@workstation ~]$ oc get pvc -n vm-images
      NAME               STATUS   VOLUME            CAPACITY   ACCESS MODES ...
      web-server-image   Bound    pvc-e32c...89b5   10Gi       RWX          ...
    3. Open a web browser and navigate to https://console-openshift-console.apps.ocp4.example.com. Select htpasswd_provider and log in as the admin user with redhatocp as the password when prompted.

  2. Use the Red Hat Enterprise Linux 8.0+ VM template to create the helloworld VM in the template-intro namespace. Define the boot source as the web-server-image PVC in the vm-images project.

    1. From the web console left panel, navigate to VirtualizationCatalog. Select the template-intro project and use the search bar to locate and click the Red Hat Enterprise Linux 8.0+ VM template. Click Customize VirtualMachine.

    2. Complete the form according to the following table:

      ParametersValue
      Namehelloworld
      Disk sourcePVC (clone PVC)
      PVC projectvm-images
      PVC nameweb-server-image
    3. Leave the other form fields with their default values and click Customize VirtualMachine parameters.

    4. Navigate to the Scripts tab. Click Edit. Define developer as the user and developer as the password in the Cloud-init form, and click Apply.

    5. Navigate to the Overview tab, and confirm that the network interface type is Masquerade. Confirm that the attached disks include cloudinitdisk and rootdisk.

    6. Confirm that Server is selected for Workload profile. Review the settings of your VM and click Create VirtualMachine.

  3. Observe the status of the helloworld VM and its resources by using the command line.

    1. Use the oc get command to observe the progress of the VM's data volume.

      [student@workstation ~]$ oc get datavolume -n template-intro
      NAME          PHASE            PROGRESS   RESTARTS   AGE
      helloworld    CloneInProgress  70.0%                 25s
      [student@workstation ~]$ oc get datavolume -n template-intro
      NAME          PHASE            PROGRESS   RESTARTS   AGE
      helloworld    Succeeded        100.0%                45s
    2. Use the oc get command to observe the status of the helloworld VM.

      [student@workstation ~]$ oc get vm -n template-intro
      NAME         AGE   STATUS    READY
      helloworld   2m    Running   True
  4. Connect to the console of the helloworld VM by using the virtctl console command and log in as the developer user with developer as the password, which you defined in a previous step.

    1. From the command line, connect to the console of the helloworld VM by using the virtctl console command. Press Enter to open the login prompt.

      [student@workstation ~]$ virtctl console helloworld -n template-intro
      Successfully connected to helloworld console. The escape sequence is ^]
      
      Red Hat Enterprise Linux 8.5 (Ootpa)
      Kernel 4.18.0-348.el8.x86_64 on an x86_64
      
      Activate the web console with: systemctl enable --now cockpit.socket
      
      helloworld login:
    2. Log in as the developer user with developer as the password.

      hellworld login: developer
      Password: developer
      [developer@helloworld ~]$
  5. Use the systemctl status httpd command to confirm that the Apache httpd service is loaded and running.

    1. Confirm that the Apache httpd service is loaded and running. Press q to exit the status log.

      [developer@helloworld ~]$ systemctl status httpd
      ● httpd.service - The Apache HTTP Server
         Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor prese>
         Active: active (running) since Mon 2024-03-11 15:14:33 EDT; 5min ago
           Docs: man:httpd.service(8)
       Main PID: 947 (httpd)
         Status: "Running, listening on: port 80"
      ...output omitted...
    2. Use the curl command to confirm that the web server responds to requests. The command returns some HTML content.

      [developer@helloworld ~]$ curl localhost
      <!DOCTYPE html>
      <html>
        <head>
          <title>Hello, World!</title>
        </head>
        <body>
          <p>Hello, World! Welcome to Red Hat Training.</p>
        </body>
      </html>
  6. Shut down and delete the helloworld VM.

    1. Use the sudo systemctl poweroff command to shut down the helloworld VM.

      [developer@helloworld ~]$ sudo systemctl poweroff
      ...output omitted...
      [ 2172.699653] reboot: Power down
      
      You were disconnected from the console. This has one of the following reasons:
      ...output omitted...
    2. Delete the helloworld VM with the oc command.

      [student@workstation ~]$ oc delete vm helloworld -n template-intro
      virtualmachine.kubevirt.io "helloworld" deleted

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-intro

Revision: do316-4.14-d8a6b80