Advertisment

Auto Scaling AWS EC2 Instances Made Easy

author-image
PANKAJ
New Update

Auto scaling is an integral component of a Cloud based IaaS service. This functionality gives the elasticity to the Virtual Instance. AWS being the pioneer in Cloud Computing, introduced Auto Scaling long back. But till recently the auto scaling was done over the AWS APIs. Making the process a bit cryptic. Last month, AWS has introduced the Management Console based, or in other words, wizard driven auto scaling configuration. It is a feature, which lets you Automatically (or manually) add/remove instances to an existing cluster of compute instances. This scaling can be either based on system triggers like latency, response time, CPU load, etc. which is monitored by AWS.

The scaling can also be controlled manually from the Management Console or via a separate instance using the API based scripts. We now show how to set up a simple Apache cluster and a simple policy based auto scaling through the new AWS Management Interface.

Advertisment

The Prerequisites

To test the setup we created a simple CentOS Micro instance and installed Apache and PHP on it. And our test Apache instance has a single PHP file called index.php which returns the Private IP of the instance. We have done this to demonstrate multiple instances that would be servicing behind our autoscale cluster and hence will be returning different Private IPs when the page is refreshed. The PHP code we have used for this:

< ?php

function get_local_ipv4() {

$out = split(PHP_EOL,shell_exec("/sbin/ifconfig"));

$local_addrs = array();

$ifname = ‘unknown';

foreach($out as $str) {

$matches = array();

if(preg_match(‘/^(+)(:\\d{1,2})?(\\s)+Link/',$str,$matches)) {

$ifname = $matches<1>;

if(strlen($matches<2>)>0) {

$ifname .= $matches<2>;

}

} elseif(preg_match(‘/inet addr:((?:25<0-5>|2<0-4>\\d|1\\d\\d|<1-

>\\d|\\d)(?:<.>(?:25<0-5>|2<0-4>\\d|1\\d\\d|<1-9>\\d|\\d)){3})\\s/',$str,$matches)) {

$local_addrs<$ifname> = $matches<1>;

}

}

return $local_addrs;

}

$addrs = get_local_ipv4();

var_export($addrs);

This will simply print the Private IP of the instance which is serving the http request as follows:

Advertisment

Once, the instance is created and the php file is in place, we shut down the instance and create an AMI (Machine Image) with it. To do this, you need to right click on the instance and click on "Create Image" as shown below:

Once you click on the Create Image button, a popup will open. You have to fill in a name and the disk size for the AMI. Put a comprehensive name and keep the default storage as shown below:

Advertisment

 

Once the AMI is created, it can be used to create an exact clone of this machine as many number of times as you want. This AMI will be used by the auto scaling triggers to initiate new nodes to the same cluster.

Creating the Elastic Load Balancer

The next step is to create a Load balancer. This load balancer like any other load balancer will be the front for receiving all the HTTP (or whatever services you want, like HTTPS, etc.) and will distribute the requests to the running nodes behind it. The beauty of the Elastic Load Balancer or the ELB is that, it can automatically add and remove nodes to its cluster based on Auto scaling triggers and availability of the nodes based on the health checks.

To create an ELB, you have to first click on the "Load Balancer" option under "Network and Security" in the EC2 Management Console. And then Click on "Create Load Balancer" Button in the top of the page. This will initiate a Wizard. Here simply put a name to the ELB. By default the ELB will be configured to listen on port 80. You will see the configuration below in the same popup. Here if you have a custom port or multiple port requirement. Put those port numbers and the port on which the Nodes will be listening on. For our test. We will go with the default setting of port 80 configured on the ELB and forwarded to port 80 of the nodes behind it.

Your wizard screen should look something like the below image. Once you have put in the name and the right ports. Click on Continue as shown below:

Advertisment

The next screen will ask you for the health check settings. This will be used to add and remove nodes from the ELB. We will go with the default settings here. All we will be changing is Ping Path from /index.html to /index.php. This is because our webserver just has a single file called index.php as for the test purpose. Once done, click on the continue button as shown:

Advertisment

Next screen will ask you to select if you want to add new preexisting instance behind the ELB. We will not select any instance here and click on continue. This is to make sure all our nodes are only controlled and initiated by the Auto scaling wizard. Next screen is just the review page. Have a detailed look on the summery of the configuration and click on the create button.

Configuring Auto Scaling

Now comes the key part of the activity. Creating the Auto Config group and the Launch Configuration. To start, click on the Launch Configuration, under Auto Scaling in the EC2 management console. Here click on "Create launch Configuration" button at the top of the page. This will initiate a wizard.

The first screen will ask you to select an AMI which will be used to initiate nodes for this auto scaling group. Here select on "My AMIs" and select the AMI which we have just created.

Next screen will ask you to select the type of the instance which will be initiated using the AMI. We will go with a Micro instance for the test purpose. You can select any instance type based on your requirements.

In the next screen give a name to this launch configuration. Now finish the wizard with the default settings. Just make sure you either create a new SSH key or use a precreated key which you already have for accessing the nodes over SSH. See the screen below to get an idea:



Once the wizard is over. It will show u a Button which says "Create an Auto Scaling Group using this launch configuration. Click on that button and a new wizard will pop up.

In the first screen you have to put 3 important information. First is the "Group name". Here give any explanatory name to the group. Next is the "Group Size". Here make sure its set to 1. This essentially sets how many number of instances will be by default started in the Auto Scaling Group.

Next you have to select the "Availability Zone". Here you can either select one or multiple availability zone in which the instances will be started. For our test, we will just select the "a" zone of Singapore region as shown below:

Now, expand the "Advanced Details". Here in the "Load balancer" head, check the box which says "Receive traffic from Elastic Load Balancer" and in the field below select the load balancer which we have just created above.

You can keep the rest of the settings as default and hit the "Next" button. Now finish the rest of the wizard with default settings as shown below:

Advertisment



This will create your Auto Scale group. And will initiate the number of instances behind the ELB you have provided in the "Group Size" filed of the wizard. Which for us was 1.

Now you can access the webserver running on this instance by hitting the DNS name of the ELB. You can find the DNS name of the ELB in "Load Balancers" under "Network and Security". See the screenshot below to know where to find it:



This DNS name can be either pointed to your Domain Name by using a CNAM or an A record entry. For our test purpose we will go with this DNS name only.

By accessing this DNS name, you should be able to see a page which will print the private IP of the only instance running behind the ELB as off now.

Scaling the Cluster

Now, there are two ways of scaling the cluster. One is simply by editing the "Auto Scaling Group" ad changing the "Desired" number to 2 or more as shown below:

Advertisment



This approach is more manual and will initiate new instance the moment you change the desired value. You can go back to the instances option to see a new machine would have been created automatically and added to the ELB. To test if the ELB is distributing the requests properly or not. You can access the ELB DNS name a few times and see the Private IP changing demonstrating multiple machines are processing the PHP file hosted on your apache server as shown below:

The other more automated option is by settings up scaling policy for the "Auto Scale group". Scaling policy by itself is a complex topic. So we will take it up in detail next month. So watch out this space.

Advertisment