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
developeruser with thedeveloperpassword. Select thestorage-volumesproject.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
developerusername and thedeveloperpassword.Select the view to access the administrative menu.
On the → page, select the
storage-volumesproject.
Identify the default storage class for the cluster.
Select the → menu option.
Figure 5.18: Storage classesThe
nfs-storagestorage class has theDefaultlabel.
Use the
registry.ocp4.example.com:8443/rhel8/mysql-80container image to create a MySQL deployment nameddb-pod. Use thestorage-volumesproject. Add a service for the database.Select the → menu option.
Verify that the
storage-volumesproject is active and select the button.Use
db-podfor the deployment name and change the image name toregistry.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.
Figure 5.19: Add ServiceClick the button.
Add a 1 Gi, RWO PVC named
db-pod-pvcto the deployment. Set the/var/lib/mysqldirectory as the mount path.Select the → menu item.
Click the three vertical dots in the row with the
db-poddeployment, and selectAdd storageoption.In the
Add Storageform, 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
Volumessection.Select the
db-pod-pvclink to see the PVC details.Figure 5.20: PVC Link
Observe how the volume mount changed the deployment.
Select the → → → tab.
Observe the
volumesandvolumeMountsadditions 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...
Use a configuration map resource to add initialization data to the database.
Observe the contents of the
init-db.sqlscript 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:6443Login successful. ...output omitted...Set the
storage-volumesproject as the active project.[student@workstation ~]$
oc project storage-volumes...output omitted...Use the contents of the
init-db.sqlfile 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.sqlconfigmap/init-db-cm createdAdd the
init-db-cmconfiguration map resource as a volume namedinit-db-volumeto the deployment. Specify the volume type asconfigmap, and set the/var/db/configdirectory 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/configdeployment.apps/db-pod volume updatedStart a remote shell session inside the container.
[student@workstation ~]$
oc rsh deployment/db-podsh-4.4$Use the
mysqlclient to execute the database script in the/var/db/config/init-dbvolume.sh-4.4$
mysql -uuser1 -pmypa55w0rd items </var/db/config/init-db.sqlmysql: [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$
exit
Delete and then re-create the
db-poddeployment.Delete the
db-poddeployment.[student@workstation ~]$
oc delete deployment/db-poddeployment.apps "db-pod" deletedVerify that the PVC still exists without the deployment.
[student@workstation ~]$
oc get pvcNAME STATUS VOLUME CAPACITY ... db-pod-pvc Bound pvc-ab5b38c7-359a-4e99-b81c-f7d11ef91cc9 1Gi ...Re-create the
db-poddeployment.[student@workstation ~]$
oc create deployment db-pod --port 3306 \ --image=registry.ocp4.example.com:8443/rhel8/mysql-80deployment.apps/db-pod createdAdd the environment variables.
[student@workstation ~]$
oc set env deployment/db-pod \ MYSQL_USER=user1 \ MYSQL_PASSWORD=mypa55w0rd \ MYSQL_DATABASE=itemsdeployment.apps/db-pod updated
Use the
oc set volumecommand 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-pvcdeployment.apps/db-pod volume updatedCreate a
query-dbpod by using theoc runcommand and theregistry.ocp4.example.com:8443/redhattraining/do180-dbinitcontainer image. Use the pod to execute a query against the database service.Create the
query-dbpod. Configure the pod to use the MySQL client to execute a query against thedb-podservice.[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
Delete the
db-poddeployment and thedb-pod-pvcPVC.Delete the
db-poddeployment.[student@workstation ~]$
oc delete deployment/db-poddeployment.apps "db-pod" deletedDelete the
db-pod-pvcPVC.[student@workstation ~]$
oc delete pvc/db-pod-pvcpersistentvolumeclaim "db-pod-pvc" deleted