【问题标题】:Move Azure VM to other subscription in other region将 Azure VM 移动到其他区域的其他订阅
【发布时间】:2018-08-13 10:25:31
【问题描述】:

我们希望将我们所有的 Linux 虚拟机从一个区域的订阅移动到另一个区域的订阅。我发现了一些线程,目前这是不可能的,但有一些解决方法。不幸的是,我被困住了。 由于我想按原样移动虚拟机(虚拟机当前运行的订阅将被终止)我想我不必取消配置图像?我是否必须概括它们以进行转移?

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

感谢你的每一个帮助,kopi

【问题讨论】:

    标签: azure virtual-machine


    【解决方案1】:

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

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

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

    $token = Grant-AzureRmDiskAccess -ResourceGroupName sourceresourcegroupname -DiskName sourcemanageddiskname -DurationInSecond 3600 -Access Read 
    $destContext = New-AzureStorageContext –StorageAccountName destinationstorageaccount -StorageAccountKey 'destinationstorageaccountkey' 
    Start-AzureStorageBlobCopy -AbsoluteUri $token.AccessSAS -DestContainer 'vhds' -DestContext $destContext -DestBlob 'destinationblobname.vhd'
    

    另一方面,如果您有非托管磁盘,则该过程会稍微复杂一些。同样,以下是 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 的副本(即,您没有创建托管磁盘的快照),您可以将 VM 容器“包装”在使用以下 PowerShell 的磁盘。重要的一行是 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,所以请做好准备。

    1. 在目标订阅中创建目标存储帐户 – 您需要使用中间存储帐户作为其中的一部分 过程。当然,您还需要一个虚拟网络。

    2. 关闭源虚拟机。

    3. 导航到“磁盘”边栏选项卡并选择其中一个磁盘。

    4. 单击创建快照。对附加到 VM 的任何其他磁盘重复此操作,当然还有所有其他 VM。获得快照后,您可以重新打开虚拟机。

    5. 有人可能会争辩说,如果您在使用访问 URL 复制源 VHD 时对 VM 保持关闭感到高兴,那么您不必创建快照,就像选择“导出”>“生成”时一样URL 而不是创建快照。我们正在创建快照,因为您实际上可能希望您的虚拟机快速再次运行。

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

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

    8. 确保复制生成的 URL 并且不要丢失它。

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

      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
      }
      
    10. 一旦 blob 复制完成,我们需要在我们的磁盘(或磁盘!)周围包装一个 VM。不过,作为此过程的一部分,我们会将现在位于目标存储帐户中的 VHD 导入托管磁盘并将其附加到 VM。不幸的是,更多的 PowerShell 但这看起来与我之前分享的 PowerShell 非常相似。有 cmets,所以你知道发生了什么。

      # 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。

    【讨论】:

    • 刘易斯您好,感谢您的帮助。正如您所怀疑的,我们所有的虚拟机都使用托管磁盘,我们希望保持这种状态。在我之前的其他尝试中,我还安装了 Azure PowerShell 模块 AzureRM,能够登录并测试一些简单的命令。
    • 别担心,你的虚拟机有数据盘吗?
    • 是的,其中一个 VM 有一个 1TB 的数据磁盘。与此同时,我按照您所说的做了,并将一个托管磁盘作为旧订阅的 vhd 复制到存储帐户。我能够将 AzCopy 复制到新订阅。
    • 我已更新我的答案以包含有关处理托管磁盘的信息,并包含有关在迁移数据磁盘后在目标订阅中配置新 VM 的信息。简而言之,该过程是将托管磁盘复制到 VHD,然后使用 VHD 作为源 blob 将它们重新包装到托管磁盘。上面的所有信息和脚本。
    • 再次感谢您的帮助。根据您的描述,我能够迁移虚拟机。我必须做更多的事情,比如创建其他网络组件(虚拟网络、网络安全组)和一些命令(如登录)需要在 PowerShell 中为德国中部地区切换环境(例如 -EnvironmentName AzureGermanCloud)
    猜你喜欢
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    相关资源
    最近更新 更多