Bookmark this page

Guided Exercise: OpenShift Templates

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

  1. Log in to the OpenShift cluster as the developer user with the developer password.

    1. Log in to the OpenShift cluster.

      [student@workstation ~]$ oc login -u developer -p developer \
        https://api.ocp4.example.com:6443
      Login successful.
      ...output omitted...
  2. Examine the available templates in the cluster, in the openshift project. Identify an appropriate template to deploy a MySQL database.

    1. Use the get command to retrieve a list of templates in the cluster, in the openshift project.

      [student@workstation ~]$ oc get templates -n openshift
      NAME                      DESCRIPTION            PARAMETERS        OBJECTS
      ...output omitted...
      mysql-ephemeral           MySQL database...       8 (3 generated)  3
      mysql-persistent          MySQL database...       9 (3 generated)  4
      ...output omitted...
    2. 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-el8

      All the required parameters have either default values or generated values.

  3. Use the mysql-persistent template to deploy a database by processing the template.

    1. Create the packaged-templates project.

      [student@workstation ~]$ oc new-project packaged-templates
      Now using project "packaged-templates" on server ...
      ...output omitted...
    2. 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.
    3. 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 pods
      NAME             READY   STATUS      RESTARTS   AGE
      mysql-1-5t8h8    1/1     Running     0          83s
      mysql-1-deploy   0/1     Completed   0          84s
    4. 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" deleted

      The 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.

  4. 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.

    1. 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 created
    2. Use 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)  4
    3. Use 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                ...                     False
    4. Use 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 created
    5. Use the oc get pods command to confirm that the application is running.

      [student@workstation ~]$ oc get pods
      NAME                           READY   STATUS      RESTARTS   AGE
      do280-roster-c7f596dd8-pqvlv   1/1     Running     0          60s
      mysql-1-bl97v                  1/1     Running     0          33m
      mysql-1-deploy                 0/1     Completed   0          33m
    6. Use the oc get routes command to view the routes.

      [student@workstation ~]$ oc get routes
      NAME           HOST/PORT                                               ...
      do280-roster   do280-roster-packaged-templates.apps.ocp4.example.com   ...
    7. 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

    8. Enter your information in the form and save it to the database.

  5. 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.

    1. 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.

    2. 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" 1
      +        image: registry.ocp4.example.com:8443/redhattraining/do280-roster:v2 2
               imagePullPolicy: IfNotPresent
               name: do280-roster-image
               ports:

      1

      The INIT_DB environment variable determines whether the application initializes the database. The default False value is used when the parameter is omitted. In the first deployment, the INIT_DB variable was set to the True value, so the database was initialized. In this second deployment, the deployment does not have to initialize the database again.

      2

      The IMAGE parameter changes the image that the template uses.

    3. 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 unchanged
    4. Use 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 pods
      NAME             READY   STATUS      RESTARTS   AGE
      do280-roster-c7f596dd8-ktlvl   1/1     Running     0          60s
      mysql-1-bl97v                  1/1     Running     0          53m
      mysql-1-deploy                 0/1     Completed   0          53m
    5. 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

Finish

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 packaged-templates

Revision: do280-4.14-08d11e1