将Azure VM移至其他区域中的其他订阅 [英] Move Azure VM to other subscription in other region

查看:60
本文介绍了将Azure VM移至其他区域中的其他订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望将所有Linux VM从一个区域中的订阅移动到另一区域中的订阅.我发现一些线程目前无法实现,但是有一些解决方法.不幸的是我被卡住了. 由于我想按原样移动VM(VM当前正在运行的订阅将终止),我想我不必取消预配映像吗?我必须归纳一下以进行转移吗?

we want to move all of our Linux VMs from a subscription in one region to a subscription in another region. I found a few threads that this isn't possible at the moment, but there are workarounds. Unfortunately I'm stuck. Since I want to move the VMs as they are (the subscription where the VMs are currently running will be terminated) I guess I don't have to deprovision the images? Do I have to generalize them for the transfer?

让他们进入其他订阅的最佳方法是什么?我玩过AzCopy命令,实际上能够将文件从第一个订阅的容器复制到另一个区域的另一个容器. 然后我以为我可以复制VM的vhds,但是在我们存储帐户的blob容器中找不到它们. 在那之后,我创建了一个快照,但是在blob容器中也找不到它.因此,我也无法使用AzCopy复制它们.

Whats the best way to get them to the other subscription? I played with the AzCopy command and actually was able to copy files from a container from the first subscription to a container of the other one in another region. Then I thought I could copy the vhds of the VMs but couldn't find them in the blob containers of our storage accounts. After that, I created a snapshot, but couldn't find it in the blob containers as well. So I also couldn't copy them with AzCopy.

谢谢你,kopi

推荐答案

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

As with everything, it depends how you've set your VM's disks up. The two options are managed and unmanaged disks.

如果磁盘是受管理的,则它们将不在存储帐户中,这可能就是为什么找不到它们的原因,但是,请务必检查VM的磁盘"刀片服务器.当您仔细查看VM资源的磁盘刀片时,非托管磁盘将显示对VHD URI的引用,并且按照此屏幕快照的括号将包括非托管".

If your disks are managed, they won't be in a storage account and that's probably why you can't find them, however you should check in the Disks blade of the VM to be certain. An unmanaged disk will show a reference to the VHD URI when you look closer inside the disks blade of the VM resource and will include "unmanaged" in brackets as per this screenshot.

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

If your disk is managed, and you want to copy it to a storage account as a VHD, these few lines will get you started. Obviously this is PowerShell. You will need to be running a recent version (WMF 5.1 preferably) of PowerShell and install the latest AzureRM modules (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信息.

If on the other hand you have unmanaged disks, the process is a little more complex. Again, the following is PowerShell. You need the source VHD URI (see the screenshot above) and then to provide the destination blob information.

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
}

我要提到的一件事是,如果您正在运行托管磁盘,则门户网站中有一些选项可以将磁盘移至另一个预订.如果您愿意,请返回当前的磁盘类型(托管或非托管),让我知道您希望/希望使用的目标类型,我们可以从那里开始工作.

One thing I will mention is that if you are running managed disks, there are options in the portal to move the disk to another subscription. If you wish, come back with your current disk type (managed or unmanaged) and let me know what the target type you expect/want it to be and we can work from there.

假设您在目标订阅中的新存储帐户中创建了VHD Blob副本(即,您没有创建托管磁盘的快照),则可以将VM容器包装"在使用以下PowerShell磁盘.重要的一行是 Set-AzureRmVMOSDisk ,在这里我们使用 Attach 选项来简单地创建配置并将磁盘插入.

On the assumption that you created a copy of a VHD blob in a new storage account in the target subscription (ie. You didn't create a snapshot of a managed disk), you can "wrap" a VM container around the disk using the following PowerShell. The important line is the Set-AzureRmVMOSDisk where we use the Attach option to simply create the config and plug the disk in.

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

Hopefully that's a decent starter for you but come back if you need more. If you're new to PowerShell though, I apologise but to do what you're asking, you either need PowerShell or the Azure CLI.

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

更加清晰,这是您的过程.

With a little more clarity, here's your process.

我个人将在PowerShell(Azure模块)中完成所有(如果不是全部)大多数任务,但是如果感觉到您是新手,我将通过门户方法进行导航.不幸的是,将需要一些PowerShell,因此请做好准备.

I personally would do most if not all of this in PowerShell (Azure modules) but sensing you’re new to it, I’ll navigate you through the portal method. Unfortunately, a little PowerShell will be needed so prepare yourself.

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

  1. Create a target storage account in the destination subscription – you need to use an intermediate storage account as part of this process. You also need a Virtual Network of course.

关闭源VM.

导航至磁盘"刀片,然后选择其中一个磁盘.

Navigate to the Disks blade and select one of the disks.

单击创建快照".对连接到VM的任何其他磁盘重复该操作,然后再重复所有其他VM.获取快照后,可以重新打开虚拟机.

Click Create Snapshot. Repeat for any other disks attached to the VM and then of course all other VMs. Once you’ve got your snapshots, you can turn the VMs back on.

有人可能会争辩说,如果您很乐意让虚拟机在使用访问URL复制源VHD的过程中保持关闭状态,就像您选择导出">生成"时所得到的那样,则不必创建快照URL,而不是创建快照.我们正在创建快照,因为您实际上可能希望虚拟机再次快速运行.

One could argue that you don’t have to create snapshots if you’re happy for the VMs to remain off while you copy the source VHD using an access URL like you’d get if you chose Export > Generate URL instead of creating a snapshot. We’re creating snapshot since you might actually want your VMs to be running again quickly.

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

For each snapshot that you created, you’ll need to copy it as a blob to a new target account.

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

Open each snapshot and click Export. Increase the valid time to 86400 seconds (one day), then click Generate URL.

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

Make sure you copy the URL that’s generated and don’t lose it.

这是PowerShell,我们将其用于将生成的URL下载到目标订阅和存储帐户中的Blob中.每个磁盘需要花费一些时间进行下载,因此请做好准备!请记住,您需要对每个磁盘的每个快照执行此操作,并根据需要更改每个VM的名称以及可能的存储帐户. (这就是为什么我选择使用PowerShell的原因.)

Here comes the PowerShell which we use to download the generated URL in to a blob in our destination subscription and storage account. The download process takes a while per disk so be prepared! Remember you need to do this for every snapshot of every disk, altering names and possibly storage accounts for each VM as required. (this is the reason why I would choose to use 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
}

  • 一旦blob复制完成,我们将需要将VM包裹在我们的磁盘(或多个磁盘!)上.不过,作为此过程的一部分,我们会将目标存储帐户中现在的VHD导入到托管磁盘中,并将其附加到VM.不幸的是,有更多的PowerShell,但这看起来与我之前共享的PowerShell非常相似.有评论,所以您知道发生了什么.

  • Once the blob copy is complete, we will need to wrap a VM around our disk (or disks!). As part of this process though, we will Import the VHD that is now in our target storage account in to a managed disk and attach it to the VM. More PowerShell unfortunately but this looks very similar to the PowerShell I’ve shared earlier. There are comments so you know what’s going on.

    # 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
    

  • 这应该可以满足您的要求.

    And that should do what you're after.

    显然,有一些注意事项,并且每次复制或重新创建VM时都需要编辑脚本.这不是理想的选择,但我尝试考虑您对PowerShell的熟悉程度.否则,整个事情本来应该是PowerShell.

    There's some considerations obviously and you'll need to edit the script each time you're doing a copy or re-creating the VM. This is not ideal but I've tried to take in to consideration your PowerShell familiarity. Otherwise this whole thing would have been PowerShell.

    这篇关于将Azure VM移至其他区域中的其他订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆