Persistent IP Addresses
As long as the virtual machines are only shut down from within the virtual machines themselves they will maintain this IP address. However, if the virtual machines are shut down through PowerShell or the management portal they are effectively de-provisioned. (Through PowerShell you can avoid this by specifying the -StayProvisioned flag to Stop-AzureVM – you are still paying for the VM when it is shut down).
Once a virtual machine is de-provisioned at next boot time it will go through the same process to find the first available IP address in the subnet.
What this means to you is without an alternative method of setting the IP address you are responsible for starting a set of virtual machines in the correct order to ensure their IP addresses are set (unless it doesn’t matter if they move).
The Solution? Static VNET IPs
If you know ahead of time that your virtual machines need to retain the same IP addresses whether de-provisioned or not you can specify the IP address they should attempt to assign using the Set-AzureStaticVNetIP cmdlet.
How to verify if a specific IP address is available
To verify if the IP address 10.16.1.5 is available in a vnet named TestVnet, run the following PowerShell command and verify the value for IsAvailable:
Test-AzureStaticVNetIP –VNetName TestVNet –IPAddress 10.16.1.5
Result
IsAvailable : True
AvailableAddresses : {}
OperationDescription : Test-AzureStaticVNetIP
OperationId : xx0000xx-0x0x-0xxx-0xx0-xxxxxxxxxxxx
OperationStatus : Succeeded
How to add a static internal IP to an existing VM
To add a static internal IP to the already created VM, run the following command:
Get-AzureVM -ServiceName MIFService -Name MIFVM | Set-AzureStaticVNetIP -IPAddress 10.16.1.5 | Update-AzureVM
How to remove a static internal IP from a VM
To remove the static internal IP added to the VM in the script above, run the following PowerShell command:
Get-AzureVM -ServiceName MIFService -Name MIFVM | Remove-AzureStaticVNetIP | Update-AzureVM
MIFServices = VM Service Name
MIFVM = Virtual Machine Name
You can collect these information by executing following command.
Get-AzureVM
Summary
In this post you have seen the problems with deploying virtual machines into a virtual network and having to maintain the correct provisioning order. Using the new Set-AzureStaticVNETIP cmdlet you can take full control of the IP addressing layout of your virtual machines. This method does not replace the existing assignment mechanism of virtual machines grabbing the first available IP address for virtual machines without a static IP so you still need to be careful to ensure that the IP you have assigned to a VM is not accidently assigned to another virtual machine.