在openstack中,Heat模板文件可以实现实例资源的批量自动化部署,避免了重复的操作,解放了我们的双手,甚至可以一劳永逸。
Heat用来模板创建栈,栈会根据模板内容创建实例,栈还可以监控它所创建的实例。
Heat服务创建栈:
[root@controller ~]# heat stack-create -f server.yml -P Image=centos7 -P Net=int-net1 mystack
+--------------------------------------+------------+--------------------+---------------------+--------------+
| id | stack_name | stack_status | creation_time | updated_time |
+--------------------------------------+------------+--------------------+---------------------+--------------+
| 38aca37e-2607-4bc9-bcf3-7039c8ca5141 | mystack | CREATE_IN_PROGRESS | 2019-05-19T05:54:25 | None |
+--------------------------------------+------------+--------------------+---------------------+--------------+
创建完成:
[root@controller ~]# heat stack-list
+--------------------------------------+------------+-----------------+---------------------+--------------+
| id | stack_name | stack_status | creation_time | updated_time |
+--------------------------------------+------------+-----------------+---------------------+--------------+
| 38aca37e-2607-4bc9-bcf3-7039c8ca5141 | mystack | CREATE_COMPLETE | 2019-05-19T05:54:25 | None |
+--------------------------------------+------------+-----------------+---------------------+--------------+
模板文件:
[root@controller ~]# cat server.yml
heat_template_version: 2013-05-23
description: Test Template
parameters:
Image:
type: string
description: Image use to boot a server
Net:
type: string
description: Network ID for the server
resources:
server1:
type: OS::Nova::Server
properties:
name: "Test Heat server"
image: { get_param: Image }
flavor: "2"
networks:
- network: { get_param: Net }
outputs:
server1_private_ip:
description: IP address of the server in the private network
value: { get_attr: [ server1, first_address ] }
模板文件解析:
[root@controller ~]# cat server.yml
heat_template_version: 2013-05-23 #版本信息(heat template-version-list可查看所有模板版本)
description: Test Template
parameters: #自定义变量
Image: #变量①
type: string #变量类型
description: Image use to boot a server #描述
Net: #变量②
type: string
description: Network for the server
resources: #描述的资源(如网络、实例、云硬盘等)
server1: #资源名称
type: OS::Nova::Server #资源类型(为云主机)(heat resource-type-list可查看所有资源类型)
properties: #云主机属性
name: "Test Heat server" #云主机名称
image: { get_param: Image } #镜像
flavor: "2" #云主机类型(cpu、内存、硬盘等)
networks: #网络(可以有不止一个)
- network: { get_param: Net }
outputs: #返回值
server1_private_ip: #返回值名称
description: IP address of the server in the private network #描述
value: { get_attr: [ server1, first_address ] }
#函数:
”get_param“作用:引用模板中指定的参数。如get_param: Image,引用parameters中Image的值。
“get_atte”作用:引用资源属性。如get_attr: [ server1, first_address ],引用server1资源中first_address的值。first_address必须是模板resouce段中指定的资源。
此栈创建了一个云主机:
+--------------------------------------+------------------+---------+------------+-------------+--------------------------------------------------------+
| ID | Name | Status | Task State | Power State | Networks |
+--------------------------------------+------------------+---------+------------+-------------+--------------------------------------------------------+
| 5d76ac4a-b7a9-4390-a8e5-d2015d902ba6 | Test Heat server | ACTIVE | - | Running | int-net1=10.0.0.24 |
+--------------------------------------+------------------+---------+------------+-------------+--------------------------------------------------------+
附:
查看mystack的事件列表:
[root@controller ~]# heat event-list mystack
+---------------+--------------------------------------+-------------------------------------+--------------------+---------------------+
| resource_name | id | resource_status_reason | resource_status | event_time |
+---------------+--------------------------------------+-------------------------------------+--------------------+---------------------+
| mystack | d0ce9553-d914-45da-9c4a-80dd80970ae5 | Stack CREATE started | CREATE_IN_PROGRESS | 2019-05-19T05:54:26 |
| server1 | 72acd450-5c3d-485d-a6ef-76bd4cb50a76 | state changed | CREATE_IN_PROGRESS | 2019-05-19T05:54:26 |
| server1 | bb004cb9-496b-42fd-8c2e-aeeb39fd941b | state changed | CREATE_COMPLETE | 2019-05-19T05:54:35 |
| mystack | e53f23f9-74f5-409f-8356-fb862d04ee73 | Stack CREATE completed successfully | CREATE_COMPLETE | 2019-05-19T05:54:35 |
+---------------+--------------------------------------+-------------------------------------+--------------------+---------------------+
查看事件信息:
格式:heat event-show 栈名称 资源名称 资源ID
[root@controller ~]# heat event-show mystack mystack e53f23f9-74f5-409f-8356-fb862d04ee73
+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Property | Value |
+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| event_time | 2019-05-19T05:54:35 |
| id | e53f23f9-74f5-409f-8356-fb862d04ee73 |
| links | http://controller:8004/v1/fda820529c814812a7ab5fdcb878b291/stacks/mystack/38aca37e-2607-4bc9-bcf3-7039c8ca5141/resources/mystack/events/e53f23f9-74f5-409f-8356-fb862d04ee73 (self) |
| | http://controller:8004/v1/fda820529c814812a7ab5fdcb878b291/stacks/mystack/38aca37e-2607-4bc9-bcf3-7039c8ca5141/resources/mystack (resource) |
| | http://controller:8004/v1/fda820529c814812a7ab5fdcb878b291/stacks/mystack/38aca37e-2607-4bc9-bcf3-7039c8ca5141 (stack) |
| logical_resource_id | mystack |
| physical_resource_id | 38aca37e-2607-4bc9-bcf3-7039c8ca5141 |
| resource_name | mystack |
| resource_properties | {} |
| resource_status | CREATE_COMPLETE |
| resource_status_reason | Stack CREATE completed successfully |
| resource_type | OS::Heat::Stack |
+------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+