快速入門:使用 Azure PowerShell 透過 Azure 應用程式閘道 引導 Web 流量
在本快速入門中,您會使用 Azure PowerShell 來建立應用程式閘道。 然後,您會進行測試以確定能正常運作。
應用程式閘道會將應用程式 Web 流量導向後端集區中的特定資源。 您可以將接聽程式指派給埠、建立規則,以及將資源新增至後端集區。 為了簡單起見,本文使用具有公用前端IP位址的簡單設定、在應用程式閘道上裝載單一月臺的基本接聽程式、基本要求路由規則,以及後端集區中的兩部虛擬機。
您也可以使用 Azure CLI 或 Azure 入口網站 來完成本快速入門。
必要條件
- 具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶。
- Azure PowerShell 1.0.0 版或更新版本 (如果您在本機執行 Azure PowerShell)。
Azure Cloud Shell
Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。
要啟動 Azure Cloud Shell:
選項 | 範例/連結 |
---|---|
選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 | |
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 | |
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 |
若要使用 Azure Cloud Shell:
啟動 Cloud Shell。
選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。
透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。
選取 Enter 鍵執行程式碼或命令。
注意
建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az。
連接到 Azure
若要與 Azure 連線,請執行 Connect-AzAccount
。
建立資源群組
在 Azure 中,您會將相關的資源配置給資源群組。 您可以使用現有的資源群組或建立新的資源群組。
若要建立新的資源群組,請使用 New-AzResourceGroup
Cmdlet:
New-AzResourceGroup -Name myResourceGroupAG -Location eastus
建立網路資源
若要讓 Azure 在您建立的資源之間進行通訊,它需要虛擬網路。 應用程式閘道子網只能包含應用程式閘道。 不允許其他資源。 您可以為 應用程式閘道 建立新的子網,或使用現有的子網。 在此範例中,您會建立兩個子網:一個用於應用程式閘道,另一個用於後端伺服器。 您可以根據您的使用案例,將 應用程式閘道 的前端IP位址設定為公用或私人。 在此範例中,您將選擇公用前端IP位址。
- 使用
New-AzVirtualNetworkSubnetConfig
建立子網組態。 - 使用
New-AzVirtualNetwork
建立具有子網組態的虛擬網路。 - 使用
New-AzPublicIpAddress
建立公用IP位址。
注意
$agSubnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name myAGSubnet `
-AddressPrefix 10.21.0.0/24
$backendSubnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name myBackendSubnet `
-AddressPrefix 10.21.1.0/24
New-AzVirtualNetwork `
-ResourceGroupName myResourceGroupAG `
-Location eastus `
-Name myVNet `
-AddressPrefix 10.21.0.0/16 `
-Subnet $agSubnetConfig, $backendSubnetConfig
New-AzPublicIpAddress `
-ResourceGroupName myResourceGroupAG `
-Location eastus `
-Name myAGPublicIPAddress `
-AllocationMethod Static `
-Sku Standard
建立應用程式閘道
建立 IP 設定與前端連接埠
- 使用
New-AzApplicationGatewayIPConfiguration
來建立組態,將您所建立的子網與應用程式網關建立關聯。 - 使用
New-AzApplicationGatewayFrontendIPConfig
建立組態,以指派您先前為應用程式閘道建立的公用IP位址。 - 使用
New-AzApplicationGatewayFrontendPort
指派埠 80 來存取應用程式閘道。
$vnet = Get-AzVirtualNetwork -ResourceGroupName myResourceGroupAG -Name myVNet
$subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name myAGSubnet
$pip = Get-AzPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress
$gipconfig = New-AzApplicationGatewayIPConfiguration `
-Name myAGIPConfig `
-Subnet $subnet
$fipconfig = New-AzApplicationGatewayFrontendIPConfig `
-Name myAGFrontendIPConfig `
-PublicIPAddress $pip
$frontendport = New-AzApplicationGatewayFrontendPort `
-Name myFrontendPort `
-Port 80
注意
應用程式閘道 前端現在支援雙堆疊 IP 位址(公開預覽)。 您現在可以建立最多四個前端IP位址:兩個IPv4位址(公用和私人)和兩個IPv6位址(公用和私人)。
建立後端集區
- 使用
New-AzApplicationGatewayBackendAddressPool
建立應用程式閘道的後端集區。 後端集區目前是空的。 在下一節中建立後端伺服器 NIC 時,您會將它們新增至後端集區。 - 使用
New-AzApplicationGatewayBackendHttpSetting
設定後端集區的設定。
$backendPool = New-AzApplicationGatewayBackendAddressPool `
-Name myAGBackendPool
$poolSettings = New-AzApplicationGatewayBackendHttpSetting `
-Name myPoolSettings `
-Port 80 `
-Protocol Http `
-CookieBasedAffinity Enabled `
-RequestTimeout 30
建立接聽程式並新增規則
Azure 需要接聽程式才能啟用應用程式閘道,以便適當地將流量路由傳送至後端集區。 Azure 也需要規則讓接聽程式知道要用於傳入流量的後端集區。
- 使用
New-AzApplicationGatewayHttpListener
搭配您先前建立的前端組態和前端埠來建立接聽程式。 - 用來
New-AzApplicationGatewayRequestRoutingRule
建立名為 rule1 的規則。
$defaultlistener = New-AzApplicationGatewayHttpListener `
-Name myAGListener `
-Protocol Http `
-FrontendIPConfiguration $fipconfig `
-FrontendPort $frontendport
$frontendRule = New-AzApplicationGatewayRequestRoutingRule `
-Name rule1 `
-RuleType Basic `
-Priority 100 `
-HttpListener $defaultlistener `
-BackendAddressPool $backendPool `
-BackendHttpSettings $poolSettings
建立應用程式閘道
既然您已建立必要的支援資源,請建立應用程式閘道:
- 使用
New-AzApplicationGatewaySku
來指定應用程式閘道的參數。 - 使用
New-AzApplicationGateway
來建立應用程式閘道。
$sku = New-AzApplicationGatewaySku `
-Name Standard_v2 `
-Tier Standard_v2 `
-Capacity 2
New-AzApplicationGateway `
-Name myAppGateway `
-ResourceGroupName myResourceGroupAG `
-Location eastus `
-BackendAddressPools $backendPool `
-BackendHttpSettingsCollection $poolSettings `
-FrontendIpConfigurations $fipconfig `
-GatewayIpConfigurations $gipconfig `
-FrontendPorts $frontendport `
-HttpListeners $defaultlistener `
-RequestRoutingRules $frontendRule `
-Sku $sku
後端伺服器
既然您已建立 應用程式閘道,請建立將裝載網站的後端虛擬機。 後端可以由 NIC、虛擬機擴展集、公用 IP 位址、內部 IP 位址、完整功能變數名稱 (FQDN) 和多租使用者後端組成,例如 Azure App 服務。
在此範例中,您會建立兩部虛擬機,以作為應用程式網關的後端伺服器。 您也可以在虛擬機上安裝 IIS,以確認 Azure 已成功建立應用程式閘道。
建立兩部虛擬機器
- 使用 取得最近建立 應用程式閘道 後端集區
Get-AzApplicationGatewayBackendAddressPool
組態。 - 使用
New-AzNetworkInterface
建立網路介面。 - 使用
New-AzVMConfig
建立虛擬機組態。 - 使用
New-AzVM
建立虛擬機。
當您執行下列程式代碼範例來建立虛擬機時,Azure 會提示您輸入認證。 輸入使用者名稱與密碼:
$appgw = Get-AzApplicationGateway -ResourceGroupName myResourceGroupAG -Name myAppGateway
$backendPool = Get-AzApplicationGatewayBackendAddressPool -Name myAGBackendPool -ApplicationGateway $appgw
$vnet = Get-AzVirtualNetwork -ResourceGroupName myResourceGroupAG -Name myVNet
$subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet -Name myBackendSubnet
$cred = Get-Credential
for ($i=1; $i -le 2; $i++)
{
$nic = New-AzNetworkInterface `
-Name myNic$i `
-ResourceGroupName myResourceGroupAG `
-Location EastUS `
-Subnet $subnet `
-ApplicationGatewayBackendAddressPool $backendpool
$vm = New-AzVMConfig `
-VMName myVM$i `
-VMSize Standard_DS2_v2
Set-AzVMOperatingSystem `
-VM $vm `
-Windows `
-ComputerName myVM$i `
-Credential $cred
Set-AzVMSourceImage `
-VM $vm `
-PublisherName MicrosoftWindowsServer `
-Offer WindowsServer `
-Skus 2016-Datacenter `
-Version latest
Add-AzVMNetworkInterface `
-VM $vm `
-Id $nic.Id
Set-AzVMBootDiagnostic `
-VM $vm `
-Disable
New-AzVM -ResourceGroupName myResourceGroupAG -Location EastUS -VM $vm
Set-AzVMExtension `
-ResourceGroupName myResourceGroupAG `
-ExtensionName IIS `
-VMName myVM$i `
-Publisher Microsoft.Compute `
-ExtensionType CustomScriptExtension `
-TypeHandlerVersion 1.4 `
-SettingString '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}' `
-Location EastUS
}
測試應用程式閘道
雖然不需要 IIS 才能建立應用程式閘道,但您在本快速入門中安裝了它,以確認 Azure 是否已成功建立應用程式閘道。
使用 IIS 測試應用程式閘道:
- 執行
Get-AzPublicIPAddress
以取得應用程式閘道的公用IP位址。 - 將公用IP位址複製並貼到瀏覽器的網址列中。 當您重新整理瀏覽器時,應該會看到虛擬機的名稱。 有效的回應會確認應用程式閘道已成功建立,而且可以成功與後端連線。
Get-AzPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress
清除資源
當您不再需要使用應用程式閘道建立的資源時,請刪除資源群組。 當您刪除資源群組時,也會刪除應用程式閘道及其所有相關資源。
呼叫 Remove-AzResourceGroup
Cmdlet 以刪除資源群組:
Remove-AzResourceGroup -Name myResourceGroupAG