Domanda

I am running e-commerce application,now I need to span instances based on the auto scaling configurations. Presently I am balancing the number of instances across the availability zones. To make it more clear I am running 2 instances in the ap-southeast-1a and 2 instances in the ap-southeast-1b, which are homogeneous in nature.

Now I want to enable the auto scaling configuration, so that I create equal amount of instances across regions. So that the load balancer can balance the load equally among the availability zones. Also the the load reduces the equal number of instances has to be shut down across the region.

How do I configure the auto scaling ???

Kindly help me out.

È stato utile?

Soluzione

You can't setup Autoscaling to scale instances that already exist. You basically need to recreate them inside a launch configuration and then have Autoscaling utilize that launch configuration. Here's a cloudformation template:

{
  "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" : {
  }
}

This template will create a launch configuration and Autoscaling group that spans ap-southeast-1a and b. To use this you will need to do 1 of 2 things.

  1. Create a golden AMI which has all of the application's code and configuration stored on the AMI. If you go this route, you need your AMI to basically create a completely working system when its launched

To do this inside this cloudformation template, create your AMI and then edit this line with the new AMI ID:

"ap-southeast-1" : { "AMI" : "ami-74dda626" },
  1. Script your environment using AWS Clouformation init or Chef/puppet/etc. Here is a link regarding this: AWS Cloudformation Init. This is the preferred option, but you will require a lot of work building up the infrastructure code.

In closing, to get autosclaing working, you need to pick option 1 or 2 and then run the cloudformation template i posted.

It will create a load balanced 4 instance environment that will be spread outa ap-southeast-1 a and b

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top