On Microsoft Azure, it's available to configure a static IP for a virtual machine in a virtual network which you created by the new Azure portal or PowerShell. In this post, I'd like to assign a static IP address for a virtual machine and move the virtual machine to a different subnet in the same virtual network. In my Azure lab environment, I have the following IP infrastructure in a virtual network which is named vmnet01 on Microsoft Azure.
There are 3 address spaces and 9 subnets within vmnet01 in this lab environment.
Prerequisites
- Microsoft Azure account is ready for testing
- Make sure there is a Azure PowerShell module on your computer
- 1 virtual machine named PIPTestVM01 is created and assigned to Subnet-1 (10.0.0.0/24) of vmnet01
Goals
- Configure a static IP for PIPTestVM01 in subnets
- Move PIPTestVM01 to another subnet within vmnet01
Lab
1. Connect to your Azure Account by performing Add-AzureAccount.
2. Perform Get-AzureVM -ServiceName PIPTestVM01 -Name PIPTestVM01 to get the current IP address of the virtual machine.
This virtual machine has been assigned a dynamic IP address, 10.0.0.5. we can perform Get-AzureVM -ServiceName PIPTestVM01 -Name PIPTestVM01 | Get-AzureStaticVNETIP to verify any static IP address is assigned to PIPTestVM01.
Then, I'd like to perform Set-AzureStaticVNetIP to assign a static IP address for PIPTestVM01. We can perform this cmdlet virtual machines which are running or not. Notice that if the virtual machine is running, the virtual machine connection is interrupted because assign a new IP address.
3. Perform Get-AzureVM -ServiceName PIPTestVM01 -Name PIPTestVM01 | Set-AzureStaticVNetIP -IPAddress 10.0.0.111 | Update-AzureVM to assign the IP address 10.0.0.111 to PIPTestVM01.
4. Perform Get-AzureVM -ServiceName PIPTestVM01 -Name PIPTestVM01 | Get-AzureStaticVNetIP to verify the result.
Now, I'd like to move PIPTestVM01 to subnet-8.
5. Perform Get-AzureVM -ServiceName PIPTestVM01 -Name PIPTestVM01 | Set-AzureSubnet Subnet-8 | Update-AzureVM to move the virtual machine to subnet-8.
We got the error message:
Update-AzureVM : BadRequest : The static address <IP address> doesn't belong to the address space defined by the role's subnets.
Because we assigned a static IP address to PIPTestVM01. We need to remove the static IP address of the virtual machine before moving it to another subnet. To remove static IP addresses, we can perform Remove-AzureStaticVNetIP.
Update-AzureVM : BadRequest : The static address <IP address> doesn't belong to the address space defined by the role's subnets.
Because we assigned a static IP address to PIPTestVM01. We need to remove the static IP address of the virtual machine before moving it to another subnet. To remove static IP addresses, we can perform Remove-AzureStaticVNetIP.
6. Perform Get-AzureVM -ServiceName PIPTestVM01 -Name PIPTestVM01 | Remove-AzureStaticVNetIP | Update-AzureVM to remove the static IP address of PIPTestVM01.
7. Then, we can perform Get-AzureVM -ServiceName PIPTestVM01 -Name PIPTestVM01 | Get-AzureStaticVNetIP to verify the result.
8. Now, we can perform Get-AzureVM -ServiceName PIPTestVM01 -Name PIPTestVM01 | Set-AzureSubnet Subnet-8 | Update-AzureVM again to move the virtual machine to subnet-8.
9. Perform Get-AzureVM -ServiceName PIPTestVM01 -Name PIPTestVM01 to verify the result.
As a result, PIPTestVM01 has been moved to subnet-8.
To create a new virtual machine and assign the static IP address, we can user new Azure Portal or perform the following cmdlet.
New-AzureVMConfig -Name <VM Name> -ImageName (Get-AzureVMImage | Where {$_.label -match 'Windows Server 2012 R2 datacenter' -and $_.PublishedDate -eq '2/11/2015 4:00:00 PM'}).ImageName -InstanceSize Small | Add-AzureProvisioningConfig -Windows -AdminUsername LabAdmin -Password p@ssw0rd1 | Set-AzureSubnet -SubnetNames Subnet-8 | Set-AzureStaticVNetIP -IPAddress 172.16.2.101 | New-AzureVM -ServiceName <Cloud Service Name> -VNetName vmnet01 -Location 'East Asia'
At this moment, we cannot directly move virtual machines to other virtual networks or move virtual machines in and out between Microsoft default cloud network by GUI or PowerShell. To move virtual machines like the above scenarios, we need to delete virtual machine but keep the virtual hard disks. Then, create a new virtual machine with using existing virtual hard disk option to move virtual machines to other network.
To create a new virtual machine and assign the static IP address, we can user new Azure Portal or perform the following cmdlet.
New-AzureVMConfig -Name <VM Name> -ImageName (Get-AzureVMImage | Where {$_.label -match 'Windows Server 2012 R2 datacenter' -and $_.PublishedDate -eq '2/11/2015 4:00:00 PM'}).ImageName -InstanceSize Small | Add-AzureProvisioningConfig -Windows -AdminUsername LabAdmin -Password p@ssw0rd1 | Set-AzureSubnet -SubnetNames Subnet-8 | Set-AzureStaticVNetIP -IPAddress 172.16.2.101 | New-AzureVM -ServiceName <Cloud Service Name> -VNetName vmnet01 -Location 'East Asia'
This posting is provided “AS IS” with no warranties, and confers no rights!
No comments:
Post a Comment