Creating an application template from an existing application

In a previous blog, we looked at how to deploy a python application, along with a database, on to OpenShift.  In this blog post we’ll be looking at how to take that running application and create an application template from it.  This will allow the whole application to form a simple repeatable deployment based on a few given parameters.  There was a rumour that template support would be removed from OpenShift but it’s still there so why not take advantage of its simplicity?  Alternatives to this approach are to create a Helm chart or use Kustomise but they are both a little more involved and beyond the scope of this blog. 

At a high level, the process to create template from an existing application is:

  • Export the objects from the existing application to a template file
  • Edit the file to remove namespace and deployment specifics, and to add extra information
  • Run the template to create a new instance of the application.

Exporting the objects

As a starting point, we’ll be using the IoT webserver, introduced in the previous blog post, running on our OpenShift cluster.

On the command line, log in to the OpenShift cluster where the application is running and make sure to change to the project containing the application:

oc project iotservice 

To export all of the application’s objects to a file run the following command.

For OpenShift 3.x

oc get -o yaml –export all > webserver_template.yaml

For OpenShift 4.x

oc get -o yaml all > webserver_template.yaml

The command outputs the application objects in yaml format to the command line, so redirect the output to a file with a yaml extension on the file name.  Many objects in the project will be output into the file so executing this command on a project containing multiple applications will yield a large number of objects that you might not want and will need to remove from the file.  Specifically, the following types of object will be exported:

  • BuildConfig
  • Build
  • DeploymentConfig
  • ImageStream
  • Pod
  • ReplicationController
  • Route
  • Service

Editing the file

There are several modifications to make to the file so that it is a working template:

  • Remove unnecessary objects
  • Remove references to the original namespace/project
  • Add in a persistent volume claim for the database storage (this is specific to this example)
  • Abstract environment variables shared between the different containers into parameters in the file

Removing objects from the file

Objects that are created in the file that are not necessary:

  • Builds
  • Pods
  • ReplicationControllers

There are multiple pods and replication controllers, one for each of the pods that were running for the application.

Remove namespace references

Where images are referenced they are, in some cases, of the form “<registry IP address>/<original project namespace>/webserver:latest” which includes the original registry server and the original namespace.  For each, it is unlikely that they would be correct where the template is to be used.  For all of these references both the registry and the namespace were removed.

172.30.48.35:5000/iotservice/webserver:latest → webserver:latest

Some images are also specifically referenced using a hash value and this was changed to the “latest” tag.

172.30.48.35:5000/iotservice/webserver@sha256:3a4d6d22d7c5a9a495d177de0575f2e8583611ded424a458719e1ecc966d180f → webserver:latest  

Adding in the persistent volume claim

A persistent volume is necessary for the database. Whilst the deployment configuration object for the database contains the necessary configuration to use a persistent volume, there is no volume claim so it is necessary to add one into the template.

Persistent Volume Object

– apiVersion: “v1”  kind: “PersistentVolumeClaim”  metadata:    name: “mysql-data”  spec:    accessModes:    – “ReadWriteOnce”    resources:      requests:        storage: “1Gi”

Add environment variables

The MYSQL variables are shared by both the database and the application for database access.  As such it makes sense to abstract the values to parameters so that they are only defined once.

Environment variables added

parameters:- name: MYSQL_USER  displayName: MySQL User  description: Username for MySQL user that will be used for accessing the database.  value: “mysql”  required: true- name: MYSQL_PASSWORD  displayName: MySQL Password  description: Password for the MySQL user.  generate: expression  from: “[a-z0-9]{8}”  required: true- name: MYSQL_DATABASE  displayName: MySQL Database Name  description: Name of the MySQL database accessed.  value: “iotdb”  required: true- name: MYSQL_ROOT_PASSWORD  displayName: MySQL Root password.  description: Password for Root user.  generate: expression  from: “[a-z0-9]{8}”  required: true- name: MYSQL_IMAGE  description: Mysql image to use  required: true  value: “registry.access.redhat.com/rhscl/mysql-57-rhel7:latest”

Run the new template

If the edits are done in place the new template is the file webserver_template.yaml.  This file can be used to create further instances of the application or it can be added to the catalogue on OpenShift.  The following command will deploy the template from the local filesystem.

oc new-project webserver

oc new-app –file /path/to/webserver_template.yml

This process is ideal for easily creating reusable application templates for Proofs of Concept, personal or team work.  For production ready templates, additional information should be added to the template (detailed at the links below) or, depending on your circumstances, either helm or customise, mentioned above, may better suit your needs.

The Openshift documentation provides more in depth information about templates and how to use them. OpenShift 3, OpenShift 4.


READ MORE ABOUT OUR PREMIER BUSINESS PARTNER – RED HAT


Creating an application template from an existing application

Ammeon |

In this blog post we’ll be looking at how to take that running application and create an application template from it.  This will allow the whole application to form a simple repeatable deployment based on a few given parameters. 

READ MORE

Ammeon awarded Container Platform Specialist status by Red Hat

Ammeon | August 27, 2021

Red Hat have awarded us with Container Platform Specialist status. This has been awarded to us for our consistent high standards of Red Hat OpenShift delivery, as well as our specialist expertise and experience that we bring to projects. Ammeon has become one of Red Hat’s leading professional service partners across Europe and our work with OpenShift has been a major reason for this award. We design, build, deploy and manage OpenShift models for our customers across a range of …

READ MORE

How Can Flow Efficiency Improve Productivity

Ammeon | July 4, 2021

Flow efficiency examines the two basic components that make up your lead time: work and wait time.

READ MORE