使用 Azure PowerShell 将 Azure 虚拟网络移到另一个区域Move an Azure virtual network to another region by using Azure PowerShell

将现有 Azure 虚拟网络从一个区域移到另一个区域的方案有多种。There are various scenarios for moving an existing Azure virtual network from one region to another. 例如,可以创建一个与现有虚拟网络使用相同测试和可用性配置的虚拟网络。For example, you might want to create a virtual network with the same configuration for testing and availability as your existing virtual network. 或者,可以将生产虚拟网络移到另一个区域,作为灾难恢复计划的一部分。Or you might want to move a production virtual network to another region as part of your disaster recovery planning.

可以使用 Azure 资源管理器模板来完成将虚拟网络移到另一个区域的过程。You can use an Azure Resource Manager template to complete the move of the virtual network to another region. 为此,可将虚拟网络导出到某个模板,根据目标区域修改参数,然后将该模板部署到新区域。You do this by exporting the virtual network to a template, modifying the parameters to match the destination region, and then deploying the template to the new region. 有关资源管理器模板的详细信息,请参阅将资源组导出到模板For more information about Resource Manager templates, see Export resource groups to templates.

先决条件Prerequisites

  • 确保你的虚拟网络位于要从中进行移动的 Azure 区域中。Make sure that your virtual network is in the Azure region that you want to move from.

  • 若要导出虚拟网络并部署模板,以便在另一区域创建虚拟网络,需要“网络参与者”角色或更高级别的角色。To export a virtual network and deploy a template to create a virtual network in another region, you need to have the Network Contributor role or higher.

  • 不会重新创建虚拟网络对等互连,如果它们仍存在于模板中,它们将会失败。Virtual network peerings won't be re-created, and they'll fail if they're still present in the template. 在导出模板之前,必须删除所有虚拟网络对等互连。Before you export the template, you have to remove any virtual network peers. 然后,可以在移动虚拟网络后重新建立对等互连。You can then reestablish them after the virtual network move.

  • 确定源网络布局和当前正在使用的所有资源。Identify the source networking layout and all the resources that you're currently using. 此布局包括但不限于负载均衡器、网络安全组 (NSG) 和公共 IP。This layout includes but isn't limited to load balancers, network security groups (NSGs), and public IPs.

  • 验证 Azure 订阅是否允许在目标区域中创建虚拟网络。Verify that your Azure subscription allows you to create virtual networks in the target region. 若要启用所需配额,请联系支持部门。To enable the required quota, contact support.

  • 确保订阅提供足够的资源,以支持在此过程中添加虚拟网络。Make sure that your subscription has enough resources to support the addition of virtual networks for this process. 有关详细信息,请参阅 Azure 订阅和服务限制、配额与约束For more information, see Azure subscription and service limits, quotas, and constraints.

准备移动Prepare for the move

在本部分,你将使用资源管理器模板来准备好要移动的虚拟网络。In this section, you prepare the virtual network for the move by using a Resource Manager template. 然后,使用 Azure PowerShell 命令将虚拟网络移到目标区域。You then move the virtual network to the target region by using Azure PowerShell commands.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

若要使用 PowerShell 导出虚拟网络并部署目标虚拟网络,请执行以下操作:To export the virtual network and deploy the target virtual network by using PowerShell, do the following:

  1. 使用 Connect-AzAccount 命令登录到 Azure 订阅,然后按屏幕说明操作:Sign in to your Azure subscription with the Connect-AzAccount command, and then follow the on-screen directions:

    Connect-AzAccount -Environment AzureChinaCloud
    
  2. 使用 Get-AzVirtualNetwork 获取要移到目标区域的虚拟网络的资源 ID,然后将其置于一个变量中:Obtain the resource ID of the virtual network that you want to move to the target region, and then place it in a variable by using Get-AzVirtualNetwork:

    $sourceVNETID = (Get-AzVirtualNetwork -Name <source-virtual-network-name> -ResourceGroupName <source-resource-group-name>).Id
    
  3. 将源虚拟网络导出到执行 Export-AzResourceGroup 命令时所在的目录中的某个 .json 文件:Export the source virtual network to a .json file in the directory where you execute the command Export-AzResourceGroup:

    Export-AzResourceGroup -ResourceGroupName <source-resource-group-name> -Resource $sourceVNETID -IncludeParameterDefaultValue
    
  4. 下载文件的名称与从中导出资源的资源组的名称相同。The downloaded file has the same name as the resource group that the resource was exported from. 找到使用上述命令导出的 <resource-group-name>.json 文件,然后在编辑器中打开它:Locate the <resource-group-name>.json file, which you exported with the command, and then open it in your editor:

    notepad <source-resource-group-name>.json
    
  5. 若要编辑虚拟网络名称的参数,请将源虚拟网络名称的 defaultValue 属性更改为目标虚拟网络的名称。To edit the parameter of the virtual network name, change the defaultValue property of the source virtual network name to the name of your target virtual network. 请务必将名称括在引号中。Be sure to enclose the name in quotation marks.

    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentmyResourceGroupVNET.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "virtualNetworks_myVNET1_name": {
            "defaultValue": "<target-virtual-network-name>",
            "type": "String"
        }
    
  6. 若要编辑要将虚拟网络移到的目标区域,请更改 resources 下的 location 属性:To edit the target region where the virtual network will be moved, change the location property under resources:

    "resources": [
        {
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2019-06-01",
            "name": "[parameters('virtualNetworks_myVNET1_name')]",
            "location": "<target-region>",
            "properties": {
                "provisioningState": "Succeeded",
                "resourceGuid": "6e2652be-35ac-4e68-8c70-621b9ec87dcb",
                "addressSpace": {
                    "addressPrefixes": [
                        "10.0.0.0/16"
                    ]
                },
    
    
  7. 若要获取区域位置代码,可以通过运行以下命令来使用 Azure PowerShell cmdlet Get-AzLocationTo obtain region location codes, you can use the Azure PowerShell cmdlet Get-AzLocation by running the following command:

    Get-AzLocation | format-table
    
  8. (可选)还可以根据要求更改 <resource-group-name>.json 文件中的其他参数:(Optional) You can also change other parameters in the <resource-group-name>.json file, depending on your requirements:

    • 地址空间:在保存该文件之前,可以更改虚拟网络的地址空间,方法是修改 resources > addressSpace 节并更改 addressPrefixes 属性:Address Space: Before you save the file, you can alter the address space of the virtual network by modifying the resources > addressSpace section and changing the addressPrefixes property:

      "resources": [
          {
              "type": "Microsoft.Network/virtualNetworks",
              "apiVersion": "2019-06-01",
              "name": "[parameters('virtualNetworks_myVNET1_name')]",
              "location": "<target-region>",
              "properties": {
                  "provisioningState": "Succeeded",
                  "resourceGuid": "6e2652be-35ac-4e68-8c70-621b9ec87dcb",
                  "addressSpace": {
                      "addressPrefixes": [
                      "10.0.0.0/16"
                      ]
                  },
      
    • 子网:可以通过更改该文件的 subnets 节来更改子网名称和子网地址空间或在其中添加内容。Subnet: You can change or add to the subnet name and the subnet address space by changing the file's subnets section. 可以通过更改 name 属性来更改子网名称。You can change the name of the subnet by changing the name property. 可以通过更改 addressPrefix 属性来更改子网地址空间:And you can change the subnet address space by changing the addressPrefix property:

      "subnets": [
          {
              "name": "subnet-1",
              "etag": "W/\"d9f6e6d6-2c15-4f7c-b01f-bed40f748dea\"",
              "properties": {
                  "provisioningState": "Succeeded",
                  "addressPrefix": "10.0.0.0/24",
                  "delegations": [],
                  "privateEndpointNetworkPolicies": "Enabled",
                  "privateLinkServiceNetworkPolicies": "Enabled"
              }
          },
          {
              "name": "GatewaySubnet",
              "etag": "W/\"d9f6e6d6-2c15-4f7c-b01f-bed40f748dea\"",
              "properties": {
                  "provisioningState": "Succeeded",
                  "addressPrefix": "10.0.1.0/29",
                  "serviceEndpoints": [],
                  "delegations": [],
                  "privateEndpointNetworkPolicies": "Enabled",
                  "privateLinkServiceNetworkPolicies": "Enabled"
              }
          }
      
      ]
      

      若要在更改地址前缀,请编辑该文件中的两个位置:前一节中的代码,以及以下代码的 type 节。To change the address prefix, edit the file in two places: in the code in the preceding section and in the type section of the following code. 更改以下代码中的 addressPrefix 属性,使之与前一节的代码中的 addressPrefix 属性相匹配。Change the addressPrefix property in the following code to match the addressPrefix property in the code in the preceding section.

          {
              "type": "Microsoft.Network/virtualNetworks/subnets",
              "apiVersion": "2019-06-01",
              "name": "[concat(parameters('virtualNetworks_myVNET1_name'), '/GatewaySubnet')]",
              "dependsOn": [
                  "[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworks_myVNET1_name'))]"
              ],
              "properties": {
                   "provisioningState": "Succeeded",
                   "addressPrefix": "10.0.1.0/29",
                   "serviceEndpoints": [],
                   "delegations": [],
                   "privateEndpointNetworkPolicies": "Enabled",
                   "privateLinkServiceNetworkPolicies": "Enabled"
              }
          },
          {
              "type": "Microsoft.Network/virtualNetworks/subnets",
              "apiVersion": "2019-06-01",
              "name": "[concat(parameters('virtualNetworks_myVNET1_name'), '/subnet-1')]",
              "dependsOn": [
                  "[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworks_myVNET1_name'))]"
               ],
               "properties": {
                  "provisioningState": "Succeeded",
                  "addressPrefix": "10.0.0.0/24",
                  "delegations": [],
                  "privateEndpointNetworkPolicies": "Enabled",
                  "privateLinkServiceNetworkPolicies": "Enabled"
              }
          }
      ]
      
  9. 保存 <resource-group-name>.json 文件。Save the <resource-group-name>.json file.

  10. 使用 New-AzResourceGroup 在目标区域中为要部署的目标虚拟网络创建资源组:Create a resource group in the target region for the target virtual network to be deployed by using New-AzResourceGroup:

    New-AzResourceGroup -Name <target-resource-group-name> -location <target-region>
    
  11. 使用 New-AzResourceGroupDeployment 将编辑的 <resource-group-name>.json 文件部署到在上一步骤中创建的资源组:Deploy the edited <resource-group-name>.json file to the resource group that you created in the previous step by using New-AzResourceGroupDeployment:

    
    New-AzResourceGroupDeployment -ResourceGroupName <target-resource-group-name> -TemplateFile <source-resource-group-name>.json
    
  12. 若要验证是否已在目标区域创建这些资源,请使用 Get-AzResourceGroupGet-AzVirtualNetworkTo verify that the resources were created in the target region, use Get-AzResourceGroup and Get-AzVirtualNetwork:

    
    Get-AzResourceGroup -Name <target-resource-group-name>
    
    
    Get-AzVirtualNetwork -Name <target-virtual-network-name> -ResourceGroupName <target-resource-group-name>
    

删除虚拟网络或资源组Delete the virtual network or resource group

部署虚拟网络后,若要重新开始部署或丢弃目标区域中的虚拟网络,请删除在目标区域中创建的资源组,这样就会删除已移动的虚拟网络。After you've deployed the virtual network, to start over or discard the virtual network in the target region, delete the resource group that you created in the target region, and the moved virtual network will be deleted.

若要删除资源组,请使用 Remove-AzResourceGroupTo remove the resource group, use Remove-AzResourceGroup:


Remove-AzResourceGroup -Name <target-resource-group-name>

清理Clean up

若要提交更改并完成虚拟网络的移动,请执行以下操作之一:To commit your changes and complete the virtual network move, do either of the following:

后续步骤Next steps

在本教程中,你已使用 PowerShell 将虚拟网络从一个区域移到了另一个区域,然后清理了不再需要的源资源。In this tutorial, you moved a virtual network from one region to another by using PowerShell and then cleaned up the unneeded source resources. 若要详细了解如何在区域之间移动资源,以及如何在 Azure 中进行灾难恢复,请参阅:To learn more about moving resources between regions and disaster recovery in Azure, see: