Using Shared Load Balancers — Advanced Level

Scenarios

You have two web applications deployed on two ECSs separately, and the web applications provide one domain name but different URLs for users to access.

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

An HTTP listener is used as an example to describe how to route requests from two URLs (/ELB01 and /ELB02) of the same domain name (www.example.com) to different backend servers.

Prerequisites

Creating ECSs

ECSs are used as backend servers.

Each ECS needs an EIP to allow you to deploy the backend service on each ECS. In actual use, you can unbind the EIP from each ECS if the ECSs do not need to access the Internet or provide Internet-accessible services after the deployment is complete. Determine whether you need an EIP for your load balancer by referring to Load Balancing on a Public or Private Network.

  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. Hover on in the upper left corner to display Service List and choose Computing > Elastic Cloud Server.
  4. Click Create ECS, configure the parameters, and click Create Now.

    The following table lists the specifications of the two ECSs.
    Table 1 ECS specifications

    Item

    Example Value

    Name

    ECS01 and ECS02

    OS

    CentOS 7.2 64bit

    vCPUs

    2

    Memory

    4 GB

    System disk

    40 GB

    Data disk

    100 GB

    Bandwidth

    5 Mbit/s

  5. Submit your request.

Deploying the Application

Deploy Nginx on the two ECSs and edit two HTML pages for the web applications 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.
    1. Install Nginx:

      yum -y install nginx

    2. Start Nginx:

      systemctl start nginx.service

    3. Enter http://EIP bound to the ECS in the address box of your browser.If the following page is displayed, Nginx has been installed.Figure 1 Nginx installed successfully
  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.
    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. 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 to be as follows:
       ...
          <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 editing mode. Then, enter :wq to save the settings and exit the file.
  4. Modify the HTML page of ECS02.Move the index.html file from the default root directory of Nginx /usr/share/nginx/html to the ELB02 directory and modify the file to identify access to ECS02.
    1. Create the ELB02 directory and copy the index.html file to this directory:

      mkdir /usr/share/nginx/html/ELB02

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

    2. 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 to be as follows:
      ...
          <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>
              </div>
          </body>
    5. Press Esc to exit editing mode. Then, enter :wq to save the settings and exit the file.
  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.

Creating a Load Balancer

  1. In the upper left corner of the page, click and select the desired region and project.
  2. Hover on in the upper left corner to display Service List and choose Network > Elastic Load Balancing.
  3. Click Create Elastic Load Balancer and then configure the parameters.
  4. Click Create Now.
  5. Confirm the configuration and submit your request.
  6. View the newly created load balancer in the load balancer list.

Adding 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 the created backend server group.

Configure two forwarding policies to forward HTTP requests to the two ECSs, for example, requests from www.example.com/ELB01/ to ECS01, and those from www.example.com/ELB02/ to ECS02.Figure 4 Traffic forwarding
  1. Hover on in the upper left corner to display Service List and choose Network > Elastic Load Balancing.
  2. Locate the created load balancer and click its name.
  3. Under Listeners, click Add Listener.
  4. Configure the listener and click Next.
  5. Create a backend server group, configure a health check, and click Finish.

Adding Forwarding Policies

  1. Click the name of the newly added listener and then click Add next to Forwarding Policies.
  2. Configure the forwarding policy and click Next.
  3. Add the backend server group and configure a health check.
  4. Select the newly added forwarding policy. On the Backend Server Groups tab page on the right, click Add.
  5. Select the server you want to add, set the backend port, and click Finish.
  6. Repeat 1 to 5 to add another forwarding policy, create a backend server group, and add ECS02 to the backend server group. Configure the parameters.

Verifying 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 load balancer EIP.View the load balancer EIP on the basic information page of the load balancer.Figure 5 hosts file on your PC
  2. On the CLI of your PC, 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.

  3. 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 6 Accessing ECS01

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

  4. 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 7 Accessing ECS02
Parent topic: Getting Started