2013-08-22 61 views
1

我正在運行電子商務應用程序,現在我需要跨越基於自動縮放配置的實例。目前我正在平衡可用區域內的實例數量。爲了更清楚地說明,我在ap-southeast-1a中運行了2個實例,在ap-southeast-1b中運行了2個實例,這些實例是同質的。亞馬遜網絡服務自動擴展可用區域

現在我想啓用自動縮放配置,以便在區域間創建等量的實例。這樣負載均衡器可以在可用區域之間平衡負載。此外,負載減少了必須在該地區關閉的實例數量相等。

如何配置自動縮放?

請幫助我。

回答

1

您無法設置自動縮放來縮放已存在的實例。您基本上需要在啓動配置中重新創建它們,然後讓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-東南-1A和1B。要使用這個,你需要做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。這是首選選項,但您需要大量工作來構建基礎架構代碼。

最後,爲了讓自動繪圖工作,您需要選擇選項1或2,然後運行我發佈的cloudformation模板。

它將創建一個負載均衡的4實例環境,將分佈在ap-southeast-1和b