配置 Azure 负载均衡器的分配模式Configure the distribution mode for Azure Load Balancer

Note

本文进行了更新,以便使用新的 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.

基于哈希的分发模式Hash-based distribution mode

Azure 负载均衡器的默认分配模式是五元组哈希。The default distribution mode for Azure Load Balancer is a five-tuple hash.

元组由以下内容组成:The tuple is composed of the:

  • 源 IPSource IP
  • 源端口Source port
  • 目标 IPDestination IP
  • 目标端口Destination port
  • 协议类型Protocol type

哈希用于将流量映射到可用的服务器。The hash is used to map traffic to the available servers. 算法仅在传输会话内部提供粘性。The algorithm provides stickiness only within a transport session. 同一会话中的数据包会定向到经过负载均衡的终结点后面的同一数据中心 IP。Packets that are in the same session are directed to the same datacenter IP behind the load-balanced endpoint. 客户端从同一源 IP 发起新会话时,源端口会更改,并导致流量定向到其他数据中心终结点。When the client starts a new session from the same source IP, the source port changes and causes the traffic to go to a different datacenter endpoint.

基于五元组哈希的分配模式

源 IP 关联模式Source IP affinity mode

还可以使用源 IP 关联分配模式配置负载均衡器。The load balancer can also be configured by using the source IP affinity distribution mode. 此分配模式也称为为会话关联或客户端 IP 关联。This distribution mode is also known as session affinity or client IP affinity. 该模式使用二元组(源 IP 和目标 IP)或三元组(源 IP、目标 IP 和协议)哈希将流量映射到可用的服务器。The mode uses a two-tuple (source IP and destination IP) or three-tuple (source IP, destination IP, and protocol type) hash to map traffic to the available servers. 使用源 IP 关联,从同一客户端计算机启动的连接会进入同一个数据中心终结点。By using source IP affinity, connections that are started from the same client computer go to the same datacenter endpoint.

下图演示二元组配置。The following figure illustrates a two-tuple configuration. 请注意二元组如何从负载均衡器运行到虚拟机 1 (VM1)。Notice how the two-tuple runs through the load balancer to virtual machine 1 (VM1). VM1 随后由 VM2 和 VM3 备份。VM1 is then backed up by VM2 and VM3.

二元组会话关联分配模式

源 IP 关联模式解决了 Azure 负载均衡器与远程桌面网关(RD 网关)之间的不兼容问题。Source IP affinity mode solves an incompatibility between Azure Load Balancer and Remote Desktop Gateway (RD Gateway). 使用此模式可在单个云服务中生成 RD 网关场。By using this mode, you can build an RD Gateway farm in a single cloud service.

另一个用例方案是媒体上传。Another use case scenario is media upload. 数据上传通过 UDP 进行,但控制平面通过 TCP 实现:The data upload happens through UDP, but the control plane is achieved through TCP:

  • 客户端启动与负载均衡公共地址的 TCP 会话,并定向到特定 DIP。A client starts a TCP session to the load-balanced public address and is directed to a specific DIP. 通道将保持活动状态以监视连接运行状况。The channel is left active to monitor the connection health.
  • 来自同一客户端计算机的新 UDP 会话在同一个负载均衡公共终结点中启动。A new UDP session from the same client computer is started to the same load-balanced public endpoint. 连接像前面的 TCP 连接一样定向到同一个 DIP 终结点。The connection is directed to the same DIP endpoint as the previous TCP connection. 能够以较高的吞吐量执行媒体上传,同时通过 TCP 维护控制通道。The media upload can be executed at high throughput while maintaining a control channel through TCP.

Note

如果通过删除或添加虚拟机来更改负载均衡集,则会重新计算客户端请求的分配。When a load-balanced set changes by removing or adding a virtual machine, the distribution of client requests is recomputed. 无法确保现有客户端的新连接最终都会抵达同一台服务器。You can't depend on new connections from existing clients to end up at the same server. 此外,使用源 IP 关联分配模式可能导致流量的不均衡分配。Additionally, using source IP affinity distribution mode can cause an unequal distribution of traffic. 在代理后面运行的客户端可被视为唯一的客户端应用程序。Clients that run behind proxies might be seen as one unique client application.

配置源 IP 关联设置Configure source IP affinity settings

Azure 门户Azure portal

可以通过修改门户中的负载均衡规则来更改分发模式的配置。You can change the configuration of the distribution mode by modifying the load-balancing rule in the portal.

  1. 登录 Azure 门户并通过单击“资源组” 找到包含要更改的负载均衡器的资源组。Sign in to the Azure portal and locate the Resource Group containing the load balancer you wish to change by clicking on Resource Groups.

  2. 在“负载均衡器概述”屏幕中,单击“设置”下的“负载均衡规则”。In the load balancer overview screen, click on Load-balancing rules under Settings.

  3. 在“负载均衡规则”屏幕中,单击要更改分发模式的负载均衡规则。In the load-balancing rules screen, click on the load-balancing rule that you wish to change the distribution mode.

  4. 在规则下,通过更改“会话持续性” 下拉框来更改分发模式。Under the rule, the distribution mode is changed by changing the Session persistence drop down box. 提供了以下选项:The following options are available:

    • 无(基于哈希) - 指定任何虚拟机可能处理来自同一客户端的后续请求。None (hash-based) - Specifies that successive requests from the same client may be handled by any virtual machine.
    • 客户端 IP (源 IP 关联 2 元组) - 指定来自同一客户端 IP 地址的后续请求将由同一虚拟机处理。Client IP (source IP affinity 2-tuple) - Specifies that successive requests from the same client IP address will be handled by the same virtual machine.
    • 客户端 IP 和协议(源 IP 关联 3 元组) - 指定来自同一客户端 IP 地址和协议组合的连续请求将由同一虚拟机处理。Client IP and protocol (source IP affinity 3-tuple) - Specifies that successive requests from the same client IP address and protocol combination will be handled by the same virtual machine.
  5. 选择分发模式,然后单击“保存” 。Choose the distribution mode and then click Save.

Azure PowerShellAzure PowerShell

对于使用资源管理器部署的虚拟机,请使用 PowerShell 更改现有负载均衡规则上的负载均衡器分发设置。For virtual machines deployed with Resource Manager, use PowerShell to change the load-balancer distribution settings on an existing load-balancing rule. 以下命令将更新分发模式:The following command updates the distribution mode:

$lb = Get-AzLoadBalancer -Name MyLb -ResourceGroupName MyLbRg
$lb.LoadBalancingRules[0].LoadDistribution = 'sourceIp'
Set-AzLoadBalancer -LoadBalancer $lb

对于经典虚拟机,请使用 Azure PowerShell 更改分发设置。For classic virtual machines, use Azure PowerShell to change the distribution settings. 将 Azure 终结点添加到虚拟机并配置负载均衡器分配模式:Add an Azure endpoint to a virtual machine and configure the load balancer distribution mode:

Get-AzureVM -ServiceName mySvc -Name MyVM1 | Add-AzureEndpoint -Name HttpIn -Protocol TCP -PublicPort 80 -LocalPort 8080 -LoadBalancerDistribution sourceIP | Update-AzureVM

设置 LoadBalancerDistribution 元素的值,实现所需的负载均衡量。Set the value of the LoadBalancerDistribution element for the amount of load balancing required. 为二元组(源 IP 和目标 IP)负载均衡指定 sourceIP。Specify sourceIP for two-tuple (source IP and destination IP) load balancing. 为三元组(源 IP、目标 IP 和协议类型)负载均衡指定 sourceIPProtocol。Specify sourceIPProtocol for three-tuple (source IP, destination IP, and protocol type) load balancing. 为五元组负载均衡的默认行为指定 none。Specify none for the default behavior of five-tuple load balancing.

使用以下设置检索终结点负载均衡器分配模式配置:Retrieve an endpoint load balancer distribution mode configuration by using these settings:

PS C:\> Get-AzureVM -ServiceName MyService -Name MyVM | Get-AzureEndpoint

VERBOSE: 6:43:50 PM - Completed Operation: Get Deployment
LBSetName : MyLoadBalancedSet
LocalPort : 80
Name : HTTP
Port : 80
Protocol : tcp
Vip : 65.52.xxx.xxx
ProbePath :
ProbePort : 80
ProbeProtocol : tcp
ProbeIntervalInSeconds : 15
ProbeTimeoutInSeconds : 31
EnableDirectServerReturn : False
Acl : {}
InternalLoadBalancerName :
IdleTimeoutInMinutes : 15
LoadBalancerDistribution : sourceIP

如果 LoadBalancerDistribution 元素不存在,Azure 负载均衡器会使用默认的五元组算法。When the LoadBalancerDistribution element isn't present, Azure Load Balancer uses the default five-tuple algorithm.

在负载均衡终结点集上配置分配模式Configure distribution mode on load-balanced endpoint set

如果终结点是负载均衡终结点集的一部分,则必须在负载均衡终结点集上配置分配模式:When endpoints are part of a load-balanced endpoint set, the distribution mode must be configured on the load-balanced endpoint set:

Set-AzureLoadBalancedEndpoint -ServiceName MyService -LBSetName LBSet1 -Protocol TCP -LocalPort 80 -ProbeProtocolTCP -ProbePort 8080 -LoadBalancerDistribution sourceIP

配置云服务终结点的分配模式Configure distribution mode for Cloud Services endpoints

使用用于 .NET 的 Azure SDK 2.5 更新云服务。Use the Azure SDK for .NET 2.5 to update your cloud service. 在 .csdef 中指定云服务的终结点设置。The endpoint settings for Cloud Services are made in the .csdef file. 若要更新云服务部署的负载均衡器分配模式,需要进行部署升级。To update the load balancer distribution mode for a Cloud Services deployment, a deployment upgrade is required.

下面是终结点设置的 .csdef 更改的示例:Here is an example of .csdef changes for endpoint settings:

<WorkerRole name="worker-role-name" vmsize="worker-role-size" enableNativeCodeExecution="[true|false]">
    <Endpoints>
    <InputEndpoint name="input-endpoint-name" protocol="[http|https|tcp|udp]" localPort="local-port-number" port="port-number" certificate="certificate-name" loadBalancerProbe="load-balancer-probe-name" loadBalancerDistribution="sourceIP" />
    </Endpoints>
</WorkerRole>
<NetworkConfiguration>
    <VirtualNetworkSite name="VNet"/>
    <AddressAssignments>
<InstanceAddress roleName="VMRolePersisted">
    <PublicIPs>
    <PublicIP name="public-ip-name" idleTimeoutInMinutes="timeout-in-minutes"/>
    </PublicIPs>
</InstanceAddress>
    </AddressAssignments>
</NetworkConfiguration>

API 示例API example

以下示例演示如何针对部署中的指定负载均衡集重新配置负载均衡器分配模式。The following example shows how to reconfigure the load balancer distribution mode for a specified load-balanced set in a deployment.

更改已部署的负载均衡集的分配模式Change distribution mode for deployed load-balanced set

使用 Azure 经典部署模型更改现有的部署配置。Use the Azure classic deployment model to change an existing deployment configuration. 添加 x-ms-version 标头,并将值设置为版本 2014-09-01 或更高。Add the x-ms-version header and set the value to version 2014-09-01 or later.

请求Request

POST https://management.core.chinacloudapi.cn/<subscription-id>/services/hostedservices/<cloudservice-name>/deployments/<deployment-name>?comp=UpdateLbSet   x-ms-version: 2014-09-01
Content-Type: application/xml

<LoadBalancedEndpointList xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="https://www.w3.org/2001/XMLSchema-instance">
  <InputEndpoint>
    <LoadBalancedEndpointSetName> endpoint-set-name </LoadBalancedEndpointSetName>
    <LocalPort> local-port-number </LocalPort>
    <Port> external-port-number </Port>
    <LoadBalancerProbe>
      <Port> port-assigned-to-probe </Port>
      <Protocol> probe-protocol </Protocol>
      <IntervalInSeconds> interval-of-probe </IntervalInSeconds>
      <TimeoutInSeconds> timeout-for-probe </TimeoutInSeconds>
    </LoadBalancerProbe>
    <Protocol> endpoint-protocol </Protocol>
    <EnableDirectServerReturn> enable-direct-server-return </EnableDirectServerReturn>
    <IdleTimeoutInMinutes>idle-time-out</IdleTimeoutInMinutes>
    <LoadBalancerDistribution>sourceIP</LoadBalancerDistribution>
  </InputEndpoint>
</LoadBalancedEndpointList>

如前所述,针对二元组关联、三元组关联或五元组关联,分别将 LoadBalancerDistribution 元素设置为 sourceIP、sourceIPProtocol 或 none(表示无关联)。As previously described, set the LoadBalancerDistribution element to sourceIP for two-tuple affinity, sourceIPProtocol for three-tuple affinity, or none for no affinity (five-tuple affinity).

响应Response

HTTP/1.1 202 Accepted
Cache-Control: no-cache
Content-Length: 0
Server: 1.0.6198.146 (rd_rdfe_stable.141015-1306) Microsoft-HTTPAPI/2.0
x-ms-servedbyregion: chinaeast2
x-ms-request-id: 9c7bda3e67c621a6b57096323069f7af
Date: Thu, 16 Oct 2014 22:49:21 GMT

后续步骤Next steps