Create an Azure VM with multiple NICs

Multiple reasons needs a virtual machine (VM) to have multiple NICs, while this isn’t possible in the Azure Portal, we can create VMs with mutiple NICs using PS.

First is a script that creates your infrastructure with a virtual networking, containing multiple subnets, because most likely the reason for having multiple NICs is to have a backend and a frontend for a service, or perhaps a backend and a middle-end. But I do wonder if having multiple NICs in the same subnet, would be able to create a higher bandwith using a loadbalancer between the two NICs.

#Variables

#Datacenter location
$locName = "northeurope"
#Resource Group Name
$rgName = "DEMO"
#Storage Account
$stName = "itiscloudydemo01"
#Virtual subnet
$subnetName = "itiscloudydemo"
#Virtual subnet 2
$subnetName2 = "itiscloudydemo2"
#Virtual network
$vnetName = "itiscloudydemo"

#Create a Resource Group
New-AzureRmResourceGroup -Name $rgName -Location $locName -Force
#Create a Storage Account
$storageAcc = New-AzureRmStorageAccount -ResourceGroupName $rgName -Name $stName -SkuName "Standard_GRS" -Kind "Storage" -Location $locName
#Create a Virtual Network Subnet
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24
$secondSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName2 -AddressPrefix 10.1.0.0/24
#Create a Virtual Network
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $locName -AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet, $secondSubnet

https://github.com/RoyApalnes/Microsoft/blob/master/StartAzureInfrastructure2.ps1

When we a virtual network with multiple subnets, we can create VMs with multiple NICs in separate Subnet. Keep in mind only some sizes of VMs support multiple NICs, see a list here, where the last column states number of NICs and Network bandwith. For now there is also a restriction to one public ip for each VM, and it is only allowed associated with the primary NIC, so be sure to set the internetfacing subnet on the NIC that becomes your primary NIC.

#Static variables
#Datacenter location
$locName = "northeurope"
#Resource Group Name
$rgName = "DEMO"
#Storage Account
$stName = "itiscloudydemo01"
#Virtual subnet
$subnetName = "itiscloudydemo"
#Virtual network
$vnetName = "itiscloudydemo"

#Dynamic variables
#Public IP Name
$ipName = "DEMO-DC03"
#Primary Network Interface name
$nicName = "DEMO-DC03-NIC01"
#Second Network Interface name
$nicName2 = "DEMO-DC03-NIC02"
#Prompt for local administrative credentials
$cred = Get-Credential -Message "Type the name and password of the local administrator account."
#VM Name
$vmName = "DEMO-DC03"
#VM Size
$vmSize = "Standard_F2"
#Computer Name
$compName = "DEMO-DC03"
#Publisher Name
$pubName = "MicrosoftWindowsServer"
#Offer Name
$offer = "WindowsServer"
#Sku Name
$sku = "Windows-Server-Technical-Preview"
#Version
$version = "latest"
#Blob Storage Path
$blobPath = "vhds/DEMO-DC03.vhd"
#OS Disk Name
$diskName = "DEMO-DC03-OSdisk01"

#Get-AzureRmStorageAccountNameAvailability $stName

$storageAcc = Get-AzureRmStorageAccount -ResourceGroupName $rgName -Name $stName
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgName -Name $vnetName
$singleSbunet = Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $vnet
$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $locName -AllocationMethod Dynamic
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id
$nic2 = New-AzureRmNetworkInterface -Name $nicName2 -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[0].Id
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $compName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName $pubName -Offer $offer -Skus $sku -Version $version
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $nic.Id -Primary
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $nic2.Id
$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + $blobPath
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption fromImage

#Create the Virtual Machine
New-AzureRmVM -ResourceGroupName $rgName -Location $locName -VM $vm

 https://github.com/RoyApalnes/Microsoft/blob/master/CreateAzureRmVm3.ps1

My next post will talk about using JSON templates rather then PowerShell, because JSON is becoming the most universal language for coding infrastructure.


Leave a Reply

Ehlo!

I am Roy Apalnes, a Microsoft Cloud Evangelist working av Sopra Steria. Main focus in Microsoft Security and Endpoint Management, with a bigger picture in mind.

Featured Posts

    %d bloggers like this: