Linux | Cloud | DevOps | Scripting

Breaking

Sunday 7 July 2019

Application Load Balancer (ALB)


A load balancer takes requests from clients and distributes them across targets in a target group.

Types of Elastic Load Balancers:

ELB supports three types of load balancers. We can select the appropriate load balancer based on our application needs.
  1. Classic Load Balancer (CLB)
  2. ALB (Application Load Balancer)
  3. NLB (Network Load Balancer)

Application Load Balancer (ALB): 

Application Load Balancer is used for conditions or rule-based load balancing. ALB only supports HTTP/HTTPS protocols. It can only be used on AWS instance. The ALB distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application. You add one or more listeners to your load balancer. We must have Target Group to create ALB or NLB (Network Load Balancer). 

Application Load Balancer (ALB)
Fig: Application Load Balancer
For more details kindly navigate to the URL:
https://aws.amazon.com/elasticloadbalancing/features/#Details_for_Elastic_Load_Balancing_Products

ALB Practical:

In this practical, we are going to maintain load balancing between multiple instances on condition/rule basis. Let's take an example of a website which contains photos and videos also. So, on one instance we will keep details of the main node. In the second instance, we can keep images and in the third instance, we can keep videos.

For a better architect, we should have at least two instances for main page. So that in case if there is an issue with one instance, we can get access from another one. Similarly, we must have multiple instances for images and videos also.

But here, we will create three instances: one instance for the main page, one for images folder and another for videos folder and then we will use ALB to provide conditions.

Steps we need to follow:

1. Make sure your VPC has at least three subnets
2. Create an instance for the Web Server
3. Create one instance for images and another for videos
4. Create Target Groups and register instances in it
5. Create Application Load Balancer
6. Add images and videos as listeners
7. Verify Web Server

Step 1: Make sure your VPC has at least three subnets:

AWS ➔ Services ➔ VPC ➔ Subnets.

Verify if you have three subnets otherwise create them by following the link:
https://redhatpanacia.blogspot.com/2019/05/vpc.html

Step 2: Create an instance for the Web Server:

AWS ➔ Services ➔ EC2 ➔ Instances ➔ Launch Instance ➔ [*] Free Tier Only ➔ Select any Linux AMI ➔ Select ➔ Instance Type: 't2.micro' ➔ Configure Instance: Number of Instances: 1 ➔ Select VPC ➔ Select Public Subnet (I'm selecting us-east-1a) ➔ Auto-assign Public IP: Enable ➔ Advanced Details ➔ User Data: As text ➔ provide a script:

          #!/bin/bash
          yum install -y httpd
          echo "*** This is the main page of Webshack.com ***" >> /var/www/html/index.html
          service httpd restart
          systemctl enable httpd

Next: Storage ➔ Next Tags: 'Click to add a Name Tag' ➔ Key: Name & Value: main_node_1 ➔ Next: Security Group ➔ Assign a security group: (*) Create a new security group OR ( ) Select an existing security group (but port for SSH (22) and HTTP (80) should be enabled ➔ Review and Launch ➔ Launch ➔ Create or provide an existing key-pair➔ Launch Instances.

Now, if we use public IP of this instance in the browser, we will get the output:

*** This is the main page of Webshack.com ***

Step 3: Create one instance for images and another for videos:

Use the same way to create both instances but do change in both instances' script.

For images use Script:
          #!/bin/bash
          yum install -y httpd
          mkdir /var/www/html/images
          echo "*** This is Webshack Images page ***" >> /var/www/html/images/index.html
          service httpd restart
          systemctl enable httpd

For Videos instance use script:
          #!/bin/bash
          yum install -y httpd
          mkdir /var/www/html/videos
          echo "*** This is Webshack Videos page ***" >> /var/www/html/videos/index.html
          service httpd restart
          systemctl enable httpd

Step 4: Create Target Groups and register instances in it:

For the main page instance:

AWS ➔ Services ➔ EC2 ➔ Target Groups ➔ Create Target Group ➔ Target group name: main ➔ Target type: instance ➔ protocol: HTTP ➔ Port: 80 ➔ VPC: webshack-vpc (select your VPC) ➔ Health Check Settings (leave default) ➔ Create ➔ Close.

Register Instance in it:

Select target group ➔ Edit ➔ click on 'main_node_1' ➔ Add to registered ➔ Save.

Similarly, do the same for images instance:

AWS ➔ Services ➔ EC2 ➔ Target Groups ➔ Create Target Group ➔ Target group name: images ➔ Target type: instance ➔ protocol: HTTP ➔ Port: 80 ➔ VPC: webshack-vpc (select your VPC) ➔ Health Check Settings (leave default) ➔ Create ➔ Close.

Register Instance in it:

Select target group ➔ Edit ➔ click on 'images_node_1' ➔ Add to registered ➔ Save.

For Videos instance:

AWS ➔ Services ➔ EC2 ➔ Target Groups ➔ Create Target Group ➔ Target group name: videos ➔ Target type: instance ➔ protocol: HTTP ➔ Port: 80 ➔ VPC: webshack-vpc (select your VPC) ➔ Health Check Settings (leave default) ➔ Create ➔ Close.

Register Instance in it:

Select target group ➔ Edit ➔ click on 'videos_node_1' ➔ Add to registered ➔ Save.

Note: We can see, all of these are still in 'unused' status.

Step 5: Create Application Load Balancer:

AWS ➔ Services ➔ EC2 ➔ Load Balancers ➔ Create Load Balancer ➔ Application Load Balancer: Create ➔ Name: webshack-alb-1 ➔ Scheme: internet-facing ➔ Listeners: HTTP/80 ➔ Availability Zones: (select all public AZ) ➔ Next ➔ Configure Security Groups (create or select any existing SG but port number 22 (SSH) and 80 (HTTP) should be enable) ➔ Next: Create Routing ➔ Target Group: Existing Target Group ➔ Name: main ➔ target Type: Instance ➔ Protocol: HTTP ➔ Port: 80 ➔ Next ➔ Review ➔ Create ➔ Close.

As a result, this will get a URL to access the site but it takes some time to get resolve the name.

DNS URL
Fig: DNS URL
We can find by which IP it is being used. For this use command:

C:\> nslookup <provided URL>

Found IP using 'nslookup' command
Fig: Found IP using 'nslookup'
Now use this IP in the browser, this will provide main page of Webshack.

Step 6: Add images and videos as listeners:

AWS ➔ Services ➔ EC2 ➔ Load Balancers ➔ Select your created Load Balancer ➔ Listeners ➔ View/edit rules ➔ Click on (+) icon besides Rules…

Add Listener
Fig: Add Listener

…Click on (+) Insert Rule ➔ Add Condition ➔ Path ➔ Value: *images* ➔ click on blue tick icon ➔ Forward to: images ➔ click on blue tick icon ➔ Save.

Insert Rule
Fig: Insert Rule

Now add Videos as listeners:

AWS ➔ Services ➔ EC2 ➔ Load Balancers ➔ Select your created Load Balancer ➔ Listeners ➔ View/edit rules ➔ Click on (+) icon besides Rules ➔ Click on (+) Insert Rule ➔ Add Condition ➔ Path ➔ Value: *videos* ➔ click on blue tick icon ➔ Forward to: videos ➔ click on blue tick icon ➔ Save.

Step 7: Verify Server using URL:

On Browser, provide URL and hit enter. This will provide output as:

*** This is the main page of Webshack.com ***

On Browser, provide <URL>/images and hit enter. This will provide output as:

*** This is Webshack Images page ***

On Browser, provide <URL>/videos and hit enter. This will provide output as:

*** This is Webshack Videos page ***


Enjoy!





No comments:

Post a Comment

Pages