What is a load balancer?
Imagine a busy intersection with no traffic signal or traffic cop. If nobody controls the traffic, the junction will be loaded with vehicles, causing traffic jams, delays in reaching the destination, and even accidents. Similarly, if many users are trying to access a server(or servers) simultaneously it can cause congestion in the network, delay in data transmission, and even crash the server.
A load balancer acts as a traffic cop for websites and applications. It distributes incoming network or application traffic across multiple servers to ensure no single server is overwhelmed. It improves application reliability and scalability
Key features of a load balancer
Load balancers are very important for modern web applications, applications that work on microservices architecture, for content delivery, disaster recovery etc. Some important features of load balancers are:
- Traffic Distribution: routes requests to different servers based on algorithms like round-robin, least connection, etc. so that no server is overburdened.
- Health Monitoring: checks the availability and performance of the servers continuously and redirects traffic away from unresponsive or unhealthy servers, so that the applications are highly available
- Fault Tolerance: provides redundancy by redistributing traffic to healthy servers if a server fails.
- Scalability: allows applications to add or remove servers dynamically based on user demand without disrupting services.
- SSL Offloading: handles SSL/TLS encryption and decryption, reducing the load on backend servers. Load balancers are used for the implementation of security policies.
- Session Persistence: allows specific users maintain a consistent connection with the same server for specific tasks.
In short, a load balancer acts as the first point of contact for incoming traffic, ensuring high availability, performance, and fault tolerance for web applications and services.
Types of Load Balancers
Load balancers can be categorized to different types depending on their use cases. On a broader classification, we can divide load balancers into three different categories based on how they are deployed.
1. Hardware load balancers - Dedicated physical appliances designed for high-performance traffic distribution. They are often used by large scale enterprises and data centers that require minimum latency and high reliability. They are also very expensive. Some examples are
2. Software load balancers - These are software solutions running on commodity hardware or virtual machines. They are cost-effective and customizable for on-premise or containerized environments. Some examples are Nginx, HAProxy, Apache HTTP Server
3. Cloud load balancers - They are fully managed services provided by cloud providers for scalable and dynamic workloads. Often used by application hosted on the cloud platforms that has fluctuating traffic. Examples are AWS Elastic Load Balancer (ELB), Azure Load Balancer, Google Cloud Load Balancer.
Now let's set up an AWS Elastic Load Balancer. There are Application Load Balancer, Network Load Balancer, and Gateway Load Balancer under AWS's elastic load balancing services under Version 2. Version 1 has Classic Load Balancer. We will set up an Application Load Balancer.
In the official documentation of Elastic Load Balancing, we can see the definition of ELB as Elastic Load Balancing automatically distributes your incoming traffic across multiple targets, such as EC2 instances, containers, and IP addresses, in one or more Availability Zones. It monitors the health of its registered targets and routes traffic only to the healthy targets. You can select the type of load balancer that best suits your needs.
For this demo, we will create 3 VMs using AWS EC2 Service. These are basically our web servers. Login to your AWS Console, Goto EC2 dashboard and click on Launch Instances.
Change the number of instances to 3, give the instance a name. I have given as Web-01, for the second and third instances the names will be changed to Web-02 and Web-03 once they are up and running. I have chosen Amazon Linux as the AMI which is the OS of our webserver and the instance type as t2.micro which is eligible for free-tier.
Select the keypair to access your VM from your laptop. Create a new key pair if you don't have any. Also select the security group for the VMs or create a new one if you want. I chose the default 8gb storage and launched the instances.
In the security group make sure you have opened ports 80 and 443 (HTTP and HTTPS) to be accessible from all IPs by editing the inbound rules.
Now, go to the EC2 dashboard, and click on instances. I have my web servers up and running and I have renamed the other 2 web servers as Web-02 and Web-03 by hovering on the name and clicking on the pencil icon
Click on the instance ID to get the details of the server like IP addresses and more.
Now let's connect to the instances and install httpd in our web servers. I am using MobaXterm to connect to my instances. Copy the IPs of the webservers and connect to them individually using the .pem key-pair file. Default username for RedHat based distributions in AWS is ec2-user
I have connected to all three webservers in MobaXterm. MobaXterm has a beautiful feature 'Multi Exec' which allows us to execute the same commands in all the terminals at the same time.
Now let's install httpd in all the systems, start the service and enable it. The following commands can be used for the same. Make sure to copy each line separately.
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
httpd service is now up and running. Let's check it out by copy pasting the IP addresses of our web servers in our browser.
Voila! It works on all three webservers. Now let's create an index.html in /var/www/html
folder in all the three webservers sudo vi /var/www/html/index.html
and write the following content to it.
<h2>My website! Served from Web Server 01</h2>
Change the content of index.html in web02 and web03 Web Server 02 and Web Server 03 respectively and restart httpd service. sudo systemctl restart httpd
and refresh the browser windows.
Now let's create our target group in EC2 dashboard. Target groups are nothing but a group of instances, IP addresses, lamba functions, or application load balancers. In our case, the target group is the group of webservers we have created. Goto Target Groups under Load Balancing in the left pane of our console and click on Create Target Groups
Choose the target group as instances and give the target group a name. Under advanced health checks, it is optional to change the healthy threshold, unhealthy threshold, timeout and interval values. Click Next, select all three instances, click on Include as pending below, and then click on Create Target Group at the bottom.
Now that our target group is ready, let us create the load balancer. Goto Load Balancers under Load Balancing in the left pane and click on Create load balancer.
In the next screen, we have the option to choose the type of load balancer we want to create. We are creating an application load balancer. Click on the Create button under the Application Load Balancer. Give the load balancer a name
Select 2 or more availability zones under Network Mapping settings to ensure high availability
Select the security group for the load balancer. It is ideal to create a separate security group for the load balancer, by clicking on Create Security group. For the time being, I have selected the same security group with which I have created the instances. Make sure to open port 80 in the SG if you have created a new one.
Under Listeners and routing select the target group that we have created. In this case webservers-tg. Click on Create load balancer button at the bottom
Wait for a few minutes for the load balancer to get provisioned. After some time we can see that the load balancer is Active
Now let's copy the DNS name and paste it into our browser's address bar. The magic of load balancer has begun. I got the response from Web Sever 02
Now if I refresh the browser window, let us see what happens
See, now the response is from Web Sever 03 even though our DNS name is the same. Now if we keep refreshing
we can see the response from Web Server 01 also.
That is how intelligently AWS Elastic Load Balancer intelligently routes the client requests to backend servers without burdening a single server.
Load Balancers serve multiple purposes as mentioned above like high availability, fault tolerance, scalability etc.
This tutorial was intended to give a brief introduction to load balancers and how they work in the backend. Play around with the instances. Stop httpd on any of the servers and see how the target goes to unhealthy state automatically in the background. ELB is capable of much more than just traffic distribution.
Once you are done with the practice make sure to delete the load balancer, target groups, and instances to avoid unnecessary billing.
Top comments (0)