All Products
Search
Document Center

Server Load Balancer:Use an ALB instance to balance loads for IPv4 services

Last Updated:Nov 14, 2024

Application Load Balancer (ALB) supports the HTTP, HTTPS, and Quick UDP Internet Connections (QUIC) protocols and is designed to balance the loads of applications at Layer 7. This topic describes how to create an ALB instance that supports IPv4 to forward requests from IPv4 clients to backend servers.

Prerequisites

  • A virtual private cloud (VPC) is deployed in the China (Shanghai) region. The VPC is named VPC1. A vSwitch (VSW1) is deployed in Zone E and another vSwitch (VSW2) is deployed in Zone G. For more information, see Create a VPC and a vSwitch.

  • An Elastic Compute Service (ECS) instance (ECS01) is deployed in VSW1 and another ECS instance (ECS02) is deployed in VSW2.

    • For more information about how to create an ECS instance, see Create an instance by using the wizard.

    • The following code blocks show how to deploy applications on ECS01 and ECS02:

      Commands for deploying an application on ECS01

      yum install -y nginx
      systemctl start nginx.service
      cd /usr/share/nginx/html/
      echo "Hello World ! This is ECS01." > index.html

      Commands for deploying an application on ECS02

      yum install -y nginx
      systemctl start nginx.service
      cd /usr/share/nginx/html/
      echo "Hello World ! This is ECS02." > index.html
  • A domain name is registered, and an Internet content provider (ICP) number is obtained for the domain name. For more information, see Register a generic domain nameRegister a domain name on Alibaba Cloud and ICP filing process.

The following table describes the configurations of VPC1. The configurations are for reference only.

Click to view VPC configurations

Parameter

Description

Name

VPC1

Region

China (Shanghai)

IPv4 CIDR Block

192.168.0.0/12

vSwitch

Name: VSW1

Zone: Zone E

IPv4 CIDR Block: 192.168.5.0/24

Name: VSW2

Zone: Zone G

IPv4 CIDR Block: 192.168.6.0/24

The following table describes the configurations of ECS01 and ECS02. The configurations are for reference only.

Click to view ECS instance configurations

Name

Region

VPC

Zone and vSwitch

ECS configuration

ECS01

China (Shanghai)

VPC1

Zone E | VSW1

  • Instance type: ecs.e-c1m1.large

  • CPU and memory: 2-core (vCPU), 2-GiB

  • Image: Alibaba Cloud Linux 3.2104 LTS 64-bit

ECS02

Zone G | VSW2

Step 1: Create an ALB instance

  1. Log on to the ALB console.
  2. On the Instances page, click Create ALB.

  3. On the Application Load Balancer buy page, configure the parameters that are described in the following table.

    The following table describes only some of the parameters. Use the default values for other parameters. For more information about the parameters, see Create an ECS instance.

    Parameter

    Description

    Region

    The region in which you want to create the ALB instance. In this example, China (Shanghai) is selected.

    Network Type

    Select a network type for the ALB instance. The system assigns public or private IP addresses to the ALB instance based on the selected network type. In this example, Internet is selected.

    VPC

    Select the VPC where you want to deploy the ALB instance.

    Zone

    Select at least two zones. In this example, Shanghai Zone E and Shanghai Zone G are selected, and VSW1 in Zone E and VSW2 in Zone G are selected.

    IP Mode

    Select an IP mode for the ALB instance. In this example, Static IP is selected.

    IP Version

    Select an IP version for the ALB instance. In this example, IPv4 is selected.

    Edition

    Select an edition for the ALB instance. In this example, Standard is selected.

    Instance Name

    Enter a name for the ALB instance.

    Service-linked Role

    The first time you create an ALB instance, click Create Service-linked Role to create the AliyunServiceRoleForAlb role. The policy AliyunServiceRolePolicyForAlb is attached to the service-linked role to allow the ALB instance to access other cloud services. For more information, see System policies for ALB.

  4. Click Buy Now and complete the payment.

  5. Return to the Instances page and select the region where the ALB instance is deployed to view the ALB instance.

Step 2: Create a server group

  1. In the left-side navigation pane, choose ALB > Server Groups.

  2. On the Server Groups page, click Create Server Group.

  3. In the Create Server Group dialog box, configure the parameters and click Create.

    The following table describes only some of the parameters. Keep the default values for other parameters. For more information, see Create and manage a server group.

    Parameter

    Description

    Server Group Type

    Select a server group type. In this example, Server is selected.

    Server Group Name

    Enter a name for the server group.

    VPC

    Select the VPC in which the ECS instances are deployed.

    Note

    Select the VPC in which the ALB instance is deployed.

    Backend Server Protocol

    Select a backend protocol. In this example, HTTP is selected.

    Scheduling Algorithm

    Select a scheduling algorithm. In this example, Weighted Round-robin is selected.

    Session Persistence

    Enable or disable session persistence. In this example, the default value is used. Session persistence is enabled.

    Backend Persistent Connection

    Specify whether to enable the persistent connection feature. In this example, the persistent TCP connection feature is disabled.

    Health Check

    Specifies whether to enable the health check feature. In this example, the health check feature is enabled.

    Health Check Settings

    After you enable the health check feature, click Modify to configure the health check settings.

  4. In the The server group is created. message, click Add Backend Server.

  5. On the Backend Servers tab of the page that appears, click Add Backend Server.

  6. In the Add Backend Server panel, select ECS01 and ECS02 and click Next.

  7. In the Ports/Weights step, specify a port and a weight for the ECS instances and click OK.

    In this example, both ECS instances use the port number 80 and the default weight 100.

Step 3: Configure a listener

  1. In the left-side navigation pane, choose ALB > Instances. On the Instances page, click the ID of the ALB instance that you want to manage.

  2. Click the Listener tab and then click Create Listener.

  3. In the Configure Listener step, configure the parameters and click Next.

    The following table describes only some of the parameters. Keep the default values for other parameters. For more information, see Add an HTTP listener.

    Parameter

    Description

    Select Listener Protocol

    Select a listener protocol. In this example, HTTP is selected.

    Listener Port

    Specify the listener port to receive and forward requests to backend servers. In this example, port 80 is specified.

    Listener Name

    Enter a name for the listener.

    Advanced Settings

    In this example, the default settings are used. You can click Modify to modify the settings.

  4. In the Server Group step, configure the Server Type parameter, select a server group based on the specified Server Type, confirm the backend servers, and then click Next.

  5. In the Configuration Review step, confirm the configurations and click Submit.

  6. Click OK to return to the Listener tab. If the listener status in the Health Check Status column is Healthy, ECS01 and ECS02 can process requests forwarded by the ALB instance.

Step 4: Create a CNAME record

In actual business scenarios, we recommend that you use CNAME records to map custom domain names to the domain name of your ALB instance.

  1. In the left-side navigation pane, choose ALB > Instances.

  2. On the Instances page, copy the domain name of the ALB instance.

  3. Perform the following steps to create a CNAME record:

    Note

    If your domain name is not registered by using Alibaba Cloud Domains, you must add your domain name to Alibaba Cloud DNS before you can configure a DNS record. For more information, see Manage domain names.

    1. Log on to the Alibaba Cloud DNS console.

    2. On the Authoritative DNS Resolution page, find your domain name and click DNS Settings in the Actions column.

    3. On the DNS Settings tab of the domain name details page, click Add DNS Record.

    4. In the Add DNS Record panel, configure the parameters and click OK. The following table describes the parameters.

      Parameter

      Description

      Record Type

      Select CNAME from the drop-down list.

      Hostname

      Enter the prefix of the domain name. In this example, @ is entered.

      Note

      If you use a root domain name, enter @.

      DNS Request Source

      Select Default.

      Record Value

      Enter the CNAME, which is the domain name of the ALB instance.

      TTL

      Select a time-to-live (TTL) value for the CNAME record to be cached on the DNS server. In this example, the default value is used.

Step 5: Test network connectivity

Test the connectivity between a client, ECS01, and ECS02. In this example, a random client that has access to the Internet is used. Enter the domain name in the address bar of a browser, for example, http://<domain name>. Refresh your browser multiple times. The results show that the requests are distributed between ECS01 and ECS02.

image

image

The preceding results indicate that the client can access the backend servers, and the ALB instance distributes requests to the backend servers based on the round-robin algorithm.

Release resources

  1. Release the ECS instances and the security groups.

    1. Delete ECS01 and its security group:

      1. Log on to the ECS console. In the top navigation bar, select the region in which ECS01 instance resides, and click the image.png icon on the right side of ECS01. In the dialog box that appears, select Release to immediately release the instance.

      2. Log on to the ECS console. In the top navigation bar, select the region in which ECS01 resides, select the security group of ECS01, and then click Delete to delete the security group.

    2. Repeat the preceding steps to delete ECS02 and its security group.

  2. Delete the DNS record

    For more information, see Delete a DNS record.

  3. Release ALB resources:

    1. Log on to the ALB console. In the top navigation bar, select the region of the ALB instance. Find the ALB instance that you want to release, click the image.png icon in the Actions column, and then click Release. In the Release Instance message, click Confirm.

    2. For more information about how to remove backend servers, see Remove a backend server.

    3. For more information about how to delete a server group, see Delete a server group.

  4. Release VPC resources:

    1. Log on to the VPC console. In the top navigation bar, select the region where the VPC resides.

    2. Click Delete in the Actions column and select Forcefully Delete to delete the VPC and the vSwitches.

References