Instradare il traffico per la disponibilità elevata delle applicazioniRoute traffic for high availability of applications

In questo script viene creato un gruppo di risorse, due piani di servizio app, due app Web, un profilo di gestione traffico e due endpoint di gestione traffico.This script creates a resource group, two app service plans, two web apps, a traffic manager profile, and two traffic manager endpoints. Gestione traffico indirizza il traffico verso l'applicazione in un'area come area primaria e nell'area secondaria quando l'applicazione nell'area primaria non è disponibile.Traffic Manager directs traffic to the application in one region as the primary region, and to the secondary region when the application in the primary region is unavailable. Prima di eseguire lo script, è necessario modificare i valori MyWebApp, MyWebAppL1 e MyWebAppL2 in valori univoci in Azure.Before executing the script, you must change the MyWebApp, MyWebAppL1 and MyWebAppL2 values to unique values across Azure. Dopo aver eseguito lo script, è possibile accedere all'app nell'area primaria con l'URL mywebapp.trafficmanager.net.After running the script, you can access the app in the primary region with the URL mywebapp.trafficmanager.net.

Se necessario, installare Azure PowerShell usando l'istruzione presente nella Guida di Azure PowerShell e quindi eseguire Login-AzureRmAccount per creare una connessione con Azure.If needed, install the Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Login-AzureRmAccount to create a connection with Azure.

Se non si ha una sottoscrizione di Azure, creare un account gratuito prima di iniziare.If you don't have an Azure subscription, create a free account before you begin.

Script di esempioSample script

# Variables for common values
$rgName1="MyResourceGroup1"
$rgName2="MyResourceGroup2"
$location1="eastus"
$location2="westeurope"

# The values of the variables below must be unique (replace with your own names).
$webApp1="mywebapp$(Get-Random)"
$webApp2="mywebapp$(Get-Random)"
$webAppL1="MyWebAppL1"
$webAppL2="MyWebAppL2"

# Create a resource group in location one.
New-AzureRmResourceGroup -Name $rgName1 -Location $location1

# Create a resource group in location two.
New-AzureRmResourceGroup -Name $rgName2 -Location $location2

# 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).

$appServicePlan = New-AzureRmAppServicePlan -Name $webappl1 -ResourceGroupName $rgName1 `
  -Location $location1 -Tier Standard 

$web1 = New-AzureRmWebApp -ResourceGroupName $rgname1 -Name $webApp1 -Location $location1 `
  -AppServicePlan $webappl1

# Configure GitHub deployment from your GitHub repo and deploy once.
$PropertiesObject = @{
    repoUrl = "$gitrepo";
    branch = "master";
    isManualIntegration = "true";
}

Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $rgname1 `
-ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webapp1/web `
-ApiVersion 2015-08-01 -Force

# Create a hosting plan and website and deploy it in location two (requires Standard 1 minimum SKU).

$appServicePlan = New-AzureRmAppServicePlan -Name $webappl2 -ResourceGroupName $rgName2 `
  -Location $location2 -Tier Standard 

$web2 = New-AzureRmWebApp -ResourceGroupName $rgname2 -Name $webApp2 `
  -Location $location2 -AppServicePlan $webappl2

$PropertiesObject = @{
    repoUrl = "$gitrepo";
    branch = "master";
    isManualIntegration = "true";
}

Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $rgname2 `
  -ResourceType Microsoft.Web/sites/sourcecontrols -ResourceName $webapp2/web `
  -ApiVersion 2015-08-01 -Force

# Create a Traffic Manager profile.
$tm = New-AzureRmTrafficManagerProfile -Name 'MyTrafficManagerProfile' -ResourceGroupName $rgname1 `
  -TrafficRoutingMethod Priority -RelativeDnsName $web1.SiteName -Ttl 60 `
  -MonitorProtocol HTTP -MonitorPort 80 -MonitorPath /


# Create an endpoint for the location one website deployment and set it as the priority target.
$endpoint = New-AzureRmTrafficManagerEndpoint -Name 'MyEndPoint1' -ProfileName $tm.Name `
  -ResourceGroupName $rgname1 -Type AzureEndpoints -Priority 1 `
  -TargetResourceId $web1.Id -EndpointStatus Enabled

# Create an endpoint for the location two website deployment and set it as the secondary target.
$endpoint2 = New-AzureRmTrafficManagerEndpoint -Name 'MyEndPoint2' -ProfileName $tm.Name `
  -ResourceGroupName $rgname1 -Type AzureEndpoints -Priority 2 `
  -TargetResourceId $web2.Id -EndpointStatus Enabled

Eseguire questo comando per rimuovere il gruppo di risorse, la macchina virtuale e tutte le risorse correlate.Run the following command to remove the resource group, VM, and all related resources.

Remove-AzureRmResourceGroup -Name myResourceGroup1
Remove-AzureRmResourceGroup -Name myResourceGroup2

Spiegazione dello scriptScript explanation

Questo script usa i comandi seguenti per creare un gruppo di risorse, un'App Web, un profilo di Gestione traffico e tutte le risorse correlate.This script uses the following commands to create a resource group, web app, traffic manager profile, and all related resources. Ogni comando della tabella include collegamenti alla documentazione specifica del comando.Each command in the table links to command specific documentation.

ComandoCommand NoteNotes
New-AzureRmResourceGroupNew-AzureRmResourceGroup Consente di creare un gruppo di risorse in cui sono archiviate tutte le risorse.Creates a resource group in which all resources are stored.
New-AzureRmAppServicePlanNew-AzureRmAppServicePlan Consente di creare un piano di servizio app.Creates an App Service plan. Equivale a una server farm per l'App Web di Azure.This is like a server farm for your Azure web app.
New-AzureRmWebAppNew-AzureRmWebApp Consente di creare un'App Web di Azure all'interno del piano di servizio app.Creates an Azure web app within the App Service plan.
Set-AzureRmResourceSet-AzureRmResource Consente di creare un'App Web di Azure all'interno del piano di servizio app.Creates an Azure web app within the App Service plan.
New-AzureRmTrafficManagerProfileNew-AzureRmTrafficManagerProfile Crea un profilo di Gestione traffico di Azure.Creates an Azure Traffic Manager profile.
New-AzureRmTrafficManagerEndpointNew-AzureRmTrafficManagerEndpoint Aggiunge un endpoint a un profilo di Gestione traffico di Azure.Adds a endpoint to an Azure Traffic Manager Profile.

Passaggi successiviNext steps

Per altre informazioni su Azure PowerShell, vedere la documentazione di Azure PowerShell.For more information on the Azure PowerShell, see Azure PowerShell documentation.

Altri esempi di script di PowerShell per la rete sono disponibili nella documentazione con la panoramica delle reti di Azure.Additional networking PowerShell script samples can be found in the Azure Networking Overview documentation.