In the previous blog, I automated EC2 instances using launch templates. In this blog let's learn about Load balancing and Load balancers in AWS.
What is Load Balancing?
Load balancing is a technique that distributes network or application traffic across several servers. This can improve performance, reliability, and availability.
Load balancers work by receiving requests from clients and then routing them to one of the servers in the pool. The load balancer uses a variety of factors to determine which server to send a request to, such as the server's load, availability, and health.
In Amazon EC2, Elastic Load Balancing (ELB) is the load balancing service provided by AWS.
Elastic Load Balancing
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.
Elastic Load Balancing scales your load balancer capacity automatically in response to changes in incoming traffic.
How does Elastic Load Balancing work?
Clients make requests to your application.
The listeners in your load balancer receive requests matching the protocol and port that you configure.
The receiving listener evaluates the incoming request against the rules you specify, and if applicable, routes the request to the appropriate target group. You can use an HTTPS listener to offload the work of TLS encryption and decryption to your load balancer.
Healthy targets in one or more target groups receive traffic based on the load-balancing algorithm, and the routing rules you specify in the listener.
The benefits of load balancing:
Improved performance: Load balancing can improve performance by distributing traffic across multiple servers. This can help to reduce latency and improve response times.
Increased reliability: Load balancing can increase reliability by ensuring that no single server is overloaded. If one server goes down, the load balancer can redirect traffic to the remaining servers.
Improved availability: Load balancing can improve availability by ensuring that your applications are always available. If one server goes down, the load balancer can redirect traffic to the remaining servers.
ELB can be created, accessed, and managed by using AWS Management Console, AWS Command Line Interface, AWS SDKs, and Query API.
Elastic Load Balancing supports the following types of load balancers:
Application Load Balancers
Network Load Balancers
Gateway Load Balancers
Classic Load Balancers
There is a key difference in how the load balancer types are configured:
With Application Load Balancers, Network Load Balancers, and Gateway Load Balancers, you register targets in target groups, and route traffic to the target groups.
With Classic Load Balancers, you register instances with the load balancer.
Cross-zone load balancing
The nodes for your load balancer distribute requests from clients to registered targets.
When cross-zone load balancing is enabled, each load balancer node distributes traffic across the registered targets in all enabled Availability Zones.
When cross-zone load balancing is disabled, each load balancer node distributes traffic only across the registered targets in its Availability Zone.
With Application Load Balancers, cross-zone load balancing is always enabled at the load balancer level.
At the target group level, cross-zone load balancing can be disabled.
With Network Load Balancers and Gateway Load Balancers, cross-zone load balancing is disabled by default. After you create the load balancer, you can enable or disable cross-zone load balancing at any time.
When you create a Classic Load Balancer, the default for cross-zone load balancing depends on how you create the load balancer. With the API or CLI, cross-zone load balancing is disabled by default. With the AWS Management Console, the option to enable cross-zone load balancing is selected by default.
Now let us understand the different types of ALB provided by AWS:
Application Load Balancer
Here is the official documentation of the Application Load Balancer.
ALB Functions at the application layer, the seventh layer of the Open Systems Interconnection (OSI) model.
It intelligently distributes incoming HTTP and HTTPS traffic to target instances, containers, or IP addresses based on the rules and configurations set by the user.
Network Load Balancer
Here is the official documentation of the Network Load Balancer.
NLB functions at the fourth layer of the Open Systems Interconnection (OSI) model. It can handle millions of requests per second.
NLB operates at the transport layer (Layer 4) of the OSI model and is designed to handle TCP, UDP, and TLS traffic.
It is highly scalable, offers low latency, and is suitable for applications that require high performance and extreme scalability.
Gateway Load Balancer
Here is the official documentation of the Gateway Load Balancer.
A Gateway Load Balancer operates at the third layer of the Open Systems Interconnection (OSI) model, the network layer.
It listens for all IP packets across all ports and forwards traffic to the target group that's specified in the listener rule.
It maintains the stickiness of flows to a specific target appliance using a 5-tuple (for TCP/UDP flows) or 3-tuple (for non-TCP/UDP flows).
Classic Load Balancer
Here is the official documentation of the Classic Load Balancer.
CLB operates at both the application layer (Layer 7) and transport layer (Layer 4) of the OSI model and is ideal for applications that require basic load-balancing features.
CLB provides basic load-balancing capabilities for distributing incoming traffic across multiple Amazon EC2 instances.
Here is the link which gives the product differentiation and pricing of the different ELBs provided by AWS.
Tasks that we will be covering in this blog.
📢 Task 1:
🍀launch 2 EC2 instances with an Ubuntu AMI and use User Data to install the Apache Web Server.
🍀Modify the index.html file to include your name so that when your Apache server is hosted, it will display your name also do it for 2nd instance which includes " DevOps Community is Super Awesome :) ".
🍀You should see a webpage displaying information about your PHP installation.
📢 Task 2:
🍀Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console.
🍀Add EC2 instances that you launch in task-1 to the ALB as target groups.
🍀Verify that the ALB is working properly by checking the health status of the target instances and testing the load balancing capabilities.
🎯Task 1:
Launch 2 EC2 instances with an Ubuntu AMI and use User Data to install the Apache Web Server.
We will do it with the help of Create Launch Template.
Use the following commands to pre-install apache2 in your instance.
#!/bin/bash
sudo apt-get update -y
sudo apt-get install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
So, finally, we can see the template Apache-Ubuntu-template is created successfully.
So, now we will launch 2 instances from the template as follows. Click on Launch Instance from the template.
Once the instances are created rename them with numbers so you can easily identify the servers.
Copy the public IP address of your EC2 instances and hit in the browser.
Before accessing a web page on the browser, make sure to check inbound security rules which should be open for all traffic on both instances.
You should see a webpage displaying information about your PHP installation as follows.
Modify the index.html file to include your name so that when your Apache server is hosted, it will display your name.
To modify the index.html file, we need to modify its content in the directory /var/www/html.
Let's connect to the web-1 instance through SSH.
Give the index.html file root user privilege.
sudo chmod +x index.html
For your reference, I am giving my HTML syntax used in the second web-2 instance which is a simple web page. The first web page is the official Apache Web Server page in which I changed some syntax for a better-looking web page.
<!DOCTYPE html>
<html>
<head>
<title>Shubham's Apache Web Server</title>
</head>
<body>
<h1>This is Shubham M</h1>
<h2> DevOps Community is Super Awesome <h2>
<h3> I am happy to use the Apache Web Server <h3>
</body>
</html>
Connect to the Web server using the instance's Public IP.
Also, do it for 2nd instance which includes "DevOps Community is Super Awesome".
And yay! We have successfully installed the Apache Web server and edited the index.html file.
🎯Task 2:
Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console.
Add EC2 instances that you launch in task-1 to the ALB as target groups.
Verify that the ALB is working properly by checking the health status of the target instances and testing the load-balancing capabilities.
Navigate to the EC2 dashboard. On the left side of the EC2 dashboard, if you scroll down you can see Load Balancers, select it.
Click on Create Load balancer. The following page opens up.
Select "Application Load Balancer" as the load balancer type and click "Create".
Basic configuration:
Load balancer name: Apache-web
Scheme: Internet Facing (access to public)
Network mapping: Select at least two Availability Zones and one subnet per zone.
Security Group: default will be selected and select the other security groups you require.
Before going ahead, let's create Target Groups. Click on Create target group link available in the Listeners and Routing section as follows.
Step 1: Specify group details
Basic configuration: Instances
Target group name: Apache-Demo
And click on Next
Step 2: Register targets
Available Instances: Select the instances which you need
Click on Create Target groups.
Make sure after the target group is created, look at the full configuration which should look like this with registered targets.
Now go back to the ALB page.
Select the target group you just created.
Check out my ALB summary of configurations and click on Create load balancer.
Below are the configurations of my ALB.
Now, we can see the Load balancers section, we can see our Apache-web is listed and in active state.
Once the ALB is in an active state, copy the DNS name of the load balancer and access it from your website.
As per the load, we can observe that sometimes Server 1 comes up and other times Server 2 comes up.
And hence we can conclude as we have created an ALB, and distributed the load!
In this blog, I have discussed how to create an AWS Application Load Balancer and use it to balance the load between two or more web servers. If you have any questions or would like to share your experiences, please leave a comment below. Don't forget to read my blogs and connect with me on LinkedIn and let's have a conversation.
To help me improve my blog and correct my mistakes, I am available on LinkedIn. Do reach me and I am open to suggestions and corrections.
In the next blog post, we will explore more advanced topics in the realm of DevOps. So, stay tuned and let me know if there is any correction.
please feel free to connect.
Git Hub Repo: https://github.com/ShubhamBMatere/90DaysOfDevOps/blob/master/2023/day41/tasks.md
Thank you for reading!