How do I set up weighted target groups for my Application Load Balancer?

4 minute read
1

I want to register weighted target groups for my Application Load Balancer.

Resolution

Complete the following steps to register your target group with a load balancer, and add weight to the target group.

Create target groups

1.    Open the Amazon Elastic Compute Cloud (Amazon EC2) console.

2.    Choose the AWS Region that your Amazon EC2 instances are located in.

3.    On the navigation pane, under LOAD BALANCING, choose Target Groups.

4.    Create the first target group:     
Choose Create Target group.
For Target group name, specify a name for the target group.
Configure the protocol, port, and virtual private cloud (VPC) for the target group.
Choose Create.
For Instances, select one or more instances.
Specify a port for the instances.
Choose Add to registered, and then choose Save.

5.    Repeat step 4 to create a second target group.

Create an Application Load Balancer

Note: If you already have an Application Load Balancer, then proceed to the next section.

1.    On the navigation pane, under LOAD BALANCING, choose Load Balancers.

2.    Choose Create Load Balancer.

3.    For Select load balancer type, choose Application Load Balancer.

4.    Choose Continue.

5.    Complete the steps in Create an Application Load Balancer.

6.    Complete the Configure Routing steps:
For Target group, choose Existing Target.
For Name, choose the first target group that you created.
Choose Next: Register Targets.

7.    On the Register Targets page, the instances that you registered with the target group appear under Registered instances. You can't modify the register targets here.

8.    On the Review page, choose Create.

9.    After you receive a notification that your load balancer is created, choose Close.

10.    Select the load balancer.

Configure the Listener rules and add weight to the target groups

1.    On the Listeners tab, choose View/edit rules.

2.    Choose Edit rules (the pencil icon).

3.    Choose Edit next to the Forward to option.

4.    Add the other target group.

5.    Enter the target group weight values. These values must be numeric values between zero and 999.

6.    Select the check mark, and then choose Update.

7.    (Optional) If the target group is sticky, then set the Group level Stickiness. When you configure this setting, routed requests remain in the target group for the duration of the session. The default value is 1 hour. After the session duration ends, requests are distributed according to the weights of the target group.

Note: The Application Load Balancer distributes traffic to the target groups based on weights. If all targets in a target group fail health checks, then the Application Load Balancer doesn't route or fail over the requests to another target group. If a target group has only unhealthy registered targets, then the load balancer nodes route requests across its unhealthy targets. When all the targets in a target group are unhealthy, don't use a weighted target group as a failover mechanism.

For example, if the weight of the first target is 70%, and the second target is 30%, most requests are from the first target group:

$ for X in `seq 6`; do curl -so -i /dev/null -w "" http://FINAL-721458494.us-east-2.elb.amazonaws.com; done

<h1> This is T1 </h1>
<h1> This is T1 </h1>
<h1> This is T1 </h1>
<h1> This is T1 </h1>
<h1> This is T2 </h1>
<h1> This is T2 </h1>

If you set the weight of the second target as 70% and the first as 30%, then most requests are from the second target group:

$ for X in `seq 7`; do curl -so -i /dev/null -w "" http://FINAL-721458494.us-east-2.elb.amazonaws.com; done

<h1> This is T2 </h1>
<h1> This is T2 </h1>
<h1> This is T2 </h1>
<h1> This is T1 </h1>
<h1> This is T1 </h1>
<h1> This is T2 </h1>
<h1> This is T2 </h1>If all targets in a target group fail health checks, then the Application Load Balancer doesn't automatically route or failover the requests to another target group
AWS OFFICIAL
AWS OFFICIALUpdated a year ago