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

本文介绍如何在通过不同部署模型创建的虚拟网络间创建虚拟网络对等互连。In this tutorial, you learn to create a virtual network peering between virtual networks created through different deployment models. 这两个虚拟网络位于同一订阅。Both virtual networks exist in the same subscription. 在两个虚拟网络之间建立对等互连可让不同虚拟网络中的资源以相同的带宽和延迟彼此通信,就像这些资源位于同一个虚拟网络中一样。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
均为资源管理器模型Both Resource Manager 不同Different
一个为资源管理器模型,一个为经典模型One Resource Manager, one classic 不同Different

不能在通过经典部署模型部署的两个虚拟网络之间创建虚拟网络对等互连。A virtual network peering can't be created between two virtual networks deployed through the classic deployment model. 如需连接两个通过经典部署模型创建的虚拟网络,可使用 Azure VPN 网关来连接它们。If you need to connect virtual networks that were both created through the classic deployment model, you can use an Azure VPN Gateway to connect the virtual networks.

本教程将在同一区域中的虚拟网络之间建立对等互连。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 门户、Azure 命令行接口 (CLI)、Azure PowerShell 或 Azure 资源管理器模板创建虚拟网络对等互连。You can use the Azure portal, the Azure command-line interface (CLI), Azure PowerShell, or an Azure Resource Manager template 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

  1. 登录到 Azure 门户Sign in to the Azure portal. 用于登录的帐户必须具有创建虚拟网络对等互连的必要权限。The account you sign 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:

    • 名称:myVnet1 Name: myVnet1
    • 地址空间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
    • 订阅:选择订阅Subscription: Select your subscription
    • 资源组:选择“新建”,并输入 myResourceGroup Resource group: Select Create new and enter myResourceGroup
    • 位置中国东部Location: China East
  4. 单击“+ 新建” 。Click + New. 在“在市场中搜索”框中,键入“虚拟网络” 。In the Search the Marketplace box, type Virtual network. 单击搜索结果中出现的“虚拟网络” 。Click Virtual network when it appears in the search results.

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

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

    • 名称:myVnet2 Name: myVnet2
    • 地址空间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
    • 订阅:选择订阅Subscription: Select your subscription
    • 资源组:选择“使用现有”,再选择“myResourceGroup” Resource group: Select Use existing and select myResourceGroup
    • 位置中国东部Location: China East
  7. 在门户顶部的“搜索资源”框中键入 myResourceGroup。 In the Search resources box at the top of the portal, type myResourceGroup. 当“myResourceGroup”出现在搜索结果中时,请单击它。 Click myResourceGroup when it appears in the search results. 随后将显示 myresourcegroup 资源组的边栏选项卡。A blade appears for the myresourcegroup resource group. 该资源组保存前面步骤中创建的两个虚拟网络。The resource group holds the two virtual networks created in previous steps.

  8. 单击“myVNet1”。 Click myVNet1.

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

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

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

    • 名称:myVnet1ToMyVnet2 Name: myVnet1ToMyVnet2
    • 虚拟网络部署模型:选择“经典”。 Virtual network deployment model: Select Classic.
    • 订阅:选择订阅Subscription: Select your subscription
    • 虚拟网络:单击“选择虚拟网络”,然后单击“myVnet2”。 Virtual network: Click Choose a virtual network, then click myVnet2.
    • 允许虚拟网络访问: 确保选中“已启用”。 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.
  12. 在上一步骤中单击“确定”后,“添加对等互连”边栏选项卡将会关闭,并再次出现“myVnet1 - 对等互连”边栏选项卡。 After clicking OK in the previous step, the Add peering blade closes and you see the myVnet1 - Peerings blade again. 几秒钟后,创建的对等互连将显示在该边栏选项卡中。After a few seconds, the peering you created appears in the blade. 创建的 myVnet1ToMyVnet2 对等互连的“对等互连状态”列中列出了“已连接” 。Connected is listed in the PEERING STATUS column for the myVnet1ToMyVnet2 peering you created.

    现已建立对等互连。The peering is now established. 在任一虚拟网络中创建的任何 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 aren't 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.

  13. 可选:尽管本教程未介绍如何创建虚拟机,但可以在每个虚拟网络中创建一个虚拟机并将其相互连接,以验证连接性。Optional: Though creating virtual machines isn't 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.

  14. 可选:若要删除在本教程中创建的资源,请完成本文的删除资源部分中所述的步骤。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

使用 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. 打开 CLI 命令并使用 azure login -e AzureChinaCloud 命令登录 Azure。Open a CLI command and sign in to Azure using the azure login -e AzureChinaCloud command.

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

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

    azure network vnet create --vnet myVnet2 --address-space 10.1.0.0 --cidr 16 --location "China East"
    
  4. 使用 CLI 而非经典 CLI 执行以下 bash CLI 脚本。Execute the following bash CLI script using the CLI, not the classic CLI. 有关在 Windows 计算机上运行 bash CLI 脚本的选项,请参阅在 Windows 上安装 Azure CLIFor options on running bash CLI scripts on Windows computer, see Install the Azure CLI on Windows.

    #!/bin/bash
    
    # Sign in the Azure China Cloud
    az cloud set -n AzureChinaCloud
    az login 
    
    # Create a resource group.
    az group create \
      --name myResourceGroup \
      --location chinaeast
    
    # Create the virtual network (Resource Manager).
    az network vnet create \
      --name myVnet1 \
      --resource-group myResourceGroup \
      --location chinaeast \
      --address-prefix 10.0.0.0/16
    
  5. 使用 CLI 在通过不同部署模型创建的两个虚拟网络之间创建虚拟网络对等互连。Create a virtual network peering between the two virtual networks created through the different deployment models using the CLI. 将以下脚本复制到电脑上的文本编辑器。Copy the following script to a text editor on your PC. <subscription id> 替换为订阅 ID。如果不知道订阅 ID,请输入 az account show 命令。Replace <subscription id> with your subscription Id. 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. Paste the modified script in to your CLI session, and then press Enter.

    # Get the ID for VNet1.
    vnet1Id=$(az network vnet show \
      --resource-group myResourceGroup \
      --name myVnet1 \
      --query id --out tsv)
    
    # Peer VNet1 to VNet2.
    az network vnet peering create \
      --name myVnet1ToMyVnet2 \
      --resource-group myResourceGroup \
      --vnet-name myVnet1 \
      --remote-vnet-id /subscriptions/<subscription id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnet2 \
      --allow-vnet-access
    
  6. 执行该脚本后,请检查虚拟网络(资源管理器)的对等互连。After the script executes, review the peering for the virtual network (Resource Manager). 复制以下命令,将其粘贴到 CLI 会话,按 EnterCopy the following command, paste it in your CLI session, and then press Enter:

    az network vnet peering list \
      --resource-group myResourceGroup \
      --vnet-name myVnet1 \
      --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 aren't 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.

  7. 可选:尽管本教程未介绍如何创建虚拟机,但可以在每个虚拟网络中创建一个虚拟机并将其相互连接,以验证连接性。Optional: Though creating virtual machines isn't 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.

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

创建对等互连 - PowerShellCreate peering - PowerShell

  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 命令登录 Azure。In PowerShell, sign in to Azure by entering the Add-AzureAccount -Environment AzureChinaCloud command. 用于登录的帐户必须具有创建虚拟网络对等互连的必要权限。The account you sign 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="myVnet2" 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>
    

    Warning

    导入更改的网络配置文件会导致订阅中现有虚拟网络(经典)发生变化。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 命令,登录 Azure,创建虚拟网络(资源管理器)。Sign in to Azure to create the virtual network (Resource Manager) by entering the Connect-AzAccount -Environment AzureChinaCloud command. 用于登录的帐户必须具有创建虚拟网络对等互连的必要权限。The account you sign in with must have the necessary permissions to create a virtual network peering. 有关权限列表,请参阅虚拟网络对等互连权限For a list of permissions, see Virtual network peering permissions.

  6. 创建资源组和虚拟网络(资源管理器)。Create a resource group and a virtual network (Resource Manager). 复制该脚本,将其粘贴到 PowerShell,按 EnterCopy the script, paste it into PowerShell, and then press Enter.

    # Create a resource group.
      New-AzResourceGroup -Name myResourceGroup -Location chinaeast
    
    # Create the virtual network (Resource Manager).
      $vnet1 = New-AzVirtualNetwork `
      -ResourceGroupName myResourceGroup `
      -Name 'myVnet1' `
      -AddressPrefix '10.0.0.0/16' `
      -Location chinaeast
    
  7. 在通过不同部署模型创建的两个虚拟网络之间创建虚拟网络对等互连。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. <subscription id> 替换为订阅 ID。Replace <subscription id> with your subscription ID. 如果不知道订阅 ID,请输入 Get-AzSubscription 命令查看。If you don't know your 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 会话中右键单击,按 EnterTo execute the script, copy the modified script from your text editor, then right-click in your PowerShell session, and then press Enter.

    # Peer VNet1 to VNet2.
    Add-AzVirtualNetworkPeering `
      -Name myVnet1ToMyVnet2 `
      -VirtualNetwork $vnet1 `
      -RemoteVirtualNetworkId /subscriptions/<subscription Id>/resourceGroups/Default-Networking/providers/Microsoft.ClassicNetwork/virtualNetworks/myVnet2
    
  8. 执行该脚本后,请检查虚拟网络(资源管理器)的对等互连。After the script executes, review the peering for the virtual network (Resource Manager). 复制以下命令,将其粘贴到 PowerShell 会话,按 EnterCopy the following command, paste it in your PowerShell session, and then press Enter:

    Get-AzVirtualNetworkPeering `
      -ResourceGroupName myResourceGroup `
      -VirtualNetworkName myVnet1 `
      | Format-Table VirtualNetworkName, PeeringState
    

    该输出会在 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 aren't 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.

  9. 可选:尽管本教程未介绍如何创建虚拟机,但可以在每个虚拟网络中创建一个虚拟机并将其相互连接,以验证连接性。Optional: Though creating virtual machines isn't 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.

  10. 可选:若要删除在本教程中创建的资源,请完成本文的删除资源中所述步骤。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. 在门户的搜索框中,输入 myResourceGroup 。In the portal search box, enter myResourceGroup. 在搜索结果中,单击“myResourceGroup”。 In the search results, click myResourceGroup.
  2. 在“myResourceGroup”边栏选项卡中,单击“删除”图标。 On the myResourceGroup blade, click the Delete icon.
  3. 若要确认删除,请在“键入资源组名称”框中输入 myResourceGroup ,然后单击“删除”。 To confirm the deletion, in the TYPE THE RESOURCE GROUP NAME box, enter myResourceGroup, and then click Delete.

Azure CLIAzure CLI

  1. 通过 Azure CLI,借助以下命令删除虚拟网络(资源管理器):Use the Azure CLI to delete the virtual network (Resource Manager) with the following command:

    az group delete --name myResourceGroup --yes
    
  2. 通过经典 CLI,借助以下命令删除虚拟网络(经典):Use the classic CLI to delete the virtual network (classic) with the following commands:

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

PowerShellPowerShell

  1. 输入以下命令,删除虚拟网络(资源管理器):Enter the following command to delete the virtual network (Resource Manager):

    Remove-AzResourceGroup -Name myResourceGroup -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="myVnet2" 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>
    

    Warning

    导入更改的网络配置文件会导致订阅中现有虚拟网络(经典)发生变化。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