“Load balancing is a technique in which workload is redistributed in such a way that there is no overuse, underuse, or no use of computing devices. In AWS, a load balancer receives requests and then transfers them to targets defined in a target group. We can create an Application Load balancer either using the AWS management console or AWS CLI. There are several routing options with AWS Application Load Balancer, e.g., Path-Based Routing and Host-Based routing.
In Path-based routing, incoming traffic is routed on the basis of the path of the given URL.”
What Will We Cover?
In this guide, we will see how to create an Application Load balancer with Path-Based routing.
What do You Need to Know?
For this guide, we assume you know about:
- What is a load balancer, and more specifically, what is the purpose of an application load balancer?
- How to set up an Application load balancer?
What Will You Need?
For performing the lab described in this guide, you need a few things:
- Access to your AWS account.
- Knowledge of application load balancer.
- Access to the Internet.
How Path-Based Routing Works?
AWS has four types(at the time of writing this article) of Elastic Load Balancers. These are Classic Load Balancers, Application Load Balancers, Gateway Load Balancers, and Network Load Balancers.
Since we are dealing with ALB, it seems good to say a bit about ALB. ALB, a layer 7 load balancer, uses two or more EC2 instances to distribute the traffic. Elastic Load Balancing adds or releases the servers based on the demand of application usage. The incoming traffic is divided up between multiple targets. These targets, residing in single or multiple Availability Zones, can include instances, containers, or IP addresses.
Further, the routing rules here can be defined on the basis of the content of the request(content-based routing). As mentioned earlier, path-based routing route requests are based on the URL path, e.g., if we specify “/orders,” the requests will be forwarded to servers handling the order requests, and if the path is “/payment,” the requests will be forwarded to servers handling the payment work. In this way, we can segment the traffic and efficiently control the server workloads.
Setting up the Instances
There are certain things you have to do for this lab: Launch two Ubuntu or Amazon Linux or any other two EC2 instances and install an apache web server on them. Customize the index page of each web server; one server displays the message “This is the payment page,” and the other displays “This is the order page.” The index file for these pages are being served from the “payment” and “order” directories inside the “/var/www/html” folder for both the web servers.
Requirement for the Lab
There are certain requirements for this lab that should be completed:
- Two availability zones with at least one serving instance in each of them. We will therefore configure two instances and two AZs, with each AZs containing at least one instance.
- A VPC contains a minimum of one public subnet in each of the above availability zones.
Setting up the Target Group
Starting with Target groups, we need two of these for the above instances. Each instance will be associated with one exclusive target group for this lab. Create one target group and repeat the same steps for the other but with a different instance.
Step 1. Head to the EC2 console and choose “Target Groups”
Step 2. Next, click on the “Create target group” for the next page:
Step 3. For the “Specify group details” page, fill in the details below:
- Choose a target type: Choose the “Instances” option.
- Target group name: Name of the target group to use. (“payment” and “order” in our case.)
- Protocol: HTTP
- Port: 80
- VPC: Choose the VPC used here.
- Protocol version: Stick to the already selected. (HTTP1)
The “Health checks” configuration:
- Health check protocol: HTTP
- Health check path: Stick to the default or change if required(“/payment/index.html” and “/order/index.html” for the web servers here.).
It is recommended not to touch “Advanced health check settings” unnecessarily. Optionally, add tags. Hit “Next” to continue.
Step 4. Now register the EC2 instances with the above target groups. Choose an instance and then hit on the “Include as pending below” button. This instance will now be available in the “Review targets” section. Click on the “Create target group” button.
After completion, the two target groups will now appear as:
Create an Application Load Balancer(ALB)
To create an application load balancer, use the load balancer configuration option from the EC2 console. We have set the following configuration for it:
Load balancer name: linuxhint-lb
Scheme: Internet-facing
IP address type: IPv4
VPC: your VPC
Mapping: For each Availability zone(AZ) containing the EC2 instances, choose the AZ and then select a public subnet for that AZ.
Security groups: Allow port 80.
Listener Protocol: HTTP and Port 80.
Default Action: Select one target group here.
Adding Rules to the Load Balancer
Step 1. After configuring the load balancer, go to the Listeners tab of the target Load Balancer and select the “View/Edit rules” link under the “Rules” column.
Step 2. Now select the “+” icon and then click the “Insert Rule” link to add a new rule. A new block will open to add rules. Click on the “+Add condition” menu and then choose the “Rule type” as Path and enter the path “/order/*” in the text field attached to the “is” label. Save it.
Step 3. Similarly, for the “Then” section, choose the “+Add action” menu and then choose the “Forward to” option and select the target group “order.” Save it.
Do the same steps for another target group named “payment.” Path should now be “/payment/*.” Review the rules before proceeding.
Test Run the Setup
Open a web browser and enter the URL of the load balancer and add it with the path of target groups as:
- Path of Target group “Order”
- Path of Target group “Payment”
Wrapping Up
Congratulations, our lab is complete now, and path-based routing is working nicely, as demonstrated above. What we can do next is to configure Host-Based routing or configure a mix set up of Host-Based and Path-Based routing load balancing.