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虛擬機(jī)快照和增量備份實(shí)現(xiàn)方法
快照針對(duì)要保存的數(shù)據(jù)分為內(nèi)存快照和磁盤快照,內(nèi)存快照就是保存當(dāng)前內(nèi)存的數(shù)據(jù),磁盤快照就是保存硬盤的數(shù)據(jù),這篇文章主要介紹了OpenStack虛擬機(jī)快照和增量備份實(shí)現(xiàn),需要的朋友可以參考下2022-04-04
Openstack安裝過(guò)程中遇到的問(wèn)題匯總
本文給大家分享的是作者在Centos7中安裝openstack過(guò)程中出現(xiàn)的一些問(wèn)題的匯總,以及解決的方法,有需要的小伙伴可以參考下2017-04-04
OpenStack Identity(Keystone)身份服務(wù)、體系結(jié)構(gòu)與中間件講解
OpenStack Identity(Keystone)服務(wù)為運(yùn)行OpenStack Compute上的OpenStack云提供了認(rèn)證和管理用戶、帳號(hào)和角色信息服務(wù),并為OpenStack Object Storage提供授權(quán)服務(wù)。對(duì)openstack identity相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-11-11
OpenStack手動(dòng)分布式部署Keystone(Queens版)
這篇文章主要介紹了OpenStack手動(dòng)分布式部署Keystone(Queens版),Keystone是OpenStack框架中負(fù)責(zé)管理身份驗(yàn)證服務(wù)訪問(wèn)規(guī)則和服務(wù)令牌功能的組件,需要的朋友可以參考下2023-03-03
詳解Openstack使用ubuntu鏡像啟動(dòng)虛擬機(jī)實(shí)例
這篇文章主要介紹了詳解Openstack使用ubuntu鏡像啟動(dòng)虛擬機(jī)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Openstack 網(wǎng)絡(luò)知識(shí)資料詳細(xì)介紹及總結(jié)
這篇文章主要介紹了Openstack 網(wǎng)絡(luò)知識(shí)資料詳細(xì)介紹及總結(jié)的相關(guān)資料,在開(kāi)發(fā)云計(jì)算的時(shí)候 Openstack 很多公司都在用,這里就整理下網(wǎng)絡(luò)知識(shí),需要的朋友可以參考下2016-12-12
Centos7環(huán)境準(zhǔn)備openstack pike的安裝
本篇文章主要介紹了Centos7環(huán)境準(zhǔn)備openstack pike的安裝,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Openstack 創(chuàng)建項(xiàng)目和虛擬機(jī)詳細(xì)介紹
這篇文章主要介紹了Openstack 創(chuàng)建項(xiàng)目和虛擬機(jī)詳細(xì)介紹的相關(guān)資料,這里舉例說(shuō)明如何實(shí)現(xiàn),圖文教程,需要的朋友可以參考下2016-11-11
centos下最簡(jiǎn)安裝openstack——使用packstack詳解
本篇文章主要介紹了centos下最簡(jiǎn)安裝openstack——使用packstack,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
openstack云計(jì)算keystone架構(gòu)源碼分析
這篇文章主要為大家介紹了openstack云計(jì)算keystone架構(gòu)源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04

