Loading ...
close

Would you like to discuss your environment with a knowledgable engineer?

Preferred communication *

Thank you. We will be in touch with you shortly

Autoscaling aiScaler Load Balancer to application auto scaling group

 

Contents

  1. 1 Overview
  2. 2 Launching an aiScaler instance
  3. 3 Create an AMI for application server
  4. 4 Configuring Auto Scaling
    1. 4.1 Using GUI
    2. 4.2 Using CLI
      1. 4.2.1 Create Auto Scaling Group
      2. 4.2.2 Creating scaling policies
      3. 4.2.3 Creating alarms to use scaling policies
  5. 7 Delete auto scaling group

Overview

Here I will explain how you can use aiScaler as a Load Balancer in Amazon Web Service with auto scaling, to launch your application servers and register them with aiScaler when scaling up and deregister them when scaling down. Specifically I will explain the followings:

  • what aiScaler instance to launch so it can be used as a Load Balancer;
  • how to create an Amazon Machine Image for your application server and tune it so it can register and deregister with aiScaler server;
  • how to create a Launch Configuration for the Auto Scaling Group;
  • how to create an Auto Scaling Group that will use aiScaler instance and Launch Configuration from above;
  • how to create an Auto Scaling Policy for scaling up, which will add a new instance to the Auto Scaling Group;
  • how to create an Auto Scaling Policy for scaling down, which will remove an instance from the Auto Scaling Group;
  • how to create CloudWatch Alarms that will use the Auto Scaling Policies for scaling up or down and when specific conditions are met.

Once done, you will have a working auto scaling group that will work like in the little diagram below: aiScaler -> one or more application instances Then I will describe how you can remove all the configuration in case you want to delete it.

Launching an aiScaler instance

To use aiScaler as a Load Balancer with auto scaling you should use the Market Place AMI of aiScaler. For this go to Amazon EC2 Console and click on Launch Instance. In the appeared window select AWS Markeplcate and search for aiScaler in “Search AWS Marketplace” field. Then just follow the instructions to launch the instance like any other Amazon Instance, as it’s described here. One note though, if you want to use aiScaler as a load balancer, I would suggest to use at least the Large instance to handle more traffic.

Create an AMI for application server

You will have to create an AMI that will contain all the software needed for your application and deployment scripts that will configure the application on boot. This is different for every application and I can not provide much support here, we can provide support for this separately, so if you like just drop an email to support@aiscaler.com and we will be happy to help. The part that you need to add here, are two small scripts that will register with aiScaler on boot, and deregister with aiScaler on stop/terminate. You can download and use the following simple scripts for the register and deregister purpose, aicache_connect.sh and aicache_disconnect.sh scripts are attached at the bottom of this page. You need to have the key pair file to connect to your aiScaler instance copied into your application AMI and the path to this file should be set in the scripts as KEY_FILE variable. Below are the variables from the script explained:

  • KEY_FILE=”/root/test.pem” – this is the key file which is used to connect to aiScaler server and register;
  • AICACHE_CONFIG=”/etc/aicache/aicache.cfg” – this is the configuration file of aiScaler which is currently in use;
  • UPDATE_SCRIPT=”/usr/local/aicache/aicache_config_server.py” – this is the location of aiScaler update script on the aiScaler server, this shouldn’t change;
  • LISTENER=”all” – this sets the website configuration section to update in the aiScaler configuration, “all” means that all sections will be updated;
  • APP_TYPE=”http” – the type of application, can be http or https;
  • APP-PORT=80 – the port on which application listens to requests on the application server.

You also have to provide the IP address of aiScaler instance as user-data, so application servers know to which server they should register, this will be covered in the creating launch configuration section. The last step here is to add aicache_connect.sh to run at boot and aicache_disconnect.sh to run at stop/terminate. For this you can do the followings on ubuntu machine:

cp aicache_connect.sh aicache_disconnect.sh /etc/init.d/ update-rc.d aicache_connect.sh defaults update-rc.d aicache_disconnect.sh defaults rm /etc/rc0.d/K20aicache_connect.sh /etc/rc1.d/K20aicache_connect.sh /etc/rc6.d/K20aicache_connect.sh rm /etc/rc2.d/S20aicache_disconnect.sh /etc/rc3.d/S20aicache_disconnect.sh /etc/rc4.d/S20aicache_disconnect.sh /etc/rc5.d/S20aicache_disconnect.sh

The above will ensure that register is done on boot, and deregister is done on stop/terminate. I strongly recommend testing that registration is working fine before going into production.

Configuring Auto Scaling

Screenshot_2014-04-12-11-48-31~2 Using GUI

Using the AWS Console we’ll:

  • Create a Launch Configuration for your Auto Scaling Group
  • Create an Auto Scaling Group
  • Create two alarms to trigger scaling of our group

To start configuring Auto Scaling navigate to the EC2 Dashboard and select Launch Configurations under Auto Scaling.

Create Auto Scaling Launch Configuration

Start the Auto Scaling Launch Configuration Wizard.

In Step 1 select My AMIs and locate the AMI you created at the previous step.

In Step 2 select your desired instance type, e.g. m1.large.

In Step 3 name your Launch Configuration, e.g. “AiScaler Launch Configuration”. Select Advanced Options, and in the User Data field input the private IP address of the aiScaler server. It is needed on the application servers so they know where to connect and register.

You can now skip to Review and Create Launch Configuration. The wizard will bring you directly to the Create Auto Scaling Group wizard.

Create Auto Scaling Group

Screenshot_2014-04-12-13-23-16~2

In Step 1 name your group,e.g. “AiScaler Auto Scaling Group”, and select your Availability Zones, we recommend selecting them all.

In Step 2 we configure scaling policies. Select “Use scaling policies to adjust the capacity of this group”.

Next to Execute Policy when select Create alarm.

Unselect “Send notification to”.

Set the alarm to “Whenever CPU Utilization is >= 80 Percent”.

Next to Take Action select Add 1 instance.

Repeat for decrease policy size, selecting “Whenever CPU Utilization is <= 20 Percent”.

Skip to Review and Create Auto Scaling group.

Using CLI

Create Auto Scaling Launch Configuration

To set up auto scaling group you need to download and set up Command Line Tools, like described here: http://docs.aws.amazon.com/AutoScaling/latest/GettingStartedGuide/SetupCLI.html. Once you have your Command Line Tools set up, create the launch configuration with the following command:

as-create-launch-config AIAutoScalingLC \ --image-id ami-0145d268 \ --instance-type t1.micro \ --group AI_AutoScaling \ --key AIAutoScalingKey \ --monitoring-disabled \ --user-data 10.140.19.26

Here are the explanations for the options used:

  • as-create-launch-config AIAutoScalingLC is for create a Launch Configuration named AIAutoScalingLC;
  • –image-id ami-0145d268 selects the Amazon Machine Image (AMI) ID of an Ubuntu Server 12.04 LTS us-east-1 region, this should be the AMI created by you with application configured, register and deregisters scripts and the key pair to connect to aiScaler server;
  • –instance-type m1.small selects the instance type, which can be m1.small, m1.large, m1.xlarge, etc. please check the page on Amazon Marketplace for price details for each instance;
  • –group AI_AutoScaling selects the Security Group used for launching the instance, this group should already be created in your Amazon Web Services account in the same region as the AMI;
  • –key AIAutoScalingKey selects the key pair to be used when launching an instance in auto scaling group, the key should already be created in your Amazon Web Services account in the same region as the AMI;
  • –monitoring-disabled disables the monitoring, as it’s on by default and charges more money, it’s your choice if you want to leave it enabled or disable it;
  • –user-data 10.140.19.26 this is the private IP address of the aiScaler server, it is needed on the application servers so they know where to connect and register.

Here’s the result of running this command, you can also check that it was created with as-describe-launch-configs command:

root@ubuntuserver64:~/ec2/as/bin# as-create-launch-config AIAutoScalingLC \ > --image-id ami-0145d268 \ > --instance-type t1.micro \ > --group AI_AutoScaling \ > --key AIAutoScalingKey \ > --monitoring-disabled \ > --user-data OK-Created launch config

root@ubuntuserver64:~/ec2/as/bin# as-describe-launch-configs –headers LAUNCH-CONFIG NAME IMAGE-ID TYPE LAUNCH-CONFIG AIAutoScalingLC ami-0145d268 t1.micro root@ubuntuserver64:~/ec2/as/bin#

Create Auto Scaling Group

Next step is to create the auto scaling group, for this we also need to use the Command Line Tools, please use the following command for this:

as-create-auto-scaling-group AIAutoScalingGroup \ --launch-configuration AIAutoScalingLC \ --availability-zones us-east-1a,us-east-1b,us-east-1c,us-east-1d,us-east-1e \ --desired-capacity 1 \ --min-size 1 \ --max-size 2 \ --grace-period 60

Below are all the options used and their meaning:

  • as-create-auto-scaling-group AIAutoScalingGroup will create an auto scaling group with the AIAutoScalingGroup name;
  • –launch-configuration AIAutoScalingLC uses the launch configuration, created earlier for this auto scaling group, named AIAutoScalingLC;
  • –availability-zones us-east-1a,us-east-1b,us-east-1c,us-east-1d,us-east-1e selects which availability zones to use, here the zones should be the same as the zones set in Elastic Load Balancer.
  • –desired-capacity 1 sets desired number of instances, that should be running at a time, under normal conditions;
  • –min-size 1 sets the minimum number of instances to keep running;
  • –max-size 2 sets the maximum number of instances to keep running;
  • –grace-period 60 sets how many seconds the instance will be left unchecked after boot, meaning only after 60 seconds after the instance is UP it will be checked for health by auto scaling group.

On successful execution of this command you should see something like:

root@ubuntuserver64:~/ec2/as/bin# as-create-auto-scaling-group AIAutoScalingGroup \ > --launch-configuration AIAutoScalingLC \ > --availability-zones us-east-1a,us-east-1b,us-east-1c,us-east-1d,us-east-1e \ > --min-size 1 \ > --max-size 2 \ > --grace-period 60 OK-Created AutoScalingGroup root@ubuntuserver64:~/ec2/as/bin# as-describe-auto-scaling-groups --headers AUTO-SCALING-GROUP GROUP-NAME LAUNCH-CONFIG AVAILABILITY-ZONES MIN-SIZE MAX-SIZE DESIRED-CAPACITY TERMINATION-POLICIES AUTO-SCALING-GROUP AIAutoScalingGroup AIAutoScalingLC us-east-1c,us-east-1b,us-east-1a,us-east-1e,us-east-1d 1 2 1 Default

Now you should see a new aiScaler instance starting in your EC2 console, also you should be able to see this with as-describe-scaling-activities command:

root@ubuntuserver64:~/ec2/as/bin# as-describe-scaling-activities ACTIVITY 67d398be-538d-4b73-b067-afd0222cad36 2013-02-07T09:45:13Z AIAutoScalingGroup Successful

In this set up health check is based on EC2 health, in case the basic checks for health of your EC2 application instance fail a new instance will automatically start. Other checks for traffic and scale for traffic will be set up against aiScaler instance, because aiScaler is acting as Load Balancer.

Creating scaling policies

Policy for scaling up

Here I will create a policy to add more instances in case traffic goes high, or in case one instance fails, for this I will use the following command:

as-put-scaling-policy AIAutoScalingPolicyUP \ --auto-scaling-group AIAutoScalingGroup \ --adjustment=1 \ --type ChangeInCapacity

This adds a new scaling policy named AIAutoScalingPolicyUP which will be added to the auto scaling group AIAutoScalingGroup and will add one instance (adjustment=1). Here is an example of output for this command:

root@ubuntuserver64:~/ec2/as/bin# as-put-scaling-policy AIAutoScalingPolicyUP \ > --auto-scaling-group AIAutoScalingGroup \ > --adjustment=1 \ > --type ChangeInCapacity arn:aws:autoscaling:us-east-1:575682692618:scalingPolicy:2d603020-3aa7-41a1-bcf0-7d9000f99c65:autoScalingGroupName/AIAutoScalingGroup:policyName/AIAutoScalingPolicyUP root@ubuntuserver64:~/ec2/as/bin#
Policy for scaling down

The next policy is to scale down, when traffic comes to normal and one instance is sufficient, the command for this is:

as-put-scaling-policy AIAutoScalingPolicyDown \ --auto-scaling-group AIAutoScalingGroup \ --adjustment=-1 \ --type ChangeInCapacity

This will create a new scaling policy named AIAutoScalingPolicyDown, added to the auto scaling group AIAutoScalingGroup and will remove one instance (adjustment=-1). Here is an example of output for this command:

root@ubuntuserver64:~/ec2/as/bin# ./as-put-scaling-policy AIAutoScalingPolicyDown \ > --auto-scaling-group AIAutoScalingGroup \ > --adjustment=-1 \ > --type ChangeInCapacity arn:aws:autoscaling:us-east-1:575682692618:scalingPolicy:d3264165-e91d-413b-b0d1-3cf2beb0d793:autoScalingGroupName/AIAutoScalingGroup:policyName/AIAutoScalingPolicyDown root@ubuntuserver64:~/ec2/as/bin#

Note: if you are using the Command Line Tools in windows then please put double quotes on “–adjustment=-1”

Creating alarms to use scaling policies

Alarms can be easily created from CloudWatch Management Console. Once logged in, select Alarms and click on Create Alarm and search for the instance ID of your aiScaler server, instance id can be found here: You will have a list of metrics of your instance, select NetworkIn, this will measure the incoming traffic of aiScaler server, then click continue: On the next screen put the name and description as you want, for example: Name: AiHighTraffic Description: when traffic is high, add a new application instance At the Define Alarm Treshhold, put it Threshold: NetworkIn > 50000 for 5 minutes: On the next page select what should happen when this alarm is triggered, meaning it should add a new application instance, so we will use the policy to scale up: You may want to add an email notification also. Once done, click on continue. On the next page review your settings and if everything is fine click on Create Alarm.

Create alarm for scaling down

Next we want to add an alarm that will scale down in case traffic comes to normal. For this you should follow the same steps as in previous section and just edit the followings:

  • change threshold from Threshold: NetworkIn > 50000 for 5 minutes to Threshold: NetworkIn < 49000 for 5 minutes
  • select the policy to scale down.

You may want to add an email notification also. Once done, click on continue. On the next page review your settings and if everything is fine click on Create Alarm. At this point you have configured auto scaling using aiScaler as load balancer and your application servers as auto scaling group.

Delete auto scaling group

To delete auto scaling group please follow the instructions from here

US 1 (408) 744-6078   EU +44 20 7993 4587