Day 41 - Application Load Balancer with AWS EC2

Day 41 - Application Load Balancer with AWS EC2

Load Balancer

What is Load Balancer?

A load balancer is the device or service that sits between the user and the server group and acts as an invisible facilitator, ensuring that all resource servers are used equally.

The job of a load balancer is to distribute use traffic across multiple instances of an application. By spreading the load, load balancer reduces the risk of applications that experiences performance, reliability or scalability issues.

Load balancers can also be used to provide failover. If one of the servers in the pool goes down, the load balancer can automatically redirect traffic to the remaining servers. This helps to ensure that the application remains available even if one of the servers is unavailable.

Load balancers are designed to both receive requests and distribute them to the appropriate servers.

How does the Load Balancer Work?

1. Load balancers handle incoming requests from users for information and other services. They sit between the servers that handle those requests and the internet.

2. Once a request is received, the load balancer first determines which server in a pool is available and online and then routes the request to that server.

3. During times of heavy loads, a load balancer acts promptly and can dynamically add servers in response to spikes in traffic.

4. Conversely, load balancers can drop servers if demand is low.

Benefits of Load Balancer

Benefits of using a load balancer include: -

  1. Application availability: Users both internal and external need to be able to rely on application availability. If an application or function is down, lagging, or frozen, precious time is lost and a potential source of friction is introduced that might drive a customer to a competitor.

  2. Application scalability: Imagine you run a ticketing company, and tickets for a popular performance are announced to be available at a certain date and time. There could be thousands or even more people trying to access your site to buy tickets. Without a load balancer, your site would be limited to whatever your single/first server can accommodate—which likely won’t be much with that much demand. Instead, you can plan for this big spike in traffic by having a load balancer to direct requests and traffic to other available compute surfaces. And that means more customers can get their desired tickets.

  3. Application security: Load balancing also lets organizations scale their security solutions. One of the primary ways is by distributing traffic across multiple backend systems, which helps to minimize the attack surface and makes it more difficult to exhaust resources and saturate links. Load balancers can also redirect traffic to other systems if one system is vulnerable or compromised. In addition, load balancers can offer an extra layer of protection against DDoS attacks by rerouting traffic between servers if a particular server becomes vulnerable.

  4. Application performance: By doing all of the above, a load balancer boosts application performance. By increasing security, by optimizing uptime, and by enabling scalability through spikes in demand, load balancers keep your applications working as designed—and the way you, and your customers, want them to.

Elastic Load Balancer

What is Elastic Load Balancer?

Elastic Load Balancer(ELB) provided by Amazon in which it automatically distributes incoming application traffic across multiple targets and virtual appliances in one or more Availability Zones (AZs) that increases speed and performance. It also helps to improve the scalability of your application and secures your applications.

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 Elastic Load Balancer Work?

  1. Clients make requests to your application.

  2. The listeners in your load balancer receive requests matching the protocol and port that you configure.

  3. 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.

  4. 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.

Benefits of using Elastic Load Balancer

Benefits of using Elastic Load Balancer includes: -

  1. High availability: The most common service that relies on Elastic Load Balancer is Amazon’s EC2 Instances, as Elastic Load Balancer performs a health checkup to confirm an instance continues to be running before sending traffic thereto. A developer can integrate Amazon Route 53 and Domain name system (DNS) failover to further boost application resiliency.

  2. Auto Scaling: ELB can automatically scale its load balancer capacity in response to changes in incoming traffic. This ensures that your application can handle increased traffic loads without manual intervention.

  3. Security: AWS Elastic Load Balancer supports applications within an Amazon VPC for stronger network security. An IT team can specify whether it wants an internet-facing or internal load balancer. The latter option enables a developer to route traffic through an ELB using private IP addresses.

  4. Traffic Distribution: ELB evenly distributes incoming traffic across multiple compute resources, such as EC2 instances or containers, to ensure that no single resource is overloaded while others remain idle. This helps optimize resource utilization.

  5. Availability and Fault Tolerance: ELB enhances the availability of your applications by automatically detecting and routing traffic away from unhealthy or failing targets. If a target becomes unhealthy, ELB stops sending traffic to it until it recovers, reducing the impact of potential failures.

  6. Health Checks: You can configure health checks that periodically assess the health of your compute resources. ELB routes traffic only to resources that pass these health checks, helping to maintain the overall reliability of your application.

Types of Elastic Load Balancer

There are 4 types of ELB. They are: -

  1. Application Load Balancers: Ideal for routing HTTP/HTTPS traffic and performing advanced traffic routing and content-based routing.

  2. Network Load Balancers: Designed for handling TCP/UDP traffic with high performance and low latency.

  3. Gateway Load Balancers: Used for deploying third-party virtual appliances, such as firewalls, intrusion detection systems, and other network appliances.

  4. Classic Load Balancers: An older type of load balancer that is still available for use, primarily for applications not yet migrated to the newer load balancer types.

There is a key difference in how these 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.

Application Load Balancers

The Application Load Balancer(ALB) stands out as a versatile solution designed to operate at the application layer, providing advanced routing and content-based traffic management.

  • The Load Balancer that distributes the traffic to appropriate target groups on the basis of content is called Application Load Balancer.

  • New feature-rich, layer 7 loads balancing platform.

  • Supports web sockets, HTTP, HTTPS, microservices, and container-based applications, including deep integration with EC2 container service.

  • Support for path-based and host-based routing. Also, provide routing requests to multiple applications on a single EC2 instance.

  • Cross-zone load balancing is always enabled and you can also specify Lambda functions are targeted to serve HTTP(S) requests.

  • Supports load balancer-generated cookies only for sticky sessions.

Network Load Balancers

The Network Load Balancer(NLB) shines as a high-performance solution designed to operate at the transport layer (Layer 4) of the Open Systems Interconnection (OSI) model.

  • Network Load Balancer handles sudden and violated traffic across the EC2 Instances in order to avoid any latency.

  • Connection baseload Balancing and it supports TCP protocol.

  • Ability to handle volatile workloads and scale to millions of requests per second.

  • Support for static IP addresses for the load balancer. or assign one Elastic IP address per subnet enabled for the load balancer.

  • Cross-zone load balancing is disabled by default

  • The source IP addresses of the clients are maintained and made available to your apps when you designate targets using an instance ID. The source IP addresses are the private IP addresses of the load balancer nodes if targets are specified by IP address.

  • Network Load Balancers support connections from clients over inter-region VPC peering. AWS managed VPN and third-party VPN solutions.

Gateway Load Balancers

The Gateway Load Balancer(GWLB) stands out as a specialized solution tailored for deploying and managing virtual appliances.

  • It makes it simple to scale, install, and manage your third-party virtual appliances.

  • Provide you with one gateway for distributing traffic across multiple virtual appliances, while scaling them up, or down, based on demand.

  • It improves availability and removes potential points of failure from your network.

  • Users can find, test, and buy virtual appliances from third-party vendors directly in AWS Marketplace.

  • This integrated experience streamlines the deployment process, so users can see value from your virtual appliances more quickly—whether you want to work with the same vendors you do today, or try something new.

Classic Load Balancers

The Classic Load Balancer(CLB) is a legacy load balancer that is no longer recommended for new applications. It is a Layer 4 load balancer that operates at the TCP/IP level and distributes traffic based on source IP address, port number, and protocol. CLB supports both HTTP and TCP applications.

  • The Load Balancer which balances the traffic across multiple instances in multiple availability zones is called a Classic Load Balancer.

  • It supports both EC2 Classic EC2-VPC and Increases the availability of your application by sending traffic to healthy Instances.

  • Supports HTTP, HTTPS, TCP, and SSL listeners and supports sticky sessions using application-generated cookies.

  • To make sure that the instances you have registered can handle the demand Keep roughly the same number of instances registered with the load balancer in each Availability Zone**.**

  • 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.

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

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.

Let's start with the steps how to configure it.👇

Step 1: Login to AWS Console and on search bar type "EC2".

Step 2: Launch an instance with the below details.

  1. Name: Apache-Server

  2. Number of instances: 2

  3. Application and OS Images (Amazon Machine Image): Ubuntu AMI

  4. Instance type: t2.micro

  5. Key pair (login): Select the key pair you want.

  6. Network Settings: Check the boxes “Allow HTTPS traffic from the Internet” & “Allow HTTP traffic from the Internet”

Step 3: In Advance details, under user data use the below script: -

#!/bin/bash
sudo apt-get update -y
sudo apt-get install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2

Step 4: Click on Launch Instance.

Step 5: Once the instances are created rename them with numbers so you can easily identify the servers, like Apache-Server1 and Apache-Server2.

Step 6: To modify the index.html file, we need to modify its content in the directory /var/www/html.

Step 7: Now, Connect to Apache-Server1. And go to /var/www/html location.

Step 8: Give the index.html file root user privilege. Modify the file.

sudo chmod +x index.html

Step 9: Contents of the index.html file.

<!DOCTYPE html>
<html>
<head>
<title>My New Page for Apache Web Server</title>
</head>
<body>
<h1>Author - Smriti Sharma</h1>
</body>
</html>

Step 10: Copy the Public IP of the instance and paste it into the browser. You should be able to see the author’s name.

Step 11: Let us do the same thing for the other server i.e., Apache-Server2 with some more additions.

Step 12: Modify index.html in this server with the below changes.

<!DOCTYPE html>
<html>
<head>
<title>My New Page for Apache Web Server</title>
</head>
<body>
<h1>Author - Smriti Sharma</h1>
<h2> TrainWithShubham Community is Super Awesome :) <h2>
<h3> I am happy to use the Apache Web Server <h3>
</body>
</html>

Step 13: Copy the Public IP of the instance and paste it into the browser. You will able to see the changes.

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.

Let's start with the steps how to configure it.👇

Step 1: On your instance page to left side of your screen, you can find Load Balancers.

Step 2: Go to Load Balancers.

Step 3: Click on Create Load balancer. You will see the below page.

Step 4: Select Application Load Balancer. And click on Create.

Step 5: In Basic Configuration,

  1. Load balancer name: PHP-load-balancer

  2. Scheme: Internet Facing (access to public)

  3. IP address type: IPv4

Step 6: In Network mapping, Select at least two Availability Zones and one subnet per zone.

Step 7: In Security Group, default will be selected and select the other security groups you require.

Step 8: In Listeners and Routing, Click on "Create Target Groups".

Step 9: In Specify group details, Select the below

  1. Basic configuration: Instances

  2. Target group name: ALB-apache

Step 10: Leave all other options as it is.

Step 11: Click on Next.

Step 12: In Register targets, Select the instances which you need.

Step 13: Click on Create Target groups.

Step 14: Now go back to the ALB page.

Step 15: And in Listeners and Routing, Select the target group we have created now i.e., ALB-apache.

Step 16: Leave the rest options as it is.

Step 17: Click on Create Load Balancer.

Step 18: Once the ALB is in an active state, copy the DNS of the load balancer and access it from your website.

Step 19: As per the load, you will observe that sometimes Server 1 comes up and other times Server 2 comes up.

Hooray!!! We have created an ALB🎉

Conclusion

In Conclusion, a load balancer distributes incoming network traffic across multiple servers or resources to optimize workload, enhance system performance, and ensure high availability. By evenly distributing the load, load balancers improve resource utilization, prevent server overload, and contribute to a more resilient and scalable infrastructure.

Elastic Load Balancer (ELB) is a crucial component in cloud computing that helps distribute incoming application traffic across multiple targets to ensure optimal availability and fault tolerance. ELB automatically scales based on demand, enhancing the overall performance and reliability of applications. Elastic Load balancers come in various types, such as Application Load Balancers (ALB), Network Load Balancers (NLB), Gateway Load Balancers (GWLB) and Classic Load Balancers (CLB), each designed to meet specific needs and traffic patterns.

In this blog, We have seen how to create an AWS Application Load Balancer and use it to balance the load between two web servers.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊

~Smriti Sharma✌