Alibaba Cloud Network Load Balancer (NLB) supports TCP, UDP, and SSL over TCP, and provides Layer 4 load balancing capabilities. Create an NLB instance with a server group, listener, and DNS record to distribute client requests across backend IPv4 servers.
Prerequisites
-
A VPC (VPC1) is created in the China (Shanghai) region with a vSwitch in Zone E (VSW1) and Zone G (VSW2). Create a VPC and a vSwitch.
Reserve at least eight IP addresses in each vSwitch for the NLB instance to scale properly.
-
Two Elastic Compute Service (ECS) instances are deployed: ECS01 in VSW1 and ECS02 in VSW2.
-
The following commands show how to deploy applications on ECS01 and ECS02:
-
A domain name is registered with an ICP number. Register a domain name on Alibaba Cloud. Overview.
Example VPC1 configurations:
Example ECS configurations:
Step 1: Create an NLB instance
-
Log on to the NLB console.
-
In the top navigation bar, select the region in which the NLB instance is deployed.
-
On the Instances page, click Create NLB.
-
On the Network Load Balancer (Pay-As-You-Go) page, configure the parameters. Click Buy Now.
Only key parameters are listed. Use defaults for other parameters. For more information, see Create and manage NLB instances.
Parameter
Description
Region
Select the region for the NLB instance.
Network Type
Select a network type. The system assigns public or private IP addresses based on the selected type. Internet-facing is selected in this example.
VPC
Select the VPC for the NLB instance.
Zone
Select at least two zones. In this example, Shanghai Zone E and Shanghai Zone G are selected, and a vSwitch in each zone is selected.
IP Version
Select an IP version. In this example, IPv4 is selected.
Instance Name
Enter a name for the NLB instance.
Resource Group
Select a resource group for the NLB instance.
Service-linked Role
When you first create an NLB instance, click Create Service-linked Role to create AliyunServiceRoleForNlb. This role uses the AliyunServiceRolePolicyForNlb policy to grant NLB access to other cloud services. For more information, see System policies for NLB.
Step 2: Create a server group
-
In the left-side navigation pane, choose NLB > Server Groups.
-
On the Server Groups page, click Create Server Group.
-
In the Create Server Group dialog box, configure the parameters and click Create. The following table describes the parameters.
Only key parameters are listed. Use defaults for other parameters. Create and manage a server group.
Parameter
Description
Server Group Type
Select a server group type. In this example, Server Type is selected.
Server Group Name
Enter a name for the server group.
VPC
Select a VPC. Only servers in this VPC can be added to the server group.
Backend Server Protocol
Select a backend protocol. Valid values: TCP, UDP, SSL over TCP. In this example, TCP is selected.
Scheduling Algorithm
Select a scheduling algorithm. In this example, Weighted Round-robin is selected.
Health Check
Health checks are enabled by default. In this example, the default setting is used.
-
Click the ID of the server group to go to the Backend Servers tab.
-
Click Add Backend Server . In the Add Backend Server panel, select ECS01 and ECS02 and click Next.
-
In the Ports/Weights step, specify port 80, use the default weight 100, and then click OK.
Step 3: Configure a listener
-
In the left-side navigation pane, choose .
-
On the Instances page, find the NLB instance that you want to manage and click Create Listener.
-
On the NLB Configuration Wizard page, configure the parameters and click Next.
Only key parameters are listed. Use defaults for other parameters. For more information, see Add a TCP listener.
Parameter
Description
Select Listener Protocol
Select a listener protocol. In this example, TCP is selected.
Listener Port
Specify the listener port for receiving and forwarding requests. Port 80 is used in this example.
Listener Name
Enter a name for the listener.
Advanced Settings
Defaults are used in this example. Click Modify to customize.
-
In the Server Group step, select a Server Type and select a server group from the drop-down list next to Server Type, view the backend servers, and then click Next.
-
In the Confirm step, confirm the configurations and click Submit.
-
Click OK to return to the Listener tab. After the status of the listener in the Health Check Status changes to Healthy, ECS01 and ECS02 can forward requests from the NLB instance.
Step 4: Create a DNS record
Use CNAME records to map your custom domain name to the NLB instance domain name.
-
In the left-side navigation pane, choose .
-
On the Instances page, copy the domain name of the NLB instance that you want to manage.
-
Perform the following steps to create a CNAME record:
NoteIf your domain name is not registered through Alibaba Cloud Domains, add it to Alibaba Cloud DNS first. For more information, see Manage domain names. Skip this step for domains registered through Alibaba Cloud Domains.
-
Log on to the Alibaba Cloud DNS console.
-
On the Authoritative DNS Resolution page, find your domain name and click DNS Settings in the Actions column.
-
On the DNS Settings tab of the domain name details page, click Add DNS Record.
-
In the Add 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
Domain name prefix. @ is used in this example.
NoteIf the domain name is a root domain name, enter @.
DNS Query Source
Select Default.
Record Value
Enter the CNAME, which is the domain name of the NLB instance.
TTL Period
TTL value for DNS caching. The default is used in this example.
-
Step 5: Verify the results
-
Test the availability of the NLB instance.
-
This example uses a Linux client with Internet access. On CentOS, install telnet if needed:
yum install -y telnet. -
Run
telnet Domain name Port. A response containing Connected to nlb-... confirms the NLB instance is forwarding requests.Trying *.*.*.*... Connected to www.example.com. Escape character is '^]'.Open
http://domain namein a browser. A response as shown below confirms the NLB instance is forwarding requests.
-
-
(Optional) Simulate faults.
-
Stop the application on ECS01 by running
systemctl stop nginx.service. -
Run
telnet Domain name Portagain. A response containing Connected to nlb-... confirms connectivity.Trying *.*.*.*... Connected to www.example.com. Escape character is '^]'.Open
http://domain namein a browser. A response as shown below confirms the NLB instance is forwarding requests.
-
Restart the application on ECS01 and stop it on ECS02. Run
systemctl start nginx.serviceon ECS01 andsystemctl stop nginx.serviceon ECS02. -
Run
telnet Domain name Portagain. A response containing Connected to nlb-... confirms connectivity.Trying *.*.*.*... Connected to www.example.com. Escape character is '^]'.Open
http://domain namein a browser. A response as shown below confirms the NLB instance is forwarding requests.
-
These tests confirm that a single server failure does not affect NLB instance availability.
-
Release resources
-
Release the ECS instances and the security groups.
-
Delete ECS01 and its security group.
-
Go to the ECS console - Instance page. In the top navigation bar, select the region in which ECS01 instance resides, and click the
icon on the right side of ECS01. In the dialog box that appears, select Release to immediately release the instance. -
Go to the ECS console - Security Groups page. 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.
-
-
Repeat the preceding steps to delete ECS02 and its security group.
-
-
Delete the DNS record.
For more information, see Delete a record.
-
Release NLB resources.
-
Go to the NLB console. In the top navigation bar, select the region where the NLB instance resides. Find the NLB instance, click the
icon in the Actions column, and click Release. In the message that appears, click Confirm. -
Go to the NLB console. In the top navigation bar, select the region in which the NLB instance resides. On the Server Group page, find the server group, click the
icon in the Actions column, and click Delete. In the message that appears, click OK.
-
-
Release VPC resources.
-
Go to the VPC console. In the top navigation bar, select the region where the VPC resides.
-
Click Delete in the Actions column and select Forcefully Delete to delete the VPC and the vSwitches.
-
References
-
For more information about the use scenarios and components of NLB, see What is NLB?
-
For more information about the features of NLB, see Functions and features.
-
For more information about NLB quotas and how to increase quotas, see Performance and usage limits.
-
For more information about the regions in which NLB is available, see Regions that support NLB.
-
For more information about the billing of NLB, see NLB billing.