Day 41: Setting up an Application Load Balancer with AWS EC2

Day 41: Setting up an Application Load Balancer with AWS EC2

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 ELB 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 that gives the product differentiation and pricing of the different ELBs provided by AWS.

Tasks

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 include " TrainWithShubham Community is Super Aweasome :) ".

  • Copy the public IP address of your EC2 instances.

  • Open a web browser and paste the public IP address into the address bar.

  • You should see a webpage displaying information about your PHP installation.

Step 1:- First we navigate to AWS and click on Instances-> Launch instance

  1. Go to the AWS console and navigate to the EC2 service.

  2. Click on Launch Instance button and select Ubuntu Server 22.04 LTS (HVM), SSD Volume Type from the list of available AMIs.

  3. Choose t2.micro from the list of available instance types.

  4. Choose an existing key pair or create a new one if you don’t have one already. You’ll need this key pair to SSH into your instance later.

  5. In the Configure Instance Details page, scroll down to the Advanced Details section and expand the User data field.

  6. In the User data field, enter the following commands to install and start the Apache Web Server:

COPY

#!/bin/bash
# Update packages
sudo apt-get update -y
# Install Apache
sudo apt-get install -y apache2
# Start Apache service
sudo systemctl start apache2
# Enable Apache service
sudo systemctl enable apache2
# Modify index.html file
sudo echo "<h1>Welcome to AWS EC2</h1>" > /var/www/html/index.html
sudo echo "<p>My name is Ajit</p>" >> /var/www/html/index.html
  1. Expand Network settings: Create a new security group or select an existing one. Make sure to allow inbound traffic on port 80 for HTTP protocol from anywhere. This will allow us to access the Apache web page from our browser.

  2. Click on the Launch instance button and wait for your instance to launch.

  3. Repeat step 2-8 to launch another instance with the same configuration, but modify the user data script as follows:

COPY

#!/bin/bash
# Update packages
sudo apt-get update -y
# Install Apache
sudo apt-get install -y apache2
# Start Apache service
sudo systemctl start apache2
# Enable Apache service
sudo systemctl enable apache2
# Modify index.html file
sudo echo "<h1>Welcome to AWS EC2</h1>" > /var/www/html/index.html
sudo echo "<p>TrainWithShubham Community is Super Awesome :)</p>" >> /var/www/html/index.html

This will change the message in the index.html file for the second instance.

You have successfully launched two EC2 instances with an Ubuntu AMI and used User Data to install the Apache Web Server.

How to Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console and Add the EC2 Instances to the ALB as Target Groups

An Application Load Balancer (ALB) is a type of load balancer that operates at layer 7 of the OSI model and is ideal for applications that require advanced routing and microservices. ALB can route the traffic based on the content of the request, such as the URL path, the host header, the query parameters, etc. ALB can also support multiple protocols, such as HTTP, HTTPS, HTTP/2, WebSocket, and gRPC. ALB can also integrate with other AWS services, such as Amazon ECS, Amazon EKS, AWS Lambda, AWS WAF, etc.

To create an ALB in EC2 using the AWS Management Console and add the EC2 instances to the ALB as target groups, we’ll use the following steps:

  1. Go to the AWS console and navigate to the EC2 service.

  2. Click on Load Balancers in the left navigation pane and then click the Create Load Balancer button.

  3. Select Create Application Load Balancer as the load balancer type.

  4. In the Basic Configuration section, enter a name for your load balancer, such as ALB-Demo.

  5. In the Network mapping section, review VPC and subnet.

  6. In the Security Groups section, create a new security group or select an existing one for your load balancer. Make sure to allow inbound traffic on port 80 for HTTP protocol from anywhere. This will allow us to access the load balancer from our browser.

  7. In the Listeners and Routing section, create a new target group or select an existing one for your load balancer. A target group is a group of targets, such as EC2 instances, that receive the traffic from the load balancer. For example, you can create a new target group with the following settings:

    • For Name, enter a name for your target group, such as ALB-Demo-TG.

    • For Target type, select the instance to specify targets by instance ID.

    • For Protocol, select HTTP.

    • For Port, enter 80.

    • For Health checks, modify the default settings as needed. For example, you can change the Path to /index.html to check the index.html file on each target.

  8. Click on the Create load balancer button and wait for your load balancer to be created.

You have successfully created an ALB in EC2 using the AWS Management Console and added the EC2 instances to the ALB as target groups.

How to Verify that the ALB is Working Properly by Checking the Health Status of the Target Instances and Testing the Load Balancing Capabilities

To verify that the ALB is working properly, we’ll use the following steps:

  1. Go to the AWS console and navigate to the EC2 service.

  2. Click on Load Balancers in the left navigation pane and select your load balancer, such as ALB-Demo.

  3. Copy the DNS name of your load balancer from the details panel. This is the address that you can use to access your load balancer from your browser.

  4. Open a web browser and paste the DNS name into the address bar. You should see a webpage displaying information about your apache2 installation and your name or message, such as “Welcome to AWS EC2” and “My name is Ajit”.

  5. Refresh the web page several times and observe the changes. You should see different names or messages on each refresh, such as “Welcome to AWS EC2” and “TrainWithShubham Community is Super Awesome :)”. This means that the load balancer is distributing the traffic across your two EC2 instances and displaying the index.html file from each instance.

  6. Go back to the AWS console and navigate to the EC2 service.

  7. Click on Target Groups in the left navigation pane and select your target group, such as ALB-Demo-TG.

  8. Click on the Targets tab and observe the health status of your target instances. You should see that both instances are healthy and in service. This means that the load balancer is able to communicate with your instances and perform health checks on them.

  9. To test the fault tolerance of your load balancer, you can stop or terminate one of your instances and see how the load balancer handles the situation. For example, you can select one of your instances, such as ALB-Demo-1, and click on Actions button and choose Instance State and then Stop or Terminate option from the drop-down menu.

  10. Wait for the instance to stop or terminate and then refresh the web page in your browser. You should see that the web page is still accessible and displaying the name or message from the other instance, such as “Welcome to AWS EC2” and “TrainWithShubham Community is Super Awesome :)”. This means that the load balancer is able to detect the unhealthy or unavailable instance and redirect the traffic to the healthy or available instance.

You have successfully verified that the ALB is working properly by checking the health status of the target instances and testing the load balancing capabilities.

Conclusion:

In this article, I've detailed the concept of load balancing and provided guidance on utilizing Elastic Load Balancing (ELB) within AWS EC2. ELB is a service designed to efficiently distribute incoming traffic among numerous EC2 instances. Additionally, I've demonstrated the process of creating and implementing an Application Load Balancer (ALB) in EC2 through the AWS Management Console, outlining the steps to add EC2 instances to the ALB as target groups. To ensure the proper functioning of the ALB, I've guided you on checking the health status of the target instances and testing the effectiveness of the load balancing capabilities.

Did you find this article valuable?

Support DevOps_journey by becoming a sponsor. Any amount is appreciated!