Bilanciare il carico del traffico tra macchine virtuali a disponibilità elevataLoad balance traffic between highly available virtual machines

Questo script di esempio crea tutti gli elementi necessari per eseguire più macchine virtuali Windows Server 2016 configurate in una configurazione a disponibilità elevata e con bilanciamento del carico.This script sample creates everything needed to run several Windows Server 2016 virtual machines configured in a highly available and load balanced configuration. Dopo aver eseguito lo script, si disporrà di tre macchine virtuali, aggiunte a un set di disponibilità di Azure e accessibili tramite Azure Load Balancer.After running the script, you will have three virtual machines, joined to an Azure Availability Set, and accessible through an Azure Load Balancer.

Questo esempio richiede il modulo Azure PowerShell 4.0 o versioni successive.This sample requires the Azure PowerShell module version 4.0 or later. Eseguire Get-Module -ListAvailable AzureRM per trovare la versione.Run Get-Module -ListAvailable AzureRM to find the version. Se è necessario eseguire l'installazione o l'aggiornamento, vedere come installare il modulo Azure PowerShell.If you need to install or upgrade, see Install Azure PowerShell module.

Eseguire Login-AzureRmAccount per creare una connessione con Azure.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
$rgName='MyResourceGroup'
$location='eastus'

# Create user object
$cred = Get-Credential -Message 'Enter a username and password for the virtual machine.'

# Create a resource group.
New-AzureRmResourceGroup -Name $rgName -Location $location

# Create a virtual network.
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name 'MySubnet' -AddressPrefix 192.168.1.0/24

$vnet = New-AzureRmVirtualNetwork -ResourceGroupName $rgName -Name 'MyVnet' `
  -AddressPrefix 192.168.0.0/16 -Location $location -Subnet $subnet

# Create a public IP address.
$publicIp = New-AzureRmPublicIpAddress -ResourceGroupName $rgName -Name 'myPublicIP' `
  -Location $location -AllocationMethod Dynamic

# Create a front-end IP configuration for the website.
$feip = New-AzureRmLoadBalancerFrontendIpConfig -Name 'myFrontEndPool' -PublicIpAddress $publicIp

# Create the back-end address pool.
$bepool = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name 'myBackEndPool'

# Creates a load balancer probe on port 80.
$probe = New-AzureRmLoadBalancerProbeConfig -Name 'myHealthProbe' -Protocol Http -Port 80 `
  -RequestPath / -IntervalInSeconds 360 -ProbeCount 5

# Creates a load balancer rule for port 80.
$rule = New-AzureRmLoadBalancerRuleConfig -Name 'myLoadBalancerRuleWeb' -Protocol Tcp `
  -Probe $probe -FrontendPort 80 -BackendPort 80 `
  -FrontendIpConfiguration $feip -BackendAddressPool $bePool

# Create three NAT rules for port 3389.
$natrule1 = New-AzureRmLoadBalancerInboundNatRuleConfig -Name 'myLoadBalancerRDP1' -FrontendIpConfiguration $feip `
  -Protocol tcp -FrontendPort 4221 -BackendPort 3389

$natrule2 = New-AzureRmLoadBalancerInboundNatRuleConfig -Name 'myLoadBalancerRDP2' -FrontendIpConfiguration $feip `
  -Protocol tcp -FrontendPort 4222 -BackendPort 3389

$natrule3 = New-AzureRmLoadBalancerInboundNatRuleConfig -Name 'myLoadBalancerRDP3' -FrontendIpConfiguration $feip `
  -Protocol tcp -FrontendPort 4223 -BackendPort 3389

# Create a load balancer.
$lb = New-AzureRmLoadBalancer -ResourceGroupName $rgName -Name 'MyLoadBalancer' -Location $location `
  -FrontendIpConfiguration $feip -BackendAddressPool $bepool `
  -Probe $probe -LoadBalancingRule $rule -InboundNatRule $natrule1,$natrule2,$natrule3

# Create a network security group rule for port 3389.
$rule1 = New-AzureRmNetworkSecurityRuleConfig -Name 'myNetworkSecurityGroupRuleRDP' -Description 'Allow RDP' `
  -Access Allow -Protocol Tcp -Direction Inbound -Priority 1000 `
  -SourceAddressPrefix Internet -SourcePortRange * `
  -DestinationAddressPrefix * -DestinationPortRange 3389

# Create a network security group rule for port 80.
$rule2 = New-AzureRmNetworkSecurityRuleConfig -Name 'myNetworkSecurityGroupRuleHTTP' -Description 'Allow HTTP' `
  -Access Allow -Protocol Tcp -Direction Inbound -Priority 2000 `
  -SourceAddressPrefix Internet -SourcePortRange * `
  -DestinationAddressPrefix * -DestinationPortRange 80

# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $RgName -Location $location `
-Name 'myNetworkSecurityGroup' -SecurityRules $rule1,$rule2

# Create three virtual network cards and associate with public IP address and NSG.
$nicVM1 = New-AzureRmNetworkInterface -ResourceGroupName $rgName -Location $location `
  -Name 'MyNic1' -LoadBalancerBackendAddressPool $bepool -NetworkSecurityGroup $nsg `
  -LoadBalancerInboundNatRule $natrule1 -Subnet $vnet.Subnets[0]

$nicVM2 = New-AzureRmNetworkInterface -ResourceGroupName $rgName -Location $location `
  -Name 'MyNic2' -LoadBalancerBackendAddressPool $bepool -NetworkSecurityGroup $nsg `
  -LoadBalancerInboundNatRule $natrule2 -Subnet $vnet.Subnets[0]

$nicVM3 = New-AzureRmNetworkInterface -ResourceGroupName $rgName -Location $location `
  -Name 'MyNic3' -LoadBalancerBackendAddressPool $bepool -NetworkSecurityGroup $nsg `
  -LoadBalancerInboundNatRule $natrule3 -Subnet $vnet.Subnets[0]

# Create an availability set.
$as = New-AzureRmAvailabilitySet -ResourceGroupName $rgName -Location $location `
  -Name 'MyAvailabilitySet' -Sku Aligned -PlatformFaultDomainCount 3 -PlatformUpdateDomainCount 3

# Create three virtual machines.

# ############## VM1 ###############

# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName 'myVM1' -VMSize Standard_DS2 -AvailabilitySetId $as.Id | `
  Set-AzureRmVMOperatingSystem -Windows -ComputerName 'myVM1' -Credential $cred | `
  Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer `
  -Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $nicVM1.Id

# Create a virtual machine
$vm1 = New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vmConfig

# ############## VM2 ###############

# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName 'myVM2' -VMSize Standard_DS2 -AvailabilitySetId $as.Id | `
  Set-AzureRmVMOperatingSystem -Windows -ComputerName 'myVM2' -Credential $cred | `
  Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer `
  -Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $nicVM2.Id

# Create a virtual machine
$vm2 = New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vmConfig

# ############## VM3 ###############

# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName 'myVM3' -VMSize Standard_DS2 -AvailabilitySetId $as.Id | `
  Set-AzureRmVMOperatingSystem -Windows -ComputerName 'myVM3' -Credential $cred | `
  Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer `
  -Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $nicVM3.Id

# Create a virtual machine
$vm3 = New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $vmConfig

Pulire la distribuzioneClean up deployment

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 myResourceGroup

Spiegazione dello scriptScript explanation

Questo script usa i comandi seguenti per creare la distribuzione.This script uses the following commands to create the deployment. Ogni elemento della tabella include collegamenti alla documentazione specifica del comando.Each item 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-AzureRmVirtualNetworkSubnetConfigNew-AzureRmVirtualNetworkSubnetConfig Crea una configurazione di subnet.Creates a subnet configuration. Questa configurazione viene usata con il processo di creazione della rete virtuale.This configuration is used with the virtual network creation process.
New-AzureRmVirtualNetworkNew-AzureRmVirtualNetwork Crea una rete virtuale.Creates a virtual network.
New-AzureRmPublicIpAddressNew-AzureRmPublicIpAddress Crea un indirizzo IP pubblico.Creates a public IP address.
New-AzureRmLoadBalancerFrontendIpConfigNew-AzureRmLoadBalancerFrontendIpConfig Crea una configurazione IP front-end per un servizio di bilanciamento del carico.Creates a front-end IP configuration for a load balancer.
New-AzureRmLoadBalancerBackendAddressPoolConfigNew-AzureRmLoadBalancerBackendAddressPoolConfig Crea una configurazione del pool di indirizzi back-end per un servizio di bilanciamento del carico.Creates a backend address pool configuration for a load balancer.
New-AzureRmLoadBalancerProbeConfigNew-AzureRmLoadBalancerProbeConfig Crea la configurazione di una porta probe per un servizio di bilanciamento del carico.Creates a probe configuration for a load balancer.
New-AzureRmLoadBalancerRuleConfigNew-AzureRmLoadBalancerRuleConfig Crea la configurazione di una regola per un servizio di bilanciamento del carico.Creates a rule configuration for a load balancer.
New-AzureRmLoadBalancerInboundNatRuleConfigNew-AzureRmLoadBalancerInboundNatRuleConfig Crea la configurazione di una regola NAT in ingresso per un servizio di bilanciamento del carico.Creates an inbound NAT rule configuration for a load balancer.
New-AzureRmLoadBalancerNew-AzureRmLoadBalancer Crea un servizio di bilanciamento del carico.Creates a load balancer.
New-AzureRmNetworkSecurityRuleConfigNew-AzureRmNetworkSecurityRuleConfig Crea una configurazione di regola del gruppo di sicurezza di rete.Creates a network security group rule configuration. Questa configurazione viene usata per creare una regola NSG quando viene creato il gruppo di sicurezza di rete.This configuration is used to create an NSG rule when the NSG is created.
New-AzureRmNetworkSecurityGroupNew-AzureRmNetworkSecurityGroup Crea un gruppo di sicurezza di rete.Creates a network security group.
Get-AzureRmVirtualNetworkSubnetConfigGet-AzureRmVirtualNetworkSubnetConfig Ottiene informazioni sulla subnet.Gets subnet information. Queste informazioni vengono usate durante la creazione di un'interfaccia di rete.This information is used when creating a network interface.
New-AzureRmNetworkInterfaceNew-AzureRmNetworkInterface Crea un'interfaccia di rete.Creates a network interface.
New-AzureRmVMConfigNew-AzureRmVMConfig Crea una configurazione di VM.Creates a VM configuration. Questa configurazione include informazioni quali il nome della VM, il sistema operativo e le credenziali amministrative.This configuration includes information such as VM name, operating system, and administrative credentials. La configurazione viene usata durante la creazione della VM.The configuration is used during VM creation.
New-AzureRmVMNew-AzureRmVM Creare una macchina virtuale.Create a virtual machine.
Remove-AzureRmResourceGroupRemove-AzureRmResourceGroup Rimuove un gruppo di risorse e tutte le risorse contenute al suo interno.Removes a resource group and all resources contained within.

Passaggi successiviNext steps

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

Altri esempi di script PowerShell della macchina virtuale sono reperibili nella documentazione della VM Windows di Azure.Additional virtual machine PowerShell script samples can be found in the Azure Windows VM documentation.