openstack-heat编排模板使用测试

基于 openstack stein版本heat,测试编排,测试通过

Heat 编排

Heat 目前支持两种格式的模板,一种是基于 JSON 格式的 CFN 模板;另外一种是基于 YAML 格式的 HOT 模板。CFN 模板主要是为了保持对 AWS 的兼容性。HOT 模板是 Heat 自有的,资源类型更加丰富,更能体现出 Heat 特点的模板。

一个典型的 HOT 模板由下列元素构成:

  • 模板版本:必填字段,指定所对应的模板版本,Heat 会根据版本进行检验。
  • 参数列表:选填,指输入参数列表。
  • 资源列表:必填,指生成的 Stack 所包含的各种资源。可以定义资源间的依赖关系,比如说生成 Port,然后再用 port 来生成 VM。
  • 输出列表:选填,指生成的 Stack 暴露出来的信息,可以用来给用户使用,也可以用来作为输入提供给其它的 Stack。
需求1 - 云盘单台编排

1.创建一个虚拟机,要求虚拟机自定义镜像、flavor、network、可用域和安全组;
2.创建一个数据卷,要求数据卷自定义存储类型、可用域和块容量;
3.将数据卷挂载给虚拟机;
4.要求所有资源通过参数传入;
5.要求返回属性包含虚拟机uuid、ip地址和vnc地址;

这个需求很简单,我们只需要查下Heat支持的模板类型,很快就可以找到只需要三个类型就可以满足需求

  • type: OS::Nova::Server
  • type: OS::Cinder::Volume
  • type: OS::Cinder::Volume

模板如下:

CREATE_VM_VOLUME_AND_ATTATCH.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
heat_template_version: 2013-05-23

description: Template for Create VM

parameters:
image_id:
type: string
description: Image ID or image name to use for the server
constraints:
- custom_constraint: glance.image

secgroup_id:
type: string
description : Id of the security groupe

network_id:
type: string
description: network id

flavor_id:
type: string
description: Flavor for the server to be created
constraints:
- custom_constraint: nova.flavor

server_az:
type: string
description: Availablity zone of the VM
default: nova

volume_size:
type: number
description: Size of volume to attach to VM
default: 1
constraints:
- range: { min: 1, max: 200 }

volume_type:
type: string
description: If specified, the type of volume to use, mapping to a specific backend.
default: sata
volume_az:
type: string
default: nova
server_name:
type: string
description: VM Name
default: heat-stack-default

resources:
server:
type: OS::Nova::Server
properties:
name: { get_param: server_name }
availability_zone: { get_param: server_az }
image: { get_param: image_id }
flavor: { get_param: flavor_id }
networks:
- network: { get_param: network_id }
security_groups:
- { get_param: secgroup_id }

volume:
type: OS::Cinder::Volume
properties:
size: { get_param: volume_size }
volume_type: { get_param: volume_type }
availability_zone: { get_param: volume_az }
description: Volume for stack

volume_attachment:
type: OS::Cinder::VolumeAttachment
properties:
volume_id: { get_resource: volume }
instance_uuid: { get_resource: server }

outputs:
server_id:
value: { get_resource: server }
server_ip:
description: Network IP address of server
value: { get_attr: [ server, first_address ] }
novnc_console_url:
value: { get_attr: [server, console_urls, novnc] }
description: novnc console URLs for the server
1
2
3
4
5
6
7
8
9
10
11
#openstack stack create -t CREATE_VM_VOLUME_AND_ATTACH.yaml \
--parameter volume_type=sata \
--parameter volume_az=nova \
--parameter volume_size=50 \
--parameter image_id=eef3ab2f-3c8f-4530-8806-15aa7603ba2f \
--parameter secgroup_id=de99ad74-aa45-44fc-bee3-0f29c7005abb \
--parameter network_id=92ef8068-46ba-4db7-b5d0-dcef1089efb2 \
--parameter flavor_id=ecs.small \
--parameter server_az=nova \
--parameter server_name=t-server-demo \
heat-test
需求2 - 云盘多台编排

1.在需求1的基础上添加批量创建资源,要求自定义数量
2.要求引入需求1的模板
3.要求返回一个批次stack里面的虚拟机uuid、虚拟机ip和vnc请求地址

这里我们需要学习一个新的资源类型OS::Heat::ResourceGroup,从名称就能够大概猜到该资源的作用:资源组,组内可以包括一个或多个相同的嵌套资源。

Multi_Num_VM_VOLUME_ATTACHE.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
heat_template_version: 2013-05-23

description: Template for Create VM Volume,the Volume will be auto Attached by heat.

parameters:

num_resources:
type: number
description: Numbers of Resrouce
default: 1
constraints:
- range: { min: 1, max: 10 }

image_id:
type: string
description: ID of the image to use for the instance to be created.
constraints:
- custom_constraint: glance.image

secgroup_id:
type: string
description : Id of the security groupe

network_id:
type: string
description: network id

flavor_id:
type: string
description: Flavor for the server to be created
constraints:
- custom_constraint: nova.flavor

server_az:
type: string
description: Availablity Zone of the VM
default: nova

volume_size:
type: number
description: Size of volume to attach to VM
default: 1
constraints:
- range: { min: 1, max: 200 }

volume_type:
type: string
description: If specified, the type of volume to use, mapping to a specific backend.

volume_az:
type: string
description: Availability Zone of the Volumes


resources:
resgroup:
type: OS::Heat::ResourceGroup
properties:
count: { get_param: num_resources }
resource_def:
type: CREATE_VM_VOLUME_AND_ATTACH.yaml #在这里引入了需求1的模板
properties:
server_name: heat-vm-%index%
image_id: { get_param: image_id }
secgroup_id: { get_param: secgroup_id }
network_id: { get_param: network_id }
flavor_id: { get_param: flavor_id }
server_az: { get_param: server_az }
volume_size: { get_param: volume_size }
volume_type: { get_param: volume_type }
volume_az: { get_param: volume_az }

outputs:
myrefs:
value: {get_attr: [resgroup, refs]}
server_ids:
value: {get_attr: [resgroup, server_id]}
server_ips:
value: {get_attr: [resgroup, server_ip]}
server_novnc_urls:
value: {get_attr: [resgroup, novnc_console_url]}
1
2
3
4
5
6
7
8
9
10
11
#openstack stack create -t Multi_Num_VM_VOLUME_ATTACH.yaml \
--parameter volume_type=sata \
--parameter volume_az=nova \
--parameter volume_size=50 \
--parameter image_id=eef3ab2f-3c8f-4530-8806-15aa7603ba2f \
--parameter secgroup_id=de99ad74-aa45-44fc-bee3-0f29c7005abb \
--parameter network_id=92ef8068-46ba-4db7-b5d0-dcef1089efb2 \
--parameter flavor_id=ecs.small \
--parameter server_az=nova \
--parameter num_resources=3 \
heat-test2

需求3 - 本地盘单台编排

CREATE_VM_LOCAL_STORAGE.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
heat_template_version: 2013-05-23

description: Template for Create VM

parameters:
image_id:
type: string
description: Image ID or image name to use for the server
constraints:
- custom_constraint: glance.image

secgroup_id:
type: string
description : Id of the security groupe

network_id:
type: string
description: network id

flavor_id:
type: string
description: Flavor for the server to be created
constraints:
- custom_constraint: nova.flavor

server_az:
type: string
description: Availablity zone of the VM
default: nova
server_name:
type: string
description: VM Name
default: heat-stack-default
resources:
server:
type: OS::Nova::Server
properties:
availability_zone: { get_param: server_az }
image: { get_param: image_id }
flavor: { get_param: flavor_id }
networks:
- network: { get_param: network_id }
security_groups:
- { get_param: secgroup_id }


outputs:
server_id:
value: { get_resource: server }
server_ip:
description: Network IP address of server
value: { get_attr: [ server, first_address ] }
novnc_console_url:
value: { get_attr: [server, console_urls, novnc] }
description: novnc console URLs for the server
1
2
3
4
5
6
7
8
#openstack stack create -t CREATE_VM_LOCAL_STORAGE.yaml \
--parameter image_id=eef3ab2f-3c8f-4530-8806-15aa7603ba2f \
--parameter secgroup_id=de99ad74-aa45-44fc-bee3-0f29c7005abb \
--parameter network_id=92ef8068-46ba-4db7-b5d0-dcef1089efb2 \
--parameter flavor_id=ecs.small_L \
--parameter server_az=nova \
--parameter server_name=t-server-demo-local \
heat-local-test

需求4 - 本地盘多台编排

Multi_Num_VM_LOCAL_STORAG.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
heat_template_version: 2013-05-23

description: Template for Create VM Volume,the Volume will be auto Attached by heat.

parameters:

num_resources:
type: number
description: Numbers of Resrouce
default: 1
constraints:
- range: { min: 1, max: 10 }

image_id:
type: string
description: ID of the image to use for the instance to be created.
constraints:
- custom_constraint: glance.image

secgroup_id:
type: string
description : Id of the security groupe

network_id:
type: string
description: network id

flavor_id:
type: string
description: Flavor for the server to be created
constraints:
- custom_constraint: nova.flavor

server_az:
type: string
description: Availablity Zone of the VM
default: nova


resources:
resgroup:
type: OS::Heat::ResourceGroup
properties:
count: { get_param: num_resources }
resource_def:
type: CREATE_VM_LOCAL_STORAGE.yaml #在这里引入了需求1的模板
properties:
server_name: heat-vm-local-%index%
image_id: { get_param: image_id }
secgroup_id: { get_param: secgroup_id }
network_id: { get_param: network_id }
flavor_id: { get_param: flavor_id }
server_az: { get_param: server_az }

outputs:
myrefs:
value: {get_attr: [resgroup, refs]}
server_ids:
value: {get_attr: [resgroup, server_id]}
server_ips:
value: {get_attr: [resgroup, server_ip]}
server_novnc_urls:
value: {get_attr: [resgroup, novnc_console_url]}
1
2
3
4
5
6
7
8
#openstack stack create -t Multi_Num_VM_LOCAL_STORAG.yaml  \
--parameter image_id=eef3ab2f-3c8f-4530-8806-15aa7603ba2f \
--parameter secgroup_id=de99ad74-aa45-44fc-bee3-0f29c7005abb \
--parameter network_id=92ef8068-46ba-4db7-b5d0-dcef1089efb2 \
--parameter flavor_id=ecs.small_L \
--parameter server_az=nova \
--parameter num_resources=3 \
heat-local-test2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
JSON post
{
"stack_name": "heat-local-test2",
"disable_rollback": true,
"parameters": {
"image_id": "eef3ab2f-3c8f-4530-8806-15aa7603ba2f",
"secgroup_id": "de99ad74-aa45-44fc-bee3-0f29c7005abb",
"network_id": "92ef8068-46ba-4db7-b5d0-dcef1089efb2",
"flavor_id": "ecs.small_L",
"server_az": "nova",
"num_resources": "3"
},
"template": {
"heat_template_version": "2013-05-23",
"description": "Template for Create VM Volume,the Volume will be auto Attached by heat.",
"parameters": {
"num_resources": {
"type": "number",
"description": "Numbers of Resrouce",
"default": 1,
"constraints": [{
"range": {
"min": 1,
"max": 10
}
}]
},
"image_id": {
"type": "string",
"description": "ID of the image to use for the instance to be created.",
"constraints": [{
"custom_constraint": "glance.image"
}]
},
"secgroup_id": {
"type": "string",
"description": "Id of the security groupe"
},
"network_id": {
"type": "string",
"description": "network id"
},
"flavor_id": {
"type": "string",
"description": "Flavor for the server to be created",
"constraints": [{
"custom_constraint": "nova.flavor"
}]
},
"server_az": {
"type": "string",
"description": "Availablity Zone of the VM",
"default": "nova"
}
},
"resources": {
"resgroup": {
"type": "OS::Heat::ResourceGroup",
"properties": {
"count": {
"get_param": "num_resources"
},
"resource_def": {
"type": "file:///opt/kolla-ansible-deploy/openstack/qcloud/dev/bjyt_region_1/CREATE_VM_LOCAL_STORAGE.yaml",
"properties": {
"image_id": {
"get_param": "image_id"
},
"secgroup_id": {
"get_param": "secgroup_id"
},
"network_id": {
"get_param": "network_id"
},
"flavor_id": {
"get_param": "flavor_id"
},
"server_az": {
"get_param": "server_az"
}
}
}
}
}
},
"outputs": {
"myrefs": {
"value": {
"get_attr": ["resgroup", "refs"]
}
},
"server_ids": {
"value": {
"get_attr": ["resgroup", "server_id"]
}
},
"server_ips": {
"value": {
"get_attr": ["resgroup", "server_ip"]
}
},
"server_novnc_urls": {
"value": {
"get_attr": ["resgroup", "novnc_console_url"]
}
}
}
},
"files": {
"file:///opt/kolla-ansible-deploy/openstack/dev/bjyt_region_1/CREATE_VM_LOCAL_STORAGE.yaml": "{\"heat_template_version\": \"2013-05-23\", \"description\": \"Template for Create VM\", \"parameters\": {\"image_id\": {\"type\": \"string\", \"description\": \"Image ID or image name to use for the server\", \"constraints\": [{\"custom_constraint\": \"glance.image\"}]}, \"secgroup_id\": {\"type\": \"string\", \"description\": \"Id of the security groupe\"}, \"network_id\": {\"type\": \"string\", \"description\": \"network id\"}, \"flavor_id\": {\"type\": \"string\", \"description\": \"Flavor for the server to be created\", \"constraints\": [{\"custom_constraint\": \"nova.flavor\"}]}, \"server_az\": {\"type\": \"string\", \"description\": \"Availablity zone of the VM\", \"default\": \"nova\"}}, \"resources\": {\"server\": {\"type\": \"OS::Nova::Server\", \"properties\": {\"availability_zone\": {\"get_param\": \"server_az\"}, \"image\": {\"get_param\": \"image_id\"}, \"flavor\": {\"get_param\": \"flavor_id\"}, \"networks\": [{\"network\": {\"get_param\": \"network_id\"}}], \"security_groups\": [{\"get_param\": \"secgroup_id\"}]}}}, \"outputs\": {\"server_id\": {\"value\": {\"get_resource\": \"server\"}}, \"server_ip\": {\"description\": \"Network IP address of server\", \"value\": {\"get_attr\": [\"server\", \"first_address\"]}}, \"novnc_console_url\": {\"value\": {\"get_attr\": [\"server\", \"console_urls\", \"novnc\"]}, \"description\": \"novnc console URLs for the server\"}}}"
},
"environment": {}
}

更多

经过以上两个实例,我相信大多数人对Heat编排都有一个大致了解,其实编排不难,难的是资源逻辑的整合。现在社区里面有一些开源的Heat模板,里面几乎包含了大部分的高级用法。感兴趣的朋友可以去了解下

1
$ git clone git://git.openstack.org/openstack/heat-templates

参考:https://www.jianshu.com/p/1f2482792683