UDP health checks can be used to improve service smoothness and user experience for UDP applications, such as online games and instant messaging. UDP health checks send UDP packets to probe backend servers. This feature can help you quickly identify potential issues and perform efficient troubleshooting to increase service liability and availability.
Scenario
An online game company runs multiple backend servers, which process game data of players over UDP. To ensure game smoothness, the company wants to monitor the health status of each server in real time. If a server becomes unhealthy, UDP health checks efficiently identify and isolate the server. Requests from players are no longer forwarded to the unhealthy server. When the server recovers, requests are automatically forwarded to the server.
To meet such requirements, the company can use UDP health checks of Network Load Balancer (NLB) to periodically probe each backend server. If UDP health checks identify an unhealthy server, NLB no longer forwards requests to the unhealthy server until it recovers. When an unhealthy server recovers, NLB automatically forwards requests to the server.
Prerequisites
A virtual private cloud (VPC) is created in the China (Hangzhou) region. A vSwitch is created in each of the two zones. For more information, see Create and manage a VPC.
An Elastic Compute Service (ECS) instance (ECS01) is deployed in VSW1 and another ECS instance (ECS02) is deployed in VSW2.
An NLB instance is created and running in the VPC. In this example, an internal-facing NLB instance is used. For more information, see Create and manage an NLB instance.
Usage notes
If UDP applications are deployed on your backend server, you can use UDP, TCP, or HTTP health checks to monitor the health status of the server. However, TCP and HTTP health checks require a TCP port. This may increase the network complexity and cause potential risks. To ensure consistent service capabilities in Classic Load Balancer (CLB)-to-NLB migration scenarios, we recommend that you use UDP health checks.
Make sure that the security groups of the backend server allow the UDP health check port.
Procedure
Step 1: Deploy an application
The following example shows how to deploy a Python application that can respond to UDP requests. In this example, the ECS instances use the CentOS 7.9 operating system. The example is for reference only. Adjust the configurations for your programs and applications.
Deploy a UDP application on ECS01 to respond to requests
Log on to ECS01.
Run the
vi udp_server.pycommand and press theIkey to enter the edit mode.Copy and paste the following code:
Press the
Esckey and enter:wqto save the configurations.Run the
sudo python3 udp_server.pycommand to execute the script.The
UDP server up and listeningresponse message indicates that the UDP application is enabled.
If you fail to enable the application, check whether the specified port is occupied by another application or errors exist in the commands or code.
Deploy a UDP application on ECS02 to respond to requests
Log on to ECS02.
Run the
vi udp_server.pycommand and press theIkey to enter the edit mode.Copy and paste the following code:
Press the
Esckey and enter:wqto save the configurations.Run the
sudo python3 udp_server.pycommand to execute the script.The
UDP server up and listeningresponse message indicates that the UDP application is enabled.
If you fail to enable the application, check whether the specified port is occupied by another application or errors exist in the commands or code.
Step 2: Create a server group and enable health checks
Log on to the NLB console.
In the top navigation bar, select the region where the NLB instance is deployed.
In the left-side navigation pane, choose .
On the Server Groups page, click Create Server Group.
In the Create Server Group dialog box, configure the parameters. The following table describes some of the parameters. Other parameters use the default values. For more information, see NLB server groups.
Parameter
Description
Server Group Type
Select the type of server group that you want to create.
Backend Server Protocol
In this example, UDP is selected.
Configure Health Check
Enable health checks and click Modify on the right side of Health Check Settings.
Health Check Protocol
In this example, UDP is selected.
Health Check Port
In this example, Backend Server Port is selected.
After you complete the configurations, click Create. After the server group is created, click Add Backend Server in the message that appears.
In the Add Backend Server panel, select ECS01 and ECS02 and click Next.
In the Ports/Weights step, enter port
161, retain the default weight, and then click OK.
Step 3: Create a UDP listener
Log on to the NLB console.
In the top navigation bar, select the region where the NLB instance is deployed.
On the Instances page, find the NLB instance and click Create Listener in the Actions column.
On the Configure Server Load Balancer page, configure the parameters. The following table describes some of the parameters. Other parameters use the default values. For more information, see Add a UDP listener. After you complete the configuration, click Submit.
Configure the listener:
Parameter
Description
Listener Protocol
In this example, UDP is selected.
Listener Port
In this example, port
161is used.Associate the listener with a server group:
Parameter
Description
Server Group
Select the server group that contains ECS01 and ECS02.
Step 4: Run tests
Probe a healthy server
In this example, a Linux client in the VPC is used to run tests.
Run the
vi udp_client.pycommand and press theIkey to enter the edit mode.Copy and paste the following code:
Press the
Esckey and enter:wqto save the configurations.
Run the
sudo python3 udp_client.pycommand. The following response message which containsPONG1indicates that ECS01 can receive requests.
Run the
sudo python3 udp_client.pycommand. The following response message which containsPONG2indicates that ECS02 can receive requests.
Simulate faults
Stop the backend UDP service on the backend server ECS01. In the script execution interface, press Ctrl+C to terminate the script. When you are returned to the command prompt, it confirms that the backend UDP service has stopped.

On the Linux client, run the
sudo python3 udp_client.pycommand for multiple times. If the command output containsPONG2, as shown in the following figure, it indicates that ECS02 can properly respond to requests.
Log on to the NLB console and view the cause of health check failures.
On the Instances page, click the ID of the NLB instance that you want to manage.
Click the Listener tab. In the Health Check Status column, find the Unhealthy health check state.
Click Unhealthy. A dialog box appears and displays the unhealthy servers and cause of health check failures.

In the dialog box that appears, click Instance Diagnostics. In the Instance Diagnostics panel, check Diagnostic Items.

Run the
sudo python3 udp_server.pycommand on ECS01 again to enable the backend UDP service. The command output containsUDP server up and listening, which indicates that the backend UDP service is enabled.Go to the Listener tab. The Health Check Status column displays Healthy.

Disable health checks.
If you disable the health check feature, NLB continues to distribute requests to unhealthy backend ECS instances, which causes service interruptions. We recommend that you enable the health check feature.
Log on to the NLB console.
In the top navigation bar, select the region where the NLB instance is deployed.
In the left-side navigation pane, choose NLB > Server Groups.
On the Server Groups page, find the server group that you want to manage and click Modify Health Check Settings in the Actions column.
In the Modify Health Check Settings dialog box, disable health checks and click Save.
Reference
For more information about UDP health checks, see NLB health checks.