在Heat中完全使用aws的语法创建一套autoscaling的template。
流程:
Create LaunchConfig (Create basic instance, send mem status to ALARM) ->
Create ASGroup (Define instance num range) ->
Create ScaleUpPolicy (+1 instance when mem_alarm_high) ->
Create MEMAlarmHigh (Monitor mem status) ->
Create ScaleDownPolicy (-1 instance when mem_alarm_low) ->
Create MEMAlarmLow (monitor mem status)
Template模板:
"ASGroup" : {
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" : {
"AvailabilityZones" : ["nova"],
"LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
"MinSize" : "1",
"MaxSize" : "10"
}
},
"ScaleUpPolicy" : {
"Type" : "AWS::AutoScaling::ScalingPolicy",
"Properties" : {
"AdjustmentType" : "ChangeInCapacity",
"AutoScalingGroupName" : { "Ref" : "ASGroup" },
"Cooldown" : "60",
"ScalingAdjustment" : "1"
}
},
"MEMAlarmHigh": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "Scale-up if MEM > 70% for 1 minute",
"MetricName": "MemoryUtilization",
"Namespace": "system/linux",
"Statistic": "Average",
"Period": "60",
"EvaluationPeriods": "1",
"Threshold": "70",
"AlarmActions": [ { "Ref": "ScaleUpPolicy" } ],
"Dimensions": [
{
"Name": "AutoScalingGroupName",
"Value": { "Ref": "ASGroup" }
}
],
"ComparisonOperator": "GreaterThanThreshold"
}
},
"ScaleDownPolicy" : {
"Type" : "AWS::AutoScaling::ScalingPolicy",
"Properties" : {
"AdjustmentType" : "ChangeInCapacity",
"AutoScalingGroupName" : { "Ref" : "ASGroup" },
"Cooldown" : "60",
"ScalingAdjustment" : "-1"
}
},
"MEMAlarmLow": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"AlarmDescription": "Scale-down if MEM < 30% for 1 minute",
"MetricName": "MemoryUtilization",
"Namespace": "system/linux",
"Statistic": "Average",
"Period": "60",
"EvaluationPeriods": "1",
"Threshold": "30",
"AlarmActions": [ { "Ref": "ScaleDownPolicy" } ],
"Dimensions": [
{
"Name": "AutoScalingGroupName",
"Value": { "Ref": "ASGroup" }
}
],
"ComparisonOperator": "LessThanThreshold"
}
},
"LaunchConfig" : {
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"files" : {
"/etc/cfn/cfn-credentials" : {
"content" : { "Fn::Join" : ["", [
"AWSAccessKeyId=", { "Ref" : "LSFKeys" }, "\n",
"AWSSecretKey=", {"Fn::GetAtt": ["LSFKeys",
"SecretAccessKey"]}, "\n"
]]},
"mode" : "000400",
"owner" : "root",
"group" : "root"
},
"/tmp/stats-crontab.txt" : {
"content" : { "Fn::Join" : ["", [
"MAIL=\"\"\n",
"\n",
"* * * * * /opt/aws/bin/cfn-push-stats --watch ",
{ "Ref" : "MEMAlarmHigh" }, " --mem-util\n",
"* * * * * /opt/aws/bin/cfn-push-stats --watch ",
{ "Ref" : "MEMAlarmLow" }, " --mem-util\n"
]]},
"mode" : "000600",
"owner" : "root",
"group" : "root"
}
}
}
}
},
"Properties": {
"ImageId": "rhel6u4",
"InstanceType": "m1.small",
"KeyName": "poc",
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -v\n",
"/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackName" },
" -r VMWARELaunchConfig ",
" --region ", { "Ref" : "AWS::Region" }, "\n",
"crontab /tmp/stats-crontab.txt\n",
]]}}
}
}
},