教程:使用 PowerShell 配置 ExpressRoute 的虚拟网络网关Tutorial: Configure a virtual network gateway for ExpressRoute using PowerShell

本教程指导你为预先存在的 VNet 添加虚拟网络 (VNet) 网关、重设网关大小以及删除网关。This tutorial helps you add, resize, and remove a virtual network (VNet) gateway for a pre-existing VNet. 此配置的步骤适用于使用资源管理器部署模型创建的 VNet(针对 ExpressRoute 配置)。The steps for this configuration apply to VNets that were created using the Resource Manager deployment model for an ExpressRoute configuration. 有关详细信息,请参阅关于 ExpressRoute 的虚拟网络网关For more information, see About virtual network gateways for ExpressRoute.

在本教程中,你将了解如何执行以下操作:In this tutorial, you learn how to:

  • 创建网关子网。Create a gateway subnet.
  • 创建虚拟网络网关。Create Virtual Network gateway.

先决条件Prerequisites

配置参考列表Configuration reference list

此任务的步骤使用的 VNet 基于以下配置参考列表中的值。The steps for this task use a VNet based on the values in the following configuration reference list. 此列表中也概述了其他设置和名称。Additional settings and names are also outlined in this list. 尽管我们确实基于此列表中的值添加变量,但是我们在任何步骤中不会直接使用此列表。We don't use this list directly in any of the steps, although we do add variables based on the values in this list. 可以复制列表作为参考,并将列表中的值替换为自己的值。You can copy the list to use as a reference, replacing the values with your own.

设置Setting Value
虚拟网络名称Virtual Network Name TestVNetTestVNet
虚拟网络地址空间Virtual Network address space 192.168.0.0/16192.168.0.0/16
资源组Resource Group TestRGTestRG
Subnet1 名称Subnet1 Name FrontEndFrontEnd
Subnet1 地址空间Subnet1 address space 192.168.1.0/24192.168.1.0/24
Subnet1 名称Subnet1 Name FrontEndFrontEnd
网关子网名称Gateway Subnet name GatewaySubnetGatewaySubnet
网关子网地址空间Gateway Subnet address space 192.168.200.0/26192.168.200.0/26
区域Region 中国东部China East
网关名称Gateway Name GWGW
网关 IP 名称Gateway IP Name GWIPGWIP
网关 IP 配置名称Gateway IP configuration Name gwipconfgwipconf
类型Type ExpressRouteExpressRoute
网关公共 IP 名称Gateway Public IP Name gwpipgwpip

添加网关Add a gateway

  1. 若要连接到 Azure,请运行 Connect-AzAccountTo connect with Azure, run Connect-AzAccount.

  2. 声明此练习的变量。Declare your variables for this exercise. 请务必编辑此示例,使之反映想要使用的设置。Be sure to edit the sample to reflect the settings that you want to use.

    $RG = "TestRG"
    $Location = "China East"
    $GWName = "GW"
    $GWIPName = "GWIP"
    $GWIPconfName = "gwipconf"
    $VNetName = "TestVNet"
    
  3. 将虚拟网络对象存储为变量。Store the virtual network object as a variable.

    $vnet = Get-AzVirtualNetwork -Name $VNetName -ResourceGroupName $RG
    
  4. 将网关子网添加到虚拟网络中。Add a gateway subnet to your Virtual Network. 网关子网必须命名为“GatewaySubnet”。The gateway subnet must be named "GatewaySubnet". 网关子网必须是 /27 或更大(/26、/25 等)。The gateway subnet has to be /27 or larger (/26, /25, and so on).

    Add-AzVirtualNetworkSubnetConfig -Name GatewaySubnet -VirtualNetwork $vnet -AddressPrefix 192.168.200.0/26
    
  5. 设置配置。Set the configuration.

    $vnet = Set-AzVirtualNetwork -VirtualNetwork $vnet
    
  6. 将网关子网存储为变量。Store the gateway subnet as a variable.

    $subnet = Get-AzVirtualNetworkSubnetConfig -Name 'GatewaySubnet' -VirtualNetwork $vnet
    
  7. 请求公共 IP 地址。Request a public IP address. 创建网关之前请求 IP 地址。The IP address is requested before creating the gateway. 无法指定要使用的 IP 地址;将自动分配该地址。You can't specify the IP address that you want to use; it�s dynamically assigned. 后面的配置部分将使用此 IP 地址。You'll use this IP address in the next configuration section. AllocationMethod 必须是动态的。The AllocationMethod must be Dynamic.

    $pip = New-AzPublicIpAddress -Name $GWIPName  -ResourceGroupName $RG -Location $Location -AllocationMethod Dynamic
    
  8. 创建网关配置。Create the configuration for your gateway. 网关配置定义要使用的子网和公共 IP 地址。The gateway configuration defines the subnet and the public IP address to use. 在此步骤中,将指定创建网关时使用的配置。In this step, you're specifying the configuration that will be used when you create the gateway. 使用以下示例创建网关配置。Use the following sample to create your gateway configuration.

    $ipconf = New-AzVirtualNetworkGatewayIpConfig -Name $GWIPconfName -Subnet $subnet -PublicIpAddress $pip
    
  9. 创建网关。Create the gateway. 在此步骤中,-GatewayType 尤其重要。In this step, the -GatewayType is especially important. 必须使用值 ExpressRouteYou must use the value ExpressRoute. 运行这些 cmdlet 后,可能需要 45 分钟或更长时间才能创建好网关。After running these cmdlets, the gateway can take 45 minutes or more to create.

    New-AzVirtualNetworkGateway -Name $GWName -ResourceGroupName $RG -Location $Location -IpConfigurations $ipconf -GatewayType Expressroute -GatewaySku Standard
    

验证是否已创建网关Verify the gateway was created

使用以下命令来验证是否已创建网关:Use the following commands to verify that the gateway has been created:

Get-AzVirtualNetworkGateway -ResourceGroupName $RG

重设网关大小Resize a gateway

有许多网关 SKUThere are a number of Gateway SKUs. 可以使用以下命令随时更改网关 SKU。You can use the following command to change the Gateway SKU at any time.

重要

此命令对 UltraPerformance 网关不起作用。This command doesn't work for UltraPerformance gateway. 要将网关更改为 UltraPerformance 网关,首先要删除现有的 ExpressRoute 网关,然后创建新的 UltraPerformance 网关。To change your gateway to an UltraPerformance gateway, first remove the existing ExpressRoute gateway, and then create a new UltraPerformance gateway. 要将网关从 UltraPerformance 网关降级,首先要删除 UltraPerformance 网关,然后创建新网关。To downgrade your gateway from an UltraPerformance gateway, first remove the UltraPerformance gateway, and then create a new gateway.

$gw = Get-AzVirtualNetworkGateway -Name $GWName -ResourceGroupName $RG
Resize-AzVirtualNetworkGateway -VirtualNetworkGateway $gw -GatewaySku HighPerformance

清理资源Clean up resources

使用以下命令删除网关:Use the following command to remove the gateway:

Remove-AzVirtualNetworkGateway -Name $GWName -ResourceGroupName $RG

后续步骤Next steps

创建 VNet 网关后,可将 VNet 链接到 ExpressRoute 线路。After you've created the VNet gateway, you can link your VNet to an ExpressRoute circuit.