为 Azure 负载均衡器配置 TCP 空闲超时设置Configure TCP idle timeout settings for Azure Load Balancer

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

本文需要 Azure PowerShell 模块 5.4.1 或更高版本。This article requires the Azure PowerShell module version 5.4.1 or later. 运行 Get-Module -ListAvailable Az 查找已安装的版本。Run Get-Module -ListAvailable Az to find the installed version. 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount -Environment AzureChinaCloud 以创建与 Azure 的连接。If you're running PowerShell locally, you also need to run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

TCP 空闲超时TCP Idle Timeout

Azure 负载均衡器的空闲超时设置为 4 分钟到 30 分钟。Azure Load Balancer has an idle timeout setting of 4 minutes to 30 minutes. 默认情况下,它设置为 4 分钟。By default, it is set to 4 minutes. 如果处于非活动状态的时间超过超时值,则不能保证在客户端和云服务之间保持 TCP 或 HTTP 会话。If a period of inactivity is longer than the timeout value, there's no guarantee that the TCP or HTTP session is maintained between the client and your cloud service.

当连接关闭时,客户端应用程序可能会收到以下错误消息:“The underlying connection was closed:A connection that was expected to be kept alive was closed by the server.”(基础连接已关闭: 服务器关闭了应保持连接状态的连接。)When the connection is closed, your client application may receive the following error message: "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server."

常见的做法是使用 TCP 保持连接状态。A common practice is to use a TCP keep-alive. 这种做法可以使连接状态保持更长时间。This practice keeps the connection active for a longer period. 有关详细信息,请参阅 .NET 示例For more information, see these .NET examples. 在启用保持连接状态的情况下,在连接处于非活动状态时发送数据包。With keep-alive enabled, packets are sent during periods of inactivity on the connection. 用于保持连接状态的数据包将确保不会达到空闲超时值,并且连接会维持很长时间。Keep-alive packets ensure the idle timeout value isn't reached and the connection is maintained for a long period.

此设置仅适用于入站连接。The setting works for inbound connections only. 为了避免断开连接,请将 TCP 保持连接的时间间隔配置为小于空闲超时设置,或者提高空闲超时值。To avoid losing the connection, configure the TCP keep-alive with an interval less than the idle timeout setting or increase the idle timeout value. 为了支持这些方案,我们添加了可配置空闲超时支持。To support these scenarios, support for a configurable idle timeout has been added.

TCP 保持连接状态适用于电池续航时间不受限制的情况。TCP keep-alive works for scenarios where battery life isn't a constraint. 不建议将其用于移动应用程序。It isn't recommended for mobile applications. 在移动应用程序中使用 TCP 保持连接状态可能会加快设备电池的耗尽速度。Using a TCP keep-alive in a mobile application can drain the device battery faster.

TCP 超时

以下部分介绍如何更改公共 IP 和负载均衡器资源的空闲超时设置。The following sections describe how to change idle timeout settings for public IP and load balancer resources.

备注

TCP 空闲超时不影响 UDP 协议的负载均衡规则。TCP idle timeout does not affect load balancing rules on UDP protocol.

将实例级公共 IP 的 TCP 超时值配置为 15 分钟Configure the TCP timeout for your instance-level public IP to 15 minutes

$publicIP = Get-AzPublicIpAddress -Name MyPublicIP -ResourceGroupName MyResourceGroup
$publicIP.IdleTimeoutInMinutes = "15"
Set-AzPublicIpAddress -PublicIpAddress $publicIP

IdleTimeoutInMinutes 是可选项。IdleTimeoutInMinutes is optional. 如果未设置,默认超时为 4 分钟。If it isn't set, the default timeout is 4 minutes. 可接受的超时范围为 4 到 30 分钟。The acceptable timeout range is 4 to 30 minutes.

将负载均衡规则的 TCP 超时设置为 15 分钟Set the TCP timeout on a load-balanced rule to 15 minutes

若要为负载均衡器设置空闲超时,请在负载均衡规则上设置“IdleTimeoutInMinutes”。To set the idle timeout for a load balancer, the 'IdleTimeoutInMinutes' is set on the load-balanced rule. 例如:For example:

$lb = Get-AzLoadBalancer -Name "MyLoadBalancer" -ResourceGroup "MyResourceGroup"
$lb | Set-AzLoadBalancerRuleConfig -Name myLBrule -IdleTimeoutInMinutes 15

后续步骤Next steps

内部负载均衡器概述Internal load balancer overview

开始配置面向 Internet 的负载均衡器Get started configuring an Internet-facing load balancer

配置负载均衡器分发模式Configure a load balancer distribution mode