将Azure VM移动到其他区域中的其他订阅

将Azure VM移动到其他区域中的其他订阅,azure,virtual-machine,Azure,Virtual Machine,我们希望将所有Linux虚拟机从一个区域的订阅移动到另一个区域的订阅。我发现一些线程目前不可能这样做,但有一些变通方法。不幸的是我被卡住了。 既然我想按原样移动虚拟机(虚拟机当前运行的订阅将被终止),我想我不必取消映像?我是否必须对他们的转移进行概括 让他们转到其他订阅的最佳方式是什么?我使用了AzCopy命令,实际上能够将文件从第一个订阅的容器复制到另一个区域中另一个订阅的容器。 然后我想我可以复制虚拟机的VHD,但在我们存储帐户的blob容器中找不到它们。 之后,我创建了一个快照,但在blo

我们希望将所有Linux虚拟机从一个区域的订阅移动到另一个区域的订阅。我发现一些线程目前不可能这样做,但有一些变通方法。不幸的是我被卡住了。 既然我想按原样移动虚拟机(虚拟机当前运行的订阅将被终止),我想我不必取消映像?我是否必须对他们的转移进行概括

让他们转到其他订阅的最佳方式是什么?我使用了AzCopy命令,实际上能够将文件从第一个订阅的容器复制到另一个区域中另一个订阅的容器。 然后我想我可以复制虚拟机的VHD,但在我们存储帐户的blob容器中找不到它们。 之后,我创建了一个快照,但在blob容器中也找不到它。所以我也不能用AzCopy复制它们


感谢您的帮助,kopi

与其他一切一样,这取决于您如何设置VM的磁盘。这两个选项是托管磁盘和非托管磁盘

如果您的磁盘被管理,它们将不在存储帐户中,这可能就是您找不到它们的原因,但是您应该检查VM的磁盘刀片以确定。当您仔细查看VM资源的磁盘刀片内部时,非托管磁盘将显示对VHD URI的引用,并将根据此屏幕截图在括号中包含“非托管”

如果您的磁盘已被管理,并且您希望将其作为VHD复制到存储帐户,那么这几行代码将帮助您开始。显然,这是PowerShell。您需要运行PowerShell的最新版本(最好是WMF 5.1),并安装最新的AzureRM模块(安装模块AzureRM-Scope CurrentUser)

另一方面,如果您有非托管磁盘,则该过程会稍微复杂一些。同样,下面是PowerShell。您需要源VHD URI(参见上面的屏幕截图),然后提供目标blob信息

Select-AzureRmSubscription 'SourceSubscription'

### Source VHD - authenticated container ###
$srcUri = "https://sourcestorageaccount.blob.core.windows.net/vhds/nameoffile.vhd" 

### Source Storage Account ###
$srcStorageAccount = "sourcestorageaccount"
$srcStorageKey = "sourcestorageaccountkey=="

### Create the source storage account context ### 
$srcContext = New-AzureStorageContext  –StorageAccountName $srcStorageAccount `
                                        -StorageAccountKey $srcStorageKey

# Target Storage Account
Select-AzureRmSubscription 'DestinationSubscription'

### Target Storage Account ###
$destStorageAccount = "destinationstorageaccount"
$destStorageKey = "destinationstorageaccountkey=="


### Create the destination storage account context ### 
$destContext = New-AzureStorageContext  –StorageAccountName $destStorageAccount `
                                        -StorageAccountKey $destStorageKey  

### Destination Container Name ### 
$containerName = "copiedvhds"

### Create the container on the destination ### 
New-AzureStorageContainer -Name $containerName -Context $destContext 

### Start the asynchronous copy - specify the source authentication with -SrcContext ### 
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `
                                    -SrcContext $srcContext `
                                    -DestContainer $containerName `
                                    -DestBlob "destinationblob.vhd" `
                                    -DestContext $destContext


### Loop until complete ###                                    
While($status.Status -eq "Pending"){
  $status = $blob1 | Get-AzureStorageBlobCopyState 
  Start-Sleep 300
  ### Print out status ###
  $status
}
我要提到的一点是,如果您运行的是托管磁盘,那么门户中有一些选项可以将磁盘移动到另一个订阅。如果您愿意,请返回您当前的磁盘类型(托管或非托管),并让我知道您期望/想要的目标类型,我们可以从那里开始工作

假设您在目标订阅的新存储帐户中创建了VHD blob的副本(即,您没有创建受管磁盘的快照),您可以使用以下PowerShell在磁盘周围“包装”VM容器。重要的一行是Set AzureRmVMOSDisk,我们使用Attach选项创建配置并插入磁盘

# Name the new server
$ServerName = 'MYSERVER'
# Provide the URI of the disk to be attached as the OS disk.
$LocationOfVHD = "https://destinationstorageaccount.blob.core.windows.net/copiedvhds/destinationblob.vhd"
# Create a NIC and get the target VNET and subnet references.
$nicName = "$ServerName-nic"
# Set the private IP Address of the NIC
$PrivateIPAddress = '10.203.99.4'
# Set the DNS server for the NIC
$DNSServerAddress = '10.203.99.4'
# Destination resource group
$DestinationResourceGroupName = 'RG-DESTINATION'
# Location where the resources are to be built
$LocationOfResources = 'UK West'

# Select the appropriate subscription
Select-AzureRmSubscription 'DestinationSubscription'

# Create a VM machine configuration
$VM = New-AzureRmVMConfig -VMSize 'Standard_DS2_v2' -VMName $ServerName

# Set the VM OS Disk value to the URI where the disk was restored/copied and attach it. Set the OS type and caching as desired
Set-AzureRmVMOSDisk -VM $VM -Name "$ServerName-OS" -VhdUri $LocationOfVHD -CreateOption "Attach" -Windows -Caching ReadWrite

# Get the reference to the VNET in which the NIC will be bound.
$vnet = Get-AzureRmVirtualNetwork -Name "TargetAzureNetwork" -ResourceGroupName 'TARGETVIRTUALNETWORK'

# Get the reference to the Subnet ID in which the NIC will be bound.
$Subnet = $vnet.Subnets | Where-Object {$_.Name -eq 'TARGETSUBNET'} | Select-Object 'Id'

# Get the ID of the NSG which will be bound to the NIC - if you want.
$NSG = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $DestinationResourceGroupName -Name 'NSG-DESTINATIONVM'

# Create the NIC with the VNET/subnet reference
# You could also define here the backend load balanced pool etc that this NIC belongs to.
$NIC = New-AzureRmNetworkInterface `
                    -Name $nicName `
                    -ResourceGroupName $DestinationResourceGroupName `
                    -Location $LocationOfResources `
                    -SubnetId $Subnet.Id `
                    -NetworkSecurityGroupId $NSG.Id  `
                    -PrivateIpAddress $PrivateIPAddress `
                    -DnsServer $DNSServerAddress

# Add the newly created NIC to the VM definition.
$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $NIC.Id

# Create the VM
New-AzureRmVM -ResourceGroupName $DestinationResourceGroupName -Location $LocationOfResources -VM $VM
希望这对你来说是一个不错的开始,但是如果你需要更多的话,请回来。如果您是PowerShell新手,我很抱歉,但要按照您的要求操作,您需要PowerShell或Azure CLI

更新(现在我知道这些是托管磁盘)

更清楚一点,这里是您的流程

我个人会在PowerShell(Azure模块)中完成大部分(如果不是全部的话)工作,但如果感觉到您对它不熟悉,我会通过门户方法为您导航。不幸的是,需要一点PowerShell,所以请做好准备

  • 在目标订阅中创建目标存储帐户- 您需要使用中间存储帐户作为此操作的一部分 过程当然,你还需要一个虚拟网络

  • 关闭源VM

  • 导航到磁盘刀片并选择其中一个磁盘

  • 单击创建快照。对连接到虚拟机的任何其他磁盘重复此操作,当然还有所有其他虚拟机。获取快照后,可以重新打开虚拟机

  • 有人可能会争辩说,如果您希望VM保持关闭状态,而您使用访问URL复制源VHD,则不必创建快照,就像您选择“导出”>“生成URL”而不是创建快照一样。我们正在创建快照,因为您可能希望虚拟机能够快速重新运行

  • 对于创建的每个快照,都需要将其作为blob复制到新的目标帐户

  • 打开每个快照并单击导出。将有效时间增加到86400秒(一天),然后单击生成URL

  • 确保复制生成的URL,并且不会丢失它

  • 下面是PowerShell,我们使用它将生成的URL下载到目标订阅和存储帐户中的blob中。每个磁盘的下载过程需要一段时间,所以请做好准备!请记住,您需要对每个磁盘的每个快照执行此操作,并根据需要更改每个VM的名称和存储帐户。(这就是我选择使用PowerShell的原因)

  • 一旦blob拷贝完成,我们将需要在磁盘(或多个磁盘!)周围包装一个虚拟机。不过,作为此过程的一部分,我们将把目标存储帐户中的VHD导入到一个托管磁盘中,并将其连接到VM。不幸的是,还有更多的PowerShell,但这看起来与我之前共享的PowerShell非常相似。有评论,所以你知道发生了什么

    # Name the new server
    $ServerName = 'DESTINATIONSERVERNAME'
    # Provide the URI of the disk to be attached as the OS disk.
    $LocationOfOSVHD = "https://destinationstorage.blob.core.windows.net/vhds/destinationblob.vhd"
    $LocationOfDataDisk1 = "https://lrdestinationstorage.blob.core.windows.net/vhds/destinationblob1.vhd"
    # Create a NIC and get the target VNET and subnet references.
    $nicName = "$ServerName-nic"
    # Set the private IP Address of the NIC
    $PrivateIPAddress = '10.0.0.4'
    # Set the DNS server for the NIC
    $DNSServerAddress = '8.8.8.8'
    # Destination resource group
    $DestinationResourceGroupName = 'RG-DESTINATION'
    # Location where the resources are to be built
    $LocationOfResources = 'West Europe'
    
    # Select the appropriate subscription
    Select-AzureRmSubscription 'DestinationSubscription'
    
    # Create a VM machine configuration
    $VM = New-AzureRmVMConfig -VMSize 'Standard_DS2_v2' -VMName $ServerName
    
    # Create a managed disk configuration and import the source VHD
    $OSDisk = New-AzureRmDiskConfig -AccountType StandardLRS -Location $LocationOfResources -CreateOption Import -SourceUri $LocationOfOSVHD
    
    # Create the managed disk using the configuration defined above.
    $Disk1 = New-AzureRmDisk -DiskName 'OS-DISK' -Disk $OSDisk -ResourceGroupName $DestinationResourceGroupName
    
    # Set the VM’s OS Disk to be the managed disk.
    Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $Disk1.Id -StorageAccountType StandardLRS -DiskSizeInGB 80 -CreateOption Attach -Windows -Caching ReadWrite
    
    # Repeat ourselves for any data disks that must also be attached to the VM in the destination.
    # Increase LUN numbering and changing names etc as required.
    $DataDisk = New-AzureRmDiskConfig -AccountType StandardLRS -Location $LocationOfResources -CreateOption Import -SourceUri $LocationOfDataDisk1
    $Disk2 = New-AzureRmDisk -DiskName 'DATA-1' -Disk $DataDisk -ResourceGroupName $DestinationResourceGroupName
    Add-AzureRmVMDataDisk -ManagedDiskId $Disk2.Id -VM $vm -CreateOption Attach -DiskSizeInGB 20 -Caching ReadWrite -StorageAccountType StandardLRS -Name 'DATA-1' -Lun 0
    
    # Get the reference to the VNET in which the NIC will be bound. Might not be in the resource group you’re migrating to so this is left manual.
    $vnet = Get-AzureRmVirtualNetwork -Name "DestinationAzureNetwork" -ResourceGroupName 'RG-DESTINATION'
    
    # Get the reference to the Subnet ID in which the NIC will be bound. Replace 'default' with the name of the target subnet
    $Subnet = $vnet.Subnets | Where-Object {$_.Name -eq 'default'} | Select-Object 'Id'
    
    # Create the NIC with the VNET/subnet reference
    # You could also define here the backend load balanced pool etc that this NIC belongs to.
    $NIC = New-AzureRmNetworkInterface `
                        -Name $nicName `
                        -ResourceGroupName $DestinationResourceGroupName `
                        -Location $LocationOfResources `
                        -SubnetId $Subnet.Id `
                        -PrivateIpAddress $PrivateIPAddress `
                        -DnsServer $DNSServerAddress
    
    # Add the newly created NIC to the VM definition.
    $VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $NIC.Id
    
    # Create the VM
    New-AzureRmVM -ResourceGroupName $DestinationResourceGroupName -Location $LocationOfResources -VM $VM
    
  • 这应该是你想要的


    显然,有一些考虑因素,您需要在每次复制或重新创建VM时编辑脚本。这并不理想,但我已尝试考虑到您对PowerShell的熟悉程度。否则整个事情都会变成PowerShell。

    与所有事情一样,这取决于您如何设置VM的磁盘。这两个选项是托管磁盘和非托管磁盘

    Source VHD - authenticated container ###
    $srcUri = "https://md-f0p4tdq5fjpc.blob.core.windows.net/txwptxxxqvct/abcd?sv=2017-04-17&sr=b&si=cce17550-75f7-429c-bf08-31d0ae2da552&sig=oI%2BNOmQ4F75H8AlSwm7rJb%2Frm2Jhl9kfNZ7Jt2cUJpY%3D" 
    
    # Target Storage Account
    Select-AzureRmSubscription 'DestinationSubscription'
    
    ### Target Storage Account ###
    $destStorageAccount = "destinationstorageaccount"
    $destStorageKey = "IkEvDdWTvTxN7v45VgAcvyEpZB9rGyYwyZhxvhG6eQaPIB15MQOa0vkvsHxMDpmUIJqq42UGiU8ji5Lqt39rAg=="
    
    
    ### Create the destination storage account context ### 
    $destContext = New-AzureStorageContext  –StorageAccountName $destStorageAccount `
                                            -StorageAccountKey $destStorageKey  
    
    ### Destination Container Name ### 
    $containerName = "vhds"
    
    ### Create the container on the destination ### 
    New-AzureStorageContainer -Name $containerName -Context $destContext 
    
    ### Start the asynchronous copy
    $blob1 = Start-AzureStorageBlobCopy -AbsoluteUri $srcUri `
                                        -DestContainer $containerName `
                                        -DestBlob "destinationblob.vhd" `
                                        -DestContext $destContext
    
    $status = $blob1 | Get-AzureStorageBlobCopyState
    
    ### Loop until complete ###                                    
    While($status.Status -eq "Pending"){
    $status = $blob1 | Get-AzureStorageBlobCopyState 
    Start-Sleep 300
    ### Print out status ###
    $status
    }
    
    如果您的磁盘被管理,它们将不在存储帐户中,这可能就是您找不到它们的原因,但是您应该检查VM的磁盘刀片以确定。当您loo时,非托管磁盘将显示对VHD URI的引用
    # Name the new server
    $ServerName = 'DESTINATIONSERVERNAME'
    # Provide the URI of the disk to be attached as the OS disk.
    $LocationOfOSVHD = "https://destinationstorage.blob.core.windows.net/vhds/destinationblob.vhd"
    $LocationOfDataDisk1 = "https://lrdestinationstorage.blob.core.windows.net/vhds/destinationblob1.vhd"
    # Create a NIC and get the target VNET and subnet references.
    $nicName = "$ServerName-nic"
    # Set the private IP Address of the NIC
    $PrivateIPAddress = '10.0.0.4'
    # Set the DNS server for the NIC
    $DNSServerAddress = '8.8.8.8'
    # Destination resource group
    $DestinationResourceGroupName = 'RG-DESTINATION'
    # Location where the resources are to be built
    $LocationOfResources = 'West Europe'
    
    # Select the appropriate subscription
    Select-AzureRmSubscription 'DestinationSubscription'
    
    # Create a VM machine configuration
    $VM = New-AzureRmVMConfig -VMSize 'Standard_DS2_v2' -VMName $ServerName
    
    # Create a managed disk configuration and import the source VHD
    $OSDisk = New-AzureRmDiskConfig -AccountType StandardLRS -Location $LocationOfResources -CreateOption Import -SourceUri $LocationOfOSVHD
    
    # Create the managed disk using the configuration defined above.
    $Disk1 = New-AzureRmDisk -DiskName 'OS-DISK' -Disk $OSDisk -ResourceGroupName $DestinationResourceGroupName
    
    # Set the VM’s OS Disk to be the managed disk.
    Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $Disk1.Id -StorageAccountType StandardLRS -DiskSizeInGB 80 -CreateOption Attach -Windows -Caching ReadWrite
    
    # Repeat ourselves for any data disks that must also be attached to the VM in the destination.
    # Increase LUN numbering and changing names etc as required.
    $DataDisk = New-AzureRmDiskConfig -AccountType StandardLRS -Location $LocationOfResources -CreateOption Import -SourceUri $LocationOfDataDisk1
    $Disk2 = New-AzureRmDisk -DiskName 'DATA-1' -Disk $DataDisk -ResourceGroupName $DestinationResourceGroupName
    Add-AzureRmVMDataDisk -ManagedDiskId $Disk2.Id -VM $vm -CreateOption Attach -DiskSizeInGB 20 -Caching ReadWrite -StorageAccountType StandardLRS -Name 'DATA-1' -Lun 0
    
    # Get the reference to the VNET in which the NIC will be bound. Might not be in the resource group you’re migrating to so this is left manual.
    $vnet = Get-AzureRmVirtualNetwork -Name "DestinationAzureNetwork" -ResourceGroupName 'RG-DESTINATION'
    
    # Get the reference to the Subnet ID in which the NIC will be bound. Replace 'default' with the name of the target subnet
    $Subnet = $vnet.Subnets | Where-Object {$_.Name -eq 'default'} | Select-Object 'Id'
    
    # Create the NIC with the VNET/subnet reference
    # You could also define here the backend load balanced pool etc that this NIC belongs to.
    $NIC = New-AzureRmNetworkInterface `
                        -Name $nicName `
                        -ResourceGroupName $DestinationResourceGroupName `
                        -Location $LocationOfResources `
                        -SubnetId $Subnet.Id `
                        -PrivateIpAddress $PrivateIPAddress `
                        -DnsServer $DNSServerAddress
    
    # Add the newly created NIC to the VM definition.
    $VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $NIC.Id
    
    # Create the VM
    New-AzureRmVM -ResourceGroupName $DestinationResourceGroupName -Location $LocationOfResources -VM $VM