Debug School

rakesh kumar
rakesh kumar

Posted on

How to distribute incoming traffic across multiple instances using AWS ELB

Define ElB
ELB Types
How to create and configure load balancer
How to launch 2 EC2 instances with an Ubuntu AMI
Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console.

AWS Elastic Load Balancers distribute incoming traffic across multiple instances to ensure your application remains available and responsive. In this article, we’ll delve into ELBs, their architecture, and a hands-on mini project to configure one.

What is an Elastic Load Balancer (ELB)?

An Elastic Load Balancer (ELB) is a managed load balancing service offered by AWS. It automatically distributes incoming application traffic across multiple targets, such as Amazon EC2 instances, in multiple Availability Zones, enhancing the fault tolerance of your applications.

Key Components
Load Balancer The ELB itself, which accepts incoming traffic and routes it to the registered targets.
Target Group A logical grouping of targets, such as EC2 instances, and a protocol and port to route traffic to.
Target Resources, such as EC2 instances or IP addresses, that receive traffic from the load balancer.
Listener A process that checks for connection requests from clients and forwards them to one or more targets.
Availability Zones (AZs) Data centers with redundant power, networking, and cooling.

Types of ELBs

Application Load Balancer (ALB) Best suited for routing HTTP/HTTPS traffic and provides advanced routing features.
Network Load Balancer (NLB) Ideal for handling TCP/UDP traffic, making it suitable for gaming, IoT, and other high-throughput workloads.
Classic Load Balancer (CLB) The legacy load balancer that can distribute traffic across EC2 instances, but with fewer features compared to ALB and NLB.

Configuring an ELB

Configure an Elastic Load Balancer (ELB) to distribute traffic to multiple EC2 instances running a web application and monitor the health of the instances.

Architecture Diagram

Image description

Image description

ELB Component Architecture

Image description

Steps
Create EC2 Instances

  • Launch at least two EC2 instances with web server software installed (e.g., Apache, Nginx)
  • Ensure they belong to the same security group allowing traffic on the web server’s port (e.g., port 80 for HTTP)
    .
    Create a Target Group

  • In the AWS Management Console, navigate to the EC2 service.

  • Create a target group, specifying the target type (e.g., instances) and the protocol and port (e.g., HTTP on port 80)
    .
    Register EC2 Instances

Register the EC2 instances you created in the target group.
Create an Application Load Balancer (ALB)

  • In the AWS Management Console, navigate to the EC2 service.
  • Create an Application Load Balancer (ALB) and configure listeners (e.g., HTTP on port 80) . Configure Listener Rules

Define listener rules to route incoming traffic to the target group.
Test the ELB

  • Access the DNS name of your ALB in a web browser. You should see your web application.
  • Verify that traffic is distributed across EC2 instances
    .
    Health Checks

  • Set up health checks in the target group to monitor the health of your EC2 instances.

  • The ELB will automatically route traffic to healthy instances
    .
    Monitoring

  • Utilize AWS CloudWatch to monitor the performance and health of your ELB and EC2 instances.

  • Set up CloudWatch alarms for proactive monitoring
    .
    Scaling (Optional)

Implement Auto Scaling to automatically adjust the number of EC2 instances based on traffic patterns.

How to create and configure load balancer

Load Balancing is the methodology to manage distributed server and client traffic in an efficient manner.
We can understand it with an example like:
Suppose we have launch an instance on AWS in a particular availability zone having a configured apache webserver on it.
If a client wants to connect with webserver, he have to send IP and Port.There might be possibility that this OS can’t accommodate 1000s of clients and due to this client won’t get proper connectivity.
Or there may be chances that due to any disaster or natural calamity that availability zone goes down and due to this clients face issue in connectivity.

So to resolve thes issue we can launch same server/instance in multiple availability zone.
Merits of launching same server in multiple AZ’s:

  1. Disaster Recovery.
  2. Load balancing.

Image description

ELB i.e. Elastic Load Balancer is the AWS service which maintains,setup and configure load balancer by managing sessions of requests made by client and sends these request to the server/ec2 instances.
As its name claims ‘elastic’ means — as requirement comes up it will increase or decrease the resources.
ELB is same like Service on Kubernetes.

ELB supports three types of load balancer:

  1. Classic Load Balancer
  2. Application Load Balancer
  3. Network Load Balancer

Creating an ELB:
To create a load balancer, first we need to launch instances and configure web server on that.

Launching an Instance: Using Amazon Linux V-2 image:

Image description

While configuring instance details- choose a subnet and disable public Ip because when two programs running inside same VPC they don’t require public IP, they can connect with private also.

Image description

Cloud-Init: Almost all cloud images contain a program which run at a time of booting that program is known as cloud Init.
This program has the capability to go behind the managed services which are consist of metadata to check whether a user given a command or script or not to run or download at the time of booting.

We can do this while launching instance while configuring instance details by User Data feature:

Image description

#!/bin/bash
yum update -y
yum install httpd php -y
systemctl start httpd.service
systemctl enable httpd.service
echo “hiii  $(hostname -f)” > /var/www/html/index.html
echo '<pre> <?php print(`ifconfig`); ?>  </pre>' > /var/www/html/myip.php
Enter fullscreen mode Exit fullscreen mode

As we have to configure apache web server, so we can do this while launching also. Now in security group we can allow only HTTP protocol and proceed without a key pair.

After launching an instance we can check our server by associating a Elastic IP.

Image description

Now we can launch one more instance like this,and only change subnet and tag.

  1. Creating a Load Balancer:

Image description

Choosing Classic Load Balancer.

Step-1: Define Load Balancer: Here we just need to give name and select VPC.

Step-2: Assign Security Group: Creating a new security group. Choosing Custom TCP type as http uses port 80 by default.

Step-3: Configure Security Settings:

Step-4: Configuring Health Check: As load balancer keep on checking backend servers about health check i.e. live or dead.

Image description

Step-5: Add Instances: Adding instances means registering these instances for load balancing.

Image description

Now our load balancer is created with our two registered instances .

Image description

  1. Testing our load balancer.

By writing url of load balancer we can access our server:

Image description

Here it is showing IP of server-1.
To check our load balancer I have stopped my first instance:

Image description

Image description

Now on browser it is showing IP pf second server:

Setting up an Application Load Balancer with AWS EC2

What is Load Balancing?
Load balancing is the distribution of workloads across multiple servers to ensure consistent and optimal resource utilization. It is an essential aspect of any large-scale and scalable computing system, as it helps you to improve the reliability and performance of your applications.

Elastic Load Balancing:
Elastic Load Balancing (ELB) is a service provided by Amazon Web Services (AWS) that automatically distributes incoming traffic across multiple EC2 instances. ELB provides three types of load balancers:

Application Load Balancer (ALB) — operates at layer 7 of the OSI model and is ideal for applications that require advanced routing and microservices.
Network Load Balancer (NLB) — operates at layer 4 of the OSI model and is ideal for applications that require high throughput and low latency.
Classic Load Balancer (CLB) — operates at layer 4 of the OSI model and is ideal for applications that require basic load balancing features.

launch 2 EC2 instances with an Ubuntu AMI and use User Data to install the Apache Web Server

.

Log in to your AWS Console and go to the EC2 dashboard.

Click on the “Launch Instance” button and select “Ubuntu Server “.

Image description

Log in to your AWS Console and go to the EC2 dashboard.

Click on the “Launch Instance” button and select “Ubuntu Server “.

Choose the instance type, configure the instance details, add storage, and configure security groups as required.

Image description

Image description

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

In the “User data” field, enter the following commands to install and start the Apache web server:

Image description

You can see two instances are created.

Image description

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 Awesome :) “.

For apache-server1

Check apache2 status using below command:

Image description

Image description

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.

Image description

For apache-server2:

Check apache2 status.

Go inside /var/www/html path and edit index.html file

Image description

Image description

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.
Image description

Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console

.
Create an Application Load Balancer (ALB) in EC2 using the AWS Management Console

Define ElB
ELB Types
create and configure load balancer
Load balancing task

Top comments (0)