Quickstart: IPv4 load balancing with ALB

Updated at:
Copy as MD

Alibaba Cloud Application Load Balancer (ALB) supports HTTP, HTTPS, and QUIC protocols and is designed for the application layer. This topic shows you how to quickly create an IPv4 ALB instance to forward requests from IPv4 clients to backend servers.

Prerequisites

  • You have created a Virtual Private Cloud (VPC) named VPC1 in the China (Shanghai) region. You have also created a vSwitch named VSW1 in Zone E and another vSwitch named VSW2 in Zone G. For more information, see Create a VPC and a vSwitch.

    If you plan to deploy the ALB instance in VSW1 and VSW2, note that an upgraded ALB instance reserves three IP addresses from each specified vSwitch: one virtual IP address (VIP) for providing services and two local IP addresses for communicating with backend servers. If a vSwitch has insufficient available IP addresses, instance creation fails. Ensure that VSW1 and VSW2 have enough available IP addresses. This limitation does not apply to non-upgraded ALB instances.

    Note
    • To ensure that all elastic capabilities are available for ALB instance upgrades, we recommend that you reserve at least 8 IP addresses in each vSwitch that contains the instance.

    • To ensure proper communication between upgraded ALB instances and backend services, if your backend services use access control policies like iptables, allow traffic from the CIDR block of the ALB instance's vSwitch.

  • You have created an Elastic Compute Service (ECS) instance named ECS01 in VSW1 and another ECS instance named ECS02 in VSW2. Application services are deployed on both ECS01 and ECS02.

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

    • The following examples show how to deploy test applications on ECS01 and ECS02:

      Commands to deploy the service on ECS01

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

      Commands to deploy the service on ECS02

      yum install -y nginx
      systemctl start nginx.service
      cd /usr/share/nginx/html/
      echo "Hello World ! This is ECS02." > index.html
  • You have registered a domain name and completed ICP filing for it. For more information, see Register a domain name and ICP filing process.

The following table describes the VPC configurations used in this tutorial. The configurations are for reference only.

Click to view the VPC configurations

Parameter

Description

Name

VPC1

Region

China (Shanghai)

IPv4 CIDR block

192.168.0.0/16

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 the ECS instances used in this tutorial. The configurations are for reference only.

Click to view the ECS configurations

Parameter

Region

VPC

Zone and vSwitch

ECS configuration

ECS01

China (Shanghai)

VPC1

Zone E | VSW1

  • Instance type: ecs.e-c1m1.large

  • CPU and memory: 2 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 (pay-as-you-go) purchase page, configure the instance.

    Only the configuration items relevant to this topic are listed here. Use the default values for all other configuration items. For more information about the parameters, see Create and manage ALB instances.

    Parameter

    Description

    Region

    Select the region where you want to deploy the instance. This tutorial uses China (Shanghai).

    Network Type

    Select a network type for the instance. The system assigns a private or public IP address based on your selection. This tutorial uses the Public.

    VPC

    Select the VPC where you want to deploy the instance.

    Zone ID

    Select at least two zones. This tutorial uses Shanghai Zone E and its vSwitch VSW1, and Shanghai Zone G and its vSwitch VSW2.

    IP Version

    Select an IP version for the instance. This tutorial uses IPv4.

    Edition (Instance Fee)

    Select an edition for the instance. This tutorial uses Standard.

    Instance Name

    Enter a custom name for the instance.

    Service-linked Role

    The first time you create an ALB instance, you must click Create Service-Linked Role to create a service-linked role named AliyunServiceRoleForAlb. The system attaches the AliyunServiceRolePolicyForAlb permission policy to this role to grant ALB access to other cloud resources. For more information, see System permission policies for load balancing.

  4. Click Buy Now and follow the on-screen instructions to complete the purchase.

  5. Return to the Instances page. Your new instance appears in the list for the selected region.

Step 2: Create a server group

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

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

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

    Only the configuration items that are highly relevant to this topic are listed here. Use the default values for all other configuration items. For more information about the parameters, see Create/Delete Server Group.

    Parameter

    Description

    Server Group Type

    Select a server group type. This tutorial uses Server.

    Server Group Name

    Enter a name for the server group.

    VPC

    Select the VPC that contains your ECS instances.

    Note

    Make sure that the selected VPC is the same as the one selected when you created the ALB instance.

    Backend Server Protocol

    Select a backend protocol. This tutorial uses HTTP.

    Scheduling Algorithm

    Select a scheduling algorithm. This tutorial uses Weighted Round-robin.

    Session Persistence

    Enable or disable session persistence. This tutorial uses the default setting (disabled).

    Backend Persistent Connection

    Enable or disable backend persistent connections. This tutorial uses the default setting (enabled).

    Health Check

    Enable or disable health checks. This feature is enabled for this tutorial.

    Health Check Settings

    After enabling health checks, click Edit for advanced configuration.

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

  5. On the Backend Servers tab, click Add Backend Server.

  6. In the Add Backend Server panel, select the ECS01 and ECS02 instances that you created, and then click Next.

  7. On the Ports/Weights wizard, set the Port and Weight for the added servers, and then click OK.

    In this tutorial, the port for the ECS instances is set to 80, and the weight is set to the default value of 100.

Step 3: Configure a listener

  1. In the left-side navigation pane, choose ALB > Instances, and then click the instance ID.

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

  3. On the Configure Listener wizard page, complete the configuration and click Next.

    This section lists only the parameters that are relevant to this topic. For all other parameters, use the default values. For more information about how to configure a listener, see Add an HTTP listener.

    Parameter

    Description

    Select Listener Protocol

    Select the protocol for the listener. This tutorial uses HTTP.

    Listener Port

    The listener port used to receive and forward requests to backend servers. This tutorial uses port 80.

    Listener Name

    Enter a custom name for the listener.

    Advanced Settings

    Use the default settings. You can click Modify to change the settings.

  4. In the Server Group configuration wizard, select a Server Type and the target server group under the Server Type, view the backend server information, and then click Next.

  5. On the Configuration Review page, confirm the configuration, and then click Submit.

  6. Click OK to return to the Listener tab. When the Health Check Status column for the listener shows Healthy, the backend servers ECS01 and ECS02 can process requests forwarded by the ALB instance.

Step 4: Configure DNS resolution

For production, we recommend mapping a custom domain name to the ALB instance's domain name by creating a CNAME record.

Important

The domain names of load balancers have been upgraded. You can no longer directly access services by using the DNS name provided by ALB.

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

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

  3. Follow these steps to add a CNAME record.

    Note

    If your domain name is registered by a provider other than Alibaba Cloud, you must add the domain name to the Alibaba Cloud DNS console before you can configure DNS resolution. For more information, see Manage domain names. If your domain name is registered with Alibaba Cloud, proceed with the following steps.

    1. Log on to the Alibaba Cloud DNS console.

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

    3. On the Settings page, click Add Record.

    4. In the Add Record panel, configure the CNAME record by setting the following parameters, and then click OK.

      Parameter

      Description

      Record Type

      Select CNAME from the drop-down list.

      Hostname

      The prefix of your domain name. This tutorial uses @.

      Note

      To use a root domain, set the hostname to @.

      Query Source

      Select Default.

      Record Value

      Enter the DNS name of the ALB instance that you copied.

      TTL

      Time to live (TTL) is the duration that a DNS record is cached on a DNS server. Use the default value.

Step 5: Test connectivity

Test the connectivity between a client and the ECS01 and ECS02 servers. This tutorial uses a client with public network access as an example.

In the address bar of your browser, enter your domain name, for example, http://<yourdomain.com>. Refresh the page multiple times. The displayed message alternates, demonstrating that the ALB is distributing requests to both backend servers.

One response will be Hello World ! This is ECS01., which indicates that the request was forwarded to the ECS01 server.

Hello World ! This is ECS02.

This confirms that the client can use the ALB instance to access different backend servers in a round-robin manner.

Clean up resources

  1. Clean up ECS, security group, and other resources:

    1. Delete the ECS01 instance and its security group:

      1. Log on to the Elastic Compute Service console. In the top navigation bar, select the region where the instance is located. Find the ECS01 instance, click the image.png icon in the Actions column, select Release in the dialog box that appears, and confirm the release.

      2. Log on to the ECS Security Group console. In the top navigation bar, select the region where the instance is located, select the custom security group for ECS01, and click Delete.

    2. Follow the preceding steps to delete the ECS02 instance and its corresponding security group.

  2. Delete the DNS record

    For more information, see Delete a DNS record.

  3. Clean up ALB resources:

    1. Release the ALB instance. Before you proceed, make sure you have deleted the listener associated with it. Log on to the ALB console. In the top navigation bar, select the region where the instance is located. Find the instance, click the image.png icon in the Actions column, select Release in the dialog box that appears, and confirm your action.

    2. Remove the backend servers from the server group. For more information, see Create and manage server groups.

    3. Delete the server group. For more information, see Create and manage server groups.

  4. Clean up VPC resources:

    1. Log on to the Virtual Private Cloud console. In the top navigation bar, select the region where the instance is located.

    2. Click Delete in the Actions column for the instance. The system checks for any undeleted cloud resources or associated resources. If dependent resources exist, you must release them completely before you can delete the VPC and vSwitches.

Related documents