在本文中,将使用 PowerShell 向现有应用程序网关添加自定义探测。 对于具有特定运行状况检查页面的应用程序,或者对于无法在默认 Web 应用上返回成功响应的应用程序,自定义探测非常有用。
注释
建议使用 Azure Az PowerShell 模块与 Azure 交互。 若要开始,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az。
先决条件:安装 Azure PowerShell 模块
若要执行本文中的步骤,需要安装和配置 Azure PowerShell 模块。 请务必完成所有指令。 安装完成后,请登录到 Azure,然后选择订阅。
注释
需要一个 Azure 帐户来完成这些步骤。 如果没有Azure帐户,可以注册试用版。
创建使用自定义探测的应用程序网关
登录并创建资源组
使用
Connect-AzAccount -Environment AzureChinaCloud进行身份验证。Connect-AzAccount -Environment AzureChinaCloud获取该帐户的订阅。
Get-AzSubscription选择要使用的 Azure 订阅。
Select-AzSubscription -Subscriptionid '{subscriptionGuid}'创建一个资源组。 如果已有资源组,可跳过此步骤。
New-AzResourceGroup -Name appgw-rg -Location 'China North 2'
Azure 资源管理器要求所有资源组指定一个位置。 该位置决定了 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 地址
在“中国北部 2”区域的资源组 appgw-rg 中创建名为 publicIP01 的标准 SKU 静态公共 IP 资源。 应用网关 v2 仅支持标准 SKU 静态公共 IP 地址。
$publicip = New-AzPublicIpAddress -ResourceGroupName appgw-rg -Name publicIP01 -Location 'China North 2' -AllocationMethod Static -Sku Standard
创建应用程序网关
在创建应用程序网关之前设置所有配置项。 以下示例将创建应用程序网关资源所需的配置项。
| 组件 | 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 30 -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 previously defined HTTP settings and backend address pool. It also associates the listener with the rule.
$rule = New-AzApplicationGatewayRequestRoutingRule -Name rule01 -RuleType Basic -BackendHttpSettings $poolSetting -HttpListener $listener -BackendAddressPool $pool -Priority 1
# Sets the SKU of the application gateway. In this example, we create a Standard v2 application gateway with two instances.
$sku = New-AzApplicationGatewaySku -Name Standard_v2 -Tier Standard_v2 -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 30 -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
获取应用网关的公共IP地址
创建网关后,取回它的静态公共IP地址。 要使用自定义域名,创建一个A记录将域名映射到该地址。 欲了解更多信息,请参见“将自定义域名映射到Azure资源。
$publicip = Get-AzPublicIpAddress -ResourceGroupName appgw-rg -Name publicIP01
$publicip.IpAddress
后续步骤
了解如何通过访问配置 TLS 卸载来配置 TLS 卸载