创建虚拟网络对等互连 - 不同的部署模型和不同的订阅Create a virtual network peering - different deployment models and subscriptions

本文介绍如何在通过不同部署模型创建的虚拟网络间创建虚拟网络对等互连。In this tutorial, you learn to create a virtual network peering between virtual networks created through different deployment models. 虚拟网络位于不同订阅。The virtual networks exist in different subscriptions. 在两个虚拟网络之间建立对等互连可让不同虚拟网络中的资源以相同的带宽和延迟彼此通信,就像这些资源位于同一个虚拟网络中一样。Peering two virtual networks enables resources in different virtual networks to communicate with each other with the same bandwidth and latency as though the resources were in the same virtual network. 了解有关虚拟网络对等互连的详细信息。Learn more about Virtual network peering.

创建虚拟网络对等互连的步骤有所不同,具体取决于虚拟网络是否位于相同订阅,以及创建虚拟网络的 Azure 部署模型The steps to create a virtual network peering are different, depending on whether the virtual networks are in the same, or different, subscriptions, and which Azure deployment model the virtual networks are created through. 单击下表中的方案,了解如何采用其他方案创建虚拟网络对等互连:Learn how to create a virtual network peering in other scenarios by clicking the scenario from the following table:

Azure 部署模型Azure deployment model Azure 订阅Azure subscription
均为资源管理器模型Both Resource Manager 相同Same
均为 Resource Manager 模型Both Resource Manager 不同Different
一个为资源管理器模型,一个为经典模型One Resource Manager, one classic 相同Same

不能在通过经典部署模型部署的两个虚拟网络之间创建对等互连。A virtual network peering cannot be created between two virtual networks deployed through the classic deployment model. 本教程使用同一区域中的虚拟网络。This tutorial uses virtual networks that exist in the same region. 本教程将在同一区域中的虚拟网络之间建立对等互连。This tutorial peers virtual networks in the same region. 还可以将不同受支持的区域中的虚拟网络对等互连。You can also peer virtual networks in different supported regions. 建议在对等互连虚拟网络之前让自己熟悉对等互连的要求和约束It's recommended that you familiarize yourself with the peering requirements and constraints before peering virtual networks.

在位于不同订阅的虚拟网络间创建虚拟网络对等互连时,两个订阅均必须与同一 Azure Active Directory 租户相关联。When creating a virtual network peering between virtual networks that exist in different subscriptions, the subscriptions must both be associated to the same Azure Active Directory tenant. 如果还没有 Azure Active Directory 租户,可快速创建一个If you don't already have an Azure Active Directory tenant, you can quickly create one. 可以使用 Azure VPN 网关连接不同订阅和不同 Azure Active Directory 租户中的虚拟网络。You can connect virtual networks in different subscriptions and different Azure Active Directory tenants using an Azure VPN Gateway.

可使用 Azure 门户、Azure 命令行接口 (CLI) 或 Azure PowerShell 创建虚拟网络对等互连。You can use the Azure portal, the Azure command-line interface (CLI), or Azure PowerShell to create a virtual network peering. 单击以前的任何工具链接即可直接转到使用所选工具创建虚拟网络对等互连的步骤。Click any of the previous tool links to go directly to the steps for creating a virtual network peering using your tool of choice.

创建对等互连 - Azure 门户Create peering - Azure portal

本教程为每个订阅使用不同的帐户。This tutorial uses different accounts for each subscription. 如果使用的帐户可访问这两个订阅,则可使用相同帐户完成所有步骤,跳过注销门户的步骤,及为虚拟网络分配其他用户权限的步骤。If you're using an account that has permissions to both subscriptions, you can use the same account for all steps, skip the steps for logging out of the portal, and skip the steps for assigning another user permissions to the virtual networks.

  1. 以 UserA 的身份登录 Azure 门户Log in to the Azure portal as UserA. 用于登录的帐户必须拥有创建虚拟网络对等互连的必要权限。The account you log in with must have the necessary permissions to create a virtual network peering. 有关权限列表,请参阅虚拟网络对等互连权限For a list of permissions, see Virtual network peering permissions.

  2. 依次单击“+ 新建”、“网络”、“虚拟网络”。 Click + New, click Networking, then click Virtual network.

  3. 在“创建虚拟网络”边栏选项卡中,为以下设置输入或选择值,然后单击“创建”: In the Create virtual network blade, enter, or select values for the following settings, then click Create:

    • 名称myVnetAName: myVnetA
    • 地址空间10.0.0.0/16Address space: 10.0.0.0/16
    • 子网名称:默认值 Subnet name: default
    • 子网地址范围10.0.0.0/24Subnet address range: 10.0.0.0/24
    • 订阅:选择订阅 A。Subscription: Select subscription A.
    • 资源组:选择“新建” ,然后输入 myResourceGroupA Resource group: Select Create new and enter myResourceGroupA
    • 位置中国东部Location: China East
  4. 在门户顶部的“搜索资源”框中键入 myVnetAIn the Search resources box at the top of the portal, type myVnetA. 单击出现在搜索结果中的“myVnetA” 。Click myVnetA when it appears in the search results. 随即显示“myVnetA”虚拟网络的边栏选项卡 。A blade appears for the myVnetA virtual network.

  5. 在显示的“myVnetA”边栏选项卡中,单击左侧垂直选项列表中的“访问控制(IAM)” 。In the myVnetA blade that appears, click Access control (IAM) from the vertical list of options on the left side of the blade.

  6. 在显示的“myVnetA - 访问控制(IAM)”边栏选项卡中,单击“+ 添加角色分配” 。In the myVnetA - Access control (IAM) blade that appears, click + Add role assignment.

  7. 在显示的“添加角色分配” 边栏选项卡中,选择“角色” 框中的“网络参与者” 。In the Add role assignment blade that appears, select Network contributor in the Role box.

  8. 在“选择”框中,选择 UserB,或者键入 UserB 的电子邮件地址来搜索用户名 。In the Select box, select UserB, or type UserB's email address to search for it. 显示的用户列表与要为其设置对等互连的虚拟网络来自同一个 Azure Active Directory 租户。The list of users shown is from the same Azure Active Directory tenant as the virtual network you're setting up the peering for. 单击出现在列表中的用户 B。Click UserB when it appears in the list.

  9. 单击“保存” 。Click Save.

  10. 以 UserA 的身份注销门户,然后以 UserB 的身份登录。Log out of the portal as UserA, then log in as UserB.

  11. 单击“+ 新建”,在“在市场中搜索”框中键入“虚拟网络”,然后在搜索结果中单击“虚拟网络” 。Click + New, type Virtual network in the Search the Marketplace box, then click Virtual network in the search results.

  12. 在显示的“虚拟网络”边栏选项卡中,在“选择部署模型”框中选择“经典”,单击“创建” 。In the Virtual Network blade that appears, select Classic in the Select a deployment model box, then click Create.

  13. 在显示的“创建虚拟网络(经典)”边栏选项卡中,输入以下值:In the Create virtual network (classic) box that appears, enter the following values:

    • 名称myVnetBName: myVnetB
    • 地址空间10.1.0.0/16Address space: 10.1.0.0/16
    • 子网名称:默认值 Subnet name: default
    • 子网地址范围10.1.0.0/24Subnet address range: 10.1.0.0/24
    • 订阅:选择订阅 B。Subscription: Select subscription B.
    • 资源组:选择“新建” ,然后输入 myResourceGroupB Resource group: Select Create new and enter myResourceGroupB
    • 位置中国东部Location: China East
  14. 在门户顶部的“搜索资源”框中键入 myVnetBIn the Search resources box at the top of the portal, type myVnetB. 单击出现在搜索结果中的“myVnetB” 。Click myVnetB when it appears in the search results. 随即显示“myVnetB”虚拟网络的边栏选项卡 。A blade appears for the myVnetB virtual network.

  15. 在显示的“myVnetB”边栏选项卡中,单击左侧垂直选项列表中的“属性” 。In the myVnetB blade that appears, click Properties from the vertical list of options on the left side of the blade. 复制“资源 ID”,在稍后的步骤中使用 。Copy the RESOURCE ID, which is used in a later step. 资源 ID 类似于以下示例:/subscriptions/<Subscription ID>/resourceGroups/myResourceGroupB/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetBThe resource ID is similar to the following example: /subscriptions/<Subscription ID>/resourceGroups/myResourceGroupB/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB

  16. 完成 myVnetB 的步骤 5-9,输入步骤 8 中的 UserAComplete steps 5-9 for myVnetB, entering UserA in step 8.

  17. 以 UserB 的身份注销门户,然后以 UserA 的身份登录。Log out of the portal as UserB and log in as UserA.

  18. 在门户顶部的“搜索资源”框中键入 myVnetAIn the Search resources box at the top of the portal, type myVnetA. 单击出现在搜索结果中的“myVnetA” 。Click myVnetA when it appears in the search results. 随即显示“myVnet”虚拟网络的边栏选项卡 。A blade appears for the myVnet virtual network.

  19. 单击“myVnetA” 。Click myVnetA.

  20. 在显示的“myVnetA”边栏选项卡中,单击左侧垂直选项列表中的“对等互连” 。In the myVnetA blade that appears, click Peerings from the vertical list of options on the left side of the blade.

  21. 在显示的“myVnetA - 对等互连”边栏选项卡中,单击“+ 添加” In the myVnetA - Peerings blade that appeared, click + Add

  22. 在显示的“添加对等互连”边栏选项卡中,输入或选择以下选项,然后单击“确定”: In the Add peering blade that appears, enter, or select the following options, then click OK:

    • 名称myVnetAToMyVnetBName: myVnetAToMyVnetB
    • 虚拟网络部署模型:选择“经典” 。Virtual network deployment model: Select Classic.
    • 我知道我的资源 ID:选中此框。I know my resource ID: Check this box.
    • 资源 ID:输入步骤 15 中 myVnetB 的资源 ID。Resource ID: Enter the resource ID of myVnetB from step 15.
    • 允许虚拟网络访问: 确保选中“已启用”。 Allow virtual network access: Ensure that Enabled is selected. 本教程不使用其他任何设置。No other settings are used in this tutorial. 若要了解所有对等互连设置,请阅读管理虚拟网络对等互连To learn about all peering settings, read Manage virtual network peerings.
  23. 在上一步骤中单击“确定”后,“添加对等互连”边栏选项卡将会关闭,并再次显示“myVnetA - 对等互连”边栏选项卡 。After clicking OK in the previous step, the Add peering blade closes and you see the myVnetA - Peerings blade again. 几秒钟后,创建的对等互连将显示在该边栏选项卡中。After a few seconds, the peering you created appears in the blade. 所创建的 myVnetAToMyVnetB 对等互连的“对等互连状态”列中列出了“已连接” 。Connected is listed in the PEERING STATUS column for the myVnetAToMyVnetB peering you created. 现已建立对等互连。The peering is now established. 无需将虚拟网络(经典)与虚拟网络 (Resource Manager) 进行对等。There is no need to peer the virtual network (classic) to the virtual network (Resource Manager).

    在任一虚拟网络中创建的任何 Azure 资源现在都可通过其 IP 地址相互通信。Any Azure resources you create in either virtual network are now able to communicate with each other through their IP addresses. 如果为虚拟网络使用默认的 Azure 名称解析,则虚拟网络中的资源无法跨虚拟网络解析名称。If you're using default Azure name resolution for the virtual networks, the resources in the virtual networks are not able to resolve names across the virtual networks. 若要跨对等互连中的虚拟网络解析名称,必须创建自己的 DNS 服务器。If you want to resolve names across virtual networks in a peering, you must create your own DNS server. 了解如何使用自己的 DNS 服务器进行名称解析Learn how to set up Name resolution using your own DNS server.

  24. 可选:尽管本教程未介绍如何创建虚拟机,但可以在每个虚拟网络中创建一个虚拟机并将其相互连接,以验证连接性。Optional: Though creating virtual machines is not covered in this tutorial, you can create a virtual machine in each virtual network and connect from one virtual machine to the other, to validate connectivity.

  25. 可选:若要删除在本教程中创建的资源,请完成本文的删除资源部分中所述的步骤。Optional: To delete the resources that you create in this tutorial, complete the steps in the Delete resources section of this article.

创建对等互连 - Azure CLICreate peering - Azure CLI

本教程为每个订阅使用不同的帐户。This tutorial uses different accounts for each subscription. 如果使用的帐户可访问这两个订阅,则可使用相同帐户完成所有步骤,可跳过注销 Azure 的步骤,并删除创建用户角色分配的脚本行。If you're using an account that has permissions to both subscriptions, you can use the same account for all steps, skip the steps for logging out of Azure, and remove the lines of script that create user role assignments. 将以下所有脚本中的 UserA@azure.com 和 UserB@azure.com 替换为 UserA 和 UserB 使用的用户名。Replace UserA@azure.com and UserB@azure.com in all of the following scripts with the usernames you're using for UserA and UserB. 使用 Azure 经典 CLI 和 Azure CLI 完成以下步骤。Complete the following steps using the Azure classic CLI and the Azure CLI. 可以通过在本地计算机上安装经典 CLICLI 并运行命令来完成这些步骤。You can complete the steps or by installing the classic CLI and CLI and running the commands on your local computer.

  1. 使用 azure login -e AzureChinaCloud 命令打开命令会话并登录 Azure。Open a command session and sign in to Azure using the azure login -e AzureChinaCloud command.

  2. 输入 azure config mode asm 命令,在服务管理模式下运行经典 CLI。Run the classic CLI in Service Management mode by entering the azure config mode asm command.

  3. 输入下述经典 CLI 命令以创建虚拟网络(经典):Enter the following classic CLI command to create the virtual network (classic):

    azure network vnet create --vnet myVnetB --address-space 10.1.0.0 --cidr 16 --location "China East"
    
  4. 必须使用 bash shell 和 Azure CLI(非经典 CLI)完成其余步骤。The remaining steps must be completed using a bash shell with the Azure CLI (not the classic CLI).

  5. 将以下脚本复制到电脑上的文本编辑器。Copy the following script to a text editor on your PC. <SubscriptionB-Id> 替换为订阅 ID。Replace <SubscriptionB-Id> with your subscription ID. 如果不知道订阅 ID,请输入 az account show 命令。If you don't know your subscription Id, enter the az account show command. 输出中的 id 值就是订阅 ID。复制修改后的脚本,将其粘贴到 CLI 会话中,然后按 EnterThe value for id in the output is your subscription Id. Copy the modified script, paste it in to your CLI session, and then press Enter.

    az role assignment create \
      --assignee UserA@azure.com \
      --role "Classic Network Contributor" \
      --scope /subscriptions/<SubscriptionB-Id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB
    

    在步骤 4 中创建虚拟网络(经典)后,Azure 在 Default-Networking 资源组中创建了虚拟网络。When you created the virtual network (classic) in step 4, Azure created the virtual network in the Default-Networking resource group.

  6. 以用户 B 的身份注销 Azure,再以用户 A 的身份在 CLI 中登录。Log UserB out of Azure and log in as UserA in the CLI.

  7. 创建资源组和虚拟网络(资源管理器)。Create a resource group and a virtual network (Resource Manager). 复制以下脚本,将其粘贴到 CLI 会话中,按 EnterCopy the following script, paste it in to your CLI session, and then press Enter.

    #!/bin/bash
    
    # Variables for common values used throughout the script.
    rgName="myResourceGroupA"
    location="chinaeast"
    
    # Create a resource group.
    az group create \
      --name $rgName \
      --location $location
    
    # Create virtual network A (Resource Manager).
    az network vnet create \
      --name myVnetA \
      --resource-group $rgName \
      --location $location \
      --address-prefix 10.0.0.0/16
    
    # Get the id for myVnetA.
    vNetAId=$(az network vnet show \
      --resource-group $rgName \
      --name myVnetA \
      --query id --out tsv)
    
    # Assign UserB permissions to myVnetA.
    az role assignment create \
      --assignee UserB@azure.com \
      --role "Network Contributor" \
      --scope $vNetAId
    
  8. 在通过不同部署模型创建的两个虚拟网络之间创建虚拟网络对等互连。Create a virtual network peering between the two virtual networks created through the different deployment models. 将以下脚本复制到电脑上的文本编辑器。Copy the following script to a text editor on your PC. <SubscriptionB-id> 替换为订阅 ID。如果不知道订阅 ID,请输入 az account show 命令。Replace <SubscriptionB-id> with your subscription Id. If you don't know your subscription Id, enter the az account show command. 输出中的 id 值就是订阅 ID。Azure 创建了虚拟网络(经典),该网络是在步骤 4 中在名为 Default-Networking 的资源组中创建的虚拟网络。The value for id in the output is your subscription Id. Azure created the virtual network (classic) you created in step 4 in a resource group named Default-Networking. 将修改后的脚本粘贴到 CLI 会话中,按 EnterPaste the modified script in your CLI session, and then press Enter.

    # Peer VNet1 to VNet2.
    az network vnet peering create \
      --name myVnetAToMyVnetB \
      --resource-group $rgName \
      --vnet-name myVnetA \
      --remote-vnet-id  /subscriptions/<SubscriptionB-id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB \
      --allow-vnet-access
    
  9. 执行该脚本后,请检查虚拟网络 (Resource Manager) 的对等互连。After the script executes, review the peering for the virtual network (Resource Manager). 复制以下脚本,将其粘贴到 CLI 会话中:Copy the following script, and then paste it in your CLI session:

    az network vnet peering list \
      --resource-group $rgName \
      --vnet-name myVnetA \
      --output table
    

    该输出会在 PeeringState 列中显示“已连接” 。The output shows Connected in the PeeringState column.

    在任一虚拟网络中创建的任何 Azure 资源现在都可通过其 IP 地址相互通信。Any Azure resources you create in either virtual network are now able to communicate with each other through their IP addresses. 如果为虚拟网络使用默认的 Azure 名称解析,则虚拟网络中的资源无法跨虚拟网络解析名称。If you're using default Azure name resolution for the virtual networks, the resources in the virtual networks are not able to resolve names across the virtual networks. 若要跨对等互连中的虚拟网络解析名称,必须创建自己的 DNS 服务器。If you want to resolve names across virtual networks in a peering, you must create your own DNS server. 了解如何使用自己的 DNS 服务器进行名称解析Learn how to set up Name resolution using your own DNS server.

  10. 可选:尽管本教程未介绍如何创建虚拟机,但可以在每个虚拟网络中创建一个虚拟机并将其相互连接,以验证连接性。Optional: Though creating virtual machines is not covered in this tutorial, you can create a virtual machine in each virtual network and connect from one virtual machine to the other, to validate connectivity.

  11. 可选:若要删除在本教程中创建的资源,请完成本文的删除资源中所述步骤。Optional: To delete the resources that you create in this tutorial, complete the steps in Delete resources in this article.

创建对等互连 - PowerShellCreate peering - PowerShell

本教程为每个订阅使用不同的帐户。This tutorial uses different accounts for each subscription. 如果使用的帐户可访问这两个订阅,则可使用相同帐户完成所有步骤,可跳过注销 Azure 的步骤,并删除创建用户角色分配的脚本行。If you're using an account that has permissions to both subscriptions, you can use the same account for all steps, skip the steps for logging out of Azure, and remove the lines of script that create user role assignments. 将以下所有脚本中的 UserA@azure.com 和 UserB@azure.com 替换为 UserA 和 UserB 使用的用户名。Replace UserA@azure.com and UserB@azure.com in all of the following scripts with the usernames you're using for UserA and UserB.

  1. 安装最新版本的 PowerShell AzureAz 模块。Install the latest version of the PowerShell Azure and Az modules. 如果不熟悉 Azure PowerShell,请参阅 Azure PowerShell 概述If you're new to Azure PowerShell, see Azure PowerShell overview.

  2. 启动 PowerShell 会话。Start a PowerShell session.

  3. 在 PowerShell 中输入 Add-AzureAccount -Environment AzureChinaCloud 命令,以 UserB 的身份登录到 UserB 的订阅。In PowerShell, log in to UserB's subscription as UserB by entering the Add-AzureAccount -Environment AzureChinaCloud command. 用于登录的帐户必须拥有创建虚拟网络对等互连的必要权限。The account you log in with must have the necessary permissions to create a virtual network peering. 有关权限列表,请参阅虚拟网络对等互连权限For a list of permissions, see Virtual network peering permissions.

  4. 若要通过 PowerShell 创建虚拟网络(经典),必须新建网络配置文件,或修改现有网络配置文件。To create a virtual network (classic) with PowerShell, you must create a new, or modify an existing, network configuration file. 了解如何导出、更新和导入网络配置文件Learn how to export, update, and import network configuration files. 该文件应包括本教程中使用的虚拟网络的以下 VirtualNetworkSite 元素:The file should include the following VirtualNetworkSite element for the virtual network used in this tutorial:

    <VirtualNetworkSite name="myVnetB" Location="China East">
      <AddressSpace>
        <AddressPrefix>10.1.0.0/16</AddressPrefix>
      </AddressSpace>
      <Subnets>
        <Subnet name="default">
          <AddressPrefix>10.1.0.0/24</AddressPrefix>
        </Subnet>
      </Subnets>
    </VirtualNetworkSite>
    

    警告

    导入更改的网络配置文件会导致订阅中现有虚拟网络(经典)发生变化。Importing a changed network configuration file can cause changes to existing virtual networks (classic) in your subscription. 请确保只添加之前的虚拟网络,且不会从订阅中更改或删除任何现有虚拟网络。Ensure you only add the previous virtual network and that you don't change or remove any existing virtual networks from your subscription.

  5. 输入 Connect-AzAccount -Environment AzureChinaCloud 命令,以 UserB 的身份登录到 UserB 的订阅,以使用资源管理器。Log in to UserB's subscription as UserB to use Resource Manager commands by entering the Connect-AzAccount -Environment AzureChinaCloud command.

  6. 向 UserA 分配访问虚拟网络 B 的权限。将以下脚本复制到电脑上的文本编辑器,并将 <SubscriptionB-id> 替换为订阅 B 的 ID。如果不知道订阅 ID,请输入 Get-AzSubscription 命令查看。Assign UserA permissions to virtual network B. Copy the following script to a text editor on your PC and replace <SubscriptionB-id> with the ID of subscription B. If you don't know the subscription Id, enter the Get-AzSubscription command to view it. 返回的输出中的 ID 值就是订阅 ID 。The value for Id in the returned output is your subscription ID. Azure 创建了虚拟网络(经典),该网络是在步骤 4 中在名为 Default-Networking 的资源组中创建的虚拟网络。Azure created the virtual network (classic) you created in step 4 in a resource group named Default-Networking. 若要执行该脚本,请复制修改后的脚本,将其粘贴到 PowerShell,按 EnterTo execute the script, copy the modified script, paste it in to PowerShell, and then press Enter.

    New-AzRoleAssignment `
      -SignInName UserA@azure.com `
      -RoleDefinitionName "Classic Network Contributor" `
      -Scope /subscriptions/<SubscriptionB-id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB
    
  7. 以 UserB 的身份注销 Azure,然后输入 Connect-AzAccount -Environment AzureChinaCloud 命令,以 UserA 的身份登录到 UserA 的订阅。Log out of Azure as UserB and log in to UserA's subscription as UserA by entering the Connect-AzAccount -Environment AzureChinaCloud command. 用于登录的帐户必须拥有创建虚拟网络对等互连的必要权限。The account you log in with must have the necessary permissions to create a virtual network peering. 有关权限列表,请参阅虚拟网络对等互连权限For a list of permissions, see Virtual network peering permissions.

  8. 复制以下脚本,将其粘贴到 PowerShell,按 Enter 创建虚拟网络(资源管理器):Create the virtual network (Resource Manager) by copying the following script, pasting it in to PowerShell, and then pressing Enter:

    # Variables for common values
      $rgName='MyResourceGroupA'
      $location='chinaeast'
    
    # Create a resource group.
    New-AzResourceGroup `
      -Name $rgName `
      -Location $location
    
    # Create virtual network A.
    $vnetA = New-AzVirtualNetwork `
      -ResourceGroupName $rgName `
      -Name 'myVnetA' `
      -AddressPrefix '10.0.0.0/16' `
      -Location $location
    
  9. 向 UserB 分配访问 myVnetA 的权限。Assign UserB permissions to myVnetA. 将以下脚本复制到电脑上的文本编辑器,并将 <SubscriptionA-Id> 替换为订阅 A 的 ID。如果不知道订阅 ID,请输入 Get-AzSubscription 命令查看。Copy the following script to a text editor on your PC and replace <SubscriptionA-Id> with the ID of subscription A. If you don't know the subscription Id, enter the Get-AzSubscription command to view it. 返回的输出中的 Id 值就是订阅 ID。The value for Id in the returned output is your subscription ID. 将修改后的脚本粘贴到 PowerShell,按 Enter 执行该脚本。Paste the modified version of the script in PowerShell, and then press Enter to execute it.

    New-AzRoleAssignment `
      -SignInName UserB@azure.com `
      -RoleDefinitionName "Network Contributor" `
      -Scope /subscriptions/<SubscriptionA-Id>/resourceGroups/myResourceGroupA/providers/Microsoft.Network/VirtualNetworks/myVnetA
    
  10. 将以下脚本复制到电脑上的文本编辑器,并将 <SubscriptionB-id> 替换为订阅 B 的 ID。将myVnetA 和 myVNetB 进行对等,复制修改后的脚本,将其粘贴到 PowerShell,按 EnterCopy the following script to a text editor on your PC, and replace <SubscriptionB-id> with the ID of subscription B. To peer myVnetA to myVNetB, copy the modified script, paste it in to PowerShell, and then press Enter.

    Add-AzVirtualNetworkPeering `
      -Name 'myVnetAToMyVnetB' `
      -VirtualNetwork $vnetA `
      -RemoteVirtualNetworkId /subscriptions/<SubscriptionB-id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnetB
    
  11. 复制以下脚本,将其粘贴到 PowerShell,按 Enter 查看 myVnetA 的对等互连状态。View the peering state of myVnetA by copying the following script, pasting it into PowerShell, and pressing Enter.

    Get-AzVirtualNetworkPeering `
      -ResourceGroupName $rgName `
      -VirtualNetworkName myVnetA `
      | Format-Table VirtualNetworkName, PeeringState
    

    状态为“已连接” 。The state is Connected. 设置从 myVnetB 到 myVnetA 的对等互连后,状态即会变为“已连接” 。It changes to Connected once you set up the peering to myVnetA from myVnetB.

    在任一虚拟网络中创建的任何 Azure 资源现在都可通过其 IP 地址相互通信。Any Azure resources you create in either virtual network are now able to communicate with each other through their IP addresses. 如果为虚拟网络使用默认的 Azure 名称解析,则虚拟网络中的资源无法跨虚拟网络解析名称。If you're using default Azure name resolution for the virtual networks, the resources in the virtual networks are not able to resolve names across the virtual networks. 若要跨对等互连中的虚拟网络解析名称,必须创建自己的 DNS 服务器。If you want to resolve names across virtual networks in a peering, you must create your own DNS server. 了解如何使用自己的 DNS 服务器进行名称解析Learn how to set up Name resolution using your own DNS server.

  12. 可选:尽管本教程未介绍如何创建虚拟机,但可以在每个虚拟网络中创建一个虚拟机并将其相互连接,以验证连接性。Optional: Though creating virtual machines is not covered in this tutorial, you can create a virtual machine in each virtual network and connect from one virtual machine to the other, to validate connectivity.

  13. 可选:若要删除在本教程中创建的资源,请完成本文的删除资源中所述步骤。Optional: To delete the resources that you create in this tutorial, complete the steps in Delete resources in this article.

删除资源Delete resources

完成本教程后,可能需要删除本教程中创建的资源,以免产生使用费。When you've finished this tutorial, you might want to delete the resources you created in the tutorial, so you don't incur usage charges. 删除资源组会删除其中包含的所有资源。Deleting a resource group also deletes all resources that are in the resource group.

Azure 门户Azure portal

  1. 在门户的搜索框中,输入 myResourceGroupAIn the portal search box, enter myResourceGroupA. 在搜索结果中,单击“myResourceGroupA” 。In the search results, click myResourceGroupA.
  2. 在“myResourceGroupA”边栏选项卡中,单击“删除”图标 。On the myResourceGroupA blade, click the Delete icon.
  3. 若要确认删除,请在“键入资源组名称”框中输入 myResourceGroupA,然后单击“删除” 。To confirm the deletion, in the TYPE THE RESOURCE GROUP NAME box, enter myResourceGroupA, and then click Delete.
  4. 在门户顶部的“搜索资源”框中键入 myVnetBIn the Search resources box at the top of the portal, type myVnetB. 单击出现在搜索结果中的“myVnetB” 。Click myVnetB when it appears in the search results. 随即显示“myVnetB”虚拟网络的边栏选项卡 。A blade appears for the myVnetB virtual network.
  5. 在“myVnetB”边栏选项卡中,单击“删除” 。In the myVnetB blade, click Delete.
  6. 若要确认删除,请单击“删除虚拟网络”框中的“是” 。To confirm the deletion, click Yes in the Delete virtual network box.

Azure CLIAzure CLI

  1. 使用 CLI 登录 Azure,通过以下命令删除虚拟网络(资源管理器):Log in to Azure using the CLI to delete the virtual network (Resource Manager) with the following command:

    az group delete --name myResourceGroupA --yes
    
  2. 使用经典 CLI 登录 Azure,通过以下命令删除虚拟网络(经典):Sign in to Azure using the classic CLI to delete the virtual network (classic) with the following commands:

    azure config mode asm 
    
    azure network vnet delete --vnet myVnetB --quiet
    

PowerShellPowerShell

  1. 在 PowerShell 命令提示符处,输入以下命令,删除虚拟网络(资源管理器):At the PowerShell command prompt, enter the following command to delete the virtual network (Resource Manager):

    Remove-AzResourceGroup -Name myResourceGroupA -Force
    
  2. 若要通过 PowerShell 删除虚拟网络(经典),必须修改现有网络配置文件。To delete the virtual network (classic) with PowerShell, you must modify an existing network configuration file. 了解如何导出、更新和导入网络配置文件Learn how to export, update, and import network configuration files. 删除本教程中使用的虚拟网络的以下 VirtualNetworkSite 元素:Remove the following VirtualNetworkSite element for the virtual network used in this tutorial:

    <VirtualNetworkSite name="myVnetB" Location="China East">
      <AddressSpace>
        <AddressPrefix>10.1.0.0/16</AddressPrefix>
      </AddressSpace>
      <Subnets>
        <Subnet name="default">
          <AddressPrefix>10.1.0.0/24</AddressPrefix>
        </Subnet>
      </Subnets>
    </VirtualNetworkSite>
    

    警告

    导入更改的网络配置文件会导致订阅中现有虚拟网络(经典)发生变化。Importing a changed network configuration file can cause changes to existing virtual networks (classic) in your subscription. 请确保只删除之前的虚拟网络,且不会从订阅中更改或删除任何其他现有虚拟网络。Ensure you only remove the previous virtual network and that you don't change or remove any other existing virtual networks from your subscription.

后续步骤Next steps