使用 PowerShell 创建 Azure 应用程序网关(经典)的自定义探测Create a custom probe for Azure Application Gateway (classic) by using PowerShell

在本文中,将使用 PowerShell 向现有应用程序网关添加自定义探测。In this article, you add a custom probe to an existing application gateway with PowerShell. 如果应用程序包含特定运行状况检查页面。或者未在默认 Web 应用程序上提供成功的响应,那么它们非常适合使用自定义探测。Custom probes are useful for applications that have a specific health check page or for applications that do not provide a successful response on the default web application.

Important

Azure 具有用于创建和处理资源的两个不同部署模型:资源管理器部署模型和经典部署模型。 本文介绍如何使用经典部署模型。 Microsoft 建议大多数新部署使用 Resource Manager 模型。 了解如何使用 Resource Manager 模型执行这些步骤

先决条件:安装 Azure PowerShell 模块Prerequisite: Install the Azure PowerShell module

若要执行本文中的步骤,需要安装和配置 Azure PowerShell 模块To perform the steps in this article, you need to install and configure the Azure PowerShell module. 请务必完成所有指令。Be sure to complete all of the instructions. 安装完成后,请登录到 Azure,然后选择订阅。After the installation is finished, sign in to Azure and select your subscription.

Note

需要一个 Azure 帐户来完成这些步骤。You need an Azure account to complete these steps. 如果没有 Azure 帐户,可以注册试用帐户If you don't have an Azure account, you can sign up for a trial account.

创建应用程序网关Create an application gateway

创建应用程序网关:To create an application gateway:

  1. 创建应用程序网关资源。Create an application gateway resource.
  2. 创建配置 XML 文件或配置对象。Create a configuration XML file or a configuration object.
  3. 将配置提交到新建的应用程序网关资源。Commit the configuration to the newly created application gateway resource.

创建使用自定义探测的应用程序网关资源Create an application gateway resource with a custom probe

要创建网关,请使用 New-AzureApplicationGateway cmdlet,并将值替换为你自己的值。To create the gateway, use the New-AzureApplicationGateway cmdlet, replacing the values with your own. 此时不会开始计收网关的费用。Billing for the gateway does not start at this point. 计费会在后面已成功启动网关时开始。Billing begins in a later step, when the gateway is successfully started.

以下示例使用名为“testvnet1”的虚拟网络和名为“subnet-1”的子网创建应用程序网关。The following example creates an application gateway by using a virtual network called "testvnet1" and a subnet called "subnet-1".

New-AzureApplicationGateway -Name AppGwTest -VnetName testvnet1 -Subnets @("Subnet-1")

若要验证是否已创建网关,可以使用 Get-AzureApplicationGateway cmdlet。To validate that the gateway was created, you can use the Get-AzureApplicationGateway cmdlet.

Get-AzureApplicationGateway AppGwTest

Note

InstanceCount 的默认值为 2,最大值为 10。 GatewaySize 的默认值为 Medium。 可以选择 Small、Medium 或 Large。

VirtualIPsDnsName 显示为空白,因为网关尚未启动。VirtualIPs and DnsName are shown as blank because the gateway has not started yet. 这些值在网关进入运行状态后立即创建。These values are created once the gateway is in the running state.

使用 XML 配置应用程序网关Configure an application gateway by using XML

在以下示例中,使用 XML 文件配置所有应用程序网关设置,并将这些设置提交到应用程序网关资源。In the following example, you use an XML file to configure all application gateway settings and commit them to the application gateway resource.

将以下文本复制到记事本中。Copy the following text to Notepad.

<ApplicationGatewayConfiguration xmlns:i="https://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/windowsazure">
<FrontendIPConfigurations>
    <FrontendIPConfiguration>
        <Name>fip1</Name>
        <Type>Private</Type>
    </FrontendIPConfiguration>
</FrontendIPConfigurations>
<FrontendPorts>
    <FrontendPort>
        <Name>port1</Name>
        <Port>80</Port>
    </FrontendPort>
</FrontendPorts>
<Probes>
    <Probe>
        <Name>Probe01</Name>
        <Protocol>Http</Protocol>
        <Host>contoso.com</Host>
        <Path>/path/custompath.htm</Path>
        <Interval>15</Interval>
        <Timeout>15</Timeout>
        <UnhealthyThreshold>5</UnhealthyThreshold>
    </Probe>
    </Probes>
    <BackendAddressPools>
    <BackendAddressPool>
        <Name>pool1</Name>
        <IPAddresses>
            <IPAddress>1.1.1.1</IPAddress>
            <IPAddress>2.2.2.2</IPAddress>
        </IPAddresses>
    </BackendAddressPool>
</BackendAddressPools>
<BackendHttpSettingsList>
    <BackendHttpSettings>
        <Name>setting1</Name>
        <Port>80</Port>
        <Protocol>Http</Protocol>
        <CookieBasedAffinity>Enabled</CookieBasedAffinity>
        <RequestTimeout>120</RequestTimeout>
        <Probe>Probe01</Probe>
    </BackendHttpSettings>
</BackendHttpSettingsList>
<HttpListeners>
    <HttpListener>
        <Name>listener1</Name>
        <FrontendIP>fip1</FrontendIP>
    <FrontendPort>port1</FrontendPort>
        <Protocol>Http</Protocol>
    </HttpListener>
</HttpListeners>
<HttpLoadBalancingRules>
    <HttpLoadBalancingRule>
        <Name>lbrule1</Name>
        <Type>basic</Type>
        <BackendHttpSettings>setting1</BackendHttpSettings>
        <Listener>listener1</Listener>
        <BackendAddressPool>pool1</BackendAddressPool>
    </HttpLoadBalancingRule>
</HttpLoadBalancingRules>
</ApplicationGatewayConfiguration>

编辑配置项的括号之间的值。Edit the values between the parentheses for the configuration items. 使用扩展名 .xml 保存文件。Save the file with extension .xml.

以下示例演示如何使用配置文件设置应用程序网关负载均衡公共端口 80 上的 HTTP 流量,然后使用自定义探测将网络流量发送到两个 IP 地址之间的后端端口 80。The following example shows how to use a configuration file to set up the application gateway to load balance HTTP traffic on public port 80 and send network traffic to back-end port 80 between two IP addresses by using a custom probe.

Important

协议项 Http 或 Https 区分大小写。

已添加用于配置自定义探测的新配置项 <Probe>。A new configuration item <Probe> is added to configure custom probes.

配置参数为:The configuration parameters are:

参数Parameter 说明Description
名称Name 自定义探测的引用名称。Reference name for custom probe.
协议Protocol 使用的协议(可能的值为 HTTP 或 HTTPS)。Protocol used (possible values are HTTP or HTTPS).
HostPathHost and Path 应用程序网关为了确定实例运行状况而调用的完整 URL 路径。Complete URL path that is invoked by the application gateway to determine the health of the instance. 例如,如果网站为 http://contoso.com/,则可以为“http://contoso.com/path/custompath.htm”配置自定义探测,使探测检查能够获得成功的 HTTP 响应。For example, if you have a website http://contoso.com/, then the custom probe can be configured for "http://contoso.com/path/custompath.htm" for probe checks to have a successful HTTP response.
时间间隔Interval 配置探测检查间隔(以秒为单位)。Configures the probe interval checks in seconds.
超时Timeout 定义 HTTP 响应检查的探测超时。Defines the probe time-out for an HTTP response check.
UnhealthyThresholdUnhealthyThreshold 将后端实例标记为“不正常”所需的失败 HTTP 响应数目。The number of failed HTTP responses needed to flag the back-end instance as unhealthy.

<BackendHttpSettings> 配置中会引用探测名称,以分配使用自定义探测设置的后端池。The probe name is referenced in the <BackendHttpSettings> configuration to assign which back-end pool uses custom probe settings.

将自定义探测添加到现有应用程序网关Add a custom probe to an existing application gateway

更改当前的应用程序网关配置需要完成三个步骤:获取当前的 XML 配置文件,对其进行修改以创建自定义探测,使用新的 XML 设置来配置应用程序网关。Changing the current configuration of an application gateway requires three steps: Get the current XML configuration file, modify to have a custom probe, and configure the application gateway with the new XML settings.

  1. 使用 Get-AzureApplicationGatewayConfig 获取 XML 文件。Get the XML file by using Get-AzureApplicationGatewayConfig. 此 cmdlet 会导出要修改的配置 XML 以添加探测设置。This cmdlet exports the configuration XML to be modified to add a probe setting.

    Get-AzureApplicationGatewayConfig -Name "<application gateway name>" -Exporttofile "<path to file>"
    
  2. 在文本编辑器中打开 XML 文件。Open the XML file in a text editor. <probe> 节添加到 <frontendport> 的后面。Add a <probe> section after <frontendport>.

    <Probes>
     <Probe>
         <Name>Probe01</Name>
         <Protocol>Http</Protocol>
         <Host>contoso.com</Host>
         <Path>/path/custompath.htm</Path>
         <Interval>15</Interval>
         <Timeout>15</Timeout>
         <UnhealthyThreshold>5</UnhealthyThreshold>
     </Probe>
    </Probes>
    

    在 XML 的 backendHttpSettings 节中,添加以下示例中所示的探测名称:In the backendHttpSettings section of the XML, add the probe name as shown in the following example:

     <BackendHttpSettings>
         <Name>setting1</Name>
         <Port>80</Port>
         <Protocol>Http</Protocol>
         <CookieBasedAffinity>Enabled</CookieBasedAffinity>
         <RequestTimeout>120</RequestTimeout>
         <Probe>Probe01</Probe>
     </BackendHttpSettings>
    

    保存该 XML 文件。Save the XML file.

  3. 通过 Set-AzureApplicationGatewayConfig 使用新的 XML 文件更新应用程序网关配置。Update the application gateway configuration with the new XML file by using Set-AzureApplicationGatewayConfig. 此 cmdlet 使用新配置更新应用程序网关。This cmdlet updates your application gateway with the new configuration.

Set-AzureApplicationGatewayConfig -Name "<application gateway name>" -Configfile "<path to file>"

后续步骤Next steps

如果要配置安全套接字层 (SSL) 卸载,请参阅配置应用程序网关以进行 SSL 卸载If you want to configure Secure Sockets Layer (SSL) offload, see Configure an application gateway for SSL offload.

如果要将应用程序网关配置为与内部负载均衡器配合使用,请参阅 Create an application gateway with an internal load balancer (ILB)(创建具有内部负载均衡器 (ILB) 的应用程序网关)。If you want to configure an application gateway to use with an internal load balancer, see Create an application gateway with an internal load balancer (ILB).