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

时间:2018-03-05 17:16:08

标签: azure virtual-machine

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

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

感谢您的帮助,kopi

1 个答案:

答案 0 :(得分:2)

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

如果您的磁盘是托管的,他们就不会进入存储帐户,这可能是您无法找到它们的原因,但是您应该检查VM的磁盘刀片某些。当您仔细查看VM资源的磁盘刀片内部时,非托管磁盘将显示对VHD URI的引用,并且将包含" unmanaged"在截图中的括号中。

Unmanaged disk sample screenshot

如果您的磁盘是受管理的,并且您想将其作为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的副本(即,您没有创建托管磁盘的快照),您可以" wrap&# 34;使用以下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,所以要做好准备。

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

  2. 关闭源VM。

  3. 导航到磁盘刀片并选择其中一个磁盘。

  4. 单击“创建快照”。对连接到VM的任何其他磁盘重复,然后重复所有其他VM。获得快照后,可以重新启动虚拟机。

  5. 有人可能会争辩说,如果您在使用访问URL复制源VHD时,如果您对VM保持关闭感到高兴,那么您就不必创建快照,如果您选择了Export>生成URL而不是创建快照。我们正在创建快照,因为您可能真的希望您的VM能够快速再次运行。

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

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

  8. 请务必复制生成的网址,不要丢失该网址。

  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非常相似。有评论,所以你知道发生了什么。

    # 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
    
  11. 这应该是你所追求的。

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

相关问题