使用适用于 Azure 资源管理器的 PowerShell 创建 Azure 应用程序网关的自定义探测

在本文中,将使用 PowerShell 向现有应用程序网关添加自定义探测。 如果应用程序包含特定运行状况检查页面。或者未在默认 Web 应用程序上提供成功的响应,那么它们非常适合使用自定义探测。

注释

建议使用 Azure Az PowerShell 模块与 Azure 交互。 请参阅安装 Azure PowerShell 以开始使用。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

先决条件:安装 Azure PowerShell 模块

若要执行本文中的步骤,需要安装和配置 Azure PowerShell 模块。 请务必完成所有指令。 安装完成后,请登录到 Azure,然后选择订阅。

注释

需要一个 Azure 帐户来完成这些步骤。 如果没有Azure帐户,可以注册试用版

创建使用自定义探测的应用程序网关

登录并创建资源组

  1. 使用 Connect-AzAccount -Environment AzureChinaCloud 进行身份验证。

    Connect-AzAccount -Environment AzureChinaCloud
    
  2. 获取该帐户的订阅。

    Get-AzSubscription
    
  3. 选择要使用的 Azure 订阅。

    Select-AzSubscription -Subscriptionid '{subscriptionGuid}'
    
  4. 创建一个资源组。 如果已有资源组,可跳过此步骤。

    New-AzResourceGroup -Name appgw-rg -Location 'China North 2'
    

Azure 资源管理器要求所有资源组指定一个位置。 此位置用作该资源组中资源的默认位置。 请确保用于创建应用程序网关的所有命令都使用相同的资源组。

在前面的示例中,我们在中国北部 2 的位置创建了名为 appgw-RG 的资源组。

创建虚拟网络和子网

以下示例将为应用程序网关创建虚拟网络和子网。 应用程序网关需要具有自己的子网才可供使用。 为此,为应用程序网关创建的子网应小于 VNET 的地址空间,以便创建和使用其他子网。

# Assign the address range 10.0.0.0/24 to a subnet variable to be used to create a virtual network.
$subnet = New-AzVirtualNetworkSubnetConfig -Name subnet01 -AddressPrefix 10.0.0.0/24

# Create a virtual network named appgwvnet in resource group appgw-rg for the China North 2 region using the prefix 10.0.0.0/16 with subnet 10.0.0.0/24.
$vnet = New-AzVirtualNetwork -Name appgwvnet -ResourceGroupName appgw-rg -Location 'China North 2' -AddressPrefix 10.0.0.0/16 -Subnet $subnet

# Assign a subnet variable for the next steps, which create an application gateway.
$subnet = $vnet.Subnets[0]

创建前端配置的公共 IP 地址

在资源组 appgw-rg 中为中国北部 2 区域创建公共 IP 资源 publicIP01。 此示例对应用程序网关的前端 IP 地址使用公共 IP 地址。 应用程序网关要求公共 IP 地址具有动态创建的 DNS 名称,因此 -DomainNameLabel 无法在创建公共 IP 地址期间指定该名称。

$publicip = New-AzPublicIpAddress -ResourceGroupName appgw-rg -Name publicIP01 -Location 'China North 2' -AllocationMethod Dynamic

创建应用程序网关

在创建应用程序网关之前设置所有配置项。 以下示例将创建应用程序网关资源所需的配置项。

组件 Description
网关 IP 配置 应用程序网关的 IP 配置。
后端池 由 IP 地址、FQDN 或 NIC 组成的池,这些池成员供托管 Web 应用程序的应用程序服务器使用
运行状况探测 用于监视后端池成员运行状况的自定义探测
HTTP 设置 端口、协议、基于 cookie 的相关性、探测和超时等一系列设置。 这些设置决定将流量路由到后端池成员的方式
前端端口 应用程序网关在该端口上侦听流量
侦听器 协议、前端 IP 配置和前端端口的组合。 侦听器用于侦听传入请求。
规则 基于 HTTP 设置将流量路由到相应的后端。
# Creates an application gateway Frontend IP configuration named gatewayIP01
$gipconfig = New-AzApplicationGatewayIPConfiguration -Name gatewayIP01 -Subnet $subnet

#Creates a backend IP address pool named pool01 with IP addresses 134.170.185.46, 134.170.188.221, 134.170.185.50.
$pool = New-AzApplicationGatewayBackendAddressPool -Name pool01 -BackendIPAddresses 134.170.185.46, 134.170.188.221, 134.170.185.50

# Creates a probe that will check health at http://contoso.com/path/path.htm
$probe = New-AzApplicationGatewayProbeConfig -Name probe01 -Protocol Http -HostName 'contoso.com' -Path '/path/path.htm' -Interval 30 -Timeout 120 -UnhealthyThreshold 8

# Creates the backend http settings to be used. This component references the $probe created in the previous command.
$poolSetting = New-AzApplicationGatewayBackendHttpSettings -Name poolsetting01 -Port 80 -Protocol Http -CookieBasedAffinity Disabled -Probe $probe -RequestTimeout 80

# Creates a frontend port for the application gateway to listen on port 80 that will be used by the listener.
$fp = New-AzApplicationGatewayFrontendPort -Name frontendport01 -Port 80

# Creates a frontend IP configuration. This associates the $publicip variable defined previously with the frontend IP that will be used by the listener.
$fipconfig = New-AzApplicationGatewayFrontendIPConfig -Name fipconfig01 -PublicIPAddress $publicip

# Creates the listener. The listener is a combination of protocol and the frontend IP configuration $fipconfig and frontend port $fp created in previous steps.
$listener = New-AzApplicationGatewayHttpListener -Name listener01  -Protocol Http -FrontendIPConfiguration $fipconfig -FrontendPort $fp

# Creates the rule that routes traffic to the backend pools.  In this example we create a basic rule that uses the previous defined http settings and backend address pool.  It also associates the listener to the rule
$rule = New-AzApplicationGatewayRequestRoutingRule -Name rule01 -RuleType Basic -BackendHttpSettings $poolSetting -HttpListener $listener -BackendAddressPool $pool

# Sets the SKU of the application gateway, in this example we create a small standard application gateway with 2 instances.
$sku = New-AzApplicationGatewaySku -Name Standard_Small -Tier Standard -Capacity 2

# The final step creates the application gateway with all the previously defined components.
$appgw = New-AzApplicationGateway -Name appgwtest -ResourceGroupName appgw-rg -Location 'China North 2' -BackendAddressPools $pool -Probes $probe -BackendHttpSettingsCollection $poolSetting -FrontendIpConfigurations $fipconfig  -GatewayIpConfigurations $gipconfig -FrontendPorts $fp -HttpListeners $listener -RequestRoutingRules $rule -Sku $sku

将探测添加到现有应用程序网关

以下代码片段将向现有应用程序网关添加探测。

# Load the application gateway resource into a PowerShell variable by using Get-AzApplicationGateway.
$getgw =  Get-AzApplicationGateway -Name appgwtest -ResourceGroupName appgw-rg

# Create the probe object that will check health at http://contoso.com/path/path.htm
$probe = Add-AzApplicationGatewayProbeConfig -ApplicationGateway $getgw -Name probe01 -Protocol Http -HostName 'contoso.com' -Path '/path/custompath.htm' -Interval 30 -Timeout 120 -UnhealthyThreshold 8

# Set the backend HTTP settings to use the new probe
$getgw = Set-AzApplicationGatewayBackendHttpSettings -ApplicationGateway $getgw -Name $getgw.BackendHttpSettingsCollection.name -Port 80 -Protocol Http -CookieBasedAffinity Disabled -Probe $probe -RequestTimeout 120

# Save the application gateway with the configuration changes
Set-AzApplicationGateway -ApplicationGateway $getgw

从现有应用程序网关中删除探测

以下代码片段将从现有应用程序网关删除探测。

# Load the application gateway resource into a PowerShell variable by using Get-AzApplicationGateway.
$getgw =  Get-AzApplicationGateway -Name appgwtest -ResourceGroupName appgw-rg

# Remove the probe from the application gateway configuration object
$getgw = Remove-AzApplicationGatewayProbeConfig -ApplicationGateway $getgw -Name $getgw.Probes.name

# Set the backend HTTP settings to remove the reference to the probe. The backend http settings now use the default probe
$getgw = Set-AzApplicationGatewayBackendHttpSettings -ApplicationGateway $getgw -Name $getgw.BackendHttpSettingsCollection.name -Port 80 -Protocol http -CookieBasedAffinity Disabled

# Save the application gateway with the configuration changes
Set-AzApplicationGateway -ApplicationGateway $getgw

获取应用程序网关 DNS 名称

创建网关后,下一步是配置前端进行通信。 使用公共 IP 地址时,应用程序网关需要动态分配的 DNS 名称,这并不友好。 为了确保最终用户能够命中应用程序网关,可以使用 CNAME 记录指向应用程序网关的公共终结点。 在Azure中配置自定义域名。 为此,请使用附加到应用程序网关的 PublicIPAddress 元素检索应用程序网关及其关联的 IP/DNS 名称的详细信息。 应用程序网关的 DNS 名称应用于创建 CNAME 记录,该记录将两个 Web 应用程序指向此 DNS 名称。 不建议使用 A 记录,因为重启应用程序网关时 VIP 可能会更改。

Get-AzPublicIpAddress -ResourceGroupName appgw-RG -Name publicIP01
Name                     : publicIP01
ResourceGroupName        : appgw-RG
Location                 : chinanorth2
Id                       : /subscriptions/<subscription_id>/resourceGroups/appgw-RG/providers/Microsoft.Network/publicIPAddresses/publicIP01
Etag                     : W/"00000d5b-54ed-4907-bae8-99bd5766d0e5"
ResourceGuid             : 00000000-0000-0000-0000-000000000000
ProvisioningState        : Succeeded
Tags                     : 
PublicIpAllocationMethod : Dynamic
IpAddress                : xx.xx.xxx.xx
PublicIpAddressVersion   : IPv4
IdleTimeoutInMinutes     : 4
IpConfiguration          : {
                                "Id": "/subscriptions/<subscription_id>/resourceGroups/appgw-RG/providers/Microsoft.Network/applicationGateways/appgwtest/frontendIP
                            Configurations/frontend1"
                            }
DnsSettings              : {
                                "Fqdn": "00000000-0000-xxxx-xxxx-xxxxxxxxxxxx.chinacloudapp.cn"
                            }

后续步骤

了解如何通过访问配置 TLS 卸载来配置 TLS 卸载