Assigning a Public Reserved IP to existing Azure Cloud Service

I have been running a SQL Server AlwaysOn Cluster in Azure for my System Center environment. I use this mostly for Demo and Development for partner solutions, so I like to shutdown and deallocate the cloud services when I’m not using them. This would also mean that the Public IP address for my Cloud Services would be deallocated and a new IP adresse would be created when I start the Cloud Service deployment again.

So, I have been looking into the new possibility of creating a reservation for the Public IP address, as described here: http://msdn.microsoft.com/en-us/library/azure/dn690120.aspx.

As described in the link:

  • You must reserve the IP address first, before deploying.
  • At this time, you can’t go back and apply a reservation to something that’s already been deployed.

Unfortunately, I had already deployed my cloud service and VMs.

The solution? Well, If I’m willing to accept a small downtime, I can easily remove and re-deploy my Cloud Service and VMs while keeping my data and configurations!

My solution was using Azure PowerShell, and save the VM configuration to XML files, then delete the VMs (NB! Important not to delete the disks). After that I recreate the VMs from the config XML files, and specify my IP address reservation which I had already created before. Now my Cloud Service and VM deployment have a reservation, and in that way my SQL AlwaysOn listener keeps a fixed IP address.

The complete solution is listed in the script window below, the script is meant to run interactively snippet by snippet. Please make sure that you do a backup first if this is required.

# PowerShell command to set a reserved IP address for Cloud Service in Azure
# Reference:
# Reserved IP addresses: http://msdn.microsoft.com/en-us/library/azure/dn690120.aspx

# Log on to my Azure account
Add-AzureAccount

# Set active subscription
Get-AzureSubscription -SubscriptionName "mysubscriptionname" | Select-AzureSubscription

# Create a Public Reserved IP for SQL AlwaysOn Listener IP
$ReservedIP = New-AzureReservedIP -ReservedIPName "SCSQLAlwaysOnListenerIP" -Label "SCSQLAlwaysOnListenerIP" -Location "West Europe"

$workingDir = (Get-Location).Path

# Define VMs and Cloud Service
$vmNames = 'az-scsql02', 'az-scsql01', 'az-scsqlquorum'
$serviceName = "mycloudsvc-az-scsql"

# Export VM Config and Stop VM
ForEach ($vmName in $vmNames) {

    $Vm = Get-AzureVM –ServiceName $serviceName –Name $vmName
    $vmConfigurationPath = $workingDir + "\exportedVM_" + $vmName +".xml"
    $Vm | Export-AzureVM -Path $vmConfigurationPath

    Stop-AzureVM –ServiceName $serviceName –Name $vmName -Force

}

# Remove VMs while keeping disks
ForEach ($vmName in $vmNames) {

    $Vm = Get-AzureVM –ServiceName $serviceName –Name $vmName
    $vm | Remove-AzureVM -Verbose

}

# Specify VNet for the VMs
$vnetname = "myvnet-prod"

# Re-create VMs in specified order
$vmNames = 'az-scsqlquorum', 'az-scsql01', 'az-scsql02'

ForEach ($vmName in $vmNames) {

    $vmConfigurationPath = $workingDir + "\exportedVM_" + $vmName +".xml"
    $vmConfig = Import-AzureVM -Path $vmConfigurationPath

    New-AzureVM -ServiceName $serviceName -VMs $vmConfig -VNetName $vnetname -ReservedIPName $ReservedIP.ReservedIPName -WaitForBoot:$false

}




13 thoughts on “Assigning a Public Reserved IP to existing Azure Cloud Service

  1. Michal Kolb

    Hello,

    i got error message:
    New-AzureQuickVM : CurrentStorageAccountName is not accessible. Ensure the current storage account is accessible and in the same location or affinity group as your cloud service

    so i had to specify current Storage Account by:
    set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccount $StorageAccount

    Reply
    1. Jan Vidar Elven Post author

      Thanks for commenting, Michal, you are right. If the selected Subscription doesn’t have a current Storage account you would have to add that command also.

      Reply
  2. Marcel

    I tried this for just one VM in my cloud service and it did not seem to work – a different IP was assigned to the VM. Do I need to list ALL the VMs in the cloud service or should this work for individual VMs – so I could potentially assign different IPs to different machines. Maybe I need to shut down all the machines before this takes effect?

    Reply
  3. Jan Vidar Elven Post author

    Hi Marcel. You must do this for all VMs in the cloud service, so that when the cloud service is provisioned again (when the first of the VMs are recreated), it will be configured with the reserved IP.

    Reply
  4. Azza Gazza

    Thanks for the article. I still don’t understand why reserved IP is linked to servicce on VM creation, after all it is cloud service wide parameter, would make more sense to link it to service on service creation/modification.

    Reply
    1. Jan Vidar Elven Post author

      Hi, thanks for your comment. This is because you have to have an actual deployment in the Cloud Service before you can have a IP address in it, either dynamically provisioned or reserved. Try this: Create a new Cloud Service with New-AzureService cmdlet. Then Get-AzureService | FL, you will see that there are no IP address configurations.
      Then, for an existing service, try Get-AzureDeployment -ServiceName . Then you will see that any Reserved IP’s, Virtual IP’s and more are configured. You will also see all the roles in the deployment (aka. the VM’s).
      Hope this makes it clearer 😉
      KR, Jan Vidar Elven

      Reply
  5. ilantz

    Thanks for sharing this !
    you might consider to add a verification that the AzureSubscription has the CurrentStorageAccountName value set.

    Tried this on a “vanilla” subscription and I’ve encountered with:
    New-AzureVM : CurrentStorageAccountName is not accessible. Ensure the current storage account is accessible and in the same location or affinity group as your cloud service.

    Easily solved by running :

    Set-AzureSubscription -SubscriptionName “Subscription Name” -CurrentStorageAccountName myazurestorageaccount

    ilantz

    Reply
  6. Håvard Øverås

    You can now convert an existing dynamic IP address to reserved IP address:
    New-AzureReservedIP -ReservedIPName $ReservedIP -Location “West Europe” -ServiceName $serviceName

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s