Use image streams with Kubernetes workload resources to ensure reproducibility of application deployments.
Configure applications by using Kubernetes secrets to initialize environment variables.
Provide applications with persistent storage volumes.
Expose applications to clients outside the cluster.
Outcomes
You should be able to create and configure OpenShift and Kubernetes resources, such as projects, secrets, deployments, persistent volumes, services, and routes.
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.
The command also creates the /home/student/DO180/labs/compreview-deploy/resources.txt file.
The resources.txt file contains the URLs of your OpenShift cluster and the image names that you use in the exercise.
You can use the file to copy and paste these URLs and image names.
[student@workstation ~]$ lab start compreview-deploy
Specifications
The API URL of your OpenShift cluster is https://api.ocp4.example.com:6443, and the oc command is already installed on your workstation machine.
The URL of the OpenShift web console is https://console-openshift-console.apps.ocp4.example.com. When you access the web console, select as the authentication mechanism.
Log in to the OpenShift cluster as the developer user with the developer password.
The password for the admin user is redhatocp, although you do not need administrator privileges to complete the exercise.
In this exercise, you deploy a web application and its database for testing purposes. The resulting configuration is not ready for production, because you do not configure probes and resource limits, which are required for production. Another comprehensive review exercise covers these subjects.
Perform the following tasks to complete the exercise:
Create a project named
reviewto store your work.Configure your project so that its workloads refer to the database image by the
mysql8:1short name.The short name must point to the
registry.ocp4.example.com:8443/rhel9/mysql-80:1-228container image. The database image name and its source registry are expected to change in the near future, and you want to isolate your workloads from that change.The classroom setup copied the image from the Red Hat Ecosystem Catalog. The original image is
registry.redhat.io/rhel9/mysql-80:1-228.Ensure that the workload resources in the
reviewproject can use themysql8:1resource. You create these workload resources in a later step.
Create the
dbparamssecret to store the MySQL database parameters. Both the database and the front-end deployment need these parameters. Thedbparamssecret must include the following variables:Name Value useroperator1passwordredhat123databasequotesdbCreate the
quotesdbdeployment and configure it as follows:Use the
mysql8:1image for the deployment.The database must automatically roll out whenever the source container in the
mysql8:1resource changes.To test your configuration, you can change the
mysql8:1image to point to theregistry.ocp4.example.com:8443/rhel9/mysql-80:1-237container image that the classroom provides, and then verify that thequotesdbdeployment rolls out. Remember to reset themysql8:1image to theregistry.ocp4.example.com:8443/rhel9/mysql-80:1-228container image before grading your work.Define the following environment variables in the deployment from the keys in the
dbparamssecret:Environment variable dbparamssecret keyMYSQL_USERuserMYSQL_PASSWORDpasswordMYSQL_DATABASEdatabaseEnsure that OpenShift preserves the database data between pod restarts. This data does not consume more than 2 GiB of disk space. The MySQL database stores its data under the
/var/lib/mysqldirectory. Use thelvms-vg1storage class for the volume.
Create a
quotesdbservice to make the database available to the front-end web application. The database service is listening on port 3306.Create the
frontenddeployment and configure it as follows:Use the
registry.ocp4.example.com:8443/redhattraining/famous-quotes:2-42image. For this deployment, you refer to the image by its full name, because your organization develops the image and controls its release process.Define the following environment variables in the deployment:
Environment variable name Value QUOTES_USERThe userkey from thedbparamssecretQUOTES_PASSWORDThe passwordkey from thedbparamssecretQUOTES_DATABASEThe databasekey from thedbparamssecretQUOTES_HOSTNAMEquotesdb
You cannot yet test the application from outside the cluster. Expose the
frontenddeployment so that the application can be reached at http://frontend-review.apps.ocp4.example.com.The
frontenddeployment is listening to port 8000.When you access the http://frontend-review.apps.ocp4.example.com URL, the application returns a list of quotations from famous authors.
Log in to the OpenShift cluster from the command line, and then create the
reviewproject.Log in as the
developeruser.[student@workstation ~]$
oc login -u developer -p developer \https://api.ocp4.example.com:6443Login successful. ...output omitted...Create the
reviewproject.[student@workstation ~]$
oc new-project reviewNow using project "review" on server "https://api.ocp4.example.com:6443". ...output omitted...
Create the
mysql8:1image stream tag from theregistry.ocp4.example.com:8443/rhel9/mysql-80:1-228image. Enable image stream resolution for themysql8image stream so that Kubernetes resources in the current project can use it.Use the
oc create istagcommand to create the image stream and the image stream tag.[student@workstation ~]$
oc create istag mysql8:1 \--from-image registry.ocp4.example.com:8443/rhel9/mysql-80:1-228imagestreamtag.image.openshift.io/mysql8:1 createdUse the
oc set image-lookupcommand to enable image lookup resolution.[student@workstation ~]$
oc set image-lookup mysql8imagestream.image.openshift.io/mysql8 image lookup updatedRun the
oc set image-lookupcommand without any arguments to verify your work.[student@workstation ~]$
oc set image-lookupNAME LOCAL mysql8true
Create the
dbparamssecret.[student@workstation ~]$
oc create secret generic dbparams \--from-literal user=operator1 --from-literal password=redhat123 \--from-literal database=quotesdbsecret/dbparams createdCreate the
quotesdbdeployment from themysql8:1image stream tag. Set the number of replicas to zero, to prevent OpenShift from deploying the database before you finish its configuration.[student@workstation ~]$
oc create deployment quotesdb --image mysql8:1 \--replicas 0deployment.apps/quotesdb createdAdd an image trigger to the
quotesdbdeployment.Retrieve the name of the container from the
quotesdbdeployment.[student@workstation ~]$
oc get deployment quotesdb -o wideNAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS ... quotesdb 0/0 0 0 11smysql8...Use the
oc set triggerscommand to add the trigger for themysql8:1image stream tag to themysql8container.[student@workstation ~]$
oc set triggers deployment/quotesdb \--from-image mysql8:1 --containers mysql8deployment.apps/quotesdb triggers updated
Add environment variables to the
quotesdbdeployment from thedbparamssecret. Add theMYSQL_prefix to each variable name.[student@workstation ~]$
oc set env deployment/quotesdb \--from secret/dbparams --prefix MYSQL_deployment.apps/quotesdb updatedAdd a 2 GiB persistent volume to the
quotesdbdeployment. Use thelvms-vg1storage class. Inside the pods, mount the volume under the/var/lib/mysqldirectory.[student@workstation ~]$
oc set volumes deployment/quotesdb --add \--claim-class lvms-vg1 --claim-size 2Gi --mount-path /var/lib/mysqlinfo: Generated volume name: volume-n7xpd deployment.apps/quotesdb volume updatedStart the database by scaling up the
quotesdbdeployment to one replica.Scale up the deployment.
[student@workstation ~]$
oc scale deployment/quotesdb --replicas 1deployment.apps/quotesdb scaledWait for the pod to start. You might have to rerun the command several times for the pod to report a
Runningstatus. The name of the pod on your system probably differs.[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGE quotesdb-99f9b4ff8-ggs7z 1/1Running0 4s
Create the
quotesdbservice for thequotesdbdeployment. The database server is listening on port 3306.Use the
oc expose deploymentcommand to create the service.[student@workstation ~]$
oc expose deployment quotesdb --port 3306service/quotesdb exposedVerify that OpenShift associates the IP address of the MySQL server with the endpoint. The endpoint IP address on your system probably differs.
[student@workstation ~]$
oc describe service quotesdbName: quotesdb Namespace: review ...output omitted... TargetPort: 3306/TCPEndpoints: 10.8.0.123:3306Session Affinity: None Events: <none>
Create the
frontenddeployment from theregistry.ocp4.example.com:8443/redhattraining/famous-quotes:2-42image. Set the number of replicas to zero, to prevent OpenShift from deploying the application before you finish its configuration.[student@workstation ~]$
oc create deployment frontend \--image registry.ocp4.example.com:8443/redhattraining/famous-quotes:2-42 \--replicas 0deployment.apps/frontend createdAdd environment variables to the
frontenddeployment from thedbparamssecret, and add theQUOTES_HOSTNAMEvariable with thequotesdbvalue.Add the variables from the
dbparamssecret. Add theQUOTES_prefix to each variable name.[student@workstation ~]$
oc set env deployment/frontend \--from secret/dbparams --prefix QUOTES_deployment.apps/frontend updatedDeclare the
QUOTES_HOSTNAMEvariable.[student@workstation ~]$
oc set env deployment/frontend QUOTES_HOSTNAME=quotesdbdeployment.apps/frontend updated
Start the application by scaling up the
frontenddeployment to one replica.Scale up the deployment.
[student@workstation ~]$
oc scale deployment/frontend --replicas 1deployment.apps/frontend scaledWait for the pod to start. You might have to rerun the command several times for the pod to report a
Runningstatus. The name of the pod on your system probably differs.[student@workstation ~]$
oc get podsNAME READY STATUS RESTARTS AGEfrontend-86cdd7c7bf-hpnwz1/1Running0 44s quotesdb-99f9b4ff8-ggs7z 1/1 Running 0 2m11s
Expose the
frontenddeployment so that the application is accessible from outside the cluster. The web application is listening on port 8000.Create the
frontendservice for thefrontenddeployment.[student@workstation ~]$
oc expose deployment frontend --port 8000service/frontend exposedCreate the route.
[student@workstation ~]$
oc expose service frontendroute.route.openshift.io/frontend exposedRetrieve the application URL from the route.
[student@workstation ~]$
oc get routeNAME HOST/PORT PATH SERVICES ... frontendfrontend-review.apps.ocp4.example.comfrontend ...Use the
curlcommand to test the application.[student@workstation ~]$
curl http://frontend-review.apps.ocp4.example.com<html> <head> <title>Quotes</title> </head> <body> <h1>Quote List</h1> <ul> <li>1: When words fail, music speaks. - William Shakespeare </li> ...output omitted...