If you are doing your DEV/Test in Azure and de-allocating your VMs frequently you may have noticed that the Internal IPs assigned to your VMs are changing. By nature Azure wants you to run your VM continuously to have a reservation for your Internal IPs as there are million tenants like you using Azure over the world and to ensure everybody gets equal service.
If you are running DNS server in Azure you need to have a static IP regardless you de-allocate the VM, you need the same IP to work next time to prevent name resolution conflicts. Today I’m going to show you how to assign a Static DIP to Azure VM and some of the best practices that you should keep in mind when assigning DIPs.
Keep in Mind
- If you have both IaaS & PaaS services in your tenant (i.e VMs and Cloud Services) it is always recommended to keep them in different subnets if you are using DIPs. Just in case if you are de-allocating a VM with static DIP it prevents your cloud services from acquiring the same DIP if they are in the same subnet.
- Only assign DIPs for workloads that needs them the most (i.e DNS). Again this is not a requirement but you’l have to worry less if you have only fewer configuration changes.
- Always use a Virtual Network with subnets properly assigned. Otherwise it would be pointless to use a DIP as there won’t be any allocation at all.
Lets take a look on the process.
Check the availability of the IP Address
You have to make sure the IP Address that you are going to use from your IP Pool is available prior assigning.
Test-AzureStaticVNetIP –VNetName JCBVNet –IPAddress 192.168.1.5
In the above example I’m checking the availability of the IP address 192.168.1.5 for the Virtual Network JCBVNet.
Assigning Static DIP
For a new VM
Make sure you get your PowerShell cmdlets right. You can use variables instead of hard coded placeholders for parameters like -Name but again that’s upto you. Parameters like -ImageName has lengthier vaules to it is better to get the image name and store it in a variable when you pass the value to a cmdlet. Some of these parameters like -AffinityGroup are not mandatory but will be useful depending on your deployment. For a complete reference just bing (yes bing not google) the cmdlet name.
New-AzureVMConfig -Name jcbdipvm1 -ImageName $img –InstanceSize Small | Set-AzureSubnet –SubnetNames JCBVNet | Set-AzureStaticVNetIP -IPAddress 192.168.1.5 | New-AzureVM –ServiceName jcbdipsvc –AffinityGroup “jcbAG”;
For an existing VM
From my experience, it is always better if you can stop the VM before you perform this on a existing VM. This is because the Update-AzureVM cmdlets restarts the VM once it has assigned the DIP. If the VM was running it will take some time and believe me you don’t want that to happen on something like DNS server. If you have previously assigned a DIP you will have to remove it first.
Get-AzureVM -ServiceName jcbdipsvc -Name jcbdipvm2| Set-AzureStaticVNetIP -IPAddress 192.168.1.6 | Update-AzureVM
Removing a DIP
Get-AzureVM -ServiceName jcbdipsvc -Name jcbdipvm1 | Remove-AzureStaticVNetIP | Update-AzureVM
As part of the update VM will automatically receive a new IP address once it is restarted by the Update-AzureVM cmdlet.
It’s not rocket since if you read carefully. This is very useful for a production environment or if you have a lab environment that is not going to be running continuously to save Azure credits but still require static IP addresses for the workloads. My advice is to learn PowerShell a bit before you deal with Azure or you can keep blaming the platform not being good enough. (Which is so not true.)