亚马逊网络服务在可用区域内自动扩展

时间:2013-08-22 12:34:07

标签: amazon-web-services amazon-ec2 autoscaling

我正在运行电子商务应用程序,现在我需要根据自动缩放配置跨越实例。目前,我正在平衡可用区域中的实例数量。为了说得更清楚,我在ap-southeast-1a运行了2个实例,在ap-southeast-1b运行了2个实例,这些实例是同质的。

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

如何配置自动缩放???

请帮助我。

1 个答案:

答案 0 :(得分:2)

您无法设置自动缩放以缩放已存在的实例。您基本上需要在启动配置中重新创建它们,然后让Autoscaling利用该启动配置。这是一个云形态模板:

{
  "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组。要使用它,您需要做两件事之一。

  1. 创建一个黄金AMI,其中包含存储在AMI上的所有应用程序代码和配置。如果你走这条路线,你需要你的AMI基本上在它启动时创建一个完全正常工作的系统
  2. 要在此云信息模板中执行此操作,请创建您的AMI,然后使用新的AMI ID编辑此行:

    "ap-southeast-1" : { "AMI" : "ami-74dda626" },
    
    1. 使用AWS Clouformation init或Chef / puppet /等编写您的环境脚本。以下是有关此内容的链接:AWS Cloudformation Init。这是首选选项,但您需要大量工作来构建基础结构代码。
    2. 最后,要使自动扫描工作,您需要选择选项1或2,然后运行我发布的云信息模板。

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