环境
操作系统:CentOS Linux release 7.5.1804 (Core)
OpenStack版本:OpenStack Ocata Allinone
注意事项:packstack安装时开启 heat-cfn,gnocchi,aodh,panko等
测试
环境修改
修改 OpenStack 为二层模式,修改方法这里不在赘述。
过程描述
测试步骤
创建 Stack
使用以下模版创建 Stack
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
|
heat_template_version: 2016-10-14
description: Example auto scale group, policy and alarm
resources:
scaleup_group:
type: OS::Heat::AutoScalingGroup
properties:
cooldown: 300
desired_capacity: 1
max_size: 3
min_size: 1
resource:
type: OS::Nova::Server
properties:
image: 'xenial-server-cloudimg-amd64'
metadata: {"metering.server_group": {get_param: "OS::stack_id"}}
flavor: m1.small
networks:
- network: net0
scaleup_policy:
type: OS::Heat::ScalingPolicy
properties:
adjustment_type: change_in_capacity
auto_scaling_group_id: { get_resource: scaleup_group }
cooldown: 300
scaling_adjustment: 1
scaledown_policy:
type: OS::Heat::ScalingPolicy
properties:
adjustment_type: change_in_capacity
auto_scaling_group_id: { get_resource: scaleup_group }
cooldown: 300
scaling_adjustment: -1
cpu_alarm_high:
type: OS::Aodh::GnocchiAggregationByResourcesAlarm
properties:
description: Scale up if CPU > 80%
metric: cpu_util
aggregation_method: mean
granularity: 300
evaluation_periods: 1
threshold: 80
resource_type: instance
comparison_operator: gt
alarm_actions:
- str_replace:
template: trust+url
params:
url: {get_attr: [scaleup_policy, signal_url]}
query:
str_replace:
template: '{"=": {"server_group": "stack_id"}}'
params:
stack_id: {get_param: "OS::stack_id"}
cpu_alarm_low:
type: OS::Aodh::GnocchiAggregationByResourcesAlarm
properties:
metric: cpu_util
aggregation_method: mean
granularity: 300
evaluation_periods: 1
threshold: 10
resource_type: instance
comparison_operator: lt
alarm_actions:
- str_replace:
template: trust+url
params:
url: {get_attr: [scaledown_policy, signal_url]}
query:
str_replace:
template: '{"=": {"server_group": "stack_id"}}'
params:
stack_id: {get_param: "OS::stack_id"}
outputs:
scaleup_policy_signal_url:
value: {get_attr: [scaleup_policy, signal_url]}
scaledown_policy_signal_url:
value: {get_attr: [scaledown_policy, signal_url]}
|
模版定义两个告警和伸缩策略,当 CPU 使用率在 300s 内大于 80% 则发出告警,heat 接收到告警开始创建新的虚拟机,最多 3 台。当 CPU 使用率在 300s 内小于 10% 则发出告警,heat 接收到告警开始删除虚拟机,最少保留 1 台。
查看创建的 stack 、alarm 和虚拟机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
~ openstack stack list
+--------------------------------------+-------------+-----------------+----------------------+--------------+
| ID | Stack Name | Stack Status | Creation Time | Updated Time |
+--------------------------------------+-------------+-----------------+----------------------+--------------+
| af75c5e5-35c6-4ab3-b899-e4d9afbbcd74 | autoscaling | CREATE_COMPLETE | 2018-10-19T03:32:29Z | None |
+--------------------------------------+-------------+-----------------+----------------------+--------------+
➜ ~ openstack alarm list
+--------------------------------------+--------------------------------------------+-----------------------------------------+-------+----------+---------+
| alarm_id | type | name | state | severity | enabled |
+--------------------------------------+--------------------------------------------+-----------------------------------------+-------+----------+---------+
| 6dfded8c-21a9-4792-a21c-eebc421e8706 | gnocchi_aggregation_by_resources_threshold | autoscaling-cpu_alarm_low-4e2ptwvvhbjq | alarm | low | True |
| 2eaa8f6c-3ec5-43ca-8c19-81470c0686a8 | gnocchi_aggregation_by_resources_threshold | autoscaling-cpu_alarm_high-2t4zh5stxmt7 | ok | low | True |
+--------------------------------------+--------------------------------------------+-----------------------------------------+-------+----------+---------+
~ openstack server list
+--------------------------------------+-------------------------------------------------------+--------+----------------+------------------------------+
| ID | Name | Status | Networks | Image Name |
+--------------------------------------+-------------------------------------------------------+--------+----------------+------------------------------+
| 3d723c6d-6c3e-41ba-a906-ded508f5006a | au-aleup_group-glnobyldea74-jnbifnyzqpr7-vghtyrjmru5u | ACTIVE | net0=10.0.1.6 | xenial-server-cloudimg-amd64 |
+--------------------------------------+-------------------------------------------------------+--------+----------------+------------------------------+
|
自动创建测试
1. 通过 stress 程序对虚拟机 CPU 进行压测
打开虚拟机控制台,运行以下命令进行 CPU 压测,持续 1200s
1
|
# stress --cpu 1 --timeout 1200
|
2. 查看虚拟机的 CPU 使用率
使用以下命令查询虚拟机的 CPU 使用率
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
~ gnocchi measures show cpu_util --resource-id instance_uuid
+---------------------------+-------------+----------------+
| timestamp | granularity | value |
+---------------------------+-------------+----------------+
| 2018-10-19T06:00:00+00:00 | 300.0 | 0.16605959835 |
| 2018-10-19T06:05:00+00:00 | 300.0 | 0.161092701601 |
| 2018-10-19T06:10:00+00:00 | 300.0 | 0.162481952842 |
| 2018-10-19T06:15:00+00:00 | 300.0 | 0.166559675456 |
| 2018-10-19T06:20:00+00:00 | 300.0 | 0.167608932622 |
| 2018-10-19T06:25:00+00:00 | 300.0 | 0.664303071272 |
| 2018-10-19T06:30:00+00:00 | 300.0 | 0.170293083241 |
| 2018-10-19T06:35:00+00:00 | 300.0 | 63.2803744387 |
| 2018-10-19T06:40:00+00:00 | 300.0 | 99.9796696522 |
+---------------------------+-------------+----------------+
|
最后两条记录显示 CPU 占用率已经 > 80% 了
3. 查看告警触发情况
1
2
3
4
5
6
7
|
~ openstack alarm list
+--------------------------------------+--------------------------------------------+-----------------------------------------+-------+----------+---------+
| alarm_id | type | name | state | severity | enabled |
+--------------------------------------+--------------------------------------------+-----------------------------------------+-------+----------+---------+
| 6dfded8c-21a9-4792-a21c-eebc421e8706 | gnocchi_aggregation_by_resources_threshold | autoscaling-cpu_alarm_low-4e2ptwvvhbjq | ok | low | True |
| 2eaa8f6c-3ec5-43ca-8c19-81470c0686a8 | gnocchi_aggregation_by_resources_threshold | autoscaling-cpu_alarm_high-2t4zh5stxmt7 | alarm | low | True |
+--------------------------------------+--------------------------------------------+-----------------------------------------+-------+----------+---------+
|
可以看到 autoscaling-cpu_alarm_high-2t4zh5stxmt7 告警已经被触发
4. 查看虚拟机
可以看到自动创建了一台虚拟机
自动删除测试
1. 停止虚拟机压测程序
停止压测程序,等待 CPU 使用率降低下来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
~ gnocchi measures show cpu_util --resource-id 578a14e4-1e00-4812-8039-81209f135116
+---------------------------+-------------+----------------+
| timestamp | granularity | value |
+---------------------------+-------------+----------------+
| 2018-10-19T06:00:00+00:00 | 300.0 | 0.16605959835 |
| 2018-10-19T06:05:00+00:00 | 300.0 | 0.161092701601 |
| 2018-10-19T06:10:00+00:00 | 300.0 | 0.162481952842 |
| 2018-10-19T06:15:00+00:00 | 300.0 | 0.166559675456 |
| 2018-10-19T06:20:00+00:00 | 300.0 | 0.167608932622 |
| 2018-10-19T06:25:00+00:00 | 300.0 | 0.664303071272 |
| 2018-10-19T06:30:00+00:00 | 300.0 | 0.170293083241 |
| 2018-10-19T06:35:00+00:00 | 300.0 | 63.2803744387 |
| 2018-10-19T06:40:00+00:00 | 300.0 | 94.3206761804 |
| 2018-10-19T06:45:00+00:00 | 300.0 | 99.9847353387 |
| 2018-10-19T06:50:00+00:00 | 300.0 | 99.984336257 |
| 2018-10-19T06:55:00+00:00 | 300.0 | 30.1423223067 |
| 2018-10-19T07:00:00+00:00 | 300.0 | 0.163830348301 |
+---------------------------+-------------+----------------+
|
可以看到 CPU 的使用率已经降了下来
2. 查看告警触发情况
1
2
3
4
5
6
7
|
~ openstack alarm list
+--------------------------------------+--------------------------------------------+-----------------------------------------+-------+----------+---------+
| alarm_id | type | name | state | severity | enabled |
+--------------------------------------+--------------------------------------------+-----------------------------------------+-------+----------+---------+
| 6dfded8c-21a9-4792-a21c-eebc421e8706 | gnocchi_aggregation_by_resources_threshold | autoscaling-cpu_alarm_low-4e2ptwvvhbjq | alarm | low | True |
| 2eaa8f6c-3ec5-43ca-8c19-81470c0686a8 | gnocchi_aggregation_by_resources_threshold | autoscaling-cpu_alarm_high-2t4zh5stxmt7 | ok | low | True |
+--------------------------------------+--------------------------------------------+-----------------------------------------+-------+----------+---------+
|
可以看到 autoscaling-cpu_alarm_low-4e2ptwvvhbjq 告警已经被触发
3. 查看虚拟机
可以看到最早创建的一台虚拟机被自动删除了
总结
本文只是最简单的测试,heat 还可以结合 lb 实现更强大的弹性伸缩功能,值得深入学习下。