Help Center> Elastic Load Balance> User Guide> Getting Started> Using Shared Load Balancers — Entry Level

Using Shared Load Balancers — Entry Level

Scenarios

You have a web application deployed on two ECSs, and the application often needs to handle a heavy load.

You can create shared load balancer to distribute traffic evenly across the two ECSs, which not only makes your application more available, but also eliminates SPOFs.

Prerequisites

  • You have added security group rules to allow traffic from the ports used by the two ECSs. (You can enable all ports first and then disable the ports that are not used after service deployment.) For details, see Configuring Security Group Rules.
  • The security group containing the two ECSs allows traffic from 100.125.0.0/16. (ELB uses these IP addresses to perform health checks and route requests to backend servers.)

Creating ECSs

ECSs are used as backend servers.

Each ECS needs an EIP for accessing the Internet. Determine whether you need an EIP for your load balancer based on your requirements by referring to Public or Private Network Load Balancing.

  1. Log in to the management console.
  2. In the upper left corner of the page, click and select the desired region and project.

    For example, select CN North-Beijing1 as the region.

  3. Click Service List. Under Computing, click Elastic Cloud Server.
  4. Click Buy ECS. Set the parameters and click Next.

    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

    Region

    CN North-Beijing1

  5. Submit your request.

Deploying the Application

Deploy Nginx on the two ECSs and edit two HTML pages for the web application 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 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://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.
    The default root directory of Nginx is /usr/share/nginx/html. Modify the index.html file to identify access to ECS01.
    1. Run the following command to open the index.html file:

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

    2. Press i to enter editing mode.
    3. 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>
    4. 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. Modify the index.html file to identify access to ECS02.
    1. Run the following command to open the index.html file:

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

    2. Press i to enter editing mode.
    3. 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>
    4. 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 and http://ECS02 EIP to verify that Nginx has been deployed.

    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

Creating a Load Balancer

  1. In the upper left corner of the page, click and select the desired region and project.

    Select CN-North-Beijing1 because the load balancer must be in the same region as the two ECSs.

  2. Click Service List. Under Network, click Elastic Load Balance.
  3. Click Buy Elastic Load Balancer and then set the parameters.
    Figure 4 Buy Elastic Load Balancer
  4. Click Next.
  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.

Figure 5 Traffic forwarding
  1. Click Service List. Under Network, click Elastic Load Balance.
  2. Locate the created load balancer (elb-01) and click its name.
  3. Under Listeners, click Add Listener.
  4. Configure the listener and click Next.
    • Name: Enter a name, for example, listener-HTTP.
    • Frontend Protocol/Port: Select a protocol and enter a port for the load balancer to receive requests. For example, set it to HTTP and 80.
    Figure 6 Configuring a listener
  5. Create a backend server group and configure a health check.
    • Backend server group
      • Name: Enter a name, for example, server_group-ELB.
      • Load Balancing Algorithm: Select an algorithm that the load balancer will use to route requests, for example, Weighted round robin.
    • Health check
      • Protocol: Select a protocol for the load balancer to perform health checks on backend servers. If the load balancer uses TCP, HTTP, or HTTPS to receive requests, the health check protocol can be TCP or HTTP. Set it to HTTP. Note that the protocol cannot be changed after the listener is added.
      • Domain Name: Enter a domain name that will be used for health checks, for example, www.example.com.
      • Port: Enter a port for the load balancer to perform health checks on backend servers, for example, 80.
    Figure 7 Creating a backend server group
    Figure 8 Configuring a health check
  6. Click the name of the newly added listener. On the Backend Server Groups tab page on the right, click Add.
    Figure 9 Adding backend servers
  7. Select the servers you want to add, set the backend port, and click Finish.
    • Backend servers: Select ECS01 and ECS02.
    • Backend port: Set it to 80. Backend servers will use this port to communicate with the load balancer.
    Figure 10 Adding backend servers

Verifying Load Balancing

After the load balancer is configured, you can access the domain name 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 11 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. If the following page is displayed, the load balancer has routed the request to ECS01.
    Figure 12 Accessing ECS01
  4. Use your browser to access http://www.example.com. If the following page is displayed, the load balancer has routed the request to ECS02.
    Figure 13 Accessing ECS02