创建应用模板

版本 日期 状态 修订人 摘要
V1.0 2018-05-09 创建 开源方案 初始版本

What is a template

The official OpenShift 3 documentation states:

A template describes a set of objects that can be parameterized and processed to produce a list of objects for creation by OpenShift. The objects to create can include anything that users have permission to create within a project, for example services, build configurations, and deployment configurations. A template may also define a set of labels to apply to every object defined in the template.

This means that typically in a template we will have:

  • A set of resources that will be created as part of “creating/deploying” the template
  • A set of values for the parameters defined in the template
  • A set of labels to describe the generated resources A template will be defined in JSON or YAML format, and will be loaded into OpenShift for user instantiation, also known as application creation.

The templates can have global visibility scope (visible for every OpenShift project) or project visibility scope (visible only for a specific project).

Benefits of using templates

A template provides developers with an easy way to create all the necessary OpenShift resources for their application to work. This allows a developer to quickly deploy an application without having to understand all of the internals of the OpenShift 3 platform.

  • As a PaaS provider you have better control on what is being created and can make better usage of your resources.
  • As a PaaS provider you can define different Service Level Agreements in templates, defining the amount of host resources (cpu, memory) each and every container can consume.

Predefined templates, or deploy your application on OpenShift

Typically, the PaaS provider will provide users with a set of predefined templates that will cover all of the usages or typologies/topologies of applications that can be deployed on OpenShift.

The set of predefined templates will be accessible through the CLI or through the Web console.

When creating your application using one of these templates, the user will typically provide the template with the source for the code of the application and some other configuration items such as the application name, database credentials, etc.

Custom templates, or OpenShiftify your application

Another use case is when you have a typology/topology of an application that does not fit into the provided templates and you want to create a template to model it. This will be the topic for the next article, a walkthrough on how to create a template for your application.

从项目中创建模板

// 导出所有 services到模板
$ oc export service --all --as-template=my_template
// 导出 标签是 name=test 的 services 和 deployment configurations 到模板
oc export svc,dc -l name=test --as-template=my_template

记住 上述命令会把模板内容输出到标准输出(stdout), 如果我们需要导出到文件,可以使用重定向到文件,也可以格式化输出 JSON 或者 YAML

$ oc export -o json --as-template=my_template > my_template.json
$ oc export -o yaml --as-template=my_template > my_template.yaml
$ oc export dc,svc,routes -o json --as-template=cboard_template > cboard_template.json

请记住,导出只是制作模板的第一步,后续还需要修改编辑参数配置,以使应用可配置化

重要的事情

最后,创建应用模板的时候,一些重要的事情您需要记住

  • Design your template visually, as it helps understand the required components.
  • Provide meaningful names to resources and use labels to describe your resources (labels are used as selectors for some resources).
  • Templates can be shared or per-project, and common templates are in the openshift namespace/project.
  • Currently there is no ability to set a Readme on templates, so be as verbose and complete in the template’s description.
  • Once the resources in a template are processed and deployed, they can be modified with the CLI.
  • You should constrain the CPU and memory a container in a pod can use.
  • When the resources in a template are created, if there is a BuildConfiguration defined, it will only start
  • an automated build if there is an ImageChange trigger defined. This will change in the next release and
  • we will be able to launch a build on resource creation.
  • Parameterize everything a user of your template might want to customize so they can control the behavior of the template when they instantiate it.

我们参考 cakephp-mysql-persistent 进行创建

参考官方示例模板 cakephp-mysql-persistent

cboard模板请参考 https://gitee.com/hunterfu/CBoard/blob/ops-tagV0.4.1/cboard-mysql-redis-persistent.json

参考资料

https://blog.openshift.com/part-1-from-app-to-openshift-runtimes-and-templates/ https://blog.openshift.com/part-2-creating-a-template-a-technical-walkthrough/