Azure 云服务定义 LoadBalancerProbe 架构Azure Cloud Services Definition LoadBalancerProbe Schema

负载均衡器探测是由客户定义的,用于探测 UDP 终结点和角色实例中的终结点的运行状况。The load balancer probe is a customer defined health probe of UDP endpoints and endpoints in role instances. LoadBalancerProbe 不是一个独立的元素;它在服务定义文件中与 Web 角色或辅助角色结合。The LoadBalancerProbe is not a standalone element; it is combined with the web role or worker role in a service definition file. LoadBalancerProbe 可由多个角色使用。A LoadBalancerProbe can be used by more than one role.

服务定义文件的默认扩展名为 .csdef。The default extension for the service definition file is .csdef.

负载均衡器探测的功能The function of a load balancer probe

Azure 负载均衡器负责将传入流量路由到角色实例。The Azure Load Balancer is responsible for routing incoming traffic to your role instances. 负载均衡器通过定期探测每个实例来确定哪些实例可以接收流量,从而确定该实例的运行状况。The load balancer determines which instances can receive traffic by regularly probing each instance in order to determine the health of that instance. 负载均衡器每分钟可以多次探测每个实例。The load balancer probes every instance multiple times per minute. 可通过两个不同的选项向负载均衡器提供实例运行状况 - 默认负载均衡器探测或自定义负载均衡器探测,这可通过在 .csdef 文件中定义 LoadBalancerProbe 来实现。There are two different options for providing instance health to the load balancer – the default load balancer probe, or a custom load balancer probe, which is implemented by defining the LoadBalancerProbe in the .csdef file.

仅当实例处于就绪状态时(就像实例未处于“繁忙”、“正在回收”或“正在停止”等状态时),默认负载均衡器探测才利用虚拟机内部的来宾代理进行侦听,并以“HTTP 200 正常”作为响应。The default load balancer probe utilizes the Guest Agent inside the virtual machine, which listens and responds with an HTTP 200 OK response only when the instance is in the Ready state (like when the instance is not in the Busy, Recycling, Stopping, etc. states). 如果来宾代理无法使用“HTTP 200 正常”响应,则 Azure 负载均衡器将实例标记为无响应,并停止向该实例发送流量。If the Guest Agent fails to respond with HTTP 200 OK, the Azure Load Balancer marks the instance as unresponsive and stops sending traffic to that instance. Azure 负载均衡器继续 ping 实例,并且如果来宾代理使用 HTTP 200 响应,Azure 负载均衡器会再次向该实例发送流量。The Azure Load Balancer continues to ping the instance, and if the Guest Agent responds with an HTTP 200, the Azure Load Balancer sends traffic to that instance again. 使用 Web 角色时,网站代码通常在不受 Azure 结构或来宾代理监视的 w3wp.exe 中运行。这意味着,系统不会向来宾代理报告 w3wp.exe 中的失败(例如,When using a web role your website code typically runs in w3wp.exe which is not monitored by the Azure fabric or guest agent, which means failures in w3wp.exe (eg. HTTP 500 响应),并且负载均衡器不会知道将该实例退出轮转。HTTP 500 responses) is not be reported to the guest agent and the load balancer does not know to take that instance out of rotation.

自定义负载均衡器探测会取代默认来宾代理探测,并允许用户创建自己的自定义逻辑来确定角色实例的运行状况。The custom load balancer probe overrides the default guest agent probe and allows you to create your own custom logic to determine the health of the role instance. 负载均衡器可定期(默认值为每 15 秒一次)探测终结点,并且如果在超时期限内(默认值为 31 秒)使用 TCP ACK 或 HTTP 200 进行了响应,则认为该实例处于轮转状态。The load balancer regularly probes your endpoint (every 15 seconds, by default) and the instance is be considered in rotation if it responds with a TCP ACK or HTTP 200 within the timeout period (default of 31 seconds). 如果想要实现自己的逻辑(例如,在实例占用的 CPU 超过 90% 时返回非 200 状态)以便从负载均衡器轮转中删除实例,这种方案可能很有用。This can be useful to implement your own logic to remove instances from load balancer rotation, for example returning a non-200 status if the instance is above 90% CPU. 对于使用 w3wp.exe 的 Web 角色,这还意味着可自动监视网站,因为网站代码中的错误会向负载均衡器探测返回非 200 状态。For web roles using w3wp.exe, this also means you get automatic monitoring of your website, since failures in your website code return a non-200 status to the load balancer probe. 如果未在 .csdef 文件中定义 LoadBalancerProbe,则会使用默认的负载均衡器行为(如上文所述)。If you do not define a LoadBalancerProbe in the .csdef file, then the default load balancer behavior (as previously described) is be used.

如果使用自定义负载均衡器探测,需要确保相关逻辑考虑了 RoleEnvironment.OnStop 方法。If you use a custom load balancer probe, you must ensure that your logic takes into consideration the RoleEnvironment.OnStop method. 如果使用默认负载均衡器探测,实例会将在调用 OnStop 之前退出轮转,但自定义负载均衡器探测可以继续在 OnStop 事件期间返回 200 正常。When using the default load balancer probe, the instance is taken out of rotation prior to OnStop being called, but a custom load balancer probe can continue to return a 200 OK during the OnStop event. 如果使用 OnStop 事件清除缓存,请停止服务,或以其他方式进行可能影响服务的运行时行为的更改,然后需要确保自定义负载均衡器探测逻辑从轮转中删除实例。If you are using the OnStop event to clean up cache, stop service, or otherwise making changes that can affect the runtime behavior of your service, then you need to ensure that your custom load balancer probe logic removes the instance from rotation.

负载均衡器探测的基本服务定义架构Basic service definition schema for a load balancer probe

包含负载均衡器探测的服务定义文件的基本格式如下。The basic format of a service definition file containing a load balancer probe is as follows.

<ServiceDefinition …>
   <LoadBalancerProbes>
      <LoadBalancerProbe name="<load-balancer-probe-name>" protocol="[http|tcp]" path="<uri-for-checking-health-status-of-vm>" port="<port-number>" intervalInSeconds="<interval-in-seconds>" timeoutInSeconds="<timeout-in-seconds>"/>
   </LoadBalancerProbes>
</ServiceDefinition>

架构元素Schema elements

服务定义文件的 LoadBalancerProbes 元素包含以下元素:The LoadBalancerProbes element of the service definition file includes the following elements:

LoadBalancerProbes 元素LoadBalancerProbes Element

LoadBalancerProbes 元素描述负载均衡器探测的内容集合。The LoadBalancerProbes element describes the collection of load balancer probes. 此元素是 LoadBalancerProbe 元素的父元素。This element is the parent element of the LoadBalancerProbe Element.

LoadBalancerProbe 元素LoadBalancerProbe Element

LoadBalancerProbe 元素可定义模型的运行状况探测。The LoadBalancerProbe element defines the health probe for a model. 可定义多个负载均衡器探测。You can define multiple load balancer probes.

下表描述 LoadBalancerProbe 元素的属性:The following table describes the attributes of the LoadBalancerProbe element:

属性Attribute 类型Type 说明Description
name string 必需。Required. 负载均衡器探测的名称。The name of the load balancer probe. 此名称必须唯一。The name must be unique.
protocol string 必需。Required. 指定终结点的协议。Specifies the protocol of the end point. 可能的值为 httptcpPossible values are http or tcp. 如果已指定 tcp,则需要接收到的 ACK 才可完成进行探测。If tcp is specified, a received ACK is required for the probe to be successful. 如果已指定 http,则需要来自指定 URI 的 200 正常响应才可成功完成探测。If http is specified, a 200 OK response from the specified URI is required for the probe to be successful.
path string 用于从 VM 请求运行状况的 URI。The URI used for requesting health status from the VM. 如果 protocol 设置为 http,则需要 pathpath is required if protocol is set to http. 否则,不允许使用该属性。Otherwise, it is not allowed.

没有默认值。There is no default value.
port integer 可选。Optional. 用于传达探测的端口。The port for communicating the probe. 这对任何终结点都是可选的,因为相同的端口之后会用于探测。This is optional for any endpoint, as the same port will then be used for the probe. 还可为探测配置其他端口。You can configure a different port for their probing, as well. 可能的值介于 1 和 65535(含)之间。Possible values range from 1 to 65535, inclusive.

默认值由终结点设置。The default value is set by the endpoint.
intervalInSeconds integer 可选。Optional. 探测终结点的运行状况的间隔(以秒为单位)。The interval, in seconds, for how frequently to probe the endpoint for health status. 通常,该间隔略小于所分配超时期限(以秒为单位)的一半,以便在退出轮转前执行两次完整的探测。Typically, the interval is slightly less than half the allocated timeout period (in seconds) which allows two full probes before taking the instance out of rotation.

默认值为 15,最小值为 5。The default value is 15, the minimum value is 5.
timeoutInSeconds integer 可选。Optional. 应用于探测的超时期限(以秒为单位),在此期限内没有响应就会停止向终结点传送后续流量。The timeout period, in seconds, applied to the probe where no response will result in stopping further traffic from being delivered to the endpoint. 该值允许终结点以比 Azure 中通常所用的时间(默认值)更短或更长的时间退出轮转。This value allows endpoints to be taken out of rotation faster or slower than the typical times used in Azure (which are the defaults).

默认值为 31,最小值为 11。The default value is 31, the minimum value is 11.

另请参阅See Also

云服务(经典)定义架构Cloud Service (classic) Definition Schema