创建自动缩放网络服务器组添加到现有 elb
Posted
技术标签:
【中文标题】创建自动缩放网络服务器组添加到现有 elb【英文标题】:Create autoscaling webserver group add to existing elb 【发布时间】:2013-04-05 01:35:59 【问题描述】:我有一个已经配置了端口和 SSL 证书等的弹性负载均衡器,并且 Route 53 设置为将我的站点的流量路由到它。
我想知道是否有一个示例 cloudFormation 模板可以创建一个自动缩放的 ec2 实例组,其中每个实例都被添加到这个现有的负载均衡器中或从中删除。
我已经在网上查看了示例 - 下面的示例似乎几乎是我所需要的,但它的问题(以及所有其他似乎使用此示例的变体)是它假定您想要创建一个新的负载均衡器。我没有。
https://s3.amazonaws.com/cloudformation-templates-us-east-1/AutoScalingMultiAZWithNotifications.template
有可能按照我的建议去做吗?有人有例子吗?
我的 CloudFormation 脚本如下所示(我删除了实际的服务器包配置部分)。这成功创建了一个新实例,但它不会添加到负载均衡器“load4”。我可以手动将主机添加到负载均衡器,但这显然违背了目的。
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "Create an Auto-scaling group that will attach to existing load balancer and inhereit existing security groups.",
"Parameters" :
"KeyName" :
"Description" : "mykeyname",
"Type" : "String"
,
"InstanceType" :
"Type" : "String",
"Default" : "m1.small",
"AllowedValues" : [ "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "c1.xlarge", "cc1.4xlarge" ],
"Description" : "EC2 instance type (e.g. m1.large, m1.xlarge, m2.xlarge)"
,
"SpotPrice":
"Description": "Spot price for application AutoScaling Group",
"Type": "Number",
"MinValue" : ".03"
,
"MinInstances" :
"Description" : "The minimum number of Workers",
"Type" : "Number",
"MinValue" : "0",
"Default" : "0",
"ConstraintDescription" : "Enter a number >=0"
,
"MaxInstances" :
"Description" : "The maximum number of Workers",
"Type" : "Number",
"MinValue" : "1",
"Default" : "4",
"ConstraintDescription" : "Enter a number >1"
,
"OperatorEmail":
"Description": "Email address to notify if there are any scaling operations",
"Type": "String"
,
"Mappings" :
"AWSInstanceType2Arch" :
"t1.micro" : "Arch" : "64" ,
"m1.small" : "Arch" : "64" ,
"m1.medium" : "Arch" : "64" ,
"m1.large" : "Arch" : "64" ,
"m1.xlarge" : "Arch" : "64" ,
"m2.xlarge" : "Arch" : "64" ,
"m2.2xlarge" : "Arch" : "64" ,
"m2.4xlarge" : "Arch" : "64" ,
"m3.xlarge" : "Arch" : "64" ,
"m3.2xlarge" : "Arch" : "64" ,
"c1.medium" : "Arch" : "64" ,
"c1.xlarge" : "Arch" : "64" ,
"cc1.4xlarge" : "Arch" : "64HVM" ,
"cc2.8xlarge" : "Arch" : "64HVM" ,
"cg1.4xlarge" : "Arch" : "64HVM"
,
"AWSRegionArch2AMI" :
"us-east-1" : "32" : "ami-31814f58", "64" : "ami-1b814f72", "64HVM" : "ami-0da96764" ,
"us-west-2" : "32" : "ami-38fe7308", "64" : "ami-30fe7300", "64HVM" : "NOT_YET_SUPPORTED" ,
"us-west-1" : "32" : "ami-11d68a54", "64" : "ami-1bd68a5e", "64HVM" : "NOT_YET_SUPPORTED" ,
"eu-west-1" : "32" : "ami-973b06e3", "64" : "ami-953b06e1", "64HVM" : "NOT_YET_SUPPORTED" ,
"ap-southeast-1" : "32" : "ami-b4b0cae6", "64" : "ami-beb0caec", "64HVM" : "NOT_YET_SUPPORTED" ,
"ap-southeast-2" : "32" : "ami-b3990e89", "64" : "ami-bd990e87", "64HVM" : "NOT_YET_SUPPORTED" ,
"ap-northeast-1" : "32" : "ami-0644f007", "64" : "ami-0a44f00b", "64HVM" : "NOT_YET_SUPPORTED" ,
"sa-east-1" : "32" : "ami-3e3be423", "64" : "ami-3c3be421", "64HVM" : "NOT_YET_SUPPORTED"
,
"Resources" :
"NotificationTopic":
"Type": "AWS::SNS::Topic",
"Properties":
"Subscription": [
"Endpoint": "Ref": "OperatorEmail" ,
"Protocol": "email" ]
,
"WebServerGroup" :
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" :
"AvailabilityZones" : "Fn::GetAZs" : "",
"LaunchConfigurationName" : "Ref" : "LaunchConfig" ,
"MinSize" : "0",
"MaxSize" : "4",
"LoadBalancerNames" : [ "load4" ],
"NotificationConfiguration" :
"TopicARN" : "Ref" : "NotificationTopic" ,
"NotificationTypes" : [ "autoscaling:EC2_INSTANCE_LAUNCH","autoscaling:EC2_INSTANCE_LAUNCH_ERROR","autoscaling:EC2_INSTANCE_TERMINATE", "autoscaling:EC2_INSTANCE_TERMINATE_ERROR"]
,
"CfnUser" :
"Type" : "AWS::IAM::User",
"Properties" :
"Path": "/",
"Policies": [
"PolicyName": "root",
"PolicyDocument": "Statement": [
"Effect":"Allow",
"Action":"cloudformation:DescribeStackResource",
"Resource":"*"
]
]
,
"HostKeys" :
"Type" : "AWS::IAM::AccessKey",
"Properties" :
"UserName" : "Ref" : "CfnUser"
,
"LaunchConfig" :
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Metadata" :
"Comment" : "Create a single webserver",
"AWS::CloudFormation::Init" :
"config" :
"packages" :
"yum" :
,
"files" :
,
"Properties" :
"KeyName" : "Ref" : "KeyName" ,
"SpotPrice" : "Ref" : "SpotPrice" ,
"ImageId" : "Fn::FindInMap" : [ "AWSRegionArch2AMI", "Ref" : "AWS::Region" ,
"Fn::FindInMap" : [ "AWSInstanceType2Arch", "Ref" : "InstanceType" ,
"Arch" ] ] ,
"SecurityGroups" : [ "webserver" ],
"InstanceType" : "Ref" : "InstanceType" ,
"UserData" : "Fn::Base64" : "Fn::Join" : ["", [
"#!/bin/bash\n",
"yum update -y aws-cfn-bootstrap\n",
"# Install the Worker application\n",
"/opt/aws/bin/cfn-init ",
" --stack ", "Ref" : "AWS::StackId" ,
" --resource LaunchConfig ",
" --configset ALL",
" --region ", "Ref" : "AWS::Region" , "\n"
]]
,
"WorkerGroup" :
"Type" : "AWS::AutoScaling::AutoScalingGroup",
"Properties" :
"AvailabilityZones" : "Fn::GetAZs" : "",
"LaunchConfigurationName" : "Ref" : "LaunchConfig" ,
"MinSize" : "Ref" : "MinInstances" ,
"MaxSize" : "Ref" : "MaxInstances"
,
"WebServerScaleUpPolicy" :
"Type" : "AWS::AutoScaling::ScalingPolicy",
"Properties" :
"AdjustmentType" : "ChangeInCapacity",
"AutoScalingGroupName" : "Ref" : "WorkerGroup" ,
"Cooldown" : "60",
"ScalingAdjustment" : "1"
,
"WebServerScaleDownPolicy" :
"Type" : "AWS::AutoScaling::ScalingPolicy",
"Properties" :
"AdjustmentType" : "ChangeInCapacity",
"AutoScalingGroupName" : "Ref" : "WorkerGroup" ,
"Cooldown" : "60",
"ScalingAdjustment" : "-1"
, ...
"WorkerThreadHigh":
"Type": "AWS::CloudWatch::Alarm",
"Properties":
"AlarmDescription": "Scale-up if Worker Thread Vs. Idle Percent > 80% for 10min",
"MetricName": "PctActiveWorkers",
"Namespace": "EC2",
"Statistic": "Average",
"Period": "300",
"EvaluationPeriods": "2",
"Threshold": "80",
"AlarmActions": [ "Ref": "WebServerScaleUpPolicy" ],
"Dimensions": [
"Name": "AutoScalingGroupName",
"Value": "Ref": "WebServerGroup"
],
"ComparisonOperator": "GreaterThanThreshold"
,
"WorkerThreadLow":
"Type": "AWS::CloudWatch::Alarm",
"Properties":
"AlarmDescription": "Scale-down if CPU < 50% for 10 minutes",
"MetricName": "PctActiveWorkers",
"Namespace": "EC2",
"Statistic": "Average",
"Period": "300",
"EvaluationPeriods": "2",
"Threshold": "50",
"AlarmActions": [ "Ref": "WebServerScaleDownPolicy" ],
"Dimensions": [
"Name": "AutoScalingGroupName",
"Value": "Ref": "WebServerGroup"
],
"ComparisonOperator": "LessThanThreshold"
【问题讨论】:
【参考方案1】:参数LoadBalancerNames 仅表示与此自动伸缩组关联的负载均衡器列表。您引用的示例AWS CloudFormation 模板(以及我知道的所有其他示例)已将其配置为LoadBalancer 资源的结果,如下所示:
"LoadBalancerNames": [
"Ref": "ElasticLoadBalancer"
],
Ref 函数的结果在LoadBalancer 底部的返回值 部分中定义:
当此资源的逻辑 ID 提供给 Ref 内部 函数,它返回资源名称。例如, mystack-myelb-1WQN7BJGDB5YQ。
这只是AWS Management Console 中所示的负载均衡器名称,因此您可以通过直接提供其名称来使用在 CloudFormation 之外创建的任何Elastic Load Balancer,例如:
"LoadBalancerNames": [ "existing-load-balancer-1" ],
【讨论】:
你这样做了吗?我真的很想知道这是否有效。 我已经成功使用手动创建的负载均衡器启动了引用的模板,但是还没有在生产中使用,所以不知道这种方法是否存在任何隐藏问题(但来自概念上的观点不应该有)。 @JohnHinnegan - 我会尝试一下,如果可行,我会告诉你并接受。 @SteffenOpel - 所以如果我的负载均衡器被称为“load4” - 我所要做的就是:“LoadBalancerNames”:[“load4”],? @Ross - 原则上是的,但如果您指的是问题中的特定模板,您可能还需要调整或删除对ElasticLoadBalancer
资源的所有引用,例如删除Output
、ElasticLoadBalancer
并将InstanceSecurityGroup
更改为使用"SourceSecurityGroupOwnerId" : "amazon-elb"
和"SourceSecurityGroupName" : "amazon-elb-sg"
(不确定我是否遗漏了什么,但你明白了)。【参考方案2】:
你不能将两者解耦。我已经通过亚马逊支持验证了一个不相关的用例。太烂了。
这是我们的讨论。 https://forums.aws.amazon.com/thread.jspa?messageID=362467񘟣亚马逊最后没有回复,他们的说法是,我们不支持。
更新我下面的答案不再正确。亚马逊已添加此功能。查看其他讨论。
【讨论】:
天哪,如果它真的是真的,那就太糟糕了!在接受之前,我将把它打开一段时间,以防其他人可能有一个相互矛盾的答案或其他一些漂亮的解决方法。谢谢。 我想目前我能想到的最好的解决方法是使用外部监控机制(脚本)来轮询 cloudwatch 指标或一组指标,并添加/删除 instacnes 代替自动扩展 cloudformation...不完全理想! 这不再是真的。您可以使用 LoadBalancerNames 属性(我刚刚验证)将实例添加到现有 ELB - 请参阅@steffenopel。【参考方案3】:当添加到“类型”资源的配置中时,我可以确认以下工作:“AWS::AutoScaling::AutoScalingGroup”:
"LoadBalancerNames" : [ "YourELBNameHere" ]
或者,如果您将名称作为参数,
"LoadBalancerNames" : [ "Ref" : "YourELBParameterNameHere" ]
如果您终止堆栈,ASG 创建的实例会自动添加/注册到 ELB,并自动删除/取消注册。
【讨论】:
以上是关于创建自动缩放网络服务器组添加到现有 elb的主要内容,如果未能解决你的问题,请参考以下文章