Bookmark this page

Lab: Use a Template to Deploy Virtual Machines

Create a virtual machine template, use that template to deploy a virtual machine, and put a cluster node into maintenance mode.

Outcomes

  • Create a virtual machine template.

  • Deploy virtual machines from a template.

  • Manage user access rights.

  • Put a cluster node into maintenance mode.

If you did not reset your workstation and server machines at the end of the last chapter, then save any work that you want to keep from earlier exercises on those machines and reset them now.

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 and creates the review-cr2 namespace.

[student@workstation ~]$ lab start review-cr2

Specifications

The Red Hat OpenShift cluster API endpoint is https://api.ocp4.example.com:6443 and the OpenShift web console is available at https://console-openshift-console.apps.ocp4.example.com. Use the admin user with redhatocp as the password to log in to your OpenShift cluster. The oc command is available on the workstation machine. During this exercise, you also use the developer user account with developer as the password.

  • Create a virtual machine template in the review-cr2 project with the following properties:

    ParameterValue
    Project review-cr2
    Template name and display name dev-web-rhel8
    Template provider Red Hat Training
    Operating system Red Hat Enterprise Linux 8.0 or higher
    Boot disk URLhttp://utility.lab.example.com:8080/openshift4/images/helloworld.qcow2
    Size Tiny
    Workload type Server
    Root disk size10 GiB
    Root disk interface virtio
    Root disk storage class ocs-external-storagecluster-ceph-rbd-virtualization

    Then, modify the template to use the template variable, ${NAME}, to configure the name of the rootdisk data volume to the name of the VM that the template creates.

    The lab command prepares the /home/student/DO316/labs/review-cr2/template_parameters.txt file that lists these parameters. You can copy and paste the parameters from the file into the OpenShift web console.

  • Grant admin access to the developer user for the review-cr2 project.

  • As the developer user, create a VM named web1 in the review-cr2 namespace. Use the dev-web-rhel8 template.

  • Put the worker02 cluster node into maintenance mode and move its workload to the remaining nodes.

  • Ensure that the web1 VM is running before grading your work.

  1. From a command line on the workstation machine, use the oc command to log in to your OpenShift cluster as the admin user with redhatocp as the password.

    Open a web browser and log in to the OpenShift web console at https://console-openshift-console.apps.ocp4.example.com.

    1. From a command line, log in to your OpenShift cluster as the admin user.

      [student@workstation ~]$ oc login -u admin -p redhatocp \
        https://api.ocp4.example.com:6443
      Login Successful
      ...output omitted...
    2. 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.

  2. Create the dev-web-rhel8 VM template in the review-cr2 project.

    1. Navigate to VirtualizationTemplates and ensure that All Projects is selected in the Project list. Enter rhel8-server in the Search by Name field.

    2. Click the vertical ellipsis icon next to the rhel8-server-tiny line, and then click Clone.

    3. Complete the form by using the following information and then click Clone.

      FieldValue
      Name dev-web-rhel8
      Template project review-cr2
      Template display name dev-web-rhel8
      Template provider Red Hat Training
    4. From the dev-web-rhel8 Details page, navigate to the Disks tab.

    5. Click the vertical ellipsis icon next to the rootdisk line, and then click Detach. Click Detach to confirm.

    6. Click Add disk. Complete the form by using the following information and then click Save.

      FieldValue
      Use this disk as a boot source enabled
      Name rootdisk
      Source URL (creates PVC)
      URL http://utility.lab.example.com:8080/openshift4/images/helloworld.qcow2
      Size 10 GiB
      Interface virtio
      StorageClass ocs-external-storagecluster-ceph-rbd-virtualization
    7. 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 values of the objects 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...
  3. Grant admin access to the developer user for the review-cr2 project.

    1. From a command-line window on the workstation machine, use the oc create rolebinding command to grant admin access to the developer user.

      [student@workstation ~]$ oc create rolebinding admin --clusterrole=admin \
        --user=developer -n review-cr2
      rolebinding.rbac.authorization.k8s.io/admin created
    2. Confirm that the developer user has admin rights.

      [student@workstation ~]$ oc get rolebindings -n review-cr2 -o wide
      NAME   ROLE               AGE   USERS      GROUPS    SERVICEACCOUNTS
      admin  ClusterRole/admin  39s   developer
      ...output omitted...
  4. As the developer user, use the dev-web-rhel8 template to create the web1 VM in the review-cr2 namespace.

    1. From the OpenShift web console, click adminLog out.

    2. Select htpasswd_provider and log in as the developer user with developer as the password.

    3. Select the Administrator perspective.

    4. Navigate to VirtualizationCatalog and then set the project to review-cr2.

    5. Click User templates and then select the dev-web-rhel8 template.

    6. Set the Virtual Machine Name field to web1 and then click Quick create VirtualMachine. Wait for the web1 VM to have the Running status.

  5. Set the worker02 cluster node in maintenance mode and drain its workload.

    1. From the command-line window on the workstation machine, use the oc adm cordon command to mark the node as unschedulable.

      [student@workstation ~]$ oc adm cordon worker02
      node/worker02 cordoned
    2. Confirm that the node has the SchedulingDisabled status.

      [student@workstation ~]$ oc get nodes
      NAME       STATUS                     ROLES                         AGE   VERSION
      master01   Ready                      control-plane,master,worker   19d   v1.27.10+28ed2d7
      master02   Ready                      control-plane,master,worker   19d   v1.27.10+28ed2d7
      master03   Ready                      control-plane,master,worker   19d   v1.27.10+28ed2d7
      worker01   Ready                      worker                        19d   v1.27.10+28ed2d7
      worker02   Ready,SchedulingDisabled   worker                      19d   v1.27.10+28ed2d7
    3. Run the oc adm drain command to evacuate all workloads from the node. This command might take a few minutes to complete.

      [student@workstation ~]$ oc adm drain worker02 \
        --delete-emptydir-data --ignore-daemonsets
      node/worker02 already cordoned
      ...output omitted...
      node/worker02 drained

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab grade review-cr2

Finish

On the workstation machine, change to the student user home directory and 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 review-cr2

Revision: do316-4.14-d8a6b80