3

我有一个Elastic Load Balancer已经配置了端口和SSL证书等,Route 53设置为将我的网站流量路由到它。创建autoscaling网络服务器组添加到现有elb

我想知道是否有一个示例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

你不能将这两者分开。我已经通过亚马逊支持验证了一个不相关的用例。它很烂。

这是我们的讨论。 https://forums.aws.amazon.com/thread.jspa?messageID=362467&#362467亚马逊最终没有回复他们的说法,我们不支持。

UPDATE我在下面的回答不再正确。亚马逊增加了这个功能。查看其他讨论。

+0

jeez,如果它确实真的吸吮!为了防止别人可能有冲突的答案或其他漂亮的解决方法,我会在开放之前留出一段时间。谢谢。 – Ross

+0

我想现在我能想到的最佳解决方法是使用外部监视机制(脚本)来轮询cloudwatch指标或一组指标,并添加/删除instacnes以取代自动缩放的cloudformation ...不完全理想! – Ross

+0

这不再是事实。您可以使用LoadBalancerNames属性(我刚刚验证过)将实例添加到现有ELB中 - 请参阅@steffenopel。 – Rilindo

3

参数LoadBalancerNames只是表示与此自动缩放组关联的负载平衡器列表。您参考样本AWS CloudFormation模板(以及所有其他的例子我所知道的)有这个配置给LoadBalancer资源的结果如下:

"LoadBalancerNames": [ 
    { 
     "Ref": "ElasticLoadBalancer" 
    } 
], 

Ref函数的结果是部分定义

当这种资源的逻辑ID提供给参考内在 函数,它返回资源名称:回在LoadBalancer底部的值。例如, mystack-myelb-1WQN7BJGDB5YQ。

这简直是负载平衡器名称如图中AWS Management Console,因此你可以通过直接供给它的名字,例如使用CloudFormation之外创建任何Elastic Load Balancer

"LoadBalancerNames": [ "existing-load-balancer-1" ], 
+0

你做到了吗?我真的很想知道这是否有效。 –

+0

我已经成功地使用手动创建的负载均衡器来启动引用的模板,但尚未在生产环境中使用它,因此不知道这种方法是否存在任何隐藏问题(但从概念角度来看,没有任何)。 –

+0

@JohnHinnegan - 我会试试这个,让你知道并接受它,如果它的工作。 – Ross

1

我可以证实,以下的作品,当加入的 “类型” 的资源的配置: “AWS ::自动缩放:: AutoScalingGroup”:

"LoadBalancerNames" : [ "YourELBNameHere" ] 

,或者,如果你拥有名作为参数,

"LoadBalancerNames" : [ {"Ref" : "YourELBParameterNameHere"} ] 
  • 由ASG创建的实例被添加到/与ELB注册,并删除/自动注销,如果你TERMINAT e堆栈。