Classic – v1
Microsoft Azure now has the ability to set Static Public IP addresses on Virtual Machines. This isn’t quite the same as giving a static IP to a normal on prem machine. Remember you don’t set IP’s using the conventional Ethernet Properties.
Now off we go to PowerShell.
NOTE IN THIS ARTICLE MY VM NAME IS GARETHJONES293 AND MY VM NAME IS GARETHJONES293
Open Microsoft Azure PowerShell and connect to your subscription. First enter the command Add-AzureAccount and enter your credentials.
If you have multiple subscriptions like I have on my account you may have to select the relevant one. To do this type Get-AzureSubscription
I will use the command Select-AzureSubscription –SubscriptionName “Demo”
You can now see that my Demo subscription is default and the current subscription.
There are four commands that are using to allocate a static IP in Azure PowerShell:
- Get-AzureStaticVNetIP
- Remove-AzureStaticVNetIP
- Set-AzureStaticVNetIP
- Test-AzureStaticVNetIP
In order to use these commands we need to know what our Azure vNet (Virtual Network) is called. Go to the Azure Portal and look at your networks.
Above you can see the two portals and the name of my vNet which is “Azure vNet”. You can also use powershell to find this by using the Get-AzureVNetSite
You can also narrow down the information given to you in the PowerShell output by typing Get-AzureVNetSite | Select Name
Lets now go and find out if we can use an IP address in a vNet Pool. Type:
Test-AzureStaticVNetIP –VNetName “Azure vNet” –IPAddress 10.0.0.4
You can see from the output above that the IsAvailable attribute is reported as False therefore the IP address of 10.0.0.4 is currently in use. Let’s try and use 10.0.0.10
Now you understand how to see if an IP address is available to allocate to Virtual Machines in Azure.
Setting a Static IP for an EXISTING Virtual Machine
You cannot just set a static IP to a VM just by specifying the name of the VM for example:
Set-AzureStaticVNetIP -VM garethjones293 -IPAddress 10.0.0.10
This is because PowerShell expect an object and not a string. We need to use the following commands:
Set the variable:
$VM = Get-AzureVM –ServiceName garethjones293 –Name garethjones293
then type the command:
Set-AzureStaticVNetIP -VM $VM -IPAddress 10.0.0.10 | Update-AzureVM
Ensure you add the Update-AzureVM on the end of the command otherwise the IP Address will not be bound to the Virtual Machine.
Or we can supply that information in the PowerShell command:
Set-AzureStaticVNetIP -VM (Get-AzureVM -ServiceName garethjones293 –Name garethjones293) -IPAddress 10.0.0.10 | Update-AzureVm
Setting a Static IP for RUNNING Virtual Machine
This is a bit of a tricky one because although you can change the IP address of a running VM it has to be rebooted in order for it to be applied. In the above example I have the VM with the name of “garethjones294” a static IP address of 10.0.0.10, in this example I will change it to 10.0.0.99.
First type the command:
Get-AzureVM –ServiceName garethjones293 | select name, *IP*, Status | ft –AutoSize
The output shows you the VM’s and the IP Address that are allocated/issued at present. If the status shows anything other than ReadyRole then the VM is probably rebooting etc. Other status’s include StoppedDeallocated (which means the VM has been turned off) RoleStateUnknown (means the VM is probably rebooting of provisioning and hasn’t reported back to Azure yet).
Let’s test to see if 10.0.0.99 is available:
Test-AzureStaticVNetIP –VNetName “Azure vNet” –IPAddress 10.0.0.99
Now we can just run the following command:
Set-AzureStaticVNetIP -VM (Get-AzureVM –ServiceName garethjones293 –Name garethjones293) -IPAddress 10.0.0.99 | Update-AzureVm
Once this has completed (it may take up to 10 mins for the VM to reboot) run:
Get-AzureVM –ServiceName garethjones293 | select name, *IP*, Status | ft –AutoSize
Done J
Removing a Static IP for a Virtual Machine
In all honestly this is pretty simple. It’s the opposite of what we have just done.
Remove-AzureStaticVNetIP -VM (Get-AzureVM –ServiceName garethjones293 –Name garethjones293) | Update-AzureVm
Setting a Static IP for a Virtual Machine on creation.
Below is an example of two script that would create a VM with a static IP. Note some of the attributes would have to be changed to match your Azure environment.
$ImageName = (Get-AzureVMImage | Where { $_.ImageFamily -eq "Windows Server 2012 R2 Datacenter" } | sort PublishedDate -Descending | Select-Object -First 1).ImageName
Add-AzureProvisioningConfig -Windows -AdminUsername $user -Password $pwd |
Set-AzureSubnet -SubnetNames "Subnet-1" |
Set-AzureStaticVNetIP -IPAddress "10.0.0.15" |
New-AzureVM -ServiceName "garethjones293" # existing cloud service in the vnet
Resource Manager ARM – v2
Much easier in ARM v2. Go to https://portal.azure.com
Navigate to All resources > Your VM > Network Interfaces > Click on the NIC name in the next blade > IP Configurations > ipconfig name > Click Static Assignment
Much easier
No comments:
Post a Comment