Demystifying AWS Elastic Load Balancers: Exploring Types and Hands-On with Application Load Balancer (ALB)

Demystifying AWS Elastic Load Balancers: Exploring Types and Hands-On with Application Load Balancer (ALB)

Elastic Load balancer

Elastic Load Balancer (ELB) is a fully managed service provided by Amazon Web Services (AWS) that plays a critical role in distributing incoming network traffic across multiple target resources, such as Amazon Elastic Compute Cloud (EC2) instances, containers, and IP addresses. ELB is designed to enhance the availability, fault tolerance, and scalability of your applications by automatically balancing the incoming traffic and ensuring that it is routed to healthy resources. Here are some key aspects and benefits of Elastic Load Balancer in AWS:

  1. Traffic Distribution: ELB evenly distributes incoming traffic or requests to a group of backend resources, which can be EC2 instances, containers, or IP addresses. This distribution helps in preventing overloading of any single resource and ensures that all resources are used efficiently.

  2. Health Monitoring: ELB continually monitors the health of the registered targets by periodically sending health checks. These health checks can be configured to assess the availability and responsiveness of each target. ELB only directs traffic to resources that pass these health checks, ensuring that only healthy resources serve requests.

  3. Availability Zones: ELB operates across multiple Availability Zones (AZs) within an AWS region. This means that even if one AZ becomes unavailable due to issues like hardware failures or network problems, ELB can automatically route traffic to healthy resources in other AZs, increasing the overall availability of your application.

  4. Auto Scaling: ELB can automatically scale its capacity to handle changes in incoming traffic. When traffic increases, it can automatically add more resources to handle the load, and when traffic decreases, it can remove resources to save costs. This elasticity helps in maintaining a responsive and cost-efficient application.

  5. High Availability: ELB itself is designed to be highly available. It has built-in redundancy, and if one ELB component fails, traffic is automatically routed to other healthy components, ensuring that your load balancer remains operational.

  6. Security: ELB can offload the work of encryption and decryption (SSL/TLS termination) from the backend resources. This means that ELB can handle SSL/TLS encryption, freeing up your compute resources to focus on their primary tasks. ELB supports various security features like AWS Web Application Firewall (WAF) integration for protection against common web application attacks.

  7. Ease of Configuration: ELB is easy to set up and configure through the AWS Management Console, AWS CLI, or AWS SDKs. You can adjust settings like health checks, listener configurations, and target groups to suit your application's requirements.

  8. Traffic Routing: ELB supports various routing algorithms, such as round-robin, least connections, and IP hash, allowing you to choose the one that best suits your application's needs.

Amazon Web Services (AWS) offers several types of Elastic Load Balancers (ELBs), each designed for specific use cases and tailored to different requirements, here are the three main types of ELBs:

  1. Application Load Balancer: Amazon Web Services (AWS) offers the Application Load Balancer (ALB) as one of the types of Elastic Load Balancers (ELBs). ALB is designed for distributing incoming application traffic across multiple targets based on the content of the request.

    Application Load Balancer Components:

    1. Load Balancer: The load balancer serves as the single entry point for clients. It distributes incoming application traffic to multiple targets, such as EC2 instances, across multiple Availability Zones (AZs) to increase the availability and fault tolerance of your application.

    2. Listener: Listeners are configured on the load balancer to check for incoming connection requests from clients. They use a specified protocol and port. Listener rules determine how the load balancer routes requests to registered targets. Each rule consists of a priority, one or more actions, and one or more conditions. When the conditions of a rule are met, the associated actions are executed. Every listener must have a default rule, and additional rules can be optionally defined.

    3. Target Group: A target group routes requests to one or more registered targets, such as EC2 instances, using a specified protocol and port number. You can register a target with multiple target groups, and you can configure health checks for each target group. Health checks ensure that the load balancer sends requests only to healthy targets.

Key Features of Application Load Balancer:

  1. Layer 7 Load Balancing: ALB operates at the application layer (Layer 7) of the OSI model, making it suitable for handling HTTP and HTTPS traffic. It can inspect the content of incoming requests to make intelligent routing decisions.

  2. Content-Based Routing: ALB allows you to create listener rules that route requests based on the content of the application traffic. This enables you to structure your application as smaller services and route requests to the correct service based on factors like URL paths, host headers, HTTP header conditions, query parameters, and source IP addresses.

  3. Target Group Level Routing Algorithms: You can configure routing algorithms at the target group level, providing flexibility in how requests are distributed among targets. The default routing algorithm is round robin, and there is an option to specify the least outstanding requests routing algorithm.

  4. Elastic Scaling: ALB can automatically scale its capacity in response to changes in incoming traffic, ensuring that your application can handle varying workloads efficiently.

  5. Health Checks: You can configure health checks for each target group to monitor the health of registered targets. ALB sends requests only to healthy targets, improving the reliability of your application.

  6. Support for Modern Applications: ALB offers features such as support for routing requests to multiple applications on a single EC2 instance, redirection of requests from one URL to another, returning custom HTTP responses, and even registering Lambda functions as targets.

  7. Advanced Load Balancing Features: ALB supports features like IP address-based target registration (including targets outside the VPC), user authentication through corporate or social identities, and integration with containerized applications using Amazon Elastic Container Service (ECS).

  8. Access Logs and Performance: ALB provides access logs with additional information stored in compressed format, helping with debugging and analysis. It is also optimized for improved load balancer performance.

  1. Network Load Balancer: The Network Load Balancer (NLB) is one of the types of Elastic Load Balancers (ELBs) offered by Amazon Web Services (AWS). It is designed to efficiently distribute incoming network traffic, such as TCP and UDP, across multiple targets, enhancing the availability and scalability of your applications.

    Network Load Balancer Components:

    1. Load Balancer: The load balancer acts as the single entry point for clients and is responsible for distributing incoming network traffic across multiple target resources. These targets can be Amazon EC2 instances, IP addresses, or other resources. Distributing traffic in this way increases the availability and fault tolerance of your application.

    2. Listener: Listeners are configured on the load balancer to check for incoming connection requests from clients. They use a specific protocol and port for this purpose. When a connection request is received, the listener forwards the request to a target group.

    3. Target Group: A target group is responsible for routing requests from the listener to one or more registered targets. These targets can include EC2 instances or IP addresses. Target groups support various protocols, including TCP, UDP, TCP_UDP, and TLS, allowing you to handle a wide range of network traffic types. Additionally, you can register a target with multiple target groups and configure health checks for each target group.

Key Features of Network Load Balancer:

  1. Layer 4 Load Balancing: NLB operates at the transport layer (Layer 4) of the Open Systems Interconnection (OSI) model, making it suitable for handling a high volume of TCP and UDP traffic. It does not inspect the content of packets but efficiently routes traffic based on network-level information.

  2. High Performance: NLB is designed to handle millions of requests per second, making it ideal for applications that require extreme performance, low latency, and high throughput. It can efficiently distribute network traffic across multiple targets.

  3. Cross-Zone Load Balancing: By default, each load balancer node distributes traffic only to registered targets in its own Availability Zone (AZ). However, you can enable cross-zone load balancing, allowing each node to distribute traffic across registered targets in all enabled AZs. This increases fault tolerance and availability.

  4. Routing Algorithm: NLB uses a flow hash algorithm to select a target for incoming traffic. For TCP traffic, it takes into account various factors such as the protocol, source IP address, source port, destination IP address, destination port, and TCP sequence number. For UDP traffic, it considers the protocol, source IP address, source port, destination IP address, and destination port.

  5. Elastic Scaling: NLB can automatically scale its capacity to accommodate changes in incoming traffic. This ensures that your application remains responsive even as traffic volume fluctuates over time.

  6. Static IP Addresses: NLB provides support for static IP addresses for the load balancer itself, enhancing predictability and simplifying DNS configuration. You can also associate one Elastic IP address per subnet for Internet-facing load balancers.

  7. Flexibility in Target Registration: You can register targets by IP address, which allows for greater flexibility, including the ability to register targets outside the VPC where the load balancer resides.

  8. Containerized Applications: NLB integrates seamlessly with containerized applications deployed using Amazon Elastic Container Service (ECS). ECS can dynamically assign unused ports to tasks and register them with target groups, making efficient use of container clusters.

  9. Independent Health Checks: Health checks are configured at the target group level, enabling you to monitor the health of individual services or components independently. This granularity allows you to scale services dynamically based on demand.

  1. Classic Load Balancer: The Classic Load Balancer, also known as the Elastic Load Balancer (ELB) Classic Load Balancer, is one of the load balancing options provided by Amazon Web Services (AWS). It helps distribute incoming application traffic across multiple EC2 instances in multiple Availability Zones (AZs), enhancing the fault tolerance and availability of your applications.

    Classic Load Balancer Overview:

    1. Load Balancer Role: The Classic Load Balancer acts as the central point of contact for clients, distributing incoming application traffic across multiple EC2 instances in one or more Availability Zones. This distribution helps improve the fault tolerance and availability of your applications.

    2. Health Monitoring: The Classic Load Balancer continuously monitors the health of its registered EC2 instances. It uses health checks to detect and identify unhealthy instances, ensuring that incoming traffic is directed only to healthy instances. This helps maintain the reliability and performance of your application.

    3. Scalability: Elastic Load Balancing scales the Classic Load Balancer automatically in response to changes in incoming traffic. As your application's traffic load fluctuates, the load balancer can adjust its capacity to handle the vast majority of workloads without manual intervention.

    4. Listeners: You configure one or more listeners on the load balancer, specifying the protocol and port that clients use to connect. Listeners check for incoming connection requests from clients and forward those requests to one or more registered EC2 instances using a protocol and port number that you configure. This allows the load balancer to distribute traffic effectively to the appropriate backend instances.

    5. Health Checks: Health checks are essential for monitoring the health of registered instances. The load balancer uses health checks to determine the status of each instance. Only instances that pass health checks are considered healthy and receive incoming traffic. This helps ensure that your application remains highly available and responsive.

    6. AZ Distribution: To maintain balanced traffic distribution and fault tolerance, it's crucial to have approximately the same number of instances registered in each Availability Zone. The Classic Load Balancer evenly distributes requests across the enabled Availability Zones by default. To distribute traffic evenly across all instances in all enabled AZs, you can enable cross-zone load balancing. However, AWS recommends maintaining roughly equivalent numbers of instances in each AZ for better fault tolerance.

Benefits of Classic Load Balancer:

Using the Classic Load Balancer offers certain benefits, including:

  1. Support for TCP and SSL Listeners: Classic Load Balancers provide support for both TCP and SSL listeners, allowing you to handle a variety of application traffic types securely.

  2. Sticky Sessions: Classic Load Balancers support sticky sessions using application-generated cookies. This feature enables clients to maintain their session affinity with specific backend instances, ensuring consistent user experiences for applications that require session persistence.

  3. It is not much used nowadays and will be deprecated soon.

Task: Creating ALB and Configuring it

Step1 Creating a Target Group

  1. Search for EC2 in the search bar of AWS and navigate to it. Now on the left list scroll down to the Load Balancer and click on the Target Group and hit Create as shown:

  2. Give it a Name and rest leave the default for now

  3. Click on Create Target Group

Step 2 Creating Load Balancer

  1. Search for EC2 in the search bar of AWS and navigate to it. Now on the left list scroll down to the Load Balancer as shown:

  2. Click on the Create load balancer and hit create on Application Load Balancer

  3. Give it a Name in the basic configuration

  4. In the Network Mapping associate the e availability zones 1a, 1b, 1c

  5. Select the Security Group to be the default one and select the Target Group in the Listeners as shown

  6. Now click on Create load balancer and view the load balancer.

Step 3 Create an Auto Scaling Group

  1. Navigate to the Auto Scaling Group under EC2 and click on create, give it a name and select the launch template, we have already created it in the previous blog.

  2. In the Network, Select the VPN we used a default one as specified in LB and just choose 3 AZ i.e 1a, 1b ,1c which matches the one we chosen in the previous step while creating ALB

  3. Now attach the auto scaling group to the load balancer already created and select the target group as well

  4. Enable the health Checks

  5. Configuring the Group Size and Setting the Scaling Policies, would ensure that a new instance spins up automatically when the load increases.

  6. Choose a Notification when Instance related event happens, and you will be notified. Click on Create and enter the SNS topic name and your email address to get a notification email.

  7. Review and hit create, after creation, you will get an email to subscribe to the SNS Topic, just subscribe to get the email notifications

Step 3 Validate and Test ALB

  1. Now navigate to instances in the EC2 and you will see 2 instances are already spinned up because we have mentioned in the ASG that our desired choice is 2.

  2. Now let's try to access the publicip:8080 of any instance, as Jenkins is running on it.

  3. Now let's burden the CPU by installing stress on the ec2 instances. Use sudo apt install stress -y and sudo stress --cpu 5 --timeout 10 and check the CPU utilization. Magic happened another EC2 spins up

  4. Check the CPU utilization it is very high for the first instance represented by orange followed by the other 2, we will see a maximum number of instances which is 4 will be coming up in a few minutes.

  5. Here we go another instance was created

  6. Let's see if we got an email notification, yay we got that as well

Make sure to Delete every resource after you are done with the lab. First, delete the ALB, ASG and Target Group, when you delete the ASG the instances will automatically get deleted.