Skip to content

Instantly share code, notes, and snippets.

@nmackenzie
Created October 25, 2015 19:23
Show Gist options
  • Save nmackenzie/54e9fe4bb34f8d6bce2e to your computer and use it in GitHub Desktop.
Save nmackenzie/54e9fe4bb34f8d6bce2e to your computer and use it in GitHub Desktop.
Create a load-balanced pair of Azure VMs
$testName = "lower-case-unique-name"
$resourceGroupName = $testName
$location = "westus"
$domainName = $testName
$subnetName = "Subnet-1"
$publisher = "MicrosoftWindowsServer"
$offer = "WindowsServer"
$sku = "2012-R2-Datacenter"
$version = "latest"
$cred = Get-Credential
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location
$vip = New-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName -Name "VIP1" `
-Location $location -AllocationMethod Dynamic -DomainNameLabel $domainName
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName `
-AddressPrefix "10.0.64.0/24"
$vnet = New-AzureRmVirtualNetwork -Name "VNET" `
-ResourceGroupName $resourceGroupName `
-Location $location -AddressPrefix "10.0.0.0/16" -Subnet $subnet
$subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
$feIpConfig = New-AzureRmLoadBalancerFrontendIpConfig -Name $testName -PublicIpAddress $vip
$inboundNatRule1 = New-AzureRmLoadBalancerInboundNatRuleConfig -Name "RDP1" `
-FrontendIpConfiguration $feIpConfig `
-Protocol TCP -FrontendPort 3441 -BackendPort 3389
$inboundNatRule2 = New-AzureRmLoadBalancerInboundNatRuleConfig -Name "RDP2" `
-FrontendIpConfiguration $feIpConfig `
-Protocol TCP -FrontendPort 3442 -BackendPort 3389
$beAddressPool = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name "LBBE"
$healthProbe = New-AzureRmLoadBalancerProbeConfig -Name "HealthProbe" `
-RequestPath "HealthProbe.aspx" -Protocol http -Port 80 `
-IntervalInSeconds 15 -ProbeCount 2
$lbrule = New-AzureRmLoadBalancerRuleConfig -Name "HTTP" `
-FrontendIpConfiguration $feIpConfig -BackendAddressPool $beAddressPool `
-Probe $healthProbe -Protocol Tcp -FrontendPort 80 -BackendPort 80
$alb = New-AzureRmLoadBalancer -ResourceGroupName $resourceGroupName `
-Name "ALB" -Location $location -FrontendIpConfiguration $feIpConfig `
-InboundNatRule $inboundNatRule1,$inboundNatRule2 `
-LoadBalancingRule $lbrule -BackendAddressPool $beAddressPool `
-Probe $healthProbe
$nic1 = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName `
-Name "nic1" -Subnet $subnet -Location $location `
-LoadBalancerInboundNatRule $alb.InboundNatRules[0] `
-LoadBalancerBackendAddressPool $alb.BackendAddressPools[0]
$nic2 = New-AzureRmNetworkInterface -ResourceGroupName $resourceGroupName `
-Name "nic2" -Subnet $subnet -Location $location `
-LoadBalancerInboundNatRule $alb.InboundNatRules[1] `
-LoadBalancerBackendAddressPool $alb.BackendAddressPools[0]
New-AzureRmAvailabilitySet -ResourceGroupName $resourceGroupName `
-Name "AVSet" -Location $location
$avset = Get-AzureRmAvailabilitySet -ResourceGroupName $resourceGroupName -Name "AVSet"
New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName `
-Name $testName -Location $location -Type Standard_LRS
Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName
[array]$nics = @($nic1,$nic2)
For ($i=0; $i -le 1; $i++) {
$vmName = "$testName-w$i"
$vmConfig = New-AzureRmVMConfig -VMName $vmName -VMSize "Standard_A1" `
-AvailabilitySetId $avSet.Id |
Set-AzureRmVMOperatingSystem -Windows -ComputerName $vmName `
-Credential $cred -ProvisionVMAgent -EnableAutoUpdate |
Set-AzureRmVMSourceImage -PublisherName $publisher -Offer $offer -Skus $sku `
-Version $version |
Set-AzureRmVMOSDisk -Name $vmName -VhdUri "https://$testName.blob.core.windows.net/vhds/$vmName-os.vhd" `
-Caching ReadWrite -CreateOption fromImage |
Add-AzureRmVMNetworkInterface -Id $nics[$i].Id
New-AzureRmVM -ResourceGroupName $resourceGroupName -Location $location `
-VM $vmConfig
}
(Get-AzureRmPublicIpAddress -ResourceGroupName $resourceGroupName).IpAddress
Get-AzureRmResource -ResourceGroupName $resourceGroupName | Select Name, ResourceType
<#
Remove-AzureRmResourceGroup -Name $resourceGroupName
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment