This post is a bit more involved than the previous ones. The format is also a little different. It expects that you are already comfortable with Alibaba ECS and SLB and the concepts around them. So if you are not comfortable with these concepts then I would strongly suggest going through the Getting Started with ECS on Alibaba Cloud and Discovering Custom Images and Load Balancing on Alibaba Cloud posts.
In this third how-to we will cover the Auto-scaling. Auto-scaling is a service that forms one of the corner stone of any true Cloud. It allows you to horizontally scale a stateless application, automatically. Lets say you were load balancing http traffic across two web servers. You would like to increase the number of web servers when the load on the existing servers became high (also known as scale-out) and then decrease the number of web servers when the load decreased (also known as scale- in.) This in a nutshell is what Auto-scaling allows us to do in Alibaba Cloud.
In order to successfully follow this guide there are a few pre-requisites that you need to fulfill. Please complete the below activities before you proceed:
- Create an ECS instance in the region of your choice. Use Ubuntu for the Operating System and set a password to login. If you need assistance with this then please refer to Getting Started with ECS guide.
Once you have successfully launched the ECS instance, run the following commands to ssh in to the instance and install and configure Apache web server:
$ ssh root@INTERNET_IP_ECSINSTANCE $ sudo apt-get update $ sudo apt-get install apache2 –y $ sudo systemctl restart apache2
- Once apache is working, create a script to dynamically generate the default html for apache. We are doing this so each apache instance displays the page with the ECS-instances hostname. So when we are load balancing the application we can identify which ECS instance the traffic is being directed to. I am assuming you know how to use vi. (Shortcut to save and exit vi is Esc + :x)
$ mkdir /etc/init.d/scripts
$ vi /etc/init.d/scripts/createapache #!/bin/sh -e cat <<EOF > /var/www/html/index.html <html> <title> Autoscale </title> <body> <h1 align="center">I am Server $(hostname)</h1><pr> <h2 align="center"> Autoscale Example </h2> </body> </html> EOF
Now edit the rc.local script to call the above script, 5 minutes after the instance boots. The reason for the 5 minute delay is to make sure that the instance processing with regards to IP/hostname etc has completed before we generate the html page. If I didn’t put the 5 minute delay, I noticed that the html page was being generated with the old hostname from the image. (Shortcut to save and exit vi is Esc + :x)
$ sudo vi /etc/rc.local at now +5 min < /etc/init.d/scripts/create apache exit 0
- Reboot the ECS instance.
- Add a rule for Inbound HTTP port 80 to the Security Group for the ECS
- Once the reboot is complete, give it 5 minutes and then point your browser to the Internet IP of the ECS instance. If all goes well you should be able to see a webpage similar to the one below:
Note that the HOSTNAME above will be a long value from your ECS instance hostname and will be different from the one above.
- Create a custom image from the above ECS instance and then launch 2 instances using this custom image. If you need any help with this then refer to the Discovering Custom Images and Load Balancers guide. Refer to the “Create custom image” section in the guide.
- Now create an Internet based SLB in the same region. Do the following configuration:
- Create a Listener with the following configuration:
- Frontend /Backend: HTTP on Port 80
- DO NOT use VServer Group
- Forwarding Rules: Weighted Round Robin
- Disable Monitoring
- Add the ECS instances that you created in Step 6 as Backend Servers to this SLB with:
- Weight 100
- Port 80.
- Create a Listener with the following configuration:
- Test the SLB to make sure its working. Basically you should be load balancing between the two Apache web servers in your browser if you access the Internet IP of the SLB. If you need any help with this then refer to the Discovering Custom Images and Load Balancers guide.
So let me just recap of what we just did:
- We launched a Ubuntu based ECS instance
- We installed Apache on it, added a few initialization scripts and then created a custom image from it
- We added HTTP port 80 inbound rule to the Security Groups
- We launched two new instances from the custom image
- We created an Internet facing SLB and added the two servers to the Backend Servers for it.
- We tested that our SLB is load balancing HTTP traffic across the two Apache instances.
Now that we have completed the pre-requisites successfully, lets get started with Auto-scaling.
On the Alibaba Cloud Console, locate Auto Scaling as highlighted above and Click it. If you are requested to activate the service then do so. You will not incur any charges due to this at this point.
Scaling group configuration
On the left panel Click on Scaling group management and then click on Create scaling group button as highlighted above. Basically a Scaling Group is the configuration used to monitor and scale in/out a cloud service.
- Give the Scaling group a name
- Max Instances: Set to 3 (This parameter defines how many maximum servers can you have serving your service under Auto Scaling. So according to our configuration it will not continue to Scale out beyond 3 servers)
- Min Instances: Set to 2 (This parameter defines how many minimum servers will be there in your scaling group. So lets say if you had a scale-in trigger it would remove servers serving your application until 2 servers were left.
- Cool down time: 300 sec (This is the time that need to pass before a second scaling activity can trigger after one finishes successfully. Note that you can have manual and scheduled triggers as well for scaling and the cool down time does not apply to those. It only applies to triggers based on monitoring.)
- Leave the Removal Policy as default
- Network Type: VPC (Same as the one for your ECS
- Select the VPC and vswitch that the two ECS instances are attached to.
- Server Load Balancer: Select the SLB that you created as part of the pre- requisites. Auto-scaling will automatically add/remove instances to the SLB as it scales out/in.
- Database: Auto-Scaling can automatically add/remove instances to Database whitelist as it scales in/out. For our case we don’t have a DB so leave this blank
You will get a pop-up to Create scaling configuration. Press that button to start creating the Scaling configuration for this Scaling Group
Here we will select which image is going to be cloned and a few other properties for the scaled instances.
Clone source ECS: This is the ECS instance that will be cloned when scaling out. A copy of this instance will be created when launching additional instances. Select any one of the two instances you launched earlier using the custom image.
- Configuration Name: Give it an appropriate name. I choose Apache_C
- Security Group: Select the security group which you created initially with the rule allowing inbound on HTTP port 80.
Peak bandwidth: Select 1Mbps
On the confirmation screen press OK.
ECS Instance List
Here we add the existing ECS instances to the scaling group. The idea is that we already have a few servers running that we created manually as part of our service. We want to scale the service out/in based on this baseline.
Click on ECS instance list under the Security Group Configuration.
- Press the Add Existing Instance button as highlighted above.
- In the pop-up move both the instances from Unselected to Selected.
Ensure that both instances are manually attached as shown above.
Here we define one or more rules to scale-out or scale-in and by how much.
Click on Scaling rule on the left pane and then click on Create scaling rule.
In the pop-up for our case we will create a Scale out rule as shown above. Note that:
- A rule can be triggered based on a monitoring or a manual / scheduled trigger.
- When this rule is triggered, it will add 1 instance to the existing instances in our group.
- It will only add an instance until the total number of servers in the Auto-Scaling group has reached the Max limit, which we set as 3 previously. Press Create scaling rule button.
Press Create scaling rule button
Now go back to the main Auto Scaling console and click on Alarm task as highlighted above. Then click on Create alarm task.
Here we are going to create a task that will trigger our rule to Scale out. An Alarm task works with Cloud Monitor. It monitors a specific KPI on your existing Auto Scale Group ECS instances and if a threshold is breached then it triggers the Scaling Activity. For our case use the following values:
- Task Name: Apache_Alarm
- Description: Put anything you like
- Monitor Resource: Apache_C (This is the name of the Security Group
Configuration that we created for apache.)
- Metric Item: CPU (What needs to be monitoring)
- Statistical Cycle: 2 min (How often do we monitor the above KPI)
- Statistical method: This is the formula used for checking the threshold. In our
case we are checking the Average CPU utilization of all the existing ECS instances in the Auto Scaling Group. The reason we have set it to >= 0 is because we want to force this task to trigger. In production this value will be higher than your normal operational value for your instances.
- Number of recurrences: 2 (How may times should this KPI breach consecutively to launch the auto scaling activity)
- Trigger on alarm task: Scale_up (This is the name of the scaling rule that we created earlier)
Go back to Scaling Group Management, find the scaling group that you created and click on Manage.
- Click on Scaling Activity. Monitoring it for around 5 minutes.
Once you see an activity similar to the one highlighted above, monitor it till it is Successful
Click on the ECS instance list in the left panel as shown above.
- Under the Automatically Create tab you should now see one instance. This is the instance, which the Auto Scaling service has added to your service since the threshold set by you was breached.
- Note that Active instances are now 3, two added manually by you and 1added by Auto-Scaling.
If you wait further you will notice that Auto Scaling will not add any more instances, since it has already reached the max value of 3.
For further verification, proceed to your SLB console, click your SLB and check the Backend Servers as shown above. There should be now three servers with 1 server added by Auto Scaling.
For your final verification, use your browser to open the Internet IP of the SLB. As you refresh the page you will notice that the hostname change. Ensure that you are getting responses from three different hosts. For me one of the outputs that showed the auto scaled instance was as follows:
I know that this was a much more involved how-to. So lets do a quick recap:
- We performed a few pre-requisites to get a load balanced Apache environment up, with an SLB and a couple of ECS instances
- We defined a Scaling Group Configuration along with:
- Scaling Configuration
- ECS Instance List
- Scaling Rule
To define the scaling group with certain parameters like min/max instances, how many instances will we add, what are the manual instances etc.
- We created a monitoring task to monitor the CPU load and then trigger the Scale-out activity
- We verified that the auto scaling worked and 1 instance was successfully added to the SLB and became part of our web layer.
Congratulations! You have successfully Auto Scaled a load balanced Apache web application.