Deploy and update an application from a template that is stored in another project.
Outcomes
Deploy and update an application from a template.
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 packaged-templates
Instructions
Log in to the OpenShift cluster as the developer user with the developer password.
Log in to the OpenShift cluster.
[student@workstation ~]$ oc login -u developer -p developer \
https://api.ocp4.example.com:6443
Login successful.
...output omitted...Examine the available templates in the cluster, in the openshift project.
Identify an appropriate template to deploy a MySQL database.
Use the get command to retrieve a list of templates in the cluster, in the openshift project.
[student@workstation ~]$oc get templates -n openshiftNAME DESCRIPTION PARAMETERS OBJECTS ...output omitted... mysql-ephemeral MySQL database... 8 (3 generated) 3mysql-persistentMySQL database... 9 (3 generated) 4 ...output omitted...
Use the oc process --parameters command to view the parameters of the mysql-persistent template.
[student@workstation ~]$ oc process --parameters mysql-persistent \
-n openshift
NAME DESCRIPTION GENERATOR VALUE
MEMORY_LIMIT ... 512Mi
NAMESPACE ... openshift
DATABASE_SERVICE_NAME ... mysql
MYSQL_USER ... expression user[A-Z0-9]{3}
MYSQL_PASSWORD ... expression [a-zA-Z0-9]{16}
MYSQL_ROOT_PASSWORD ... expression [a-zA-Z0-9]{16}
MYSQL_DATABASE ... sampledb
VOLUME_CAPACITY ... 1Gi
MYSQL_VERSION ... 8.0-el8All the required parameters have either default values or generated values.
Use the mysql-persistent template to deploy a database by processing the template.
Create the packaged-templates project.
[student@workstation ~]$ oc new-project packaged-templates
Now using project "packaged-templates" on server ...
...output omitted...Use the oc new-app command to deploy the application.
[student@workstation ~]$ oc new-app --template=mysql-persistent \
-p MYSQL_USER=user1 \
-p MYSQL_PASSWORD=mypasswd
--> Deploying template "packaged-templates/mysql-persistent" to project packaged-templates
...output omitted...
--> Success
Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
'oc expose service/mysql'
Run 'oc status' to view your app.Use the watch command to verify that the pods are running.
Wait for the mysql-1-deploy pod to show a Completed status.
Press Ctrl+C to exit the watch command.
[student@workstation ~]$watch oc get podsNAME READY STATUS RESTARTS AGE mysql-1-5t8h8 1/1 Running 0 83s mysql-1-deploy 0/1Completed0 84s
Connect to the database to verify that it is working.
[student@workstation ~]$ oc run query-db -it --rm \
--image registry.ocp4.example.com:8443/rhel8/mysql-80 \
--restart Never --command -- \
/bin/bash -c \
"mysql -uuser1 -pmypasswd --protocol tcp \
-h mysql -P3306 sampledb -e 'SHOW DATABASES;'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| performance_schema |
| sampledb |
+--------------------+
pod "query-db" deletedThe query-db pod uses the mysql command from the mysql-80 image to send the SHOW DATABASES; query.
The --rm option deletes the pod after execution terminates.
Deploy the application from the custom template, in the ~/DO280/labs/packaged-templates/custom-template/roster-template.yaml file, to the project.
The application initializes and uses the database that the mysql-persistent template deployed.
Upload the custom template to the project.
[student@workstation ~]$ oc create -f \
~/DO280/labs/packaged-templates/custom-template/roster-template.yaml
template.template.openshift.io/roster-template createdUse oc get templates to view the available templates in the packaged-templates project.
[student@workstation ~]$ oc get templates
NAME DESCRIPTION PARAMETERS OBJECTS
roster-template Example application for DO280... 8 (2 blank) 4Use the oc process --parameters command to view the parameter of the roster-template template.
[student@workstation ~]$ oc process --parameters roster-template
NAME DESCRIPTION GENERATOR VALUE
IMAGE ... registry.../do280-roster:v1
APPNAME ... do280-roster
NAMESPACE ... packaged-templates
DATABASE_SERVICE_NAME ... mysql
MYSQL_USER ...
MYSQL_PASSWORD ...
MYSQL_DATABASE ... sampledb
INIT_DB ... FalseUse the oc process command to generate the manifests for the roster-template application resources, and use the oc apply command to create the resources in the Kubernetes cluster.
You must use the same database credentials that you used in an earlier step to configure the database, so that the application can access the database.
[student@workstation ~]$ oc process roster-template \
-p MYSQL_USER=user1 -p MYSQL_PASSWORD=mypasswd -p INIT_DB=true | oc apply -f -
...output omitted...
secret/mysql configured
deployment.apps/do280-roster created
service/do280-roster created
route.route.openshift.io/do280-roster createdUse the oc get pods command to confirm that the application is running.
[student@workstation ~]$oc get podsNAME READY STATUS RESTARTS AGE do280-roster-c7f596dd8-pqvlv 1/1Running0 60s mysql-1-bl97v 1/1 Running 0 33m mysql-1-deploy 0/1 Completed 0 33m
Use the oc get routes command to view the routes.
[student@workstation ~]$oc get routesNAME HOST/PORT ... do280-rosterdo280-roster-packaged-templates.apps.ocp4.example.com...
Open the application URL in the web browser. The header confirms the use of version 1 of the application.
http://do280-roster-packaged-templates.apps.ocp4.example.com
Enter your information in the form and save it to the database.
Deploy an updated version of the do280/roster application from the custom template in the roster-template template.
Use version 2 of the application and do not overwrite the data in the database.
Create a text file named roster-parameters.env with the following content:
MYSQL_USER=user1 MYSQL_PASSWORD=mypasswd IMAGE=registry.ocp4.example.com:8443/redhattraining/do280-roster:v2
The option of using a parameter file helps version control software to track changes.
Use the oc process command and the oc diff command to view the changes in the new manifests when compared to the live application.
[student@workstation ~]$oc process roster-template \ --param-file=roster-parameters.env | oc diff -f -diff -u -N ...output omitted... --- /tmp/LIVE-1948327112/apps.v1.Deployment.packaged-templates... +++ /tmp/MERGED-2797490080/apps.v1.Deployment.packaged-templates... ...output omitted... key: database-service name: mysql - name: INIT_DB - value: "true" - image: registry.ocp4.example.com:8443/redhattraining/do280-roster:v1 +value: "False"+
image: registry.ocp4.example.com:8443/redhattraining/do280-roster:v2imagePullPolicy: IfNotPresent name: do280-roster-image ports:
The | |
The |
Use the oc process command to generate the manifests for the roster-template application objects, and use the oc apply command to create the application objects.
With the changes from a previous step, you use the IMAGE variable to use a different image for the update and omit the INIT_DB variable.
[student@workstation ~]$ oc process roster-template \
--param-file=roster-parameters.env | oc apply -f -
secret/mysql configured
deployment.apps/do280-roster configured
service/do280-roster unchanged
route.route.openshift.io/do280-roster unchangedUse watch to verify that the pods are running.
Wait for the mysql-1-deploy pod to show a Completed status.
Press Ctrl+C to exit the watch command.
[student@workstation ~]$watch oc get podsNAME READY STATUS RESTARTS AGE do280-roster-c7f596dd8-ktlvl 1/1Running0 60s mysql-1-bl97v 1/1 Running 0 53m mysql-1-deploy 0/1 Completed 0 53m
Open the application URL in the web browser. The route is unchanged, so you can refresh the previous browser page if the page is still open. The header confirms the use of version 2 of the application. The data that is pulled from the database is unchanged.
http://do280-roster-packaged-templates.apps.ocp4.example.com