OpenStack Heat AutoScaling详解及实例代码
openstack heat autoscaling
一、背景
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
伸缩组是具有相同应用场景的实例的集合,定义了组内实例数的最大值和最小值,冷却时间等等。
注:冷却时间是指一个伸缩活动后的一段锁定时间,在这个时间内不能进行其他的伸缩活动。
语法如下:
{ "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用于配置组内的实例。
语法如下:
{ "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添加伸缩的策略,定义了具体的扩展或者收缩的操作,以及伸缩的数量。
语法如下:
{ "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
下面是一个简单的例子:
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%,将会减少一个实例。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!