共用方式為


快速入門:使用 Azure CLI 建立高可用性 Web 應用程式的流量管理員配置檔

本快速入門會說明如何建立流量管理員設定檔,以便為 Web 應用程式提供高可用性。

在本快速入門中,您會建立 Web 應用程式的兩個執行個體。 每個執行個體會在不同的 Azure 區域中執行。 您會建立以端點優先順序為基礎的流量管理員設定檔。 此設定檔會將使用者流量導向執行 Web 應用程式的主要網站。 流量管理員會持續監視 Web 應用程式。 如果主要網站無法使用,它會提供自動容錯移轉至備份網站。

流量管理員部署環境的圖表。

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

先決條件

  • 本文需要 2.0.28 版或更新版本的 Azure CLI。 如果使用 Azure Cloud Shell,則已安裝最新版本。

建立資源群組

使用 az group create 來建立資源群組。 Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。

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


  az group create \
    --name myResourceGroup \
    --location eastus

建立流量管理員設定檔

使用 az network traffic-manager profile create 建立流量管理員配置檔,以根據端點優先順序引導使用者流量。


mytrafficmanagerprofile='mytrafficmanagerprofile'$RANDOM

az network traffic-manager profile create \
	--name $mytrafficmanagerprofile \
	--resource-group myResourceGroup \
	--routing-method Priority \
	--path '/' \
	--protocol "HTTP" \
	--unique-dns-name $mytrafficmanagerprofile  \
	--ttl 30 \
--port 80

建立 Web 應用程式

在本快速入門中,您將需要兩個部署在兩個不同 Azure 區域(美國東部西歐)的 Web 應用程式實例。 每個執行個體都會作為流量管理員的主要和容錯移轉端點。

建立網頁應用服務方案

使用 az appservice plan create 建立 Web App Service 方案,以針對您將部署在兩個不同的 Azure 區域中的兩個 Web 應用程式實例。


az appservice plan create \
    --name myAppServicePlanEastUS \
    --resource-group myResourceGroup \
    --location eastus \
    --sku S1

az appservice plan create \
    --name myAppServicePlanWestEurope \
    --resource-group myResourceGroup \
    --location westeurope \
    --sku S1

在 App Service 方案中建立 Web 應用程式

美國東部西歐 Azure 區域的 App Service 方案中,使用 az webapp create 建立 Web 應用程式兩個實例。


mywebappeastus='myWebAppEastUS'$RANDOM
myWebAppWestEurope='myWebAppWestEurope'$RANDOM

az webapp create \
    --name $mywebappeastus \
    --plan myAppServicePlanEastUS \
    --resource-group myResourceGroup

az webapp create \
    --name $myWebAppWestEurope \
    --plan myAppServicePlanWestEurope \
    --resource-group myResourceGroup

新增流量管理員端點

使用 az network traffic-manager endpoint create 將兩個 Web Apps 新增為流量管理員端點至流量管理員配置檔,如下所示:

  • 判斷 Web 應用程式識別碼,並將位於 美國東部 Azure 區域的 Web 應用程式新增為主要端點,以路由傳送所有使用者流量。
  • 判斷 Web 應用程式識別碼,並將位於 西歐 Azure 區域的 Web 應用程式新增為故障轉移端點。

當主要端點無法使用時,流量就會自動路由傳送到容錯移轉端點。

美國東部端點


App1ResourceId=$(az webapp show --name $mywebappeastus --resource-group myResourceGroup --query id --output tsv)

az network traffic-manager endpoint create \
    --name $mywebappeastus \
    --resource-group myResourceGroup \
    --profile-name $mytrafficmanagerprofile \
    --type azureEndpoints \
    --target-resource-id $App1ResourceId \
    --priority 1 \
    --endpoint-status Enabled

西歐端點


App2ResourceId=$(az webapp show --name $myWebAppWestEurope --resource-group myResourceGroup --query id --output tsv)

az network traffic-manager endpoint create \
    --name $myWebAppWestEurope \
    --resource-group myResourceGroup \
    --profile-name $mytrafficmanagerprofile \
    --type azureEndpoints \
    --target-resource-id  $App2ResourceId \
    --priority 2 \
    --endpoint-status Enabled

測試流量管理器設定檔

在本節中,您會檢查流量管理員設定檔的網域名稱。 您也會將主要端點設定為無法使用。 最後,您可以看到 Web 應用程式仍可使用。 這是因為流量管理員將流量傳送至容錯移轉端點。

在下列範例中,將 <app1name_eastus><app2name_westeurope> 替換為上一節中為每個區域所建立的應用程式名稱。 然後將<profile_name>替換為上一節中使用的配置檔名稱。

確定 DNS 名稱

使用 az network traffic-manager profile show 來判斷流量管理員配置檔的 DNS 名稱。


az network traffic-manager profile show \
    --name $mytrafficmanagerprofile \
    --resource-group myResourceGroup \
    --query dnsConfig.fqdn

複製 RelativeDnsName 值。 流量管理員設定檔的 DNS 名稱為 http://<relativednsname>.trafficmanager.net

檢視流量管理員的運作

  1. 在網頁瀏覽器中,輸入流量管理員設定檔的 DNS 名稱 (http://<relativednsname>.trafficmanager.net),以檢視 Web 應用程式的預設網站。

    備註

    在此快速入門案例中,所有要求都會路由傳送至主要端點。 它會設定為 [優先順序 1]

  2. 若要檢視進行中的 Traffic Manager 故障轉移,請使用 az network traffic-manager endpoint update 停用您的主要站台。

    
     az network traffic-manager endpoint update \
         --name $mywebappeastus \
         --resource-group myResourceGroup \
         --profile-name $mytrafficmanagerprofile \
         --type azureEndpoints \
         --endpoint-status Disabled
    
    
  3. 複製流量管理員設定檔的 DNS 名稱 (http://<relativednsname>.trafficmanager.net),以在新的網頁瀏覽器工作階段中檢視網站。

  4. 確認 Web 應用程式仍可使用。

清理資源

完成時,請使用 az group delete 刪除資源群組、Web 應用程式和所有相關資源。


az group delete \
    --resource-group myResourceGroup

後續步驟

在本快速入門中,您已建立流量管理員設定檔,以便為 Web 應用程式提供高可用性。 若要深入了解如何路由傳送流量,請繼續進行流量管理員的教學課程。