快速入門:使用 Azure CLI 為高可用性 Web 應用程式建立流量管理員設定檔
本快速入門說明如何建立可為您的 Web 應用程式提供高可用性的流量管理員設定檔。
在本快速入門中,您將建立兩個 Web 應用程式的實例。 每一個都在不同的 Azure 區域中執行。 您將根據 端點優先順序 建立流量管理員設定檔。 設定檔會將使用者流量導向執行 Web 應用程式的主要月臺。 流量管理員會持續監視 Web 應用程式。 如果主要月臺無法使用,它會提供自動容錯移轉至備份月臺。
如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶。
必要條件
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱 Azure Cloud Shell 中的 Bash 快速入門。
若要在本地執行 CLI 參考命令,請安裝 Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱〈如何在 Docker 容器中執行 Azure CLI〉。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,完成驗證程序。 如需其他登入選項,請參閱使用 Azure CLI 登入。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能詳細資訊,請參閱使用 Azure CLI 擴充功能。
執行 az version 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
- 本文需要 Azure CLI 2.0.28 版或更新版本。 如果您是使用 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 應用程式實例。 每個都會作為流量管理員的主要和容錯移轉端點。
建立 Web App Service 方案
使用 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。 >
檢視運作中的流量管理員
在網頁瀏覽器中,輸入流量管理員設定檔的 DNS 名稱( HTTP:// relativednsname.trafficmanager.net <> ),以檢視 Web 應用程式的預設網站。
注意
在本快速入門案例中,所有要求都會路由傳送至主要端點。 它設定為 優先順序 1 。
若要檢視流量管理員運作中的容錯移轉,請使用 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
複製流量管理員設定檔的 DNS 名稱( HTTP:// relativednsname.trafficmanager.net <> ),以在新網頁瀏覽器會話中檢視網站。
確認 Web 應用程式仍然可用。
清除資源
完成時,請使用 az group delete 刪除資源群組、Web 應用程式和所有相關資源。
az group delete \
--resource-group myResourceGroup
下一步
在本快速入門中,您已建立流量管理員設定檔,為您的 Web 應用程式提供高可用性。 若要深入瞭解路由流量,請繼續進行流量管理員教學課程。