Guided Exercise: Provision Persistent Data Volumes

Deploy a MySQL database with persistent storage from a PVC and identify the PV and storage provisioner that backs the application.

Outcomes

You should be able to do the following tasks:

  • Deploy a MySQL database with persistent storage from a PVC.

  • Identify the PV that backs the application.

  • Identify the storage provisioner that created the PV.

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

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

[student@workstation ~]$ lab start storage-volumes

Instructions

  1. Log in to the OpenShift cluster as the developer user with the developer password. Select the storage-volumes project.

    1. Use a web browser to navigate to the OpenShift web console at https://console-openshift-console.apps.ocp4.example.com.

    2. Log in by using Red Hat Identity Management with the developer username and the developer password.

    3. Select the Administrator view to access the administrative menu.

    4. On the HomeProjects page, select the storage-volumes project.

  2. Identify the default storage class for the cluster.

    1. Select the StorageStorageClasses menu option.

      Figure 5.18: Storage classes

      The nfs-storage storage class has the Default label.

  3. Use the registry.ocp4.example.com:8443/rhel8/mysql-80 container image to create a MySQL deployment named db-pod. Use the storage-volumes project. Add a service for the database.

    1. Select the WorkloadsDeployments menu option.

    2. Verify that the storage-volumes project is active and select the Create Deployment button.

    3. Use db-pod for the deployment name and change the image name to registry.ocp4.example.com:8443/rhel8/mysql-80.

    4. Add the environment variables.

      Table 5.5. MYSQL Environment Variables

      NameValue
      MYSQL_USERuser1
      MYSQL_PASSWORDmypa55w0rd
      MYSQL_DATABASEitems

    5. Select the Advanced optionsScaling link and set the replicas to one.

    6. Click Create. Wait for the blue circle to indicate that a single pod is running.

    7. Click NetworkingServicesCreate Service.

    8. Add the service values.

      Table 5.6. Service Fields

      Field nameValue
      Namedb-pod
      selectorapp=db-pod
      Port3306
      Target port3306

      Figure 5.19: Add Service
    9. Click the Create button.

  4. Add a 1 Gi, RWO PVC named db-pod-pvc to the deployment. Set the /var/lib/mysql directory as the mount path.

    1. Select the WorkloadsDeployments menu item.

    2. Click the three vertical dots in the row with the db-pod deployment, and select Add storage option.

    3. In the Add Storage form, click Create new claim.

    4. Add the following field values to the form.

      Table 5.7. Add PVC Storage Fields

      Field nameValue
      PersistentVolumeClaim namedb-pod-pvc
      Access modeRWO
      Size1 GiB
      Volume modeFilesystem
      Mount path/var/lib/mysql

    5. Click Save.

    6. Scroll down in the deployment details to the Volumes section.

    7. Select the db-pod-pvc link to see the PVC details.

      Figure 5.20: PVC Link
  5. Observe how the volume mount changed the deployment.

    1. Select the WorkloadsDeploymentsdp-podYAML tab.

    2. Observe the volumes and volumeMounts additions to the deployment.

      apiVersion: apps/v1
      kind: Deployment
      
      ...output omitted...
      
            volumes:
            - name: db-pod-pvc
              persistentVolumeClaim:
                claimName: db-pod-pvc
      
      ...output omitted...
      
              volumeMounts:
              - mountPath: /var/lib/mysql
                name: db-pod-pvc
      
      ...output omitted...
  6. Use a configuration map resource to add initialization data to the database.

    1. Observe the contents of the init-db.sql script that initializes the database.

      [student@workstation ~]$ cat \
      ~/DO180/labs/storage-volumes/configmap/init-db.sql
       DROP TABLE IF EXISTS `Item`;
       CREATE TABLE `Item` (`id` BIGINT not null auto_increment primary key, `description` VARCHAR(100), `done` BIT);
       INSERT INTO `Item` (`id`,`description`,`done`) VALUES (1,'Pick up newspaper', 0);
       INSERT INTO `Item` (`id`,`description`,`done`) VALUES (2,'Buy groceries', 1);
    2. Log in to the OpenShift cluster.

      [student@workstation ~]$ oc login -u developer -p developer \
        https://api.ocp4.example.com:6443
      Login successful.
      ...output omitted...
    3. Set the storage-volumes project as the active project.

      [student@workstation ~]$ oc project storage-volumes
      ...output omitted...
    4. Use the contents of the init-db.sql file to create a configuration map named init-db-cm.

      [student@workstation ~]$ oc create configmap init-db-cm \
      --from-file=/home/student/DO180/labs/storage-volumes/configmap/init-db.sql
      configmap/init-db-cm created
    5. Add the init-db-cm configuration map resource as a volume named init-db-volume to the deployment. Specify the volume type as configmap, and set the /var/db/config directory as the mount path.

      [student@workstation ~]$ oc set volumes deployment/db-pod \
      --add --name init-db-volume --type configmap --configmap-name init-db-cm \
      --mount-path /var/db/config
      deployment.apps/db-pod volume updated
    6. Start a remote shell session inside the container.

      [student@workstation ~]$ oc rsh deployment/db-pod
      sh-4.4$
    7. Use the mysql client to execute the database script in the /var/db/config/init-db volume.

      sh-4.4$ mysql -uuser1 -pmypa55w0rd items </var/db/config/init-db.sql
      mysql: [Warning] Using a password on the command line interface can be insecure.
      sh-4.4$
    8. Execute a query to verify the database contents.

      sh-4.4$ mysql -uuser1 -pmypa55w0rd items -e 'select * from Item;'
      mysql: [Warning] Using a password on the command line interface can be insecure.
      +----+-------------------+------------+
      | id | description       | done       |
      +----+-------------------+------------+
      |  1 | Pick up newspaper | 0x00       |
      |  2 | Buy groceries     | 0x01       |
      +----+-------------------+------------+
      sh-4.4$
    9. Exit the shell session.

      sh-4.4$ exit
  7. Delete and then re-create the db-pod deployment.

    1. Delete the db-pod deployment.

      [student@workstation ~]$ oc delete deployment/db-pod
      deployment.apps "db-pod" deleted
    2. Verify that the PVC still exists without the deployment.

      [student@workstation ~]$ oc get pvc
      NAME         STATUS   VOLUME                                     CAPACITY   ...
      db-pod-pvc   Bound    pvc-ab5b38c7-359a-4e99-b81c-f7d11ef91cc9   1Gi        ...
    3. Re-create the db-pod deployment.

      [student@workstation ~]$ oc create deployment db-pod --port 3306 \
        --image=registry.ocp4.example.com:8443/rhel8/mysql-80
      deployment.apps/db-pod created
    4. Add the environment variables.

      [student@workstation ~]$ oc set env deployment/db-pod \
        MYSQL_USER=user1 \
        MYSQL_PASSWORD=mypa55w0rd \
        MYSQL_DATABASE=items
      deployment.apps/db-pod updated
  8. Use the oc set volume command to attach the existing PVC to the deployment.

    [student@workstation ~]$ oc set volumes deployment/db-pod \
      --add --type pvc \
      --mount-path /var/lib/mysql \
      --name db-pod-vol \
      --claim-name db-pod-pvc
    deployment.apps/db-pod volume updated
  9. Create a query-db pod by using the oc run command and the registry.ocp4.example.com:8443/redhattraining/do180-dbinit container image. Use the pod to execute a query against the database service.

    1. Create the query-db pod. Configure the pod to use the MySQL client to execute a query against the db-pod service.

      [student@workstation ~]$ oc run query-db -it --rm \
        --image registry.ocp4.example.com:8443/redhattraining/do180-dbinit \
        --restart Never \
        -- /bin/bash -c "mysql -uuser1 -pmypa55w0rd --protocol tcp \
        -h db-pod -P3306 items -e 'select * from Item;'"
      mysql: [Warning] Using a password on the command line interface can be insecure.
      +----+-------------------+------------+
      | id | description       | done       |
      +----+-------------------+------------+
      |  1 | Pick up newspaper | 0x00       |
      |  2 | Buy groceries     | 0x01       |
      +----+-------------------+------------+
      pod "query-db" deleted
  10. Delete the db-pod deployment and the db-pod-pvc PVC.

    1. Delete the db-pod deployment.

      [student@workstation ~]$ oc delete deployment/db-pod
      deployment.apps "db-pod" deleted
    2. Delete the db-pod-pvc PVC.

      [student@workstation ~]$ oc delete pvc/db-pod-pvc
      persistentvolumeclaim "db-pod-pvc" 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 storage-volumes