Share via


使用 CLI 建立路由式 VPN 閘道

本文可協助您使用 Azure CLI 快速建立路由式 Azure VPN 閘道。 建立內部部署網路的 VPN 連線時,會使用 VPN 閘道。 您也可以使用 VPN 閘道來連線 VNet。

在本文中,您將建立 VNet、子網、閘道子網和路由式 VPN 閘道(虛擬網路閘道)。 建立閘道通常可能需要 45 分鐘或更久,視選取的閘道 SKU 而定。 網關建立完成後,您就可以建立連線。 這些步驟需要 Azure 訂用帳戶。

VPN 閘道只是連線架構的一部分,可協助您安全地存取虛擬網路內的資源。

Diagram that shows a virtual network and a VPN gateway.

  • 圖表左側會顯示使用本文步驟所建立的虛擬網路和 VPN 閘道。
  • 您稍後可以新增不同類型的連線,如下圖所示。 例如,您可以建立 站對站點對站 連線。 若要檢視您可以建置的不同設計架構,請參閱 VPN 閘道設計

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

  • 本文需要 2.0.4 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

建立資源群組

使用 az group create 命令建立資源群組。 資源群組是在其中部署與管理 Azure 資源的邏輯容器。

az group create --name TestRG1 --location eastus

建立虛擬網路

使用 az network vnet create 命令建立虛擬網路。 下列範例會在 EastUS 位置中建立名為 VNet1虛擬網路:

az network vnet create \
  -n VNet1 \
  -g TestRG1 \
  -l eastus \
  --address-prefix 10.1.0.0/16 \
  --subnet-name Frontend \
  --subnet-prefix 10.1.0.0/24

新增閘道子網路

閘道子網路包含虛擬網路閘道服務所使用的保留 IP 位址。 使用下列範例來新增閘道子網:

az network vnet subnet create \
  --vnet-name VNet1 \
  -n GatewaySubnet \
  -g TestRG1 \
  --address-prefix 10.1.255.0/27 

要求公用IP位址

VPN 閘道必須具有公用 IP 位址。 公用IP位址會設定給您為虛擬網路建立的 VPN 閘道。 使用下列範例,使用 az network public-ip create 命令來要求公用 IP 位址:

az network public-ip create \
  -n VNet1GWIP \
  -g TestRG1 \

建立 VPN 閘道

使用 az network vnet-gateway create 命令建立 VPN 閘道。

如果您使用 --no-wait 參數來執行此命令,就不會看到任何意見反應或輸出。 --no-wait 參數允許在背景中建立閘道。 這並不表示 VPN 閘道會立即建立。

az network vnet-gateway create \
  -n VNet1GW \
  -l eastus \
  --public-ip-address VNet1GWIP \
  -g TestRG1 \
  --vnet VNet1 \
  --gateway-type Vpn \
  --sku VpnGw2 \
  --vpn-gateway-generation Generation2 \
  --no-wait

VPN 閘道可能需要 45 分鐘以上的時間才能建立。

檢視 VPN 閘道

az network vnet-gateway show \
  -n VNet1GW \
  -g TestRG1

回應看起來會像這樣:

{
  "activeActive": false,
  "bgpSettings": {
    "asn": 65515,
    "bgpPeeringAddress": "10.1.255.30",
    "bgpPeeringAddresses": [
      {
        "customBgpIpAddresses": [],
        "defaultBgpIpAddresses": [
          "10.1.255.30"
        ],
        "ipconfigurationId": "/subscriptions/<subscription ID>/resourceGroups/TestRG1/providers/Microsoft.Network/virtualNetworkGateways/VNet1GW/ipConfigurations/vnetGatewayConfig0",
        "tunnelIpAddresses": [
          "20.228.164.35"
        ]
      }
    ],
    "peerWeight": 0
  },
  "disableIPSecReplayProtection": false,
  "enableBgp": false,
  "enableBgpRouteTranslationForNat": false,
  "enablePrivateIpAddress": false,
  "etag": "W/\"6c61f8cb-d90f-4796-8697\"",
  "gatewayType": "Vpn",
  "id": "/subscriptions/<subscription ID>/resourceGroups/TestRG1/providers/Microsoft.Network/virtualNetworkGateways/VNet1GW",
  "ipConfigurations": [
    {
      "etag": "W/\"6c61f8cb-d90f-4796-8697\"",
      "id": "/subscriptions/<subscription ID>/resourceGroups/TestRG1/providers/Microsoft.Network/virtualNetworkGateways/VNet1GW/ipConfigurations/vnetGatewayConfig0",
      "name": "vnetGatewayConfig0",
      "privateIPAllocationMethod": "Dynamic",
      "provisioningState": "Succeeded",
      "publicIPAddress": {
        "id": "/subscriptions/<subscription ID>/resourceGroups/TestRG1/providers/Microsoft.Network/publicIPAddresses/VNet1GWIP",
        "resourceGroup": "TestRG1"
      },
      "resourceGroup": "TestRG1",
      "subnet": {
        "id": "/subscriptions/<subscription ID>/resourceGroups/TestRG1/providers/Microsoft.Network/virtualNetworks/VNet1/subnets/GatewaySubnet",
        "resourceGroup": "TestRG1"
      }
    }
  ],
  "location": "eastus",
  "name": "VNet1GW",
  "natRules": [],
  "provisioningState": "Succeeded",
  "resourceGroup": "TestRG1",
  "resourceGuid": "69c269e3-622c-4123-9231",
  "sku": {
    "capacity": 2,
    "name": "VpnGw2",
    "tier": "VpnGw2"
  },
  "type": "Microsoft.Network/virtualNetworkGateways",
  "vpnGatewayGeneration": "Generation2",
  "vpnType": "RouteBased"
}

檢視公用IP位址

若要檢視指派給您閘道的公用IP位址,請使用下列範例:

az network public-ip show \
  --name VNet1GWIP \
  --resource-group TestRG1

ipAddress 字段相關聯的值是 VPN 閘道的公用IP位址。

範例回應:

{
  "dnsSettings": null,
  "etag": "W/\"69c269e3-622c-4123-9231\"",
  "id": "/subscriptions/<subscription ID>/resourceGroups/TestRG1/providers/Microsoft.Network/publicIPAddresses/VNet1GWIP",
  "idleTimeoutInMinutes": 4,
  "ipAddress": "13.90.195.184",
  "ipConfiguration": {
    "etag": null,
    "id": "/subscriptions/<subscription ID>/resourceGroups/TestRG1/providers/Microsoft.Network/virtualNetworkGateways/VNet1GW/ipConfigurations/vnetGatewayConfig0",

清除資源

當您不再需要您所建立的資源時,請使用 az group delete 來刪除資源群組。 這會刪除資源群組及其包含的所有資源。

az group delete --name TestRG1 --yes

下一步

網關完成建立之後,您就可以建立虛擬網路與另一個 VNet 之間的連線。 或者,建立虛擬網路與內部部署位置之間的連線。