1

我正在运行电子商务应用程序,现在我需要根据 Auto Scaling 配置跨越实例。目前,我正在平衡可用区域中的实例数量。为了更清楚,我在 ap-southeast-1a 中运行 2 个实例,在 ap-southeast-1b 中运行 2 个实例,它们本质上是同质的。

现在我想启用 Auto Scaling 配置,以便跨区域创建相等数量的实例。这样负载均衡器就可以在可用区之间均衡负载。此外,负载减少了相同数量的实例必须在整个区域中关闭。

如何配置自动缩放???

请帮帮我。

4

1 回答 1

2

您无法设置 Autoscaling 来扩展已存在的实例。您基本上需要在启动配置中重新创建它们,然后让 Autoscaling 使用该启动配置。这是一个cloudformation模板:

{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "AWS CloudFormation Template to configure chef on an EC2 Instance",

  "Parameters" : {
    "KeyName" : {
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
      "Type" : "String"
    }
  },

  "Mappings" : {
    "RegionMap" : {
      "us-east-1"      : { "AMI" : "ami-7f418316" },
      "us-west-1"      : { "AMI" : "ami-951945d0" },
      "us-west-2"      : { "AMI" : "ami-16fd7026" },
      "eu-west-1"      : { "AMI" : "ami-24506250" },
      "sa-east-1"      : { "AMI" : "ami-3e3be423" },
      "ap-southeast-1" : { "AMI" : "ami-74dda626" },
      "ap-northeast-1" : { "AMI" : "ami-dcfa4edd" }
    }
  },

  "Resources" : {
    "User" : {
      "Type" : "AWS::IAM::User",
      "Properties" : {
        "Path": "/",
        "Policies": [{
          "PolicyName": "root",
          "PolicyDocument": { "Statement":[{
            "Effect":"Allow",
            "Action":"*",
            "Resource":"*"
          }
                                          ]}
        }]
      }
    },

    "HostKeys" : {
      "Type" : "AWS::IAM::AccessKey",
      "Properties" : {
        "UserName" : { "Ref": "User" }
      }
    },
    "ElasticLoadBalancer" : {
      "Type" : "AWS::ElasticLoadBalancing::LoadBalancer",
      "Properties" : {
        "AvailabilityZones" : ["ap-southeast-1a", "ap-southeast-1b"],
        "Listeners" : [{
          "LoadBalancerPort" : "80",
          "InstancePort" : "8080",
          "Protocol" : "HTTP"
        }],
        "HealthCheck" : {
          "Target" : "HTTP:8080/jenkins/",
          "HealthyThreshold" : "3",
          "UnhealthyThreshold" : "5",
          "Interval" : "30",
          "Timeout" : "5"
        }
      }
    },

    "WebServerGroup" : {
      "Type" : "AWS::AutoScaling::AutoScalingGroup",
      "Properties" : {
        "AvailabilityZones" : ["ap-southeast-1a", "ap-southeast-1b"],
        "LoadBalancerNames" : [{"Ref" : "ElasticLoadBalancer"}],
        "LaunchConfigurationName" : { "Ref" : "LaunchConfig" },
        "MinSize" : "1",
        "MaxSize" : "10",
        "DesiredCapacity" : "4"
      }
    },

    "LaunchConfig" : {
      "Type" : "AWS::AutoScaling::LaunchConfiguration",

      "Properties" : {
        "InstanceType" : "m1.small",
        "KeyName" : { "Ref" : "KeyName" },
        "SecurityGroups" : [ {"Ref" : "FrontendGroup"} ],
        "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
      }
    },

    "FrontendGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Enable SSH and access to Apache and Tomcat",
        "SecurityGroupIngress" : [
          {"IpProtocol" : "tcp", "FromPort" : "8080", "ToPort" : "8080", "CidrIp" : "0.0.0.0/0"},
          {"IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0"},
          {"IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0"}
        ]
      }
    },

    "WaitHandle" : {
      "Type" : "AWS::CloudFormation::WaitConditionHandle"
    },

    "WaitCondition" : {
      "Type" : "AWS::CloudFormation::WaitCondition",
      "DependsOn" : "LaunchConfig",
      "Properties" : {
        "Handle" : { "Ref" : "WaitHandle" },
        "Timeout" : "1200"
      }
    }
  },

  "Outputs" : {
  }
}

此模板将创建跨 ap-southeast-1a 和 b 的启动配置和 Autoscaling 组。要使用它,您需要做 2 件事中的 1 件事。

  1. 创建一个黄金 AMI,其中所有应用程序的代码和配置都存储在 AMI 上。如果你走这条路,你需要你的 AMI 在启动时基本上创建一个完整的工作系统

要在此 cloudformation 模板中执行此操作,请创建您的 AMI,然后使用新的 AMI ID 编辑此行:

"ap-southeast-1" : { "AMI" : "ami-74dda626" },
  1. 使用 AWS Clouformation init 或 Chef/puppet/etc 为您的环境编写脚本。这是一个与此相关的链接:AWS Cloudformation Init。这是首选选项,但您将需要大量工作来构建基础架构代码。

最后,要让 autosclaing 正常工作,您需要选择选项 1 或 2,然后运行我发布的 cloudformation 模板。

它将创建一个负载平衡的 4 个实例环境,该环境将分布在 ap-southeast-1 a 和 b

于 2013-08-22T13:42:57.160 回答