使用 Azure CLI 根據 URL 路由傳送網路流量

身為管理 Web 流量的 IT 系統管理員,您會想協助客戶和使用者盡快取得他們所需的資訊。 讓使用者獲得最佳體驗的方法之一,就是將不同種類的 Web 流量路由傳送到不同的伺服器資源。 本文說明如何針對來自您應用程式的不同流量類型,使用 Azure CLI 來安裝及設定應用程式閘道路由。 路由接著會根據 URL,將流量導向不同的伺服器集區。

URL routing example

在本文中,您將學會如何:

  • 為您所需的網路資源建立資源群組
  • 建立網路資源
  • 針對來自您應用程式的流量,建立應用程式閘道
  • 針對不同類型的流量,指定伺服器集區和路由規則
  • 建立每個集區的擴展集,以便自動調整集區
  • 執行測試,以便確認不同類型的流量會前往正確的集區

您可以依偏好使用 Azure PowerShellAzure 入口網站來完成此程序。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

  • 本教學課程需要 2.0.4 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

建立資源群組

資源群組是一種邏輯容器,您會在其中部署與管理 Azure 資源。 使用 az group create (機器翻譯) 建立資源群組。

下列範例會在 eastus 位置建立名為 myResourceGroupAG 的資源群組。

az group create --name myResourceGroupAG --location eastus

建立網路資源

使用 az network vnet create 建立名為 myVNet 的虛擬網路,以及名為 myAGSubnet 的子網路。 然後使用 az network vnet subnet create 新增名為 myBackendSubnet 的子網路,後端伺服器需要該子網路。 使用 az network public-ip create 建立名為 myAGPublicIPAddress 的公用 IP 位址。

az network vnet create \
  --name myVNet \
  --resource-group myResourceGroupAG \
  --location eastus \
  --address-prefix 10.0.0.0/16 \
  --subnet-name myAGSubnet \
  --subnet-prefix 10.0.1.0/24

az network vnet subnet create \
  --name myBackendSubnet \
  --resource-group myResourceGroupAG \
  --vnet-name myVNet \
  --address-prefix 10.0.2.0/24

az network public-ip create \
  --resource-group myResourceGroupAG \
  --name myAGPublicIPAddress \
  --allocation-method Static \
  --sku Standard

建立包含 URL 對應的應用程式閘道

使用 az network application-gateway create 建立名為 myAppGateway 的應用程式閘道。 當您使用 Azure CLI 建立應用程式閘道時,需要指定設定資訊,例如容量、SKU 和 HTTP 設定。 應用程式閘道會指派給 myAGSubnet 和 myAGPublicIPAddress

az network application-gateway create \
  --name myAppGateway \
  --location eastus \
  --resource-group myResourceGroupAG \
  --vnet-name myVNet \
  --subnet myAGsubnet \
  --capacity 2 \
  --sku Standard_v2 \
  --http-settings-cookie-based-affinity Disabled \
  --frontend-port 80 \
  --http-settings-port 80 \
  --http-settings-protocol Http \
  --public-ip-address myAGPublicIPAddress \
  --priority 100

可能需要幾分鐘的時間來建立應用程式閘道。 建立應用程式閘道後,您可以看到這些新功能:

功能 描述
appGatewayBackendPool 應用程式閘道必須至少有一個後端位址集區。
appGatewayBackendHttpSettings 指定以連接埠 80 和 HTTP 通訊協定來進行通訊。
appGatewayHttpListener 與 appGatewayBackendPool 相關聯的預設接聽程式。
appGatewayFrontendIP 將 myAGPublicIPAddress 指派給 appGatewayHttpListener。
rule1 與 appGatewayHttpListener 相關聯的預設路由規則。

新增映像和視訊後端集區及連接埠

使用 az network application-gateway address-pool create,將名為 imagesBackendPool 和 videoBackendPool 的後端集區新增至應用程式閘道。 使用 az network application-gateway frontend-port create 新增集區的前端連接埠。

az network application-gateway address-pool create \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name imagesBackendPool

az network application-gateway address-pool create \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name videoBackendPool

az network application-gateway frontend-port create \
  --port 8080 \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name port8080

新增後端接聽程式

使用 az network application-gateway http-listener create 新增名為 backendListener、路由流量所需的後端接聽程式。

az network application-gateway http-listener create \
  --name backendListener \
  --frontend-ip appGatewayFrontendIP \
  --frontend-port port8080 \
  --resource-group myResourceGroupAG \
  --gateway-name myAppGateway

新增 URL 路徑對應

URL 路徑對應可確保特定 URL 會路由傳送到特定的後端集區。 使用 az network application-gateway url-path-map createaz network application-gateway url-path-map rule create,建立名為 imagePathRule 和 videoPathRule 的 URL 路徑對應。

az network application-gateway url-path-map create \
  --gateway-name myAppGateway \
  --name myPathMap \
  --paths /images/* \
  --resource-group myResourceGroupAG \
  --address-pool imagesBackendPool \
  --default-address-pool appGatewayBackendPool \
  --default-http-settings appGatewayBackendHttpSettings \
  --http-settings appGatewayBackendHttpSettings \
  --rule-name imagePathRule

az network application-gateway url-path-map rule create \
  --gateway-name myAppGateway \
  --name videoPathRule \
  --resource-group myResourceGroupAG \
  --path-map-name myPathMap \
  --paths /video/* \
  --address-pool videoBackendPool \
  --http-settings appGatewayBackendHttpSettings

新增路由規則

路由規則會將 URL 對應與您所建立的接聽程式產生關聯。 使用 az network application-gateway rule create 新增名為 rule2 的規則。

az network application-gateway rule create \
  --gateway-name myAppGateway \
  --name rule2 \
  --resource-group myResourceGroupAG \
  --http-listener backendListener \
  --rule-type PathBasedRouting \
  --url-path-map myPathMap \
  --address-pool appGatewayBackendPool \
  --priority 200

建立虛擬機器擴展集

在本文中,您會建立三個虛擬機器擴展集,以支援您所建立的三個後端集區。 建立名為 myvmss1、myvmss2 和 myvmss3 的擴展集。 每個擴展集都會包含兩個您安裝 NGINX 的虛擬機器執行個體。

for i in `seq 1 3`; do

  if [ $i -eq 1 ]
  then
    poolName="appGatewayBackendPool"
  fi

  if [ $i -eq 2 ]
  then
    poolName="imagesBackendPool"
  fi

  if [ $i -eq 3 ]
  then
    poolName="videoBackendPool"
  fi

  az vmss create \
    --name myvmss$i \
    --resource-group myResourceGroupAG \
    --image Ubuntu2204 \
    --admin-username azureuser \
    --admin-password Azure123456! \
    --instance-count 2 \
    --vnet-name myVNet \
    --subnet myBackendSubnet \
    --vm-sku Standard_DS2 \
    --upgrade-policy-mode Automatic \
    --app-gateway myAppGateway \
    --backend-pool-name $poolName
done

安裝 NGINX

for i in `seq 1 3`; do
  az vmss extension set \
    --publisher Microsoft.Azure.Extensions \
    --version 2.0 \
    --name CustomScript \
    --resource-group myResourceGroupAG \
    --vmss-name myvmss$i \
    --settings '{ "fileUris": ["https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/install_nginx.sh"], "commandToExecute": "./install_nginx.sh" }'
done

測試應用程式閘道

若要取得應用程式閘道的公用 IP 位址,請使用 az network public-ip show。 將公用 IP 位址複製並貼到您瀏覽器的網址列。 例如 http://40.121.222.19http://40.121.222.19:8080/images/test.htmhttp://40.121.222.19:8080/video/test.htm

az network public-ip show \
  --resource-group myResourceGroupAG \
  --name myAGPublicIPAddress \
  --query [ipAddress] \
  --output tsv

Test base URL in application gateway

將 URL 變更為 http://<ip-address>:8080/images/test.html、使用您的 IP 位址取代 <ip-address>,您應該會看到類似下列的範例:

Test images URL in application gateway

將 URL 變更為 http://<ip-address>:8080/video/test.html、使用您的 IP 位址取代 <ip-address>,您應該會看到類似下列的範例。

Test video URL in application gateway

清除資源

若不再需要,可移除資源群組、應用程式閘道和所有相關資源。

az group delete --name myResourceGroupAG

下一步

建立包含 URL 路徑型重新導向的應用程式閘道