AzureRm | Create Internal Load Balancer with two VMs

AzureRm | Create Internal Load Balancer with two VMs

This post is part of a general idea, to create an end-to-end high available application infrastructure solution in Azure using internal load balancer with the new AzureRm commands and Azure PowerShell v.1.0 preview.

The first part is to create an Internal Load Balancer in Azure to use it for two VMs. This setup is ideal for Web server farms and also for SQL clusters. We will create the VNET with the Front End subnet, the internal load balancer and finally two VMs behind the load balancer. The result will be something like the below image.

web1

In order to run the new AzureRm commands we need to have the Windows Management Framework 5.0 Production Preview. If you have Windows 10 then no action is needed since it is embeded. For Windows 7-8.1 we can download it here: https://www.microsoft.com/en-us/download/details.aspx?id=48729

The AzureRm commands are installed directly from the PowerShell using the Install-Module AzureRM & Install-AzureRM commands.

So lets start:

#Login
Login-AzureRmAccount

#Create a new resource group
New-AzureRmResourceGroup -Name RMDemoRG -Location "West Europe"

#Create Virtual Network and a private IP address for Front End IP pool
#Front End Subnet 172.16.5.0/24
#Address Space 172.16.0.0/16
$FESubnet = New-AzureRmVirtualNetworkSubnetConfig -Name LBnetFE -AddressPrefix 172.16.5.0/24
$vnet = New-AzureRmVirtualNetwork `
        -Name NRPVnet `
        -ResourceGroupName RMDemoRG `
        -Location "West Europe" `
        -AddressPrefix 172.16.0.0/16 -Subnet $FESubnet

#Create the internal load balancer FrontEnd IP pool and BackEnd address pool
$frontendIP = New-AzureRmLoadBalancerFrontendIpConfig `
        -Name LB-Frontend `
        -PrivateIpAddress 172.16.5.10 `
        -SubnetId $vnet.Subnets.Id
$beaddresspool= New-AzureRmLoadBalancerBackendAddressPoolConfig -Name "LB-backend"

#Create internal load balancer rules, NAT rules, probe and the internal load balancer
$inboundNATRule1= New-AzureRMLoadBalancerInboundNatRuleConfig `
                    -Name "SSH1" `
                    -FrontendIpConfiguration $frontendIP `
                    -Protocol TCP `
                    -FrontendPort 6622 `
                    -BackendPort 22
$inboundNATRule2= New-AzureRMLoadBalancerInboundNatRuleConfig `
                    -Name "SSH2" `
                    -FrontendIpConfiguration $frontendIP `
                    -Protocol TCP `
                    -FrontendPort 6623 `
                    -BackendPort 22
$healthProbe = New-AzureRMLoadBalancerProbeConfig `
                    -Name "HealthProbe" `
                    -RequestPath "HealthProbe.html" `
                    -Protocol http `
                    -Port 80 `
                    -IntervalInSeconds 15 `
                    -ProbeCount 2
$lbrule = New-AzureRMLoadBalancerRuleConfig `
                    -Name "HTTP" `
                    -FrontendIpConfiguration $frontendIP `
                    -BackendAddressPool $beAddressPool `
                    -Probe $healthProbe `
                    -Protocol Tcp `
                    -FrontendPort 80 `
                    -BackendPort 80
$NRPLB = New-AzureRMLoadBalancer `
                    -ResourceGroupName "RMDemoRG" `
                    -Name "NRP-LB" `
                    -Location "West Europe" `
                    -FrontendIpConfiguration $frontendIP `
                    -InboundNatRule $inboundNATRule1,$inboundNatRule2 `
                    -LoadBalancingRule $lbrule `
                    -BackendAddressPool $beAddressPool `
                    -Probe $healthProbe

#Create two network interfaces
$vnet = Get-AzureRMVirtualNetwork -Name NRPVNet -ResourceGroupName RMDemoRG
$frontendSubnet = Get-AzureRMVirtualNetworkSubnetConfig -Name LBnetFE -VirtualNetwork $vnet
#Create 1st NIC with first NAT rule for RDP
$frontendnic1 = New-AzureRMNetworkInterface `
                    -ResourceGroupName "RMDemoRG" `
                    -Name lb-nic1-be `
                    -Location "West Europe" `
                    -PrivateIpAddress 172.16.5.6 `
                    -Subnet $frontendSubnet `
                    -LoadBalancerBackendAddressPool $nrplb.BackendAddressPools[0] `
                    -LoadBalancerInboundNatRule $nrplb.InboundNatRules[0]
#Create 2nd NIC with second NAT rule for RDP
$frontendnic2 = New-AzureRMNetworkInterface `
                    -ResourceGroupName "RMDemoRG" `
                    -Name lb-nic2-be `
                    -Location "West Europe" `
                    -PrivateIpAddress 172.16.5.7 `
                    -Subnet $frontendSubnet `
                    -LoadBalancerBackendAddressPool $nrplb.BackendAddressPools[0] `
                    -LoadBalancerInboundNatRule $nrplb.InboundNatRules[1]

#Create a Virtual Machine and assign the NIC

# Set values for existing resource group and storage account names
$resourcegroupName="RMDemoRG"
$locationName="West Europe"
$storageaccountName="rmdemostrg"

# Set the existing virtual network and subnet index
$vnetName="NRPVnet"
$subnetIndex=0
$vnet=Get-AzureRMVirtualNetwork -Name $vnetName -ResourceGroupName $resourcegroupName

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

# Specify the name, size, and existing availability set
$vmName="RMDemoLBVM1"
$vmSize="Standard_A1"
$availabilitysetName="RMDemoAS1"
$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="RMDemoAS1Data"
$diskName="RMDemoAS1Data"
$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
$pubName="SUSE"
$offerName="SLES"
$skuName="11-SP4"
$cred=Get-Credential -Message "Type the name and password of the local administrator account."
$vm=Set-AzureRmVMOperatingSystem -VM $vm -Linux -ComputerName $vmName -Credential $cred
$vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest"
$vm=Add-AzureRmVMNetworkInterface -VM $vm -Id $frontendnic1.Id

#Specify the OS disk name and create the VM
$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

#To create the second VM repeat the following steps:
#1. Specify the name, size, and existing availability set | change the vmName
#2. Add a 1023 GB additional data disk | change the diskLabel & diskName
#3. Specify the image and local administrator account, and then add the NIC | change the $frontendnic1 to $frontendnic2
#4. Specify the OS disk name and create the VM

The same script, changing some names, can be used to create the Back End Subnet for the SQL servers.

At the next posts we will create the VPN Gateway and the NSGs.

Share

Leave a Reply

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

6 + two =