AzureRm | Create External Load Balancer with two VMs

After my previous post, the internal load balancer with two VMs, this is a scenario using the External Load Balancer. The configuration includes a Load Balancer with a Static Public IP at the frond end and two VMs at the back end. The load balancer has two static routes for RDP, one for each VM and one load balance rule, the TCP port 80, common for web sites and applications. It uses a probe that checks a web page on both hosts to verify if they are active.

Lets start. First we need to install the AzureRm module. If not Windows 10 then first install the https://www.microsoft.com/en-us/download/details.aspx?id=48729
Then Open Powershell ISE and execute the following commands. I have added a lot of comments to help customize based to the needs.

Set-ExecutionPolicy RemoteSigned
Install-Module AzureRM
Login-AzureRmAccount

#Define the variables
$ResourceGroupName = "myresourcegroup"
$StorageAccountName = "mystorageaccount"
$vnetname = "VNET-01"
$NSGname = "NSG-01"
$locationName = "West Europe"
$publicipname = "mypublicip"
$vnet = Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $ResourceGroupName

#Create a new resource group
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $locationName

#Create storage account
New-AzureRmStorageAccount `
                -ResourceGroupName $resourcegroupName `
                -Name $storageaccountName `
                -Type Standard_LRS `
                -Location $locationName

#Create Virtual Network and a private IP address for front end IP pool
$FESubnet = New-AzureRmVirtualNetworkSubnetConfig -Name FE-SUBNET -AddressPrefix 10.0.0.16/28
$BESubnet = New-AzureRmVirtualNetworkSubnetConfig -Name BE-SUBNET -AddressPrefix 10.0.0.32/28

$vnet = New-AzureRmVirtualNetwork `
        -Name $vnetname `
        -ResourceGroupName $ResourceGroupName `
        -Location $locationName `
        -AddressPrefix 10.0.0.0/24 -Subnet $FESubnet,$BESubnet

$FESubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name FE-SUBNET -VirtualNetwork $vnet
$BESubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name BE-SUBNET -VirtualNetwork $vnet

#Create Public IP
$publicIP = New-AzureRmPublicIpAddress `
                            -Name PublicIp `
                            -ResourceGroupName $ResourceGroupName `
                            -Location $locationName `
                            –AllocationMethod Static `
                            -DomainNameLabel $publicipname

#Create FrontEnd IP pool and BackEnd address pool
$APPfrontendIP = New-AzureRmLoadBalancerFrontendIpConfig `
        -Name APP-LB-Frontend `
        -PublicIpAddress $publicIP

$APPbeaddresspool= New-AzureRmLoadBalancerBackendAddressPoolConfig -Name "APP-LB-backend"

#Create load balancer rules, NAT rules, probe and load balancer
$APPinboundNATRule1= New-AzureRMLoadBalancerInboundNatRuleConfig `
                    -Name "RDP1" `
                    -FrontendIpConfiguration $APPfrontendIP `
                    -Protocol TCP `
                    -FrontendPort 33389 `
                    -BackendPort 3389
$APPinboundNATRule2= New-AzureRMLoadBalancerInboundNatRuleConfig `
                    -Name "RDP2" `
                    -FrontendIpConfiguration $APPfrontendIP `
                    -Protocol TCP `
                    -FrontendPort 33390 `
                    -BackendPort 3389
$APPhealthProbe = New-AzureRMLoadBalancerProbeConfig `
                    -Name "HealthProbe" `
                    -RequestPath "/index.aspx" `
                    -Protocol http `
                    -Port 80 `
                    -IntervalInSeconds 15 `
                    -ProbeCount 2
$APPlbrule = New-AzureRMLoadBalancerRuleConfig `
                    -Name "HTTP" `
                    -FrontendIpConfiguration $APPfrontendIP `
                    -BackendAddressPool $APPbeAddressPool `
                    -Probe $GAPPhealthProbe `
                    -Protocol Tcp `
                    -FrontendPort 80 `
                    -BackendPort 80
$APPLB = New-AzureRMLoadBalancer `
                    -ResourceGroupName $ResourceGroupName `
                    -Name "APP-LB" `
                    -Location $locationName `
                    -FrontendIpConfiguration $APPfrontendIP `
                    -InboundNatRule $APPinboundNATRule1,$APPinboundNATRule2 `
                    -LoadBalancingRule $APPlbrule `
                    -BackendAddressPool $APPbeAddressPool `
                    -Probe $APPhealthProbe

#Create the network interfaces for the backend VMs
$vnet = Get-AzureRMVirtualNetwork -Name $vnetname -ResourceGroupName $ResourceGroupName
$APPbackendSubnet = Get-AzureRMVirtualNetworkSubnetConfig -Name FE-SUBNET -VirtualNetwork $vnet

#Create 1st NIC with first NAT rule for RDP
$APPbackendnic1 = New-AzureRMNetworkInterface `
                    -ResourceGroupName $ResourceGroupName `
                    -Name APP-lb-nic1-be `
                    -Location $locationName `
                    -PrivateIpAddress 10.0.0.21 `
                    -Subnet $APPbackendSubnet `
                    -LoadBalancerBackendAddressPool $APPLB.BackendAddressPools[0] `
                    -LoadBalancerInboundNatRule $APPLB.InboundNatRules[0]
#Create 2nd NIC with second NAT rule for RDP
$APPbackendnic2 = New-AzureRMNetworkInterface `
                    -ResourceGroupName $ResourceGroupName `
                    -Name APP-lb-nic2-be `
                    -Location $locationName `
                    -PrivateIpAddress 10.0.0.22 `
                    -Subnet $APPbackendSubnet `
                    -LoadBalancerBackendAddressPool $APPLB.BackendAddressPools[0] `
                    -LoadBalancerInboundNatRule $APPLB.InboundNatRules[1]

#Create a Virtual Machine and assign the NIC
# Set the existing virtual network and subnet index
$subnetIndex=0
$vnet=Get-AzureRMVirtualNetwork -Name $vnetName -ResourceGroupName $resourcegroupName

#Create Availability Set
$availabilitysetName="APP-AS"
New-AzureRmAvailabilitySet –Name $availabilitysetName –ResourceGroupName $resourcegroupName -Location $locationName

# First VM
# Specify the name, size, and existing availability set
$vmName="APP-01"
$vmSize="Standard_A1"
$availabilitysetName="APP-AS"
$availabilitysetSet=Get-AzureRmAvailabilitySet –Name $availabilitysetName –ResourceGroupName $resourcegroupName
$vm=New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize -AvailabilitySetId $availabilitysetSet.Id

#Add a 1023 GB additional data disk
$diskSize=1023
$diskLabel="AS1Data"
$diskName="AS1Data"
$storageAccount=Get-AzureRmStorageAccount -ResourceGroupName $resourcegroupName -Name $storageaccountName
$vhdURI=$storageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName + $diskName  + ".vhd"
Add-AzureRmVMDataDisk -VM $vm -Name $diskLabel -DiskSizeInGB $diskSize -VhdUri $vhdURI -CreateOption empty

#Specify the image and local administrator account, and then add the NIC
#To find the Publisher, Offer and SKU use the Get-AzureRmVMImagePublisher, Get-AzureRmVMImageOffer and Get-AzureRmVMImageSku commands
$pubName="MicrosoftWindowsServer"
$offerName="WindowsServer"
$skuName="2012-R2-Datacenter"
$cred=Get-Credential -Message "Type the name and password of the local administrator account."
$vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred
$vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $backendnic1.Id

#Specify the OS disk name and create the VM / For Create NEW OS Disk
$diskName="OSDisk"
$storageAccount=Get-AzureRmStorageAccount -ResourceGroupName $resourcegroupName -Name $storageaccountName
$osDiskUri=$storageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName + $diskName  + ".vhd"
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
New-AzureRmVM -ResourceGroupName $resourcegroupName -Location $locationName -VM $vm

#Second VM
# Specify the name, size, and existing availability set
$vmName="APP-02"
$vmSize="Standard_A1"
$availabilitysetName="APP-AS"
$availabilitysetSet=Get-AzureRmAvailabilitySet –Name $availabilitysetName –ResourceGroupName $resourcegroupName
$vm=New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize -AvailabilitySetId $availabilitysetSet.Id

#Add a 1023 GB additional data disk
$diskSize=1023
$diskLabel="AS2Data"
$diskName="AS2Data"
$storageAccount=Get-AzureRmStorageAccount -ResourceGroupName $resourcegroupName -Name $storageaccountName
$vhdURI=$storageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName + $diskName  + ".vhd"
Add-AzureRmVMDataDisk -VM $vm -Name $diskLabel -DiskSizeInGB $diskSize -VhdUri $vhdURI -CreateOption empty

#Specify the image and local administrator account, and then add the NIC
#To find the Publisher, Offer and SKU use the Get-AzureRmVMImagePublisher, Get-AzureRmVMImageOffer and Get-AzureRmVMImageSku commands
$pubName="MicrosoftWindowsServer"
$offerName="WindowsServer"
$skuName="2012-R2-Datacenter"
$cred=Get-Credential -Message "Type the name and password of the local administrator account."
$vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred
$vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $backendnic2.Id

#Specify the OS disk name and create the VM / For Create NEW OS Disk
$diskName="OSDisk"
$storageAccount=Get-AzureRmStorageAccount -ResourceGroupName $resourcegroupName -Name $storageaccountName
$osDiskUri=$storageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName + $diskName  + ".vhd"
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage
New-AzureRmVM -ResourceGroupName $resourcegroupName -Location $locationName -VM $vm

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.