In this lab, 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.
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:
{% if PARKSMAP_PY %}
$ oc create -f https://raw.githubusercontent.com/openshift-roadshow/mlbparks-py/{{MLBPARKS_VERSION}}/ose3/application-template.json
{% else %}
$ oc create -f https://raw.githubusercontent.com/openshift-roadshow/mlbparks/{{MLBPARKS_VERSION}}/ose3/application-template-{{MW_NAME}}.json
{% endif %}
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
You will see output like the following:
{% if PARKSMAP_PY %}
NAME DESCRIPTION PARAMETERS OBJECTS
mlbparks-py Application template MLB Parks backend running on Python/Flask and using mongodb 14 (4 blank) 8
{% else %}
mlbparks Application template MLBParks backend running on {{MW_TEXT}} and using MongoDB 15 (5 blank) 8
{% endif %}
Are you ready for the magic command? Here it is:
{% if USE_MAVEN %}
oc new-app mlbparks --name=mlbparks -p GIT_REF={{MLBPARKS_VERSION}} -p MAVEN_MIRROR_URL=http://nexus.workshop-infra.svc.cluster.local:8081/content/groups/public
{% else %}
{% if PARKSMAP_PY %}
oc new-app mlbparks-py -p APPLICATION_NAME=mlbparks -p GIT_URI=https://github.com/openshift-roadshow/mlbparks-py -p GIT_REF={{MLBPARKS_VERSION}}
{% else %}
oc new-app mlbparks -p APPLICATION_NAME=mlbparks -p GIT_REF={{MLBPARKS_VERSION}}
{% endif %}
{% endif %}
You will see the following output:
{% if USE_MAVEN %}
--> Deploying template mlbparks
mlbparks
---------
Application template MLBParks backend running on {{MW_TEXT}} and using MongoDB
* With parameters:
* Application Name=mlbparks
* Application route=
* Mongodb App=mongodb-mlbparks
* Git source repository=https://github.com/openshift-roadshow/mlbparks
* Git branch/tag reference={{MLBPARKS_VERSION}}
* Maven mirror url=http://nexus.workshop-infra.svc.cluster.local:8081/content/groups/public
* Database name=mongodb
* MONGODB_NOPREALLOC=
* MONGODB_SMALLFILES=
* MONGODB_QUIET=
* Database user name=userGhR # generated
* Database user password=KhnHKCQI # generated
* Database admin password=UyUV6ReU # generated
* GitHub Trigger=dAOuD7s4 # generated
* Generic Trigger=tWSkmNLn # generated
--> Creating resources ...
configmap "mlbparks" created
service "mongodb-mlbparks" created
deploymentconfig "mongodb-mlbparks" 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.
{% else %} {% if PARKSMAP_PY %}
--> Deploying template "{{USER_NAME}}/mlbparks-py" to project {{USER_NAME}}
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
* 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=userxmt # generated
* Database user password=lk8hYPOe # generated
* Database admin password=uuvr7vRG # generated
* GitHub Trigger=hbJ8Fdw7 # generated
* Generic Trigger=ho4s1c6f # generated
--> Creating resources ...
configmap "mlbparks" created
service "mongodb-mlbparks" created
deploymentconfig "mongodb-mlbparks" 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.
{% else %}
--> Deploying template mlbparks
mlbparks
---------
Application template MLBParks backend running on {{MW_TEXT}} and using MongoDB
* With parameters:
* Application Name=mlbparks
* Application route=
* Mongodb App=mongodb-mlbparks
* Git source repository=https://github.com/openshift-roadshow/mlbparks
* Git branch/tag reference={{MLBPARKS_VERSION}}
* Database name=mongodb
* MONGODB_NOPREALLOC=
* MONGODB_SMALLFILES=
* MONGODB_QUIET=
* Database user name=userGhR # generated
* Database user password=KhnHKCQI # generated
* Database admin password=UyUV6ReU # generated
* GitHub Trigger=dAOuD7s4 # generated
* Generic Trigger=tWSkmNLn # generated
--> Creating resources ...
configmap "mlbparks" created
service "mongodb-mlbparks" created
deploymentconfig "mongodb-mlbparks" 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.
{% endif %} {% endif %}
OpenShift will now:
-
Configure and start a build {% if USE_MAVEN %}
-
Using the supplied Maven mirror URL {% endif %}
-
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.
{% if PARKSMAP_PY %}
$ oc get template mlbparks-py -o yaml
{% else %}
$ oc get template mlbparks -o yaml
{% endif %}