Deploy a database server and a web application that connects to that database and expose the web application to external access.
Outcomes
Deploy a MySQL database from a container image.
Deploy a web application from a container image.
Configure environment variables for a deployment.
Expose the web application for external access.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that the cluster is accessible and that all exercise resources are available.
It also creates the database-applications project.
[student@workstation ~]$ lab start deploy-review
Instructions
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.
Log in to the OpenShift cluster as the developer user with the developer password.
Use the database-applications project for your work.
Log in to the OpenShift cluster and change to the
database-applicationsproject.Log in to the OpenShift cluster.
[student@workstation ~]$
oc login -u developer -p developer \ https://api.ocp4.example.com:6443Login successful ...output omitted...Change to the
database-applicationsproject.[student@workstation ~]$
oc project database-applicationsNow using project "database-applications" on server "https://api.ocp4.example.com:6443". ...output omitted...
Create a MySQL database deployment named
mysql-appby using theregistry.ocp4.example.com:8443/redhattraining/mysql-app:v1image, and identify the root cause of the failure.Create the MySQL database deployment. Ignore the warning message.
[student@workstation ~]$
oc create deployment mysql-app \ --image registry.ocp4.example.com:8443/redhattraining/mysql-app:v1deployment.apps/mysql-app createdVerify the deployment status. The pod name might differ in your output.
[student@workstation ~]$
oc get podsNAME READY STATUS ... mysql-app-75dfd58f99-5xfqc 0/1 Error ... [student@workstation ~]$oc status...output omitted... Errors: pod/mysql-app-75dfd58f99-5xfqc is crash-looping 1 error, 1 info identified, use 'oc status --suggest' to see details.Identify the root cause of the deployment failure.
[student@workstation ~]$
oc logs mysql-app-...output omitted... You must either specify the following environment variables: MYSQL_USER MYSQL_PASSWORD MYSQL_DATABASE Or the following environment variable: MYSQL_ROOT_PASSWORD (regex: '^[a-zA-Z0-9_~!@#$%^&*()-=<>,.?;:|]+$') ...output omitted...75dfd58f99-5xfqc
Configure the environment variables for the
mysql-appdeployment by using the following information:Field Value MYSQL_USERredhatMYSQL_PASSWORDredhat123MYSQL_DATABASEworld_xThen, execute the following command in the
mysql-appdeployment pod to load theworld_xdatabase:/bin/bash -c "mysql -uredhat -predhat123 </tmp/world_x.sql"
Update the environment variables for the
mysql-appdeployment.[student@workstation ~]$
oc set env deployment/mysql-app \ MYSQL_USER=redhat MYSQL_PASSWORD=redhat123 MYSQL_DATABASE=world_xdeployment.apps/mysql-app updatedVerify that the
mysql-appapplication pod is in theRUNNINGstate. The pod name might differ in your output.[student@workstation ~]$
oc get podsNAME READY STATUS ... mysql-app-57c44f646-5qt2k 1/1 Running ...Load the
world_xdatabase.[student@workstation ~]$
oc exec -it mysql-app-...output omitted.. [student@workstation ~]$57c44f646-5qt2k\ -- /bin/bash -c "mysql -uredhat -predhat123 </tmp/world_x.sql"Confirm that you can access the MySQL database.
[student@workstation ~]$
oc rsh mysql-app-57c44f646-5qt2ksh-4.4$
mysql -uredhat -predhat123 world_x...output omitted... mysql>Exit the MySQL database, and then exit the container.
mysql>
exitBye sh-4.4$exit
Create a service for the
mysql-appdeployment by using the following information:Field Value Name mysql-servicePort 3306Target port 3306Expose the
mysql-appdeployment.[student@workstation ~]$
oc expose deployment mysql-app --name mysql-service \ --port 3306 --target-port 3306service/mysql-service createdVerify the service configuration. The endpoint IP address might differ in your output.
[student@workstation ~]$
oc get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql-service ClusterIP 172.30.146.213 <none> 3306/TCP 10s [student@workstation ~]$oc get endpointsNAME ENDPOINTS AGE mysql-service 10.8.0.102:3306 19s
Create a web application deployment named
php-appby using theregistry.ocp4.example.com:8443/redhattraining/php-webapp:v1image.Create the web application deployment. Ignore the warning message.
[student@workstation ~]$
oc create deployment php-app \ --image registry.ocp4.example.com:8443/redhattraining/php-webapp:v1deployment.apps/php-app createdVerify the deployment status. Verify that the
php-appapplication pod is in theRUNNINGstate.[student@workstation ~]$
oc get podsNAME READY STATUS ... php-app-725... 1/1 Running ... mysql-app-57c... 1/1 Running ...[student@workstation ~]$
oc status...output omitted... deployment/php-app deploys registry.ocp4.example.com:8443/redhattraining/php-webapp:v1 deployment #1 running for about a minute - 1 pod ...output omitted...
Create a service for the
php-appdeployment by using the following information:Field Value Name php-svcPort 8080Target port 8080Then, create a route named
phpappto expose the web application to external access.Expose the
php-appdeployment.[student@workstation ~]$
oc expose deployment php-app --name php-svc \ --port 8080 --target-port 8080service/php-svc exposedVerify the service configuration. The endpoint IP address might differ in your output.
[student@workstation ~]$
oc get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql-service ClusterIP 172.30.146.213 <none> 3306/TCP 7m47sphp-svcClusterIP 172.30.228.80 <none> 8080/TCP 4m34s [student@workstation ~]$oc get endpointsNAME ENDPOINTS AGE mysql-service 10.8.0.102:3306 7m50sphp-svc10.8.0.107:8080 4m37sExpose the
php-svcservice.[student@workstation ~]$
oc expose service/php-svc --name phpapproute.route.openshift.io/phpapp exposed[student@workstation ~]$
oc get routesNAME HOST/PORT ... phpapp phpapp-database-applications.apps.ocp4.example.com ...
Test the connectivity between the web application and the MySQL database. In a web browser, navigate to the
phpapp-database-applications.apps.ocp4.example.comroute, and verify that the application retrieves data from the MySQL database.