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-applications project.
Log in to the OpenShift cluster.
[student@workstation ~]$ oc login -u developer -p developer \
https://api.ocp4.example.com:6443
Login successful
...output omitted...Change to the database-applications project.
[student@workstation ~]$ oc project database-applications
Now using project "database-applications" on server "https://api.ocp4.example.com:6443".
...output omitted...Create a MySQL database deployment named mysql-app by using the registry.ocp4.example.com:8443/redhattraining/mysql-app:v1 image, 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:v1
deployment.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-75dfd58f99-5xfqc
...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...Configure the environment variables for the mysql-app deployment by using the following information:
| Field | Value |
|---|---|
MYSQL_USER
|
redhat
|
MYSQL_PASSWORD
|
redhat123
|
MYSQL_DATABASE
|
world_x
|
Then, execute the following command in the mysql-app deployment pod to load the world_x database:
/bin/bash -c "mysql -uredhat -predhat123 </tmp/world_x.sql"
Update the environment variables for the mysql-app deployment.
[student@workstation ~]$ oc set env deployment/mysql-app \
MYSQL_USER=redhat MYSQL_PASSWORD=redhat123 MYSQL_DATABASE=world_x
deployment.apps/mysql-app updatedVerify that the mysql-app application pod is in the RUNNING state.
The pod name might differ in your output.
[student@workstation ~]$ oc get pods
NAME READY STATUS ...
mysql-app-57c44f646-5qt2k 1/1 Running ...Load the world_x database.
[student@workstation ~]$ oc exec -it mysql-app-57c44f646-5qt2k \
-- /bin/bash -c "mysql -uredhat -predhat123 </tmp/world_x.sql"
...output omitted..
[student@workstation ~]$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-app deployment by using the following information:
| Field | Value |
|---|---|
| Name |
mysql-service
|
| Port |
3306
|
| Target port |
3306
|
Expose the mysql-app deployment.
[student@workstation ~]$ oc expose deployment mysql-app --name mysql-service \
--port 3306 --target-port 3306
service/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-app by using the registry.ocp4.example.com:8443/redhattraining/php-webapp:v1 image.
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:v1
deployment.apps/php-app createdVerify the deployment status.
Verify that the php-app application pod is in the RUNNING state.
[student@workstation ~]$ oc get pods
NAME 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-app deployment by using the following information:
| Field | Value |
|---|---|
| Name |
php-svc
|
| Port |
8080
|
| Target port |
8080
|
Then, create a route named phpapp to expose the web application to external access.
Expose the php-app deployment.
[student@workstation ~]$ oc expose deployment php-app --name php-svc \
--port 8080 --target-port 8080
service/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 4m37s
Expose the php-svc service.
[student@workstation ~]$ oc expose service/php-svc --name phpapp
route.route.openshift.io/phpapp exposed[student@workstation ~]$ oc get routes
NAME 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.com route, and verify that the application retrieves data from the MySQL database.