使用 Azure PowerShell 将 Azure 公共 IP 移到另一个区域Move Azure Public IP to another region using Azure PowerShell

在多种情况下,你可能希望将现有的 Azure 公共 IP 从一个区域移到另一个区域。There are various scenarios in which you'd want to move your existing Azure Public IPs from one region to another. 例如,可能需要创建一个具有相同配置和 SKU 的公共 IP,以便进行测试。For example, you may want to create a public IP with the same configuration and sku for testing. 还可能需要按照灾难恢复规划将公共 IP 移到另一个区域。You may also want to move a public IP to another region as part of disaster recovery planning.

Azure 公共 IP 特定于区域,不能从一个区域移到另一个区域。Azure Public IPs are region specific and can't be moved from one region to another. 但是,可以使用 Azure 资源管理器模板来导出公共 IP 的现有配置。You can however, use an Azure Resource Manager template to export the existing configuration of a public IP. 然后,可以将资源暂存在另一区域,方法是:将公共 IP 导出到模板,根据目标区域的情况修改参数,然后将模板部署到新区域。You can then stage the resource in another region by exporting the public IP to a template, modifying the parameters to match the destination region, and then deploy the template to the new region. 有关资源管理器和模板的详细信息,请参阅将资源组导出到模板For more information on Resource Manager and templates, see Export resource groups to templates

先决条件Prerequisites

  • 请确保 Azure 公共 IP 位于要从中移动的 Azure 区域中。Make sure that the Azure Public IP is in the Azure region from which you want to move.

  • Azure 公共 IP 无法在区域之间移动。Azure Public IPs cannot be moved between regions. 必须将新的公共 IP 关联到目标区域中的资源。You'll have to associate the new public ip to resources in the target region.

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

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

  • 验证 Azure 订阅是否允许在已使用的目标区域中创建公共 IP。Verify that your Azure subscription allows you to create public IPs in the target region that's used. 请联系支持部门,启用所需配额。Contact support to enable the required quota.

  • 确保订阅提供足够的资源,以便为此过程添加公共 IP。Make sure that your subscription has enough resources to support the addition of public IPs for this process. 请参阅 Azure 订阅和服务限制、配额和约束See Azure subscription and service limits, quotas, and constraints.

准备并移动Prepare and move

以下步骤介绍了如何使用资源管理器模板准备公共 IP,以便进行配置的移动,通过 Azure PowerShell 将公共 IP 配置移到目标区域。The following steps show how to prepare the public IP for the configuration move using a Resource Manager template, and move the public IP configuration to the target region using Azure PowerShell.

备注

本文进行了更新,以便使用新的 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.

通过脚本导出模板并进行部署Export the template and deploy from a script

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

    Connect-AzAccount -Environment AzureChinaCloud
    
  2. 使用 Get-AzPublicIPAddress 获取要移到目标区域的公共 IP 的资源 ID,将其置于一个变量中:Obtain the resource ID of the public IP you want to move to the target region and place it in a variable using Get-AzPublicIPAddress:

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

    Export-AzResourceGroup -ResourceGroupName <source-resource-group-name> -Resource $sourceVNETID -IncludeParameterDefaultValue
    
  4. 已下载的文件将根据从其导出了资源的资源组来命名。The file downloaded will be named after the resource group the resource was exported from. 找到通过名为“<resource-group-name>.json”的命令导出的文件,在所选编辑器中将其打开****:Locate the file that was exported from the command named <resource-group-name>.json and open it in an editor of your choice:

    notepad <source-resource-group-name>.json
    
  5. 若要编辑公共 IP 名称的参数,请将源公共 IP 名称的属性 defaultValue 更改为目标公共 IP 的名称,确保对名称使用引号:To edit the parameter of the public IP name, change the property defaultValue of the source public IP name to the name of your target public IP, ensure the name is in quotes:

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "publicIPAddresses_myVM1pubIP_name": {
            "defaultValue": "<target-publicip-name>",
            "type": "String"
            }
    }
    
    
  6. 若要编辑要将公共 IP 移到其中的目标区域,请更改 resources 下的 location 属性:To edit the target region where the public IP will be moved, change the location property under resources:

    "resources": [
        {
            "type": "Microsoft.Network/publicIPAddresses",
            "apiVersion": "2019-06-01",
            "name": "[parameters('publicIPAddresses_myPubIP_name')]",
            "location": "<target-region>",
            "sku": {
                "name": "Basic",
                "tier": "Regional"
            },
            "properties": {
                "provisioningState": "Succeeded",
                "resourceGuid": "7549a8f1-80c2-481a-a073-018f5b0b69be",
                "ipAddress": "52.177.6.204",
                "publicIPAddressVersion": "IPv4",
                "publicIPAllocationMethod": "Dynamic",
                "idleTimeoutInMinutes": 4,
                "ipTags": []
            }
        }
    ]       
    
  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. 也可选择更改模板中的其他参数,这些参数是可选的,具体取决于你的要求:You can also change other parameters in the template if you choose, and are optional depending on your requirements:

    • SKU - 可以在配置中将公共 IP 的 SKU 从 standard 更改为 basic 或从 basic 更改为 standard,只需更改 <resource-group-name>.json 文件中的“sku > name”属性即可**** **** ****:Sku - You can change the sku of the public IP in the configuration from standard to basic or basic to standard by altering the sku > name property in the <resource-group-name>.json file:
    "resources": [
        {
        "type": "Microsoft.Network/publicIPAddresses",
        "apiVersion": "2019-06-01",
        "name": "[parameters('publicIPAddresses_myPubIP_name')]",
        "location": "<target-region>",
        "sku": {
            "name": "Basic",
            "tier": "Regional"
        },
    
     For more information on the differences between basic and standard sku public ips, see [Create, change, or delete a public IP address](/virtual-network/virtual-network-public-ip-address).
    
    • 公共 IP 分配方法空闲超时 - 可以在模板中更改这两个选项,只需将 publicIPAllocationMethod 属性从 Dynamic 更改为 Static 或从 Static 更改为 Dynamic 即可。Public IP allocation method and Idle timeout - You can change both of these options in the template by altering the publicIPAllocationMethod property from Dynamic to Static or Static to Dynamic. 若要更改空闲超时,可以将 idleTimeoutInMinutes 属性更改为所需时间。The idle timeout can be changed by altering the idleTimeoutInMinutes property to your desired amount. 默认为 4The default is 4:

      "resources": [
          {
              "type": "Microsoft.Network/publicIPAddresses",
              "apiVersion": "2019-06-01",
              "name": "[parameters('publicIPAddresses_myPubIP_name')]",
              "location": "<target-region>",
              "sku": {
                  "name": "Basic",
                  "tier": "Regional"
              },
              "properties": {
                  "provisioningState": "Succeeded",
                  "resourceGuid": "7549a8f1-80c2-481a-a073-018f5b0b69be",
                  "ipAddress": "52.177.6.204",
                  "publicIPAddressVersion": "IPv4",
                  "publicIPAllocationMethod": "Dynamic",
                  "idleTimeoutInMinutes": 4,
                  "ipTags": []
              }
          }     
      

      若要详细了解分配方法和空闲超时值,请参阅创建、更改或删除公共 IP 地址For more information on the allocation methods and the idle timeout values, see Create, change, or delete a public IP address.

  9. 保存 <resource-group-name>.json 文件****。Save the <resource-group-name>.json file.

  10. 使用 New-AzResourceGroup 在目标区域创建资源组,以便部署目标公共 IP。Create a resource group in the target region for the target public IP to be deployed 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 created in the previous step using New-AzResourceGroupDeployment:

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

    
    Get-AzResourceGroup -Name <target-resource-group-name>
    
    
    
    Get-AzPublicIPAddress -Name <target-publicip-name> -ResourceGroupName <target-resource-group-name>
    
    

弃用Discard

部署以后,如果希望重新开始或弃用目标中的公共 IP,请删除在目标中创建的资源组,系统就会删除已移动的公共 IP。After the deployment, if you wish to start over or discard the public ip in the target, delete the resource group that was created in the target and the moved public IP will be deleted. 若要删除资源组,请使用 Remove-AzResourceGroupTo remove the resource group, use Remove-AzResourceGroup:


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

清理Clean up

若要提交所做的更改并完成虚拟网络的移动,以及删除源虚拟网络或资源组,请使用 Remove-AzResourceGroupRemove-AzPublicIPAddressTo commit the changes and complete the move of the virtual network, delete the source virtual network or resource group, use Remove-AzResourceGroup or Remove-AzPublicIPAddress:


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


Remove-AzPublicIpAddress -Name <source-publicip-name> -ResourceGroupName <resource-group-name>

后续步骤Next steps

在本教程中,我们将 Azure 公共 IP 从一个区域移到了另一个区域,并清理了源资源。In this tutorial, you moved an Azure Public IP from one region to another and cleaned up the source resources. 若要详细了解如何在区域之间移动资源,以及如何在 Azure 中进行灾难恢复,请参阅:To learn more about moving resources between regions and disaster recovery in Azure, refer to: