Load Balancer in AWS, its types, and practical implementation

In the previous blog, we saw how to install and configure the Apache web server.

We deployed our website on a single EC2 instance and shared our web server's IP address (EC2 Instance) to access the web contents through a browser.

https://hashnode.com/post/cley8ikzw000c09mmezhx4wxe

But in the real world, it does not happen like this, there will be more than one web server to manage the website.

If multiple requests come, it's divided requests and forwarded to the different web servers.

The incoming request should be forwarded equally to an available web server using a Load-balancer

Structure for load-balancer

The above picture shows that three web servers are responsible for managing a website ex. xyz.com

Each Web server has a unique IP address and the same website data inside the server.

At the same time, if U1, U2, and U3 are trying to visit xyz.com, the load balancer divides the incoming request and forwards it to WEB-1, WEB-2, and WEB-3.

We will share the load balancer IP address with the outside world to access the website, so no one has an instance IP address due to that some extra security will be provided to our EC2 instance.

In case WEB-1 is down then the load balancer forwards the request to WEB-2 and WEB-3 only until WEB-1 comes into a running state.

Load Balancer in AWS

In Amazon Web Services (AWS), a load balancer is a service that distributes incoming network traffic across multiple servers to improve the availability and scalability of applications running on those servers. AWS provides several types of load balancers, including:

  1. Classic Load Balancer (CLB): The original AWS load balancer, designed to distribute traffic across EC2 instances in a single Availability Zone or multiple Availability Zones. It supports TCP, HTTP, and HTTPS protocols.

  2. Application Load Balancer (ALB): A Layer 7 load balancer that routes traffic to targets based on the content of the request. It supports path-based routing, and host-based routing, and can route traffic to multiple ports on a single EC2 instance.

  3. Network Load Balancer (NLB): A Layer 4 load balancer that routes traffic to targets based on IP protocol data. It is designed to handle millions of requests per second and supports static IP addresses for clients.

  4. Gateway Load Balancer (GLB): It is a recently launched service in AWS that allows customers to deploy a single virtual appliance as a load balancer for multiple services across different VPCs (Virtual Private Clouds). It operates at the network layer (Layer 3) and provides support for both TCP and UDP protocols.

    The Gateway Load Balancer is a powerful new service in AWS that provides flexible and scalable load balancing for a wide range of use cases, making it a useful tool for managing network traffic in complex environments.

Each type of load balancer has its unique features and capabilities. The choice of load balancer depends on the specific needs of the application being deployed.

Pre-requisites for implementation of Load Balancer in AWS

  1. Required 3 Linux EC2 instance which has Apache web server software installed.

  2. Need one load balancer to distribute the traffic to web servers.

Creating Apache web server using Post installation script

Using the post-installation script, we don't need to install Apache software on each server separately.

We can initialize the task before the creation of the server

Step 1: Go to EC2 dashboard

Step 2: Click on Launch Instance

Step 3:

  1. Give a name to instance

  2. choose AMI

  3. choose instance type

  4. Create new key pair

  5. Edit network setting

  6. Add port no. 80 to access web content through a browser by clicking on add security group rule

    1. click on advanced details

      in last we have to created post-installation script

    2. Select number of instances = 3

and launch the instances

And another way to create an EC2 instance and manually configure the Apache server using the below blog link

https://hashnode.com/post/cley8ikzw000c09mmezhx4wxe

Verify Instances

Click on the EC2 dashboard

Verify Apache web server software

Login with putty

refer below blog for how to connect the EC2 instance

https://hashnode.com/post/cles9corf001j3mnv90ay8kau

Login with ec2-user

check apache service is running

#systemctl status httpd

Do this step on all remaining servers and make sure that the Apache service is in a running state.

For Testing purpose, to check the Behavior of the Load balancer and how it works in case any server fails, for that we make some changes in index.html file of each server

On Web-Server-1

Go to:-

cd /var/www/html

vi index.html

"This is WebServer1"

same as above edit the index.html contents as per the server's name

After all these steps are done, we have to configure the load balancer

Configure the Load Balancer

In services click on EC2, then click on Load-balancing and load-balancer

Step1:- click on Create Load Balancer

There are four types of load-balancers

All the load-balancer have the same operation and configuration choose a load balancer depends on our requirements depends on how much the load is.

For

1 to 100 servers (Classic Load-balancer)

101 to 1000 servers (Application load-balancer)

more than 1001 servers (Network Load balancer)

Step 2:- Click on classic load-balancer and assign the name

Step 3:- Assign Security Group

You can create a new security group or add existing which already been created

Step 4:- Configure security settings

The above message is showing due to we are using HTTP protocol not HTTPS

Step 5: Configure Health Check

Your load balancer will automatically perform health checks on your EC2 instances and only route traffic to instances that pass the health check. If an instance fails the health check, it is automatically removed from the load balancer. Customize the health check to meet your specific needs

Details on Health Check

Ping Protocol: HTTP

This is the default protocol is used to connect with load balancer

Ping port : 80

This is the port number for HTTP

Ping Path : /index.html

This is the file where our web content is available, the location of the file in the web server is /var/www/html

Advance Details:

Response timeout: 2 (Seconds)

Here, the load balancer sent the ping request to the web server, If the server is not responding in 2 Seconds then the load balancer understands that the web server is unavailable

If the load balancer gets the response in two seconds, then its understand that the web server is available

Interval: 5 (Seconds)

Here, if the response comes in 2 seconds or not, the load balancer checks in an interval of 5 seconds to verify whether the web server is active or not.

Unhealthy threshold: 2 (times)

Here, if the ping response does not get in two times, the load balancer understands that the instance is not reachable.

Healthy threshold: 2 (times)

Here, if it gets a response two times, that means the load balancer understands that the web server is now back in a running state, and then it sends the request.

Step 6: Add EC2 instance

Here we add all web servers which will manage by load-balancer.

Step 7: Add tags

Not mandatory, we can add it if we need

Step 8: Review

We can verify and edit the configuration from here also

Step 9: Launch a load-balancer

Verify

Click on the EC2 dashboard and then click on the load balancer tab

Under load-balancer in description section there is DNS details

Copy this link and paste it on any browser

NOTE:- The load balancer has only DNS details, not IP addresses, because the IP address changes while rebooting but the DNS details remain the same.

The output look something like below

If we reload the page the load balancer sent the request on different server

Before it is sent to server 3 and after reloading it the response comes from server 2

Here we can test through a different method,

  1. Stop down any one of the web servers

  2. Stop two web servers

  3. Stop all servers

  4. Start all stopped instances and check how much time it takes to display the contents

AWS load balancers can automatically scale up or down based on incoming traffic and can route traffic to healthy instances. They can also perform SSL/TLS termination, connection draining, and sticky sessions, among other features. Pricing for load balancers in AWS is based on the number of requests, data processing, and availability zones used.

This is all about the load-balancer service in AWS!

Thank you for reading my post

If you like my work please share it with your friends

Pawan Bhagat