Lab: Deploy Web Applications
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 Red Hat Identity Management 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 all of 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 | | --- | --- | |
user|operator1| |password|redhat123| |database|quotesdb|Create 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 key | | --- | --- | |MYSQL_USER|user| |MYSQL_PASSWORD|password| |MYSQL_DATABASE|database|Ensure 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_USER| Theuserkey from thedbparamssecret | |QUOTES_PASSWORD| Thepasswordkey from thedbparamssecret | |QUOTES_DATABASE| Thedatabasekey from thedbparamssecret | |QUOTES_HOSTNAME|quotesdb|
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:6443 Login successful. ...output omitted...Create the
reviewproject.[student@workstation ~]$ oc new-project review Now 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-228 imagestreamtag.image.openshift.io/mysql8:1 createdUse the
oc set image-lookupcommand to enable image lookup resolution.[student@workstation ~]$ oc set image-lookup mysql8 imagestream.image.openshift.io/mysql8 image lookup updatedRun the
oc set image-lookupcommand without any arguments to verify your work.[student@workstation ~]$ oc set image-lookup NAME LOCAL mysql8 true
Create the
dbparamssecret.[student@workstation ~]$ oc create secret generic dbparams \ --from-literal user=operator1 --from-literal password=redhat123 \ --from-literal database=quotesdb secret/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. Ignore the warning message.[student@workstation ~]$ oc create deployment quotesdb --image mysql8:1 \ --replicas 0 Warning: would violate PodSecurity "restricted:v1.24": ...output omitted... deployment.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 wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS ... quotesdb 0/0 0 0 11s mysql8 ...Use the
oc set triggerscommand to add the trigger for themysql8:1image stream tag to themysql8container. Ignore the warning message.[student@workstation ~]$ oc set triggers deployment/quotesdb \ --from-image mysql8:1 --containers mysql8 Warning: would violate PodSecurity "restricted:v1.24": ...output omitted... deployment.apps/quotesdb triggers updated
Add environment variables to the
quotesdbdeployment from thedbparamssecret. Add theMYSQL_prefix to each variable name. Ignore the warning message.[student@workstation ~]$ oc set env deployment/quotesdb \ --from secret/dbparams --prefix MYSQL_ Warning: would violate PodSecurity "restricted:v1.24": ...output omitted... 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. Ignore the warning message.[student@workstation ~]$ oc set volumes deployment/quotesdb --add \ --claim-class lvms-vg1 --claim-size 2Gi --mount-path /var/lib/mysql info: Generated volume name: volume-n7xpd Warning: would violate PodSecurity "restricted:v1.24": ...output omitted... 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 1 deployment.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 pods NAME READY STATUS RESTARTS AGE quotesdb-99f9b4ff8-ggs7z 1/1 Running 0 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 3306 service/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 quotesdb Name: quotesdb Namespace: review ...output omitted... TargetPort: 3306/TCP Endpoints: 10.8.0.123:3306 Session 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. Ignore the warning message.[student@workstation ~]$ oc create deployment frontend \ --image registry.ocp4.example.com:8443/redhattraining/famous-quotes:2-42 \ --replicas 0 Warning: would violate PodSecurity "restricted:v1.24": ...output omitted... deployment.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. Ignore the warning message.[student@workstation ~]$ oc set env deployment/frontend \ --from secret/dbparams --prefix QUOTES_ Warning: would violate PodSecurity "restricted:v1.24": ...output omitted... deployment.apps/frontend updatedDeclare the
QUOTES_HOSTNAMEvariable. Ignore the warning message.[student@workstation ~]$ oc set env deployment/frontend QUOTES_HOSTNAME=quotesdb Warning: would violate PodSecurity "restricted:v1.24": ...output omitted... deployment.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 1 deployment.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 pods NAME READY STATUS RESTARTS AGE frontend-86cdd7c7bf-hpnwz 1/1 Running 0 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 8000 service/frontend exposedCreate the route.
[student@workstation ~]$ oc expose service frontend route.route.openshift.io/frontend exposedRetrieve the application URL from the route.
[student@workstation ~]$ oc get route NAME HOST/PORT PATH SERVICES ... frontend frontend-review.apps.ocp4.example.com frontend ...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...
Evaluation
As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.
[student@workstation ~]$ lab grade compreview-deploy
Finish
As the student user 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 compreview-deploy
