Share via


Enrutamiento del tráfico para la alta disponibilidad de las aplicaciones con la CLI de Azure

Este script crea un grupo de recursos, dos planes de App Service, dos aplicaciones web, un perfil de Traffic Manager y dos puntos de conexión de Traffic Manager. Traffic Manager dirige el tráfico a la aplicación en una región como la región primaria y a la región secundaria cuando no está disponible la aplicación en la región primaria. Antes de ejecutar el script, debe cambiar los valores de MyWebApp, MyWebAppL1 y MyWebAppL2 a valores únicos en todo Azure. Después de ejecutar el script, puede tener acceso a la aplicación en la región primaria con la dirección URL mywebapp.trafficmanager.net.

Para ejecutar este ejemplo, instale la versión más reciente de la CLI de Azure. Para empezar, ejecute az login para crear una conexión con Azure.

Los ejemplos de la CLI de Azure están escritos para el shell bash. Para ejecutar este ejemplo en Windows PowerShell o en el símbolo del sistema, es posible que necesite cambiar algunos elementos del script.

Si no tiene una suscripción a Azure, cree una cuenta gratuita de Azure antes de empezar.

Script de ejemplo

#!/bin/bash
# Passed validation in Cloud Shell on 2/28/2022

# <FullScript>
# Route traffic for high availability of applications

# Variables for Traffic Manager resources
let "randomIdentifier=$RANDOM*$RANDOM"
location1="East US"
location2="West Europe"
resourceGroup1="msdocs-tm-rg1-$randomIdentifier"
resourceGroup2="msdocs-tm-rg2-$randomIdentifier"
tag="direct-traffic-for-increased-application-availability"
webApp="msdocs-webapp-tm-$randomIdentifier"
webAppL1="msdocs-tm-webapp-L1-$randomIdentifier"
webAppL2="msdocs-tm-webapp-L2-$randomIdentifier"
trafficManagerProfile="msdocs-traffic-manager-profile-$randomIdentifier"

# Create a resource group in location one
echo "Creating $resourceGroup1 in $location1..."
az group create --name $resourceGroup1 --location "$location1" --tags $tag

# Create a resource group in location two
echo "Creating $resourceGroup2 in $location2..."
az group create --name $resourceGroup2 --location "$location2" --tags $tag

# Create a website deployed from GitHub in both regions (replace with your own GitHub URL).
gitrepo="https://github.com/Azure-Samples/app-service-web-dotnet-get-started.git"

# Create a hosting plan and website and deploy it in location one (requires Standard 1 minimum SKU).
echo "Creating $webAppL1 app service plan"
az appservice plan create \
  --name $webAppL1 \
  --resource-group $resourceGroup1 \
  --sku S1

echo "Creating $webAppL1 web app"
az webapp create \
  --name $webAppL1 \
  --resource-group $resourceGroup1 \
  --plan $webAppL1

echo "Deploying $gitrepo to $webAppL1"
az webapp deployment source config \
  --name $webAppL1 \
  --resource-group $resourceGroup1 \
  --repo-url $gitrepo \
  --branch master \
  --manual-integration

# Create a hosting plan and website and deploy it in westus (requires Standard 1 minimum SKU).
echo "Creating $webAppL2 app service plan"
az appservice plan create \
  --name $webAppL2 \
  --resource-group $resourceGroup2 \
  --sku S1

echo "Creating $webAppL2 web app"
az webapp create \
  --name $webAppL2 \
  --resource-group $resourceGroup2 \
  --plan $webAppL2

echo "Deploying $gitrepo to $webAppL2"
az webapp deployment source config \
  --name $webAppL2 \
  --resource-group $resourceGroup2 \
  --repo-url $gitrepo \
  --branch master --manual-integration

# Create a Traffic Manager profile.
echo "Creating $trafficManagerProfile for $webApp"
az network traffic-manager profile create \
  --name $trafficManagerProfile \
  --resource-group $resourceGroup1 \
  --routing-method Priority \
  --unique-dns-name $webApp

# Create a traffic manager endpoint for the location one website deployment and set it as the priority target.
echo "Create traffic manager endpoint for $webAppL1"
l1Id=$(az webapp show \
  --resource-group $resourceGroup1 \
  --name $webAppL1 \
  --query id \
  --out tsv)
az network traffic-manager endpoint create \
  --name endPoint1 \
  --profile-name $trafficManagerProfile \
  --resource-group $resourceGroup1 \
  --type azureEndpoints \
  --priority 1 \
  --target-resource-id $l1Id

# Create a traffic manager endpoint for the location two website deployment and set it as the secondary target.
echo "Create traffic manager endpoint for $webAppL1"
l2Id=$(az webapp show \
  --resource-group $resourceGroup2 \
  --name $webAppL2 \
  --query id --out tsv)
az network traffic-manager endpoint create \
  --name endPoint2 \
  --profile-name $trafficManagerProfile \
  --resource-group $resourceGroup1 \
  --type azureEndpoints \
  --priority 2 \
  --target-resource-id $l2Id
# </FullScript>

# echo "Deleting all resources"
# az group delete --name $resourceGroup1 -y
# az group delete --name $resourceGroup2 -y

Limpieza de la implementación

Después de ejecutar el script de ejemplo, se puede usar el comando siguiente para quitar el grupo de recursos, la aplicación de App Service y todos los recursos relacionados.

az group delete --name myResourceGroup1 --yes
az group delete --name myResourceGroup2 --yes

Explicación del script

Este script usa los siguientes comandos para crear un grupo de recursos, una aplicación web, un perfil de Traffic Manager y todos los recursos relacionados. Cada comando de la tabla crea un vínculo a documentación específica del comando.

Get-Help Notas
az group create Crea un grupo de recursos en el que se almacenan todos los recursos.
az appservice plan create Crea un plan de App Service, que es como una granja de servidores para una aplicación web de Azure.
az webapp create Crea una aplicación web de Azure en el plan de App Service.
az network traffic-manager profile create Crea un perfil de Azure Traffic Manager.
az network traffic-manager endpoint create Agrega un punto de conexión a un perfil de Azure Traffic Manager.

Pasos siguientes

Para más información sobre la CLI de Azure, consulte la documentación de la CLI de Azure.

Puede encontrar ejemplos de script adicionales de la CLI de App Service en la documentación de Redes de Azure.