Vertical & Horizontal Scaling in EC2 instances
- Vertical scaling is add more power to your existing machines (Scaling up)
- Horizontal scaling is adding more machines which do the same job (Scaling out)
- In AWS we can do both Vertical & Horizontal scaling.
- Vertical scaling can be done by changing instance size of the vm, This requires ec2 instance to be stopped
- Horizontal scaling can be done by adding more ec2 instances & this can be automated by certain metrics like cpu utilization, disk IO etc using Auto scaling groups
- Lets do Vertical scaling of t2.micro instance in AWS to t2.large (Scale up)
- In the same we can also (Scale down) changing from larger instance size to smaller instance size
- Consider a scenario where your company has food delivering app and you are using ec2 instance for hosting the application
- Now our application has become popular & lots of users are using it (of-course we have discounts)
- As load increases and decreases we might want a solution which will automatically add and remove servers depending on some metric (Generally for web/mobile applications CPU Utilization is considered)
- In our case since this a food delivering app, we do have peak hours.
- Amazon offers Auto Scaling Groups which can automatically add/remove ec2 instances depending on some metric (Cloud watch)
- Now in the ec2 machines our applications should be running so we have two options
- Create an AMI with your application
- Create an os based AMI (ubuntu) & install your application after ec2 instance is up?
- Having an AMI with application is better, so lets create an AMI to simulate the scenario. In this case since we don’t have users we will create artificial load on ec2 instance
- Lets install lamp stack with one additional tool called as stress
sudo apt-get install stress -y
- note: for lamp stack refer here
sudo apt-get update sudo apt-get install apache2 -y sudo apt install php libapache2-mod-php php-mysql -y sudo apt install php-cli stress -y sudo -i echo "<?php phpinfo(); ?>" >> /var/www/html/info.php sudo systemctl restart apache2
- Auto scaling helps you ensure that you have correct number EC2 instances available for handling load of your application.
- Auto scaling components
- Groups: Your ec2 instances will be organized as groups. In reality consider each application/service as a group
- Configuration Template: In this we specify which AMI id, instance type, key pair etc
- Scaling OPtions: What is minimum size , maximum size, conditions for scaling out (increasing ec2 instances) & scaling in (decreasing ec2 instances)
- Autoscaling Lifecycle
- Lets create an autoscaling group with our ami