使用 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來驗證。

    Connect-AzAccount
    
  2. 取得帳戶的訂用帳戶。

    Get-AzSubscription
    
  3. 選擇其中一個要使用的 Azure 訂用帳戶。

    Select-AzSubscription -Subscriptionid '{subscriptionGuid}'
    
  4. 建立資源群組。 如果您有現有的資源群組,則可略過此步驟。

    New-AzResourceGroup -Name appgw-rg -Location 'West US'
    

Azure 資源管理員需要所有的資源群組指定一個位置。 這個位置決定了 Azure 將資源群組元資料存放在哪裡。 請確定所有用來建立應用程式閘道的命令都使用同一個資源群組。

在上述範例中,我們建立名為 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 West US 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 'West US' -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 中建立一個名為 publicIP01 的標準 SKU 靜態公共 IP 資源。 應用閘道 v2 僅支援標準 SKU 靜態公共 IP 位址。

$publicip = New-AzPublicIpAddress -ResourceGroupName appgw-rg -Name publicIP01 -Location 'West US' -AllocationMethod Static -Sku Standard

建立應用程式閘道

您先設定所有組態項目,再建立應用程式閘道。 下列範例會建立應用程式閘道資源所需的組態項目。

元件 說明
閘道 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 'West US' -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 卸載