OpenStack Heat AutoScaling詳解及實(shí)例代碼
OpenStack Heat AutoScaling
一、背景
Openstack的Heat是在H版之后加入的組件,旨在創(chuàng)建一套業(yè)務(wù)流程,更輕松的管理一個(gè)集群。集群內(nèi)的虛擬機(jī)可以作為一個(gè)整體,統(tǒng)一的為客戶提供服務(wù)。Heat中把功能定義成資源,在Heat中會(huì)用到Nova,Neutron,Ceilometer等組件,這些都可以看成是資源,通過(guò)模板文件來(lái)描述,模板文件可以是yaml格式,也可以是json格式,一般是yaml格式。
AutoScaling的概念最早出現(xiàn)在AWS,AutoScaling是一項(xiàng)Web服務(wù),目的是根據(jù)用戶定義的策略,時(shí)間表的運(yùn)行狀態(tài)檢查啟動(dòng)或終止虛擬機(jī),達(dá)到自動(dòng)伸縮。
Openstack里的Auto Scale是由Heat和Ceilometer模塊一起配合完成的。Ceilometer負(fù)責(zé)收集處理性能數(shù)據(jù),一旦達(dá)到Heat模版里定義的閥值,就發(fā)告警信息給heat-engine,由heat-engine調(diào)動(dòng)Heat模版里定義的其它的OpenStack資源實(shí)現(xiàn)auto scale。
二、Heat AutoScaling Resources
實(shí)現(xiàn)AutoScaling功能涉及到的資源如下:
1.AWS::AutoScaling::AutoScalingGroup
伸縮組是具有相同應(yīng)用場(chǎng)景的實(shí)例的集合,定義了組內(nèi)實(shí)例數(shù)的最大值和最小值,冷卻時(shí)間等等。
注:冷卻時(shí)間是指一個(gè)伸縮活動(dòng)后的一段鎖定時(shí)間,在這個(gè)時(shí)間內(nèi)不能進(jìn)行其他的伸縮活動(dòng)。
語(yǔ)法如下:
{ "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
伸縮配置定義了用于彈性伸縮的實(shí)例的配置。由AutoScalingGroup用于配置組內(nèi)的實(shí)例。
語(yǔ)法如下:
{ "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添加伸縮的策略,定義了具體的擴(kuò)展或者收縮的操作,以及伸縮的數(shù)量。
語(yǔ)法如下:
{ "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監(jiān)控實(shí)例的運(yùn)行狀況,一旦超過(guò)閾值,則會(huì)產(chǎn)生告警。
三、 Heat AutoScaling Template
下面是一個(gè)簡(jiǎn)單的例子:
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中的實(shí)例的配置 type: AWS::AutoScaling::LaunchConfiguration properties: ImageId: { get_param: image }#實(shí)例使用的image InstanceType: { get_param: flavor }#實(shí)例使用的flavor SecurityGroups: [ get_resource: nova_server_security_group ] UserData: |#實(shí)例啟動(dòng)時(shí)運(yùn)行的腳本 #!/bin/bash passwd root << EOD 123456 123456 EOD server_group:#伸縮組 type: AWS::AutoScaling::AutoScalingGroup properties: AvailabilityZones: [] Cooldown: '60'#冷卻時(shí)間 LaunchConfigurationName: { get_resource: launch_config }#組中實(shí)例的配置 MinSize: '1'#最小實(shí)例數(shù) MaxSize: '4'#最大實(shí)例數(shù) VPCZoneIdentifier: [ get_resource: neutron_subnet ] scaleout_policy:#向上擴(kuò)展的策略 type: AWS::AutoScaling::ScalingPolicy properties: AdjustmentType: ChangeInCapacity #heat 支持三種調(diào)整方式:change_in_capacity (new = current + adjustment), #exact_capacity (new = adjustment), percent_change_in_capacity (在current 的基#礎(chǔ)上上按照 adjustment 的 百分比調(diào)整) AutoScalingGroupName: { get_resource: server_group } ScalingAdjustment: '1'#每次的調(diào)整量,即增加一個(gè)實(shí)例 scalein_policy:#向下收縮的策略 type: AWS::AutoScaling::ScalingPolicy properties: AdjustmentType: ChangeInCapacity AutoScalingGroupName: { get_resource: server_group } ScalingAdjustment: '-1'#每次的調(diào)整量,即減少一個(gè)實(shí)例 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: #定義一個(gè) ceilometer alarm type: OS::Ceilometer::Alarm properties: description: Scale-up if the average CPU > 80% for 10 minute meter_name: cpu_util #監(jiān)控虛擬機(jī)的 cpu_util statistic: avg #statistic 的計(jì)算方法為 avg 即平均值法 period: 600 #統(tǒng)計(jì)周期 evaluation_periods: 1 #連續(xù)幾個(gè)周期才算有效 repeat_actions: true threshold: { get_param: alarm_scaleout_threshold }# cpu_util 的閾值 alarm_actions: #該告警在alarm 狀態(tài)時(shí)的 action。 - {get_attr: [scaleout_policy, AlarmUrl]} matching_metadata: {'metadata.user_metadata.groupname': {get_resource: 'server_group'}} comparison_operator: gt #檢測(cè)值和閾值的比較方式為 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#檢測(cè)值和閾值的比較方式為 lt 即小于 outputs: scale_in_url: value: { get_attr: [ scalein_policy, AlarmUrl ] } scale_out_url: value: { get_attr: [ scaleout_policy, AlarmUrl ] }
這個(gè)stack的功能是監(jiān)控實(shí)例的CPU使用率,當(dāng)CPU使用率大于80%時(shí),將會(huì)啟動(dòng)一個(gè)新的實(shí)例,當(dāng)CPU使用率小于20%,將會(huì)減少一個(gè)實(shí)例。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Openstack 創(chuàng)建項(xiàng)目和虛擬機(jī)詳細(xì)介紹
這篇文章主要介紹了Openstack 創(chuàng)建項(xiàng)目和虛擬機(jī)詳細(xì)介紹的相關(guān)資料,這里舉例說(shuō)明如何實(shí)現(xiàn),圖文教程,需要的朋友可以參考下2016-11-11CentOS 6.4下安裝部署OpenStack云計(jì)算平臺(tái)的方法
現(xiàn)在好多公司都使用Openstack,所以也想著學(xué)習(xí)下用OpenStack云計(jì)算平臺(tái),這篇文章給加詳細(xì)介紹了CentOS 6.4下安裝部署OpenStack云計(jì)算平臺(tái)的方法,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-10-10java.util.ConcurrentModificationException 解決方法
這篇文章主要介紹了 java.util.ConcurrentModificationException 解決方法的相關(guān)資料,需要的朋友可以參考下2016-11-11openstack pike單機(jī)一鍵安裝shell的方法
這篇文章主要介紹了openstack pike單機(jī)一鍵安裝shell的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Openstack 啟動(dòng)instance ''hvm''錯(cuò)誤問(wèn)題解決辦法
這篇文章主要介紹了Openstack 啟動(dòng)instance 'hvm'錯(cuò)誤問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2016-11-11openstack使用openvswitch實(shí)現(xiàn)vxlan的方法
這篇文章主要介紹了openstack使用openvswitch實(shí)現(xiàn)vxlan的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Openstack 節(jié)點(diǎn)維護(hù)詳細(xì)講解
這篇文章主要介紹了Openstack 節(jié)點(diǎn)維護(hù)詳細(xì)講解的相關(guān)資料,需要的朋友可以參考下2016-11-11云計(jì)算openstack框架分類及發(fā)展階段概述
這篇文章主要為大家介紹了云計(jì)算openstack框架分類及發(fā)展階段概述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04openstack云計(jì)算cinder架構(gòu)及各組件功能介紹
這篇文章主要為大家介紹了openstack云計(jì)算之cinder架構(gòu)及各組件功能介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04