How to create predictible (static) ip address for Azure VMs

In Microsoft Azure all created VMs have internal IP addresses assigned from DHCP server, and those IP addresses are within the defined virtual network scope if you have created virtual network. The IP address assigned by the DHCP to the Azure VM, will remain the same for the vm's lifetime as long as the VM is not in Stop (Deallocated) state. Here is output from the ipconfig /all from the Azure VM:


This means that when the VM is shut down from the operating system (you're paying for the VM in this state), it will obtain the same IP (in this example 10.0.0.5) when the VM will start up again. Also, while this VM is in Stopped state (shut down from the OS), no other VM will be offered the same 10.0.0.5 address. And here is the state of the VM from Azure Portal:


In case when you don't want to pay for the VM, you have to shut down the VM from the Azure portal or Powershell, and the VM will be in Stopped (Deallocated) state :


Starting the VM from this (Stopped -Deallocated) state, the VM will start the new provisioning process and there is a chance to obtain some other IP address. So, here is a question: Is there a way to define some preferred address for the VM, when the VM is starting from Stopped (Deallocated) state ?
And, the answer is fortunately Yes. There is Set-AzureStaticVNetIP cmdlet which can set preferred IP address for the VM when starting from Stopped (Deallocated) state. For example the syntax for assigning the static (preferred) IP address for previously created Azure VM is:

Get-AzureVM -ServiceName testService -Name test | Set-AzureStaticVNetIP -IPAddress 10.0.0.5 | Update-AzureVM

Note that running this command will trigger reboot of the VM. Related cmdlets for checking, testing and removing the assigned static IP addresses from the Azure VMs are also available ( Get-AzureStaticVNetIP, Test-AzureStaticVNetIP, Remove-AzureStaticVNetIP )

It's important to understand that, this static IP address assigned with Set-AzureStaticVNetIP will NOT create reservation of the IP address for that VM. Instead, it will create preferred IP address for the VM when starting from Stopped (Deallocated) state, which means that that IP address might be already taken by other virtual machine, while the VM with static assigned IP was in Stopped (deallocated) state !
 

No comments:

Post a Comment

How to check EMBG (Unique Master Citizen Number) using regex

In this post, I will share my implementation of how to check if some number looks like EMBG or Unique Master Citizen Number. For those of yo...