Skip to content

Latest commit

 

History

History
112 lines (73 loc) · 6.52 KB

16_templates.md

File metadata and controls

112 lines (73 loc) · 6.52 KB

Templates

Video Walkthrough Thumbnail

Video walkthrough

So far in the lab exercises, we've primarily used the 'new-app' command to create many our OpenShift objects. However, all objects can be created and modified using YAML. Templates can be used to rapidly create multiple OpenShift objects from a single YAML file. This can be useful for testing your application in different environments (dev, test, prod) or for recreating similar applications. It is also helpful to use manifests and templates as a strategy for recovering from an incident.

In this section, we'll create a template that includes all of the objects we've created for our rocketchat application and mongo database.

You may find it helpful to install a tool such as Visual Studio Code to help work through this section of the lab.

Get the YAML

We can use the oc get command to gather the YAML files that represent our OpenShift commands and their configuration. We need to collect the YAML for a variety of objects. After replacing [username] we can do this with the following command:

oc -n [-dev] get deployment,route,service,configmap,pvc,secrets -l app=rocketchat-[username] -o yaml > template.yaml

The command above will save the YAML definitions for these objects on your local machine in a file named template.yaml.

Note: we've also created some objects in the [-tools] namespace including our buildconfig and imagestream, but for the purposes of this demonstration we're not going to add them to our template.

Remove unnecessary information

Explore the new file that you just created called template.yaml

We're now looking to strip unique information generated by OpenShift from the YAML to allow it to be reusable. Search for each of these fields and remove their contents from your template.yaml then save it:

  • annotations
  • status
  • clusterIP
  • clusterIPs
  • generation
  • uid
  • resourceVersion
  • creationTimestamp
  • volumeName

There is also a script you can use to simplify the process of stripping out the auto-generated information from OpenShift in order to make a template.

You could stop at this point if you just want to work with the YAML files as they are. By running oc apply on your template.yaml file, you can recreate all of these objects from your template. We will continue however, and add some parameters into our template to make it easier to apply the template into other environments.

Parameterizing your template

In this section, we're going to parameterize our OpenShift objects using the built-in OpenShift templates feature. There are alternatives to using OpenShift templates though, including HELM.

In order to tell OpenShift that we're creating a YAML file that should be used as a template, we need to make some more changes to our template.yaml file.

  1. On the first line, replace apiVersion: v1 with apiVersion: template.openshift.io/v1
  2. On the second line, add a new field: kind: Template be sure capitilize the T in Template!! and then rename the items: field to objects:
  3. Remove kind: List

Next, add a new field called 'parameters' to our yaml file, above objects. Replace [username] with your username.

parameters: 
  - name: OWNER
    required: true
    value: '[username]'
  - name: APP_NAMESPACE
    required: true
    value: 'dev'

Next, let's search through the file for the places where our username or namespace suffix -dev is being used and replace them with ${OWNER} and ${APP_NAMESPACE}. This way, in the future we can just make changes to a parameter values and they'll be applied throughout our template. Be sure to check carefully, don't just find and replace all! Be sure to save your template.yaml file.

Test the template

The oc process command is used to process a template into a resource list. The -f flag used to indicate that we need to process a file.

The oc create command can be used to create new objects in OpenShift, again using the -f flag.

Below, we can use the pipe character | to take the output from the oc process command and feed it into the oc create command. We add --dry run=client to get a simulated output before actually running the command. This way, we can check that our template is working correctly:

oc -n [-dev] process -f template.yaml | oc create -f - --dry-run=client

Your output should look similar to the below. Note, any object that already exists with these same names will not be created by oc create:

oc -n d8f105-dev process -f template.yaml | oc apply -f - --dry-run=client  

deployment.apps/rocketchat-mattspencer configured (dry run)
deployment.apps.openshift.io/mongodb-mattspencer configured (dry run)
route.route.openshift.io/rocketchat-mattspencer configured (dry run)
service/mongodb-mattspencer configured (dry run)
service/rocketchat-mattspencer configured (dry run)
configmap/rocketchat-mattspencer-configmap configured (dry run)
persistentvolumeclaim/mongodb-mattspencer-file configured (dry run)
secret/mongodb-mattspencer configured (dry run)
secret/rocketchat-mattspencer-secret configured (dry run)

If the dry run looks to be producing a successful output, let's move on to test our template.

Delete objects, recreate them with the template

Currently, our template would not be very helpful as it is mostly trying to create objects that already exist. The oc create command won't overrite objects existing objects with the same names, and will skip these.

Let's create a situation where all of the objects that we'd previously created in the -dev namespace have been deleted.

Before deleting objects, it's also good practice to do a dry run to make sure you're only deleing the objects you intend to:

oc -n [-dev] delete deployment,route,service,configmap,pvc,secrets -l app=rocketchat-[username] --dry-run=client

If you can see that you'll only be deleting your own objects, then proceed without the dry-run flag:

oc delete deployment,route,service,configmap,pvc,secrets -l app=rocketchat-[username]

Now that we've deleted all of our objects that we created in the [-dev] namespace, let's use our template to recreate them.

oc -n [-dev] process -f template.yaml | oc create -f -

After some time, once your mongodb restarts and rocketchat application is back up and running, you should see your application is recreated and functioning.