几分钟搞定 OpenStack Heat AutoScaling 根据CPU 自动伸缩VM

一、背景

Openstack的Heat是在H版之后加入的组件,旨在创建一套业务流程,更轻松的管理一个集群。集群内的虚拟机可以作为一个整体,统一的为客户提供服务。Heat中把功能定义成资源,在Heat中会用到Nova,Neutron,Ceilometer等组件,这些都可以看成是资源,通过模板文件来描述,模板文件可以是yaml格式,也可以是json格式,一般是yaml格式。
AutoScaling的概念最早出现在AWS,AutoScaling是一项Web服务,目的是根据用户定义的策略,时间表的运行状态检查启动或终止虚拟机,达到自动伸缩。

Openstack里的Auto Scale是由Heat和Ceilometer模块一起配合完成的。Ceilometer负责收集处理性能数据,一旦达到Heat模版里定义的阀值,就发告警信息给heat-engine,由heat-engine调动Heat模版里定义的其它的OpenStack资源实现auto scale。

二、Heat AutoScaling Resources

实现AutoScaling功能涉及到的资源如下:

1.AWS::AutoScaling::AutoScalingGroup

伸缩组是具有相同应用场景的实例的集合,定义了组内实例数的最大值和最小值,冷却时间等等。
注:冷却时间是指一个伸缩活动后的一段锁定时间,在这个时间内不能进行其他的伸缩活动。

语法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  "Type" : "AWS::AutoScaling::AutoScalingGroup" ,
  "Properties" : {
   "AvailabilityZones" : [ String, ... ],
   "Cooldown" : String,
   "DesiredCapacity" : String,
   "HealthCheckGracePeriod" : Integer,
   "HealthCheckType" : String,
   "InstanceId" : String,
   "LaunchConfigurationName" : String,
   "LoadBalancerNames" : [ String, ... ],
   "MaxSize" : String,
   "MetricsCollection" : [ MetricsCollection, ... ]
   "MinSize" : String,
   "NotificationConfigurations" : [ NotificationConfigurations, ... ],
   "PlacementGroup" : String,
   "Tags" : [ Auto Scaling Tag, ..., ],
   "TargetGroupARNs" : [ String, ... ],
   "TerminationPolicies" : [ String, ..., ],
   "VPCZoneIdentifier" : [ String, ... ]
  }
}

2.AWS::AutoScaling::LaunchConfiguration

伸缩配置定义了用于弹性伸缩的实例的配置。由AutoScalingGroup用于配置组内的实例。

语法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
   "Type" : "AWS::AutoScaling::LaunchConfiguration" ,
   "Properties" : {
    "AssociatePublicIpAddress" : Boolean,
    "BlockDeviceMappings" : [ BlockDeviceMapping, ... ],
    "ClassicLinkVPCId" : String,
    "ClassicLinkVPCSecurityGroups" : [ String, ... ],
    "EbsOptimized" : Boolean,
    "IamInstanceProfile" : String,
    "ImageId" : String,
    "InstanceId" : String,
    "InstanceMonitoring" : Boolean,
    "InstanceType" : String,
    "KernelId" : String,
    "KeyName" : String,
    "PlacementTenancy" : String,
    "RamDiskId" : String,
    "SecurityGroups" : [ SecurityGroup, ... ],
    "SpotPrice" : String,
    "UserData" : String
   }
}

3.AWS::AutoScaling::ScalingPolicy

为auto scale group添加伸缩的策略,定义了具体的扩展或者收缩的操作,以及伸缩的数量。

语法如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
   "Type" : "AWS::AutoScaling::ScalingPolicy" ,
   "Properties" : {
    "AdjustmentType" : String,
    "AutoScalingGroupName" : String,
    "Cooldown" : String,
    "EstimatedInstanceWarmup" : Integer,
    "MetricAggregationType" : String,
    "MinAdjustmentMagnitude" : Integer,
    "PolicyType" : String,
    "ScalingAdjustment" : Integer,
    "StepAdjustments" : [ StepAdjustments, ... ]
   }
}  

此外,Heat中AutoScaling还需配合OS::Ceilometer::Alarm使用,由Alarm监控实例的运行状况,一旦超过阈值,则会产生告警。

三、 Heat AutoScaling Template

下面是一个简单的例子:

?
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
heat_template_version: 2013-05-23
description: Heat template for autoscaling
parameters: #定义一些变量
  flavor:
   type : string
   default: m1.small
  image:
   type : string
   default: 1a2b3c4f-1a2b-3c4f-5d6e-4130ff5203de
  availability_zone:
   type : string
   default: nova
  alarm_scaleout_threshold: #阈值
   type : number
   default: 80
  alarm_scalein_threshold: #阈值
   type : number
   default: 20
resources:
  neutron_network:
   type : OS::Neutron::Net
   properties:
    name: {get_param: "OS::stack_name" }
 
  neutron_subnet:
   type : OS::Neutron::Subnet
   properties:
    name: {get_param: "OS::stack_name" }
    network_id: { get_resource: neutron_network }
    cidr: '192.168.111.0/24'
    gateway_ip: '192.168.111.1'
    allocation_pools:
     - start: '192.168.111.2'
      end: '192.168.111.254'
  neutron_router:
   type : OS::Neutron::Router
   properties:
    name: {get_param: "OS::stack_name" }
  add_router_interface:
   type : OS::Neutron::RouterInterface
   properties:
    router_id: { get_resource: neutron_router }
    subnet_id: { get_resource: neutron_subnet }
  nova_server_security_group:
   type : OS::Neutron::SecurityGroup
   properties:
    description: 'security group for VM'
    name: {get_param: "OS::stack_name" }
    rules: [
     {direction: 'ingress' ,
      remote_ip_prefix: '0.0.0.0/0' ,
      port_range_min: 0,
      port_range_max: 30000,
      ethertype: IPv4,
      protocol: 'tcp' },
     {direction: 'egress' ,
      remote_ip_prefix: '0.0.0.0/0' ,
      port_range_min: 0,
      port_range_max: 65535,
      ethertype: 'IPv4' ,
      protocol: 'tcp' },
     {direction: 'egress' ,
      remote_ip_prefix: '0.0.0.0/0' ,
      port_range_min: 0,
      port_range_max: 65535,
      ethertype: 'IPv4' ,
      protocol: 'udp' },
     {direction: 'ingress' ,
      remote_ip_prefix: '0.0.0.0/0' ,
      port_range_min: null,
      port_range_max: null,
      ethertype: 'IPv4' ,
      protocol: 'icmp' },
     {direction: egress,
      remote_ip_prefix: '0.0.0.0/0' ,
      port_range_min: null,
      port_range_max: null,
      ethertype: 'IPv4' ,
      protocol: 'icmp' }
    ]
  launch_config: #Scale group中的实例的配置
   type : AWS::AutoScaling::LaunchConfiguration
   properties:
    ImageId: { get_param: image } #实例使用的image
    InstanceType: { get_param: flavor } #实例使用的flavor
    SecurityGroups: [ get_resource: nova_server_security_group ]
    UserData: | #实例启动时运行的脚本
      #!/bin/bash
      passwd root << EOD
      123456
      123456
      EOD
  server_group: #伸缩组
   type : AWS::AutoScaling::AutoScalingGroup
   properties:
    AvailabilityZones: []
    Cooldown: '60' #冷却时间
    LaunchConfigurationName: { get_resource: launch_config } #组中实例的配置
    MinSize: '1' #最小实例数
    MaxSize: '4' #最大实例数
    VPCZoneIdentifier: [ get_resource: neutron_subnet ]
  scaleout_policy: #向上扩展的策略
   type : AWS::AutoScaling::ScalingPolicy
   properties:
    AdjustmentType: ChangeInCapacity
#heat 支持三种调整方式:change_in_capacity (new = current + adjustment), #exact_capacity (new = adjustment), percent_change_in_capacity (在current 的基#础上上按照 adjustment 的 百分比调整)
    AutoScalingGroupName: { get_resource: server_group }
    ScalingAdjustment: '1' #每次的调整量,即增加一个实例
  scalein_policy: #向下收缩的策略
   type : AWS::AutoScaling::ScalingPolicy
   properties:
    AdjustmentType: ChangeInCapacity
    AutoScalingGroupName: { get_resource: server_group }
    ScalingAdjustment: '-1' #每次的调整量,即减少一个实例
  neutron_port:
   type : OS::Neutron::Port
   properties:
    network_id: { get_resource: neutron_network }
    fixed_ips:
     - subnet_id: { get_resource: neutron_subnet }
    security_groups: [ { get_resource: nova_server_security_group } ]
  alarm_scaleout: #定义一个 ceilometer alarm
   type : OS::Ceilometer::Alarm
   properties:
    description: Scale-up if the average CPU > 80% for 10 minute
    meter_name: cpu_util #监控虚拟机的 cpu_util
    statistic: avg #statistic 的计算方法为 avg 即平均值法
    period: 600 #统计周期
    evaluation_periods: 1 #连续几个周期才算有效
    repeat_actions: true
    threshold: { get_param: alarm_scaleout_threshold } # cpu_util 的阈值
    alarm_actions: #该告警在alarm 状态时的 action。
     - {get_attr: [scaleout_policy, AlarmUrl]}
    matching_metadata: { 'metadata.user_metadata.groupname' : {get_resource: 'server_group' }}
    comparison_operator: gt #检测值和阈值的比较方式为 gt 即大于
  alarm_scalein:
   type : OS::Ceilometer::Alarm
   properties:
    description: Scale-down if the average CPU < 20% for 10 minutes
    meter_name: cpu_util
    statistic: avg
    period: 600
    evaluation_periods: 1
    repeat_actions: true
    threshold: { get_param: alarm_scalein_threshold }
    alarm_actions:
     - {get_attr: [scalein_policy, AlarmUrl]}
    matching_metadata: { 'metadata.user_metadata.groupname' : {get_resource: 'server_group' }}
    comparison_operator: lt #检测值和阈值的比较方式为 lt 即小于
outputs:
  scale_in_url:
   value: { get_attr: [ scalein_policy, AlarmUrl ] }
  scale_out_url:
   value: { get_attr: [ scaleout_policy, AlarmUrl ] }

这个stack的功能是监控实例的CPU使用率,当CPU使用率大于80%时,将会启动一个新的实例,当CPU使用率小于20%,将会减少一个实例。



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值