Help Center/ Elastic Load Balance/ Getting Started/ Using ELB to Distribute Traffic to Multiple Web Applications Across ECSs
Updated on 2024-09-05 GMT+08:00

Using ELB to Distribute Traffic to Multiple Web Applications Across ECSs

Scenarios

To forward requests based on domain names and paths, you need to create a load balancer, add an HTTP or HTTPS listener, and add forwarding policies to specify the domain names and paths.

If you have two web applications that are deployed on two ECSs (ECS01 and ECS02 in this example) but use the same domain name for access, you can set different paths to process requests.

Figure 1 Using ELB to distribute traffic to two applications

Procedure

You can follow the process in Figure 2 to use a load balancer to route requests to two web applications.

Figure 2 Routing requests to two web applications deployed in separated ECSs

Procedure

What to Do

Preparations

Before using cloud services, sign up for a HUAWEI ID, enable Huawei Cloud services, complete real-name authentication, and top up your account.

Step 1: Create a VPC and Two ECSs

  • Create a VPC with an IPv4 CIDR block and create a subnet in the VPC.
    • VPC IPv4 CIDR block: 192.168.0.0/16
    • Subnet IPv4 CIDR block: 192.168.0.0/24
  • Buy two ECSs in the VPC subnet you have created.

Step 2: Deploy the Applications

Deploy Nginx on the two ECSs.

Step 3: Create a Load Balancer

Create a load balancer with elastic specifications to receive requests from clients and distribute the requests to backend servers.

Step 4: Configure Security Group Rules

Configure security group rules to allow traffic from the backend subnet where the load balancer works to the backend servers.

Step 5: Add a Listener

Add an HTTP listener to the load balancer to check requests from clients and route requests to backend servers in the backend server group.

Step 6: Add Two Forwarding Policies

Configure two forwarding policies for the HTTP listener to enable the listener to forward requests to different backend server groups based on the configured domain name and URL.

Step 7: Verify Load Balancing

Access the domain name of the load balancer to check whether different backend servers can be accessed.

Step 1: Create a VPC and Two ECSs

You need to plan the region for your load balancer, and create a VPC and two ECSs. Ensure that the ECSs and load balancer are in the same AZ and VPC.

  1. Create a VPC.

    Configure the VPC as shown in the below figure. For details, see Creating a VPC.

    Figure 3 Configuring a VPC
    Figure 4 Configuring a VPC subnet
  2. Create two ECSs.

    Configure the ECSs as described in the below table. For details, see Quickly Creating an ECS.

Step 2: Deploy the Applications

Deploy Nginx on the two ECSs and edit two HTML pages so that a page with message "Welcome to ELB test page one!" is returned when ECS01 is accessed, and the other page with message "Welcome to ELB test page two!" is returned when ECS02 is accessed.

  1. Log in to the ECSs.
  2. Install and start Nginx.

    CentOS 7.6 is used as an example here.

  3. Modify the HTML page of ECS01.

    Move the index.html file from the default root directory of Nginx /usr/share/nginx/html to the ELB01 directory and modify the file to identify access to ECS01.

    Modifying the HTML Page of ECS01

    1. Create the ELB01 directory and copy the index.html file to this directory:
      mkdir /usr/share/nginx/html/ELB01
      cp /usr/share/nginx/html/index.html /usr/share/nginx/html/ELB01/
    2. Run the following command to open the index.html file:
      vim /usr/share/nginx/html/ELB01/index.html
    3. Press i to enter editing mode.
    4. Modify the index.html file.
      Modify the following content:
       ...
          <body>
              <h1>Welcome to <strong>ELB</strong> test page one!</h1>
      
              <div class="content">
                  <p>This page is used to test the <strong>ELB</strong>!</p>
      
                  <div class="alert">
                      <h2>ELB01</h2>
                      <div class="content">
                          <p><strong>ELB test (page one)!</strong></p>
                          <p><strong>ELB test (page one)!</strong></p>
                          <p><strong>ELB test (page one)!</strong></p>
                      </div>
                  </div>
              </div>
          </body>
    5. Press Esc to exit the editing mode. Then, enter :wq to save the settings and exit the file.
  4. Modify the HTML page of ECS02 by referring to step 3 to identify the access to ECS02.
  5. Use your browser to access http://ECS01 EIP/ELB01/ and http://ECS02 EIP/ELB02/ to verify that Nginx has been deployed.

    If the modified HTML pages are displayed, Nginx has been deployed.

    • HTML page of ECS01
      Figure 6 Nginx successfully deployed on ECS01
    • HTML page of ECS02
      Figure 7 Nginx successfully deployed on ECS02

Step 3: Create a Load Balancer

The load balancer needs an EIP to access the application deployed on the ECSs over the Internet. You can determine whether to bind an EIP to the load balancer based on your requirements.

  1. Go to the Buy Elastic Load Balancer page.
  2. On the displayed page, set the parameters as required.
    Figure 8 lists the basic parameters in this example.
    Figure 8 Configuring the basic information
  3. Set Specifications to Elastic, as shown in Figure 9.
    Figure 9 Load balancer specifications
  4. Configure the network parameters and EIP information based on Figure 10.
    Figure 10 Network parameters
  5. Specifies the maximum bandwidth. Click to expand the advanced settings and add a description and tags to the load balancer.
  6. Click Next.
  7. Confirm the configuration and submit your request.
  8. View the newly created load balancer in the load balancer list.

Step 4: Configure Security Group Rules

To ensure normal communications between the load balancer and backend servers, you need to check the security group and network ACL rules.

  • The security groups configured for backend servers must have inbound rules to allow health check and service traffic from backend subnet of the load balancer to backend servers. By default, the backend subnet of a load balancer is the same as the subnet where the load balancer works.
  • Network ACL rules are optional for subnets. If network ACL rules are configured for the backend subnet of the load balancer, the inbound rules must allow traffic from the backend subnet to the backend servers.

For details about how to configure security group and network ACL rules, see Security Group and Network ACL Rules

You can configure security group rules based on Table 1.

Table 1 Security group rules

Direction

Action

Type

Protocol & Port

Source/Destination

Description

Inbound

Allow

IPv4

TCP: 80

Source: 192.168.0.0/24

Allows outbound traffic to ECSs in the security group.

Outbound

Allow

IPv4

All

Destination: 0.0.0.0/0

Allows the ECSs in the security group to access the Internet.

Step 5: Add a Listener

Add a listener to the created load balancer. When you add the listener, create a backend server group, configure a health check, and add the two ECSs to this backend server group. If a backend server is detected unhealthy, the load balancer will stop routing traffic to it until the backend server recovers.

  1. On the load balancer list page, locate load balancer elb-test and click its name.
  2. On the Listeners tab, click Add Listener and configure parameters based on Table 2.
    Table 2 Parameters for configuring a listener

    Parameter

    Example Value

    Description

    Name

    listener

    Specifies the listener name.

    Frontend Protocol

    HTTP

    Specifies the protocol that will be used by the load balancer to receive requests from clients.

    Frontend Port

    80

    Specifies the port that will be used by the load balancer to receive requests from clients.

    Redirect

    -

    Specifies whether to redirect requests from the HTTP listener to an HTTPS listener.

    Disable it in this example.

    Access Control

    All IP addresses

    Specifies how access to the listener is controlled. For details, see What Is Access Control?

    Transfer Client IP Address

    -

    Specifies whether to transmit IP addresses of the clients to backend servers.

    This feature is enabled for dedicated load balancers by default and cannot be disabled.

    Advanced Forwarding

    -

    Specifies whether to enable the advanced forwarding policy. Once it is enabled, more forwarding rules and actions are supported.

    Enable it in this example.

  3. Retain the default values for Advanced Settings. Click Next: Configure Request Routing Policy. On the Configure Routing Policy page, select Create new for Backend Server Group.

    Set Load Balancing Algorithm to Weighted round robin and retain the default values for other parameters.

  4. Click Next: Add Backend Server.
    • Backend servers: Click Add Backend Server and select ECS01 and ECS02 from the server list.
    • Backend ports: Set them to 80. ECS01 and ECS02 will use this port to communicate with the load balancer.
  5. Configure health check parameters. In this example, retain the default settings.
  6. Click Next: Confirm, confirm the settings, and click Submit.

Step 6: Add Two Forwarding Policies

The following describes how to configure forwarding policies to forward HTTP requests to the two ECSs, for example, requests from www.example.com/ELB01/ to ECS01 and www.example.com/ELB02/ to ECS02.

  1. On the listener list page, locate the listener you have added in the previous step and click Add/Edit Forwarding Policy in the Forwarding Policies column. In the displayed page, click Add Forwarding Policy.
    Configure the forwarding policy as shown in Figure 11. For details about the parameters, see Table 3.
    Figure 11 Configuring a forwarding policy
    Table 3 An example forwarding policy

    Forwarding Policy Item

    Setting

    Name

    Enter a forwarding policy name, for example, forwarding_policy-ELB01.

    Forwarding rule

    Domain name: Enter a domain name that will be used to forward the requests, for example, www.example.com. The domain name in the request must exactly match what is specified in the forwarding policy.

    Path: Specify a path to forward the requests, for example, /ELB01/.

    Action

    Select Forward to a backend server group.

  2. Create a backend server group for forwarding policy forwarding_policy-ELB01.
    1. Select Create Backend Server Group from the drop-down list to the right of Forward to a backend server group.
    2. In the Configure Routing Policy step, set the backend server group name to server_group-ELB01.

      Use default settings for other parameters.

    3. Click Next. In the Add Backend Server step, click Add Backend Server.
  3. On the displayed page, select cloud server ECS01, set the backend port to 80, and click OK.
  4. Repeat 1 to 3 to add another forwarding policy, create a backend server group, and add ECS02 to the backend server group.

Step 7: Verify Load Balancing

After the load balancer is configured, you can access the domain name or the specified URL to check whether the two ECSs are accessible.

  1. Modify the C:\Windows\System32\drivers\etc\hosts file on your PC to map the domain name to the EIP bound to the load balancer .
    View the EIP on the Summary page of the load balancer.
    Figure 12 hosts file on your PC
  2. Choose Start and enter cmd to open the CLI.
  3. Run the following command to check whether the domain name is mapped to the load balancer EIP:
    ping www.example.com

    If data packets are returned, the domain name has been mapped to the load balancer EIP.

  4. Use your browser to access http://www.example.com/ELB01/.
    If the following page is displayed, the load balancer has routed the request to ECS01.
    Figure 13 Accessing ECS01

    ELB01/ indicates that the default directory named ECS01 is accessed, while ELB01 indicates the file name. This means the slash (/) following ELB01 must be retained.

  5. Use your browser to access http://www.example.com/ELB02/.
    If the following page is displayed, the load balancer has routed the request to ECS02.
    Figure 14 Accessing ECS02