Bookmark this page

Guided Exercise: Create and Access a Virtual Machine

Create a Red Hat Enterprise Linux VM from an existing MariaDB image and use it to manage a database server.

Outcomes

  • Create a VM.

  • Access the VNC console with the virtctl command.

  • Access the database with the oc port-forward command.

  • Manage the VM from the command line.

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

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

This command ensures that all resources are available for this exercise.

Instructions

  1. As the admin user, locate and navigate to the Red Hat OpenShift web console.

    1. Use the terminal to log in to your Red Hat 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. Identify the URL for the OpenShift web console.

      [student@workstation ~]$ oc whoami --show-console
      https://console-openshift-console.apps.ocp4.example.com
    3. Open a web browser and navigate to https://console-openshift-console.apps.ocp4.example.com.

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

  2. Create a VM that contains a preconfigured MariaDB database server.

    1. From the OpenShift web console, locate the left panel. If you do not see the left panel, then click the main menu icon at the upper left of the web console.

    2. Navigate to VirtualizationVirtualMachines. Select the Projectcreating-accessing-vms project.

      Figure 2.5: Select the project
    3. Navigate to Create virtual machineFrom template.

      Figure 2.6: Create the VM from a template
    4. Select the Red Hat Enterprise Linux 8 VM template and click the Customize VirtualMachine button.

      Figure 2.7: Select the Red Hat Enterprise Linux 8 template
    5. Specify databasevm as the VM name.

    6. Select the Disk sourceURL (creates PVC) menu option.

    7. Specify http://utility.lab.example.com:8080/openshift4/images/mariadb-server.qcow2 in the Import URL field.

      Figure 2.8: Select the disk source
    8. From the Optional parameters section, enter redhat123 for the CLOUD_USER_PASSWORD field.

      Figure 2.9: Add the password
    9. Click the Customize VirtualMachine parameters button.

    10. Select the Scripts tab and then click the Cloud-init Edit icon.

      Figure 2.10: Select Cloud-init Edit
    11. Change the Cloud-init user to operator1 and click Apply.

      Figure 2.11: Change the Cloud-init User
    12. Navigate to the Overview tab. Click the edit icon for CPU | Memory. Increase the memory value to 4 GiB.

      Figure 2.12: Increase VM memory
    13. Select the Network Interface tab and confirm that the network interface type is masquerade.

      Figure 2.13: Network interface definition
    14. Select the Disk tab and confirm that the attached disks include cloudinitdisk and rootdisk.

      Figure 2.14: Disk definition
    15. Review the settings and click Create Virtual Machine to create the VM.

      Figure 2.15: Create VirtualMachine
  3. Review the details of your new VM.

    1. From the VM Overview tab, wait until the Status changes to Running before continuing this exercise.

      Figure 2.16: Review the VM
  4. Access the VNC console with the virtctl command and retrieve the status of the database server.

    1. In your terminal, select the creating-accessing-vms project.

      [student@workstation ~]$ oc project creating-accessing-vms
      Now using project "creating-accessing-vms" on server "https://api.ocp4.example.com:6443".
    2. List the available VM instances.

      [student@workstation ~]$ oc get vmi
      NAME                  AGE   PHASE     IP          NODENAME   READY
      databasevm	      3m    Running   10.8.2.12   worker02   True
    3. Open a new terminal window and use the virtctl vnc command to access the VM's VNC console. Log in to the console as the operator1 user with redhat123 as the password, as previously defined in the Optional parameters settings.

      [student@workstation ~]$ virtctl vnc databasevm
      Figure 2.17: virtctl VNC login

      Note

      You can find the credentials in the OpenShift web console. Navigate to VirtualizationVirtualMachines and select your VM. Click the Console tab and then click Guest login credentials.

    4. Retrieve the status of the database server.

      [cloud-user@databasevm ~]$ systemctl status mariadb
       mariadb.service - MariaDB 10.3 database server
         Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled)...
         Active: active (running)...
      ...output omitted...
  5. Access the MariaDB service with the oc port-forward command.

    1. Open a new terminal window and use the oc get pods command to determine the name of the virt-launcher pod of the VMI. Your virt-launcher pod has a different name from the following example.

      [student@workstation ~]$ oc get pods
      NAME                             READY   STATUS    RESTARTS   AGE
      virt-launcher-databasevm-g92gf   1/1     Running   0          9m22s
      [student@workstation ~]$ PODNAME=virt-launcher-databasevm-g92gf
      [student@workstation ~]$ echo $PODNAME
      virt-launcher-databasevm-g92gf
    2. Use the oc port-forward command to connect to the virt-launcher pod of the VMI. Specify the 33066:3306 ports.

      [student@workstation ~]$ oc port-forward $PODNAME 33066:3306
      Forwarding from 127.0.0.1:33066 -> 3306
      Forwarding from [::1]:33066 -> 3306

      A successful connection does not return your console to the command-line prompt. This behavior is expected.

    3. Open another terminal window and use the mysql command to connect to the MariaDB database on 127.0.0.1. Log in as the devuser user with developer as the password, and specify the port with the -P 33066 option.

      [student@workstation ~]$ mysql -u devuser -h 127.0.0.1 -P 33066 -p
      Enter password: developer
      Welcome to the MariaDB monitor. Commands end with ; or \g.
      ...output omitted...
    4. The VM already contains a loaded database named sakila that has several tables. Retrieve the data from the actor and city tables.

      MariaDB [(none)]> USE sakila;
      ...output omitted...
      Database changed
      MariaDB [sakila]> SELECT * FROM actor;
      +----------+-------------+--------------+---------------------+
      | actor_id | first_name  | last_name    | last_update         |
      +----------+-------------+--------------+---------------------+
      |        1 | PENELOPE    | GUINESS      | 2006-02-15 04:34:33 |
      |        2 | NICK        | WAHLBERG     | 2006-02-15 04:34:33 |
      |        3 | ED          | CHASE        | 2006-02-15 04:34:33 |
      |        4 | JENNIFER    | DAVIS        | 2006-02-15 04:34:33 |
      ...output omitted...
      
      MariaDB [sakila]> SELECT * FROM city;
      +---------+----------------------------+------------+---------------------+
      | city_id | city                       | country_id | last_update         |
      +---------+----------------------------+------------+---------------------+
      |       1 | A Corua (La Corua)         |         87 | 2006-02-15 04:45:25 |
      |       2 | Abha                       |         82 | 2006-02-15 04:45:25 |
      |       3 | Abu Dhabi                  |        101 | 2006-02-15 04:45:25 |
      |       4 | Acua                       |         60 | 2006-02-15 04:45:25 |
      ...output omitted...
    5. Exit the database.

      MariaDB [sakila]> EXIT;
      bye
    6. Return to the terminal that is executing the oc port-forward command. Press Ctrl+C to end the connection.

      [student@workstation ~]$ oc port-forward virt-launcher-databasevm-4qqj6 33066:3306
      Forwarding from 127.0.0.1:33066 -> 3306
      Forwarding from [::1]:33066 -> 3306
      Handling connection for 33066
      ​^C[student@workstation ~]$
  6. Manage the VM instance from the command line.

    1. Use the virtctl command to stop the VMI and then delete the VM with the oc delete command.

      [student@workstation ~]$ oc get vmi
      NAME                      AGE   PHASE     IP          NODENAME    READY
      databasevm                3m    Running   10.8.2.12   worker02    True
      [student@workstation ~]$ virtctl stop databasevm
      VM databasevm was scheduled to stop
      [student@workstation ~]$ oc delete vm databasevm
      virtualmachine.kubevirt.io "databasevm" deleted
    2. Confirm that the resource no longer exists.

      [student@workstation ~]$ oc get vmi
      No resources found in creating-accessing-vms namespace.
      [student@workstation ~]$ oc get vm
      No resources found in creating-accessing-vms namespace.

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 accessing-create

Revision: do316-4.14-d8a6b80