Heat Orchestration Template (HOT) specification
updated: 'Fri Sep 22 14:44:51 2017, commit 31175a5'
这片文章定义了HEAT模板的格式。HEAT是一个软件,用于管理OpenStack的stack。这里的stack,是将各种OpenStack的资源捆绑在一起,方便创建和删除。如果希望对HEAT模板有个初步和形象的认识,可以读这篇文章:http://www.jianshu.com/writer#/notebooks/18275097/notes/19531272
1 HEAT模板格式
heat_template_version: 2016-10-14
description:
# a description of the template
parameter_groups:
# a declaration of input parameter groups and order
parameters:
# declaration of input parameters
resources:
# declaration of template resources
outputs:
# declaration of output parameters
conditions:
# declaration of conditions
1.1 heat_template_version
For example,
heat_template_version: 2013-05-23
heat_template_version: 2014-10-16
heat_template_version: 2015-04-30
heat_template_version: 2015-10-15
heat_template_version: 2016-04-08
heat_template_version: 2016-10-14 (or newton)
heat_template_version: 2017-02-24 (or ocata)
heat_template_version: 2017-09-01 (or pike)
1.2 description
For example,
description: Simple template to deploy a virtual machine.
1.3 parameter_groups (可省略)
通过把有共性的参数组合在一起,增强逻辑性。
1.4 parameter (可省略)
在stack被创建之前,这些参数的值必须被提供。参数的作用是,帮助模板变得灵活,适应不同的场合。
1.5 resources
被创建的各种资源,如虚拟机,网络/子网,路由器,floating IP等,都定义在这里。
1.6 outputs (可省略)
Stack 创建完后,通过下面的命令显示一些信息。好像没太大用处。
$ heat output-list
$ heat output-show
1.7 conditions (Newton以后版本才支持)
我现在的试验环境,最新仅支持2016-04-8,暂时无法试验conditions相关的用法。
2 举例
例2.1 一个虚拟机
在这个例子中,我们将练习:
- description 采用多行格式
- network_name 作为命令行参数传入
- resources 中只有一个虚拟机
heat_template_version: 2016-04-08
description: >
This template will deploy a single virtual machine, and
this virtual machine will connect to an existing network.
There are three networks can be selected.
parameters:
network_name:
type: string
constraints:
- allowed_values:
- extnet-vxlan-0
- extnet-vxlan-1
- extnet-vxlan-2
resources:
my_virtual_machine:
type: OS::Nova::Server
properties:
image: cirros
flavor: m1.small
networks:
- network: { get_param: network_name }
通过命令行,启动 stack:
$ heat stack-create -f example_2_1.yaml --parameters "network_name=extnet-vxlan-0" xxx
$ heat stack-list
+--------------------------------------+------------+-----------------+----------------------------+
| id | stack_name | stack_status | creation_time |
+--------------------------------------+------------+-----------------+----------------------------+
| 221c247c-0fd4-4283-b0a6-16ff2894f044 | xxx | CREATE_COMPLETE | 2017-11-13T08:02:13.088757 |
+--------------------------------------+------------+-----------------+----------------------------+
$ nova list
+--------------------------------------+-------------------------------------+--------+------------+-------------+-----------------------------+
| ID | Name | Status | Task State | Power State | Networks |
+--------------------------------------+-------------------------------------+--------+------------+-------------+-----------------------------+
| db9135aa-8065-44dd-8b46-b825948f4a23 | xxx-my_virtual_machine-v27lqcztcq34 | ACTIVE | - | Running | extnet-vxlan-0=10.37.227.35 |
+--------------------------------------+-------------------------------------+--------+------------+-------------+-----------------------------+
Stack 也可以在 OpenStack 的 Dash-board 中被启动,这时候 network_name 这个参数被显示在网页中,很容易输入。
例2.2 两个虚拟机+一个网络
在这个例子中,我们将练习:parameter_groups 看它是如何工作的。
heat_template_version: 2016-04-08
description: ...
parameter_groups:
- label: network parameters
parameters:
- network_name
- subnet_name
- subnet_cidr
parameters:
network_name:
type: string
subnet_name:
type: string
subnet_cidr:
type: string
resources:
simple_net:
type: OS::Neutron::Net
properties:
name: { get_param: network_name }
simple_subnet:
type: OS::Neutron::Subnet
properties:
network_id: { get_resource: simple_net }
name: { get_param: subnet_name }
cidr: { get_param: subnet_cidr }
VM1:
type: OS::Nova::Server
properties:
name: VM1
image: cirros
flavor: m1.small
networks:
- network: { get_resource: simple_net }
VM2:
type: OS::Nova::Server
properties:
name: VM2
image: cirros
flavor: m1.small
networks:
- network: { get_resource: simple_net }
$ heat stack-create -f example_2_2.yaml --parameters "network_name=GREEN;subnet_name=10_10_10;subnet_cidr=10.10.10.0/24" xxx
$ heat stack-list
+--------------------------------------+------------+-----------------+----------------------------+
| id | stack_name | stack_status | creation_time |
+--------------------------------------+------------+-----------------+----------------------------+
| 5bbac33f-fc54-4dbc-a622-cc27acb13b88 | xxx | CREATE_COMPLETE | 2017-11-14T00:53:45.609428 |
+--------------------------------------+------------+-----------------+----------------------------+
$ neutron net-list --name GREEN
+-------------------------------+-------+-------------------------------+
| id | name | subnets |
+-------------------------------+-------+-------------------------------+
| 6f356b52-f2ab- | GREEN | e5903726-080f-4b3a-bc1f- |
| 4c73-8221-cf0f7d827d8d | | 77c1235cef20 10.10.10.0/24 |
+-------------------------------+-------+-------------------------------+
$ nova list
+--------------------------------------+------+--------+------------+-------------+------------------+
| ID | Name | Status | Task State | Power State | Networks |
+--------------------------------------+------+--------+------------+-------------+------------------+
| 0c07eb32-efab-4582-a77c-ce4c9689f9ae | VM1 | ACTIVE | - | Running | GREEN=10.10.10.4 |
| 35c9d4b7-b4b8-4267-aba8-df8fe7e62c54 | VM2 | ACTIVE | - | Running | GREEN=10.10.10.3 |
+--------------------------------------+------+--------+------------+-------------+------------------+
$ heat stack-delete xxx
通过dash-board启动stack,也没有看到 parameter_groups。
结论: parameter_groups 好像没有什么用?
例2.3 虚拟机的metadata
这个例子,我们练习一下,每个虚拟机的metadata是如何工作的。
语法:
resources:
:
type:
properties:
:
metadata:
heat_template_version: 2016-04-08
description: Simple template to deploy a network with two VMs.
resources:
simple_net:
type: OS::Neutron::Net
properties:
name: GREEN
simple_subnet:
type: OS::Neutron::Subnet
properties:
network_id: { get_resource: simple_net }
name: 10_10_10
cidr: 10.10.10.0/24
VM1:
type: OS::Nova::Server
properties:
name: VM1
image: cirros
flavor: m1.small
networks:
- network: { get_resource: simple_net }
metadata:
param_1: value_1
VM2:
type: OS::Nova::Server
properties:
name: VM2
image: cirros
flavor: m1.small
networks:
- network: { get_resource: simple_net }
metadata:
param_1: value_2
$ heat stack-create -f example_2_3.yaml xxx
$ openstack server show VM1 | grep properties
| properties | param_1='value_1'
$ openstack server set VM1 --property param_1=xxx
$ openstack server show VM1 | grep properties
| properties | param_1='xxx'
在虚拟机中得到这个参数:
$ curl http://169.254.169.254/openstack/latest/meta_data.json
参考文献
1 Heat Orchestration Template (HOT) specification
2 Heat Orchestration Template (HOT) Guide
3 OpenStack document - Environments