$ oc create -f https://raw.githubusercontent.com/openshift-roadshow/mlbparks-py/1.0.0/ose3/application-template.json template "mlbparks-py" created
Using Templates Application description
In this section, we’re going to deploy a complete backend application, consisting of an REST API backend and a mongo database. The complete application will already be wired together and described as a backend for the map visualization tool, so that once the application is build and deployed, you will be able to see the new map.
Running all these individual commands can be tedious and error prone. Fortunately for you, all of this configuration can be put together into a single
Template which can then be processed to create a full set of resources. As you saw with MongoDB, a
Template may define parameters for certain values, such as DB username or password, and they can be automatically generated by OpenShift at processing time.
Administrators can load
Templates into OpenShift and make them available to all users, even via the web console. Users can create
Templates and load them into their own
Projects for other users (with access) to share and use.
The great thing about
Templates is that they can speed up the deployment workflow for application development by providing a "recipe" of sorts that can be deployed with a single command. Not only that, they can be loaded into OpenShift from an external URL, which will allow you to keep your templates in a version control system. And, even further, they can be stored in a version control system (even in the same repository as the application source code) — they’re just serialized text data!
Let’s combine all of the exercises we have performed in the last several labs by using a
Template that we can instantiate with a single command. While we could have used templates to deploy everything in the workshop today, remember that it is important for you to understand how to create, deploy, and wire resources together.
Exercise: Instantiate a Template
The front end application we’ve been working with this whole time will display as many back end services' data as are created. Adding more stuff with the right
Label will make more stuff show up on the map.
Now you will deploy a map of Major League Baseball stadiums in the US by using a template. It is pre-configured to build the back end Java application, and deploy the Mongo database. It also uses a
Hook to call the /ws/data/load endpoint to cause the data to be loaded into the database from a JSON file in the source code repository. Execute the following command:
What just happened? What did you just create? The item that we passed to the create command is a
Template. create simply makes the template available in your
Project. You can see this with the following command:
$ oc get template NAME DESCRIPTION PARAMETERS OBJECTS mlbparks-py Application template MLB Parks backend running on Python/Flask and using mongodb 14 (4 blank) 8
Are you ready for the magic command? Here it is:
$ oc new-app mlbparks-py -p APPLICATION_NAME=mlbparks -p GIT_URI=https://github.com/openshift-roadshow/mlbparks-py -p GIT_REF=1.0.0
You will see the following output:
--> Deploying template "project-ksoong/mlbparks-py" to project project-ksoong mlbparks-py --------- Application template MLB Parks backend running on Python/Flask and using mongodb * With parameters: * Application Name=mlbparks * Application route= * Mongodb App=mongodb-mlbparks-py * Git source repository=https://github.com/openshift-roadshow/mlbparks-py * Git branch/tag reference=1.0.0 * Database name=mongodb * MONGODB_NOPREALLOC= * MONGODB_SMALLFILES= * MONGODB_QUIET= * Database user name=usermlN # generated * Database user password=Yov00t2U # generated * Database admin password=VK0rGi1N # generated * GitHub Trigger=ieNNUJT7 # generated * Generic Trigger=CBCXEjrs # generated --> Creating resources ... configmap "mlbparks-py" created service "mongodb-mlbparks-py" created deploymentconfig "mongodb-mlbparks-py" created imagestream "mlbparks" created buildconfig "mlbparks" created deploymentconfig "mlbparks" created service "mlbparks" created route "mlbparks" created --> Success Build scheduled, use 'oc logs -f bc/mlbparks' to track its progress. Run 'oc status' to view your app.
OpenShift will now:
Configure and start a build
From the supplied source code repository
Configure and deploy MongoDB
Using auto-generated user, password, and database name
Configure environment variables for the app to connect to the DB
Create the correct services
Label the app service with type=parksmap-backend
All with one command!
When the build is complete, visit the parks map. Does it work? Think about how this could be used in your environment. For example, a template could define a large set of resources that make up a "reference application", complete with several app servers, databases, and more. You could deploy the entire set of resources with one command, and then hack on them to develop new features, microservices, fix bugs, and more.
As a final exercise, look at the template that was used to create the resources for our mlbparks application.
$ oc get template mlbparks-py -o yaml