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%,将会减少一个实例。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!