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
Log in to the OpenShift cluster as the developer user with the developer password.
Select the storage-volumes project.
Use a web browser to navigate to the OpenShift web console at https://console-openshift-console.apps.ocp4.example.com.
Log in by using Red Hat Identity Management with the developer username and the developer password.
Select the view to access the administrative menu.
On the → page, select the storage-volumes project.
Identify the default storage class for the cluster.
Select the → menu option.
![]() |
The nfs-storage storage class has the Default label.
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.
Select the → menu option.
Verify that the storage-volumes project is active and select the button.
Use db-pod for the deployment name and change the image name to registry.ocp4.example.com:8443/rhel8/mysql-80.
Add the environment variables.
Table 5.5. MYSQL Environment Variables
| Name | Value |
|---|---|
| MYSQL_USER | user1 |
| MYSQL_PASSWORD | mypa55w0rd |
| MYSQL_DATABASE | items |
Select the → link and set the replicas to one.
Click . Wait for the blue circle to indicate that a single pod is running.
Click → → .
Add the service values.
![]() |
Click the button.
Add a 1 Gi, RWO PVC named db-pod-pvc to the deployment.
Set the /var/lib/mysql directory as the mount path.
Select the → menu item.
Click the three vertical dots in the row with the db-pod deployment, and select the menu option.
In the Add Storage form, click .
Add the following field values to the form.
Table 5.7. Add PVC Storage Fields
| Field name | Value |
|---|---|
| PersistentVolumeClaim name | db-pod-pvc |
| Access mode | RWO |
| Size | 1 GiB |
| Volume mode | Filesystem |
| Mount path | /var/lib/mysql |
Click .
Scroll down in the deployment details to the Volumes section.
Select the db-pod-pvc link to see the PVC details.
![]() |
Observe how the volume mount changed the deployment.
Select the → → → tab.
Observe the volumes and volumeMounts additions to the deployment.
apiVersion: apps/v1 kind: Deployment ...output omitted... volumes: - name: nfs-volume-storage persistentVolumeClaim: claimName: db-pod-pvc ...output omitted... volumeMounts: - mountPath: /var/lib/mysql name: nfs-volume-storage ...output omitted...
Use a configuration map resource to add initialization data to the database.
Observe the contents of the init-db.sql script that initializes the database.
[student@workstation ~]$ cat \
~/DO180/labs/storage-volumes/configmap/init-db.sqlDROP 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);
Log in to the OpenShift cluster.
[student@workstation ~]$ oc login -u developer -p developer \
https://api.ocp4.example.com:6443
Login successful.
...output omitted...Set the storage-volumes project as the active project.
[student@workstation ~]$ oc project storage-volumes
...output omitted...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 createdAdd 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 updatedStart a remote shell session inside the container.
[student@workstation ~]$ oc rsh deployment/db-pod
sh-4.4$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$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$Exit the shell session.
sh-4.4$ exitDelete and then re-create the db-pod deployment.
Delete the db-pod deployment.
[student@workstation ~]$ oc delete deployment/db-pod
deployment.apps "db-pod" deletedVerify 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 ...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 createdAdd the environment variables.
[student@workstation ~]$ oc set env deployment/db-pod \
MYSQL_USER=user1 \
MYSQL_PASSWORD=mypa55w0rd \
MYSQL_DATABASE=items
deployment.apps/db-pod updatedUse 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 updatedCreate 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.
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" deletedDelete the db-pod deployment and the db-pod-pvc PVC.
Delete the db-pod deployment.
[student@workstation ~]$ oc delete deployment/db-pod
deployment.apps "db-pod" deletedDelete the db-pod-pvc PVC.
[student@workstation ~]$ oc delete pvc/db-pod-pvc
persistentvolumeclaim "db-pod-pvc" deleted