When building on the cloud, one of the advantages of cloud computing is High availability which means that people can always access your application about 99.99% of the time without worrying about downtime of your application/website. The tenets of High availability dictate that you run your application/system in at least 2 Availability Zones.
Load balancers are servers that forward internet traffic to multiple servers (EC2 Instances) downstream. If one of your servers is down or unhealthy, the load balancer helps to direct traffic to the available server to ensure no downtime. Load balancers ensure organisations can maintain the performance and availability of their applications which helps them stay a step ahead of the competition. Load balancers are super helpful for sudden traffic spikes where application load balancers effectively split network load to the cloud, boosting availability and scalability.
Why do we use a load balancer
-Load balancer helps to spread the load across multiple downstream instances
-Expose a single point of access (DNS) to your application
-Seamlessly handle failures of downstream instances
-Do regular health checks on your instances
-Provide SSL termination (HTTPS) for your websites
-Enforce stickiness with cookies
-Separate public traffic from private traffic
-High availability across zones
Types of load balancer
There are 4 types of Load balancers in AWS
1-Classic Load Balancer (v1 - old generation) – 2009 – CLB (soon to be phased out)
Supports HTTP, HTTPS, TCP, SSL (secure TCP)
2-Application Load Balancer (v2 - new generation) – 2016 – ALB
Supports HTTP, HTTPS, WebSocket
3-Network Load Balancer (v2 - new generation) – 2017 – NLB
Support TCP, TLS (secure TCP), UDP
4-Gateway Load Balancer – 2020 – GWLB
Operates at layer 3 (Network layer) – IP Protocol
We will be focusing on Application Load Balancer today
Application load balancer is Layer 7 (HTTP) which balances the load to multiple HTTP applications across target groups. The application load balancer handles load balancing to multiple applications on the same machine (e.g containers). It supports HTTP/2, WebSocket and redirects traffic from HTTP to HTTPS.
Application load balancer enable routing tables to different/multiple target groups. They are a great fit for microservices & container-based applications (e.g Docker & Amazon ECS). It has a port mapping feature to redirect to a dynamic port in ECS. Application load balancers usually have a fixed hostname (e.g Application-LB-506567568.us-east-1.elb.amazonaws.com) and the application servers don’t see the IP of the client directly.
Now let’s get our hands dirty in provisioning an Application Load Balancer
-Login to your AWS Console
-Then navigate or type in the search bar EC2
-Click on Instances
-Click on launch instance
-We are going to be creating two EC2 instances. Let us name our first instance “Webserver1”
-Make sure you are on the free tier
-Create a “Keypair” and give it any name you like
-Create a new Security group that allows SSH traffic
-Expand Advanced details, scroll down to user data and input the script below:
#!/bin/bash
# Please make sure to launch Amazon Linux 2
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from Webserver1</h1>" > /var/www/html/index.html
echo "<h1>Healthy</h1>" > /var/www/html/health.html
-Then click Launch Instance
-Click view all Instances
As I mentioned earlier, we are creating two EC2 Instances. Follow the same process to create another EC2 instance with the name Webserver2.
-A keypair and a public IP address.
-Security group that allows SSH traffic
-Expand Advanced details, scroll down to user data and input the script below:
#!/bin/bash
# Please make sure to launch Amazon Linux 2
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from Webserver2</h1>" > /var/www/html/index.html
echo "<h1>Healthy</h1>" > /var/www/html/health.html
-Click Launch instance
-Click view all Instances
-We have our two instances running now
Great you have made it thus far. Next for us is to create a Target Group.
-Look at the left pane on your EC2 Instance dashboard and scroll down to Target group, then click Create Target group
-Give it the name "WebserverTG"
-Leave everything as default
-Ensure to select the same VPC used when creating the Instances
-Under Health checks path paste: "health.html"
-Then click Create
-Select the created Target group, click the Actions drop-down and click Register and deregister instance/IP targets
-Select the instances created and click Add to registered, then click Save.
-Still on the left pane of your EC2 Instance dashboard click Load Balancers
Click Create Load Balancer
-Select Application Load Balancer and click Create
-Give it the name "Application-LB"
-Leave it as internet facing and IPv4
-Select your VPC and all your public subnets
-Click the Security Group created with the first instance to the ALB
-Under Configure routing--> select the Existing target group “WebserverTG”
-Review and Create load balancer
Great job. We are almost there
-Go to the Security Group of each of the instances created and modify the Inbound rule
-Edit inbound rule allowing All Traffic from the Security Group of your load balancer
Alas, we got to the finish line.Yipeee!!!
-Navigate on your Instance dashboard to the Load balancer. Copy the DNS name and paste in a browser.
-Refresh the browser to confirm the load balancer is directing traffic to the other EC2 Instance
-On the web browser where you pasted the DNS name of the Application LB, add “/health.html” at the end of the URL to confirm if the EC2 Instance is Healthy
Do not forget to clean up your resources (i.e delete everything you have provisioned)
Please drop a comment in the chat box and let me know if you find this helpful.
Top comments (0)