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
Procedure 5.2. Instructions
Log in to the OpenShift cluster as the
developeruser with thedeveloperpassword. Use thestorage-volumesproject.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-volumesproject as the active project.[student@workstation ~]$ oc project storage-volumes ...output omitted...
Identify the default storage class for the cluster.
Use the
oc get storageclasscommand to identify the default storage class.[student@workstation ~]$ oc get storageclass NAME PROVISIONER ... nfs-storage (default) k8s-sigs.io/nfs-subdir-external-provisioner ... lvms-vg1 topolvm.io ...
Use the
registry.ocp4.example.com:8443/rhel8/mysql-80container image to create a MySQL deployment nameddb-pod. Add the missing environment variables for the pod to run.Create the
db-poddeployment. Ignore the warning message.[student@workstation ~]$ oc create deployment db-pod --port 3306 \ --image=registry.ocp4.example.com:8443/rhel8/mysql-80 ...output omitted... deployment.apps/db-pod createdAdd the environment variables. Ignore the warning message.
[student@workstation ~]$ oc set env deployment/db-pod \ MYSQL_USER=user1 \ MYSQL_PASSWORD=mypa55w0rd \ MYSQL_DATABASE=items ...output omitted... deployment.apps/db-pod updatedConfirm that the pod is running.
[student@workstation ~]$ oc get pods NAME READY STATUS RESTARTS AGE db-pod-6ccc485cfc-vrc4r 1/1 Running 0 2m30sExpose the
db-poddeployment to create a service.[student@workstation ~]$ oc expose deployment/db-pod service/db-pod exposed
Use the
oc get deploymentcommand to view the pod template specification for the deployment.Retrieve the pod template specification for the deployment.
[student@workstation ~]$ oc get deployment/db-pod -o json | \ jq .spec.template.spec.containers [ { "env": [ { "name": "MYSQL_USER", "value": "user1" }, { "name": "MYSQL_PASSWORD", "value": "mypa55w0rd" }, { "name": "MYSQL_DATABASE", "value": "items" } ], "image": "registry.ocp4.example.com:8443/rhel8/mysql-80", "imagePullPolicy": "Always", "name": "mysql-80", "ports": [ { "containerPort": 3306, "protocol": "TCP" } ], "resources": {}, "terminationMessagePath": "/dev/termination-log", "terminationMessagePolicy": "File" } ]
Add a 1 Gi, RWO PVC named
db-pod-pvcto the deployment. Specify the volume name asnfs-volume-storage, and set the/var/lib/mysqldirectory as the mount path.Use the
oc set volumecommand to create a PVC for the deployment. Ignore the warning message.[student@workstation ~]$ oc set volumes deployment/db-pod \ --add --name nfs-volume-storage --type pvc \ --claim-mode rwo --claim-size 1Gi --mount-path /var/lib/mysql \ --claim-name db-pod-pvc ...output omitted... deployment.apps/db-pod volume updatedUse the
oc get pvccommand to view the status of the PVC. Identify the name of the PV, and confirm that the PVC uses thenfs-storagedefault storage class.[student@workstation ~]$ oc get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS ... db-pod-pvc Bound pvc-13...ca45 1Gi RWO nfs-storage ...
Observe how the
oc set volumecommand changed the deployment.Use the
oc get deploymentcommand to view the deployment again.[student@workstation ~]$ oc get deployment/db-pod -o yaml apiVersion: apps/v1 kind: Deployment ...output omitted... volumeMounts: - mountPath: /var/lib/mysql name: nfs-volume-storage ...output omitted... volumes: - name: nfs-volume-storage persistentVolumeClaim: claimName: db-pod-pvc ...output omitted...
Use a
configMapresource as a temporary volume 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);Use the contents of the
init-db.sqlfile to create aconfigMapAPI object namedinit-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-cmconfigMapresource as a volume namedinit-db-volumeto the deployment. Specify the volume type asconfigmap, and set the/tmp/init-dbdirectory as the mount path. Ignore the warning message.[student@workstation ~]$ oc set volumes deployment/db-pod \ --add --name init-db-volume \ --configmap-name init-db-cm --type configmap \ --mount-path /tmp/init-db ...output omitted... deployment.apps/db-pod volume updatedCapture the name of the pod in a variable.
[student@workstation ~]$ PODNAME=$(oc get pods \ -o jsonpath='{.items[0].metadata.name}')Start a remote shell session inside the container.
[student@workstation ~]$ oc rsh $PODNAME sh-4.4$Use the
mysqlclient to execute the database script in the/tmp/init-dbvolume.sh-4.4$ mysql -uuser1 -pmypa55w0rd items </tmp/init-db/init-db.sql mysql: [Warning] Using a password on the command line interface can be insecure. sh-4.4$Exit the shell session.
sh-4.4$ exitUse the
oc set volumecommand to remove theinit-db-volumevolume from thedp-poddeployment. Ignore the warning message.[student@workstation ~]$ oc set volumes deployment/db-pod \ --remove --name init-db-volume ...output omitted... deployment.apps/db-pod volume updated
Create 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. Ignore the warning message.[student@workstation ~]$ oc run query-db -it --rm \ --image registry.ocp4.example.com:8443/redhattraining/do180-dbinit \ --restart Never --command \ -- /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 and then re-create the
db-poddeployment.Delete the
db-poddeployment.[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-poddeployment. Ignore the warning message.[student@workstation ~]$ oc create deployment db-pod --port 3306 \ --image=registry.ocp4.example.com:8443/rhel8/mysql-80 ...output omitted... deployment.apps/db-pod createdAdd the environment variables. Ignore the warning message.
[student@workstation ~]$ oc set env deployment/db-pod \ MYSQL_USER=user1 \ MYSQL_PASSWORD=mypa55w0rd \ MYSQL_DATABASE=items ...output omitted... deployment.apps/db-pod updated
Use the
oc set volumecommand to attach the existing PVC to the deployment. Ignore the warning message.[student@workstation ~]$ oc set volumes deployment/db-pod \ --add --type pvc \ --mount-path /var/lib/mysql \ --claim-name db-pod-pvc info: Generated volume name: volume-kfl28 ...output omitted... deployment.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. Ignore the warning message.[student@workstation ~]$ oc run query-db -it --rm \ --image registry.ocp4.example.com:8443/redhattraining/do180-dbinit \ --restart Never --command \ -- /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-pod deployment.apps "db-pod" deletedDelete the
db-pod-pvcPVC.[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
