Help Center > > Getting Started> Advanced Level

Advanced Level

Updated at: May 10, 2019 17:46

Scenarios

Suppose that you have two web applications, each of which is deployed on an ECS, and that the two applications use a domain name (www.example.com). You can set two URLs (/ELB01 and /ELB02) to determine which application is to be accessed.

You can add forwarding policies to load balancers to specify the domain names or URLs for forwarding requests to target backend server groups. Currently, you can add forwarding policies only to HTTP or HTTPS listeners.

This section describes how to add forwarding policies to an HTTP listener to route requests from different URLs of the same domain name to the target backend server groups.

Prerequisites

  • The required ports are open in the security group of backend servers. (Alternatively, enable all ports first and then block the ports that are not used after service deployment.)
  • To ensure normal health checks, the security group of backend servers must allow access from the 100.125.0.0/16 network segment.
  • You have purchased two EIPs and bound them to the two ECSs respectively.

Create ECSs

In this section, ECSs are used as backend servers. Perform the following operations to create two ECSs to process the requests forwarded by the load balancers:

  1. Log in to the management console.
  2. In the upper left corner of the page, click and select the desired region and project.
  3. Click Service List. Under Computing, choose Elastic Cloud Server.
  4. Click Buy ECS. On the displayed page, set the parameters and click Next.
  5. The following table lists the specifications of two ECSs.
    Table 1 ECS specifications

    Item

    Value

    Name

    ECS01 and ECS02

    OS

    CentOS 7.2 64bit

    CPU

    2 vCPUs

    Memory

    4 GB

    System disk

    40 GB

    Data disk

    100 GB

    Bandwidth

    5 Mbit/s

  6. Click Submit.

Set Up Backend Services

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 a page with message "Welcome to ELB test page two!" is returned when ECS02 is accessed.

  1. Log in to the ECSs.
  2. Install Nginx.
    1. Run the following command to install Nginx:

      yum -y install nginx

    2. Run the following command to start Nginx:

      systemctl start nginx.service

    3. Enter http://ECS IP address in a browser address bar. If the following page is displayed, Nginx has been installed.
      Figure 1 Nginx installed successfully
  3. Modify the HTML page of ECS01.
    The default root directory of Nginx is /usr/share/nginx/html. Move and modify the index.html page to identify access to ECS01.
    1. Run the following commands in sequence to create the ELB01 directory and copy the index.html file to the ELB01 directory:

      mkdir /usr/share/nginx/html/ELB01

      cp /usr/share/nginx/html/index.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.
      The following displays part of the file 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>
          </body>
    5. Press Esc to exit editing mode. Then, enter :wq to save the settings and exit the file.
  4. Modify the HTML page of ECS02.
    The default root directory of Nginx is /usr/share/nginx/html. Move and modify the index.html page to identify access to ECS02.
    1. Run the following commands in sequence to create the ELB02 directory and copy the index.html file to the ELB02 directory:

      mkdir /usr/share/nginx/html/ELB02

      cp /usr/share/nginx/html/index.html ELB02

    2. Run the following command to open the index.html file:

      vim /usr/share/nginx/html/ELB02/index.html

    3. Press i to enter editing mode.
    4. Modify the index.html file.

      The following displays part of the file content:

      ...
          <body>
              <h1>Welcome to <strong>ELB</strong> test page two!</h1>
      
              <div class="content">
                  <p>This page is used to test the <strong>ELB</strong>!</p>
      
                  <div class="alert">
                      <h2>ELB02</h2>
                      <div class="content">
                          <p><strong>ELB test (page two)!</strong></p>
                          <p><strong>ELB test (page two)!</strong></p>
      		    <p><strong>ELB test (page two)!</strong></p
                      </div>
              </div>
          </body>
    5. Press Esc to exit editing mode. Then, enter :wq to save the settings and exit the file.
  5. Use a browser to access http://ECS01 IP address/ELB01/ and http://ECS02 IP address/ELB02/ to verify the Nginx service.

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

    • HTML page of ECS01
      Figure 2 Nginx successfully deployed on ECS01
    • HTML page of ECS02
      Figure 3 Nginx successfully deployed on ECS02

Create a Load Balancer

  1. Log in to the management console.
  2. In the upper left corner of the page, click and select the desired region and project.
  3. Click Service List. Under Network, click Elastic Load Balance.
  4. Click Buy Load Balancer. On the displayed page, set the parameters as needed.
  5. Click Next.
  6. Confirm the configuration and click Submit.
  7. View the newly created load balancer in the load balancer list.

Add a Listener

A listener checks for requests from clients, using the protocol and port that you configure, and forwards requests to one or more backend server groups, based on the rules that you define. A listener also defines the health check configuration, through which the load balancer automatically checks running statuses of backend servers. If a backend server becomes faulty, the load balancer will stop forwarding the traffic to this server.

To forward HTTP requests to the two ECSs, you need to configure two forwarding policies. Requests from www.example.com/ELB01/ are routed to ECS01, and those from www.example.com/ELB02/ to ECS02.
Figure 4 Traffic forwarding
  1. Log in to the management console.
  2. In the upper left corner of the page, click and select the desired region and project.
  3. Click Service List. Under Network, click Elastic Load Balance.
  4. Locate the created load balancer and click its name (elb-mq01).
  5. Under Listeners, click Add Listener.
  6. Configure the listener and click Next.
    • Name: specifies the listener name, for example, listener-HTTP.
    • Frontend Protocol/Port: specifies the protocol and port number the load balancer uses to receive requests. For example, set it to HTTP and 80.
  7. Configure the backend server group and health check, and click Finish.
    • Backend server group
      • Name: specifies the backend server group name, for example, server_group-ELB.
      • Load Balancing Algorithm: specifies the algorithm the load balancer uses to route requests. Select Weighted round robin.
    • Health check
      • Protocol: specifies the protocol the load balancer uses to perform health checks on backend servers. If the frontend protocol is TCP, HTTP, or HTTPS, the health check protocol can be TCP or HTTP. Set it to HTTP, which cannot be changed after being set.
      • Domain Name: specifies the domain name in the health check request, for example, www.example.com.
      • Port: specifies the port number the load balancer uses to perform health checks on backend servers, for example, 80.

        If no port is specified, the port of each backend server will be used for health checks by default.

Add Forwarding Policies

  1. Locate the newly added listener and click Add next to Forwarding Policies.
  2. After configuring the forwarding policy, click Next.
    • Name: specifies the forwarding policy name, for example, forwarding_policy-ELB01.
    • Domain Name: specifies the domain name for triggering the forwarding, for example, www.example.com. Only exact match is supported.
    • URL: specifies the URL for triggering the forwarding, for example, /ELB01/.
    • URL Matching Rule: specifies the rule for matching the preset URL with the one in the request. Three options are available, Exact match, Prefix match, and Regular expression match. Exact match enjoys the highest priority, and Regular expression match the lowest priority. Select Exact match here.
  3. Add the backend server group and health check, and click Finish.
    • Backend server group
      • Name: specifies the backend server group name, for example, server_group-ELB01.
      • Load Balancing Algorithm: specifies the algorithm the load balancer uses to route requests. Select Weighted round robin.
    • Health check configuration
      • Protocol: specifies the protocol the load balancer uses to perform health checks on backend servers. If the frontend protocol is TCP, HTTP, or HTTPS, the health check protocol can be TCP or HTTP. Set it to HTTP, which cannot be changed after being set.
      • Domain Name: specifies the domain name in the health check request, for example, www.example.com.
      • Port: specifies the port number the load balancer uses to perform health checks on backend servers, for example, 80.

        If no port is specified, the port of each backend server will be used for health checks by default.

  4. Locate the newly added forwarding policy. On Backend Server Groups tab page on the right, click Add.
  5. Set the backend port, select the servers to be added, and click OK.
    • Backend Port: specifies the port number used by the server, for example, 80.
    • Select ECS01 as the backend server.
  6. Repeat 1 to 5 to add another forwarding policy.

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 a local PC to map the domain name to the load balancer EIP.
    Figure 5 hosts file on the local PC
  2. In the CLI of the local PC, run the following command to check whether the domain name is mapped to the load balancer EIP:

    ping www.example.com

    If a data packet is returned, the domain name has been mapped to the load balancer EIP.

  3. Use a browser to access http://www.example.com/ELB01/. If the following page is displayed, the load balancer routes the access request to ECS01.
    Figure 6 Accessing ECS01
    NOTE:

    ELB01/ indicates that the default directory named ECS01 is accessed, while ELB01 indicates the file whose name is ELB01. Therefore, the slash (/) following ELB01 must be reserved.

  4. Use a browser to access http://www.example.com/ELB02/. If the following page is displayed, the load balancer routes the access request is forwarded to ECS02.
    Figure 7 Accessing ECS02

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel