Auto scaling, also referred to as autoscaling, auto-scaling, and sometimes automatic scaling, is a cloud computing technique for dynamically allocating computational resources. Depending on the load to a server farm or pool, the number of servers that are active will typically vary automatically as user needs fluctuate.
Autoscaling is a cloud computing feature that enables organizations to scale cloud services such as server capacities or virtual machines up or down automatically, based on defined situations such as traffic ir utilization levels. Cloud computing providers, such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP), offer autoscaling tools.
Auto scaling is useful for managing microservices, as we do not know when demand for service changes and by how much and auto scaling automates this tracking and provides resources needed according to the configurations layed out in the auto scale policy.
Load balancing is a technique used in computer networking and distributed systems to distribute incoming network traffic across multiple servers, ensuring optimal resource utilization, improved performance, and high availability. It helps to evenly distribute the workload and prevent any single server from becoming overloaded, thus enhancing the overall reliability and scalability of the system.
Auto Scaling Group - configures launch templates then spins up the instances. Has an autoscaling policy.
Auto Scale Policy - Specify the launch templates. Set threshold - when it checks to make more instances (scale out), what the minimum and maximum number of instances. (Ours will be min 2, desired 2, max 3, Scales out at CPU 50%).
Load Balancer - receives then directs/distributes traffic to instances.
When an auto scaling group is deleted, so too are it's instances.
See steps here: EC2 App
Or follow steps to automate the EC2 set up here: EC2 App Automation
Or try use the following in your user data for creating your instance:
#!/bin/bash
# Update the sources list
sudo apt update -y
# Upgrade any available packages
sudo apt upgrade -y
# navigates to home directory
cd /home/ubuntu
# gets sources list that could potentially be needed for the following installations
sudo apt update
# Installs Nginx
sudo apt install nginx -y
# Installs git
sudo apt install git -y
# sets source to retrieve nodejs
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
# installs node.js
sudo apt install -y nodejs
# installs pm2
sudo npm install -g pm2
# Enables Nginx to run on start up of API or VM
sudo systemctl enable nginx
# Moves app directory from GitHub to EC2 or VM if not already there
if [ -d "/home/ubuntu/app" ]; then
echo "App folder already exists."
else
echo "Cloning app folder..."
git clone https://github.com/bradley-woods/app.git /home/ubuntu/app
fi
# changes line for reverse proxy
sudo sed -i "s/try_files \$uri \$uri\/ =404;/proxy_pass http:\/\/localhost:3000\/;/" /etc/nginx/sites-available/default
# Restarts Nginx to update configuration changes
sudo systemctl restart nginx
# navigates into the correct directory
cd /home/ubuntu/app
# installs app
npm install
# Runs/Starts the app in the background
pm2 start app.js
Ensure that your Sparta test page works. Have Nginx, the static page for the app and the reverse proxy set up.
- With your app EC2 instance up and running with your app working create an AMI.
- Name the AMI appropriately and review configurations.
- Navigate to AMIs page and ensure it has a name.
- Navigate to the "Launch templates" page and select "Create Launch Template".
- Name your template appropriately.
- Choose the AMI you want to base your template on.
- Specify instance type (in this case t2.micro).
- Specify ssh key pair login.
- Specify security group for template.
- You may choose to add the code below in “User Data” under “Advanced Settings” with start up commands.
#!/bin/bash
# navigates to correct folder
cd /home/ubuntu/app
# installs app
npm install
# starts app
pm2 start app.js
- Create template by clicking on “Create launch template” once you are satisfied with your settings. You may choose to terminate your instance now.
Create/Configure the Auto Scaling Group Create/Configure the Auto Scaling Group
- Navigate to Auto Scaling Groups, then click on “Create an Auto Scaling group”.
- Name your group, then select the launch template you want. Then "Next".
- Configure VPC and Availability Zones. Then "Next".
- Create a load balancer. Name it and configure it for HTTP and internet-facing. Then create a target group and name it. Turn on ‘Elastic Load Balancing health checks’. Then "Next".
- Set the group size capacities (in this case 2, 2, 3). Add a scaling policy for averaging 50% CPU usage. Then "Next".
-
You may choose to add notifications so that you are notified when the instances are created or destroyed. We will not be adding this for now. Then "Next".
-
Add a tag to identify your Auto Scaling Group.
- Review and check your settings and configurations. When ready click "Create Auto Scaling group".
- You should be able to access the app static page from the load balancer DNS address in your browser.