实验:NCC VPC 作为 Spoke

1. 简介

概览

在本实验中,用户将探索如何通过对 VPC Spoke 的支持,使用 Network Connectivity Center(NCC) 大规模建立 VPC 间连接。当用户将 VPC 定义为 VPC spoke 时,他们就可以通过 NCC 中心将该 VPC 同时连接到多个 VPC 网络。将 NCC 与 VPC spoke 配置搭配使用,可降低通过 VPC 对等互连管理 VPC 成对间连接的操作复杂性,而不再使用集中式连接管理模型。

回想一下,Network Connectivity Center (NCC) 是一个中心辐射型控制平面模型,用于在 Google Cloud 中进行网络连接管理。Hub 资源提供集中式连接管理模型来互连 spoke。

构建内容

在此 Codelab 中,您将使用 NCC 中心 (hub) 构建一个逻辑中心辐射拓扑,从而跨三个不同的 VPC 实现全网状 VPC 连接结构。

学习内容

  • 使用 NCC 的全网状 VPC 连接
  • 跨 VPC 的专用 NAT

所需条件

  • 了解 GCP VPC 网络
  • 了解 Cloud Router 和 BGP 路由
  • 两个单独的 GCP 项目
  • 此 Codelab 需要 5 个 VPC。其中一个 VPC 必须位于与 NCC hub 不同的项目中。
  • 检查您的配额:网络并根据需要请求添加额外网络,屏幕截图如下:

6d1b99c6da87fd84

目标

  • 设置 GCP 环境
  • 将 Network Connectivity Center 与 VPC 配置为 spoke
  • 验证数据路径
  • 探索 NCC 可服务性功能
  • 清理已使用的资源

准备工作

Google Cloud 控制台和 Cloud Shell

为了与 GCP 交互,我们将在整个实验中同时使用 Google Cloud 控制台和 Cloud Shell。

NCC Hub 项目 Google Cloud 控制台

您可以访问 https://console.cloud.google.com 访问 Cloud 控制台。

在 Google Cloud 中设置以下各项,以便更轻松地配置 Network Connectivity Center:

在 Google Cloud Console 的“项目选择器”页面上,选择或创建一个 Google Cloud 项目。

启动 Cloud Shell。此 Codelab 利用 $variables 辅助在 Cloud Shell 中实现 gcloud 配置。

gcloud auth list
gcloud config list project
gcloud config set project [HUB-PROJECT-NAME]
projectname=[HUB-PROJECT-NAME]
echo $projectname
gcloud config set compute/zone us-central1-a
gcloud config set compute/region us-central1

IAM 角色

NCC 需要 IAM 角色才能访问特定 API。请务必根据需要为您的用户配置 NCC IAM 角色。

角色/说明

权限

networkconnectivity.networkAdmin - 允许网络管理员管理 hub 和 spoke。

networkconnectivity.hubs.networkconnectivity.spokes

networkconnectivity.networkSpokeManager - 允许在 hub 中添加和管理 spoke。将在宿主项目拥有 Hub 的共享 VPC 中使用,但其他项目中的其他管理员可以为其连接向 Hub 添加 spoke。

networkconnectivity.spokes。**

networkconnectivity.networkUsernetworkconnectivity.networkViewer - 允许网络用户查看 hub 和 spoke 的不同属性。

networkconnectivity.hubs.getnetworkconnectivity.hubs.listnetworkconnectivity.spokes.getnetworkconnectivity.spokes.listnetworkconnectivity.spokes.aggregatedList

2. 设置网络环境

概览

在本部分中,我们将在单个项目中部署 VPC 网络和防火墙规则。此逻辑图说明了将在此步骤中设置的网络环境。

为了演示跨项目 spoke 支持,在后面的步骤中,我们将在其他项目中部署 VPC 和防火墙规则。

245f1002db33ca98

创建 VPC 和子网

VPC 网络包含您将安装 GCE 虚拟机来验证数据路径的子网

gcloud compute networks create vpc1-ncc --subnet-mode custom
gcloud compute networks create vpc2-ncc --subnet-mode custom
gcloud compute networks create vpc3-ncc --subnet-mode custom
gcloud compute networks create vpc4-ncc --subnet-mode custom

gcloud compute networks subnets create vpc1-ncc-subnet1 \
--network vpc1-ncc --range 10.1.1.0/24 --region us-central1

gcloud compute networks subnets create vpc1-ncc-subnet2 \
--network vpc1-ncc --range 10.1.2.0/25 --region us-central1

gcloud compute networks subnets create vpc1-ncc-subnet3 \
--network vpc1-ncc --range 10.1.2.128/25 --region us-central1

gcloud compute networks subnets create vpc2-ncc-subnet1 \
--network vpc2-ncc --range 10.2.2.0/24 --region us-central1

VPC 支持的子网范围

NCC 支持所有有效的 IPv4 子网范围(以非公开方式使用的公共 IP 地址除外)。在此步骤中,请在 VPC4 中创建将导入中心路由表的有效 IP 范围。

gcloud compute networks subnets create benchmark-testing-rfc2544 \
--network vpc4-ncc --range 198.18.0.0/15 --region us-east1

gcloud compute networks subnets create class-e-rfc5735 \
--network vpc4-ncc --range 240.0.0.0/4 --region us-east1

gcloud compute networks subnets create ietf-protcol-assignment-rfc6890 \
--network vpc4-ncc --range 192.0.0.0/24 --region us-east1

gcloud compute networks subnets create ipv6-4-relay-rfc7526 \
--network vpc4-ncc --range 192.88.99.0/24 --region us-east1

gcloud compute networks subnets create pupi \
--network vpc4-ncc --range 50.50.50.0/24 --region us-east1

gcloud compute networks subnets create test-net-1-rfc5737 \
--network vpc4-ncc --range 192.0.2.0/24 --region us-east1

gcloud compute networks subnets create test-net-2-rfc5737 \
--network vpc4-ncc --range 198.51.100.0/24 --region us-east1

gcloud compute networks subnets create test-net-3-rfc5737 \
--network vpc4-ncc --range 203.0.113.0/24 --region us-east1

创建重叠子网范围

NCC 不会将重叠的 IP 范围导入中心路由表。用户可以在后续步骤中解决此限制。现在,请为 VPC2 和 VPC3 创建两个重叠的 IP 范围。

gcloud compute networks subnets create overlapping-vpc2 \
--network vpc3-ncc --range 10.3.3.0/24 --region us-central1

gcloud compute networks subnets create overlapping-vpc3 \
--network vpc2-ncc --range 10.3.3.0/24 --region us-central1

配置 VPC 防火墙规则

在每个 VPC 上配置防火墙规则,

  • SSH
  • 内部 IAP
  • 10.0.0.0/8 范围
gcloud compute firewall-rules create ncc1-vpc-internal \
--network vpc1-ncc \
--allow all \
--source-ranges 10.0.0.0/8

gcloud compute firewall-rules create ncc2-vpc-internal \
--network vpc2-ncc \
--allow all \
--source-ranges 10.0.0.0/8

gcloud compute firewall-rules create ncc3-vpc-internal \
--network vpc3-ncc \
--allow all \
--source-ranges 10.0.0.0/8

gcloud compute firewall-rules create ncc4-vpc-internal \
--network vpc4-ncc \
--allow all \
--source-ranges 10.0.0.0/8

gcloud compute firewall-rules create ncc1-vpc-iap \
--network vpc1-ncc \
--allow all \
--source-ranges 35.235.240.0/20

gcloud compute firewall-rules create ncc2-vpc-iap \
--network vpc2-ncc \
--allow=tcp:22 \
--source-ranges 35.235.240.0/20

gcloud compute firewall-rules create ncc3-vpc-iap \
--network vpc3-ncc \
--allow=tcp:22  \
--source-ranges 35.235.240.0/20

gcloud compute firewall-rules create ncc4-vpc-iap \
--network vpc4-ncc \
--allow=tcp:22  \
--source-ranges 35.235.240.0/20

在每个 VPC 中配置 GCE 虚拟机

您需要临时访问互联网才能在“vm1-vpc1-ncc”上安装软件包。

创建四个虚拟机,每个虚拟机将分配给之前创建的 VPC 之一

gcloud compute instances create vm1-vpc1-ncc \
--subnet vpc1-ncc-subnet1 \
--metadata=startup-script='#!/bin/bash
  apt-get update
  apt-get install apache2 -y
  apt-get install tcpdump -y
  service apache2 restart
  echo "
<h3>Web Server: www-vm1</h3>" | tee /var/www/html/index.html'


gcloud compute instances create vm2-vpc2-ncc \
--zone us-central1-a \
--subnet vpc2-ncc-subnet1 \
--no-address 

gcloud compute instances create pnat-vm-vpc2 \
--zone us-central1-a \
--subnet overlapping-vpc3 \
--no-address 


gcloud compute instances create vm1-vpc4-ncc \
--zone us-east1-b \
--subnet class-e-rfc5735 \
--no-address

3. Network Connectivity Center 中心

概览

在本部分中,我们将使用 gcloud 命令配置 NCC Hub。NCC 中心将充当控制平面,负责在每个 VPC spoke 之间构建路由配置。

8acc7651f52e251e

启用 API 服务

启用 NetworkConnectivity API(如果尚未启用):

gcloud services enable networkconnectivity.googleapis.com

创建 NCC Hub

使用 gcloud 命令创建 NCC hub

gcloud network-connectivity hubs create ncc-hub

输出示例

Create request issued for: [ncc-hub]
Waiting for operation [projects/user-3p-dev/locations/global/operations/operation-1668793629598-5edc24b7ee3ce-dd4c765b-5ca79556] to complete...done.     
Created hub [ncc-hub]

描述新创建的 NCC 中心。请记下名称和关联的路径。

gcloud network-connectivity hubs describe ncc-hub
gcloud network-connectivity hubs describe ncc-hub
createTime: '2023-11-02T02:28:34.890423230Z'
name: projects/user-3p-dev/locations/global/hubs/ncc-hub
routeTables:
- projects/user-3p-dev/locations/global/hubs/ncc-hub/routeTables/default
state: ACTIVE
uniqueId: de749c4c-0ef8-4888-8622-1ea2d67450f8
updateTime: '2023-11-02T02:28:48.613853463Z'

NCC Hub 引入了路由表,用于定义用于创建数据连接的控制平面。查找 NCC Hub 的路由表的名称

 gcloud network-connectivity hubs route-tables list --hub=ncc-hub
NAME: default
HUB: ncc-hub
DESCRIPTION:

查找 NCC 默认路由表的 URI。

gcloud network-connectivity hubs route-tables describe default --hub=ncc-hub
createTime: '2023-02-24T17:32:58.786269098Z'
name: projects/user-3p-dev/locations/global/hubs/ncc-hub/routeTables/default
state: ACTIVE
uid: eb1fdc35-2209-46f3-a8d6-ad7245bfae0b
updateTime: '2023-02-24T17:33:01.852456186Z'

列出 NCC Hub 的默认路由表的内容。注意* 在 spoke 之前,NCC Hub 的路由表将为空

gcloud network-connectivity hubs route-tables routes list --hub=ncc-hub --route_table=default

NCC Hub 的路由表应为空。

4. 使用 VPC Spoke 的 NCC

概览

在本部分中,您将使用 gcloud 命令将三个 VPC 配置为 NCC Spoke。

a70bc80037927bb0.png

将 VPC 配置为 NCC Spoke

按此顺序将以下 VPC 配置为 NCC Spoke

  • VPC4
  • VPC1
  • VPC2
  • VPC3

将 VPC4 配置为 NCC spoke,并将其分配给之前创建的 NCC hub。NCC spoke 调用需要指定位置。标志“-global”简化了 gcloud 语法,方法是在配置新的 NCC spoke 时允许用户避免指定完整的 URI 路径。

gcloud network-connectivity spokes linked-vpc-network create vpc4-spoke4 \
--hub=ncc-hub \
--vpc-network=vpc4-ncc \
--global

将 VPC1 配置为 NCC spoke。

管理员可以禁止子网路由从 VPC spoke 导出到 NCC hub 的路由表。在此 Codelab 的这一部分中,请根据摘要前缀创建排除导出规则,以防止 VPC1 的子网导出到 NCC Hub 路由表。

使用此 gcloud 命令列出属于 VPC1 的所有子网。

gcloud config set accessibility/screen_reader false
gcloud compute networks subnets list --network=vpc1-ncc

请注意之前在设置部分中创建的一对 /25 子网。

NAME              REGION       NETWORK   RANGE          STACK_TYPE  
vpc1-ncc-subnet1  us-central1  vpc1-ncc  10.1.1.0/24    IPV4_ONLY
vpc1-ncc-subnet2  us-central1  vpc1-ncc  10.1.2.0/25    IPV4_ONLY
vpc1-ncc-subnet3  us-central1  vpc1-ncc  10.1.2.128/25  IPV4_ONLY

将 VPC1 配置为 NCC spoke,并使用“export-exclude-ranges”排除将一对 /25 子网导入至中心路由表关键字来过滤特定范围内的 /24 摘要路由。

gcloud network-connectivity spokes linked-vpc-network create vpc1-spoke1 \
--hub=ncc-hub \
--vpc-network=vpc1-ncc \
--exclude-export-ranges=10.1.2.0/24 \
--global 

注意* 对于每个 NCC spoke,用户最多可以过滤 16 个唯一的 IP 范围。

列出 NCC Hub 的默认路由表的内容。NCC Hub 路由表中的一对 /25 子网发生了什么情况?

gcloud network-connectivity hubs route-tables routes list --hub=ncc-hub --route_table=default --filter="NEXT_HOP:vpc1-ncc"
IP_CIDR_RANGE  STATE   TYPE                  NEXT_HOP  HUB      ROUTE_TABLE
10.1.1.0/24    ACTIVE  VPC_PRIMARY_SUBNET    vpc1-ncc  ncc-hub  default

将 VPC2 配置为 NCC spoke

gcloud network-connectivity spokes linked-vpc-network create vpc2-spoke2 \
--hub=ncc-hub \
--vpc-network=vpc2-ncc \
--global

将 VPC3 配置为 NCC spoke,并将其分配给之前创建的 NCC hub。

gcloud  network-connectivity spokes linked-vpc-network create vpc3-spoke3 \
--hub=ncc-hub \
--vpc-network=vpc3-ncc \
--global

发生了什么情况?

ERROR: (gcloud.network-connectivity.spokes.linked-vpc-network.create) Invalid resource state for "https://www.googleapis.com/compute/v1/projects/xxxxxxxx/global/networks/vpc3-ncc": 10.3.3.0/24 (SUBNETWORK) overlaps with 10.3.3.0/24 (SUBNETWORK) from "projects/user-3p-dev/global/networks/vpc2-ncc" (peer)

NCC Hub 检测到 IP 范围与 VPC2 重叠。回想一下,VPC2 和 VPC3 都设置有相同的 10.3.3.0/24 IP 子网。

使用“排除”导出功能过滤重叠的 IP 范围

在编写此 Codelab 时,一个已知问题是用户必须删除并重新创建 NCC spoke,才能对导出过滤器进行配置更改。

gcloud network-connectivity spokes delete vpc2-spoke2 --global --quiet

注意:删除与特定 VPC 关联的 VPC spoke 需要 10 分钟的冷却期到期,才能重新创建引用同一 VPC 的新 spoke。

gcloud  network-connectivity spokes linked-vpc-network create vpc2-spoke2 \
--hub=ncc-hub \
--vpc-network=vpc2-ncc \
--exclude-export-ranges=10.3.3.0/24 \
--global

将 VPC3 配置为 NCC spoke,并将其分配给之前创建的 NCC hub。尝试将 VPC3 作为 spoke 添加到 NCC 应该可以成功。

gcloud network-connectivity spokes linked-vpc-network create vpc3-spoke3 \
--hub=ncc-hub \
--vpc-network=vpc3-ncc \
--exclude-export-ranges=10.3.3.0/24 \
--global

列出 NCC Hub 的默认路由表的内容并检查输出。

gcloud network-connectivity hubs route-tables routes list --hub=ncc-hub --route_table=default

不包括 VPC2 和 VPC3 中的重叠 IP 范围。NCC Hub 路由表支持所有有效的 IPv4 有效范围类型,但以非公开方式使用的公共 IP 地址 (PUPI) 除外。

5. 具有跨项目 Spoke 的 NCC

概览

到目前为止,您已配置与 Hub 属于同一项目的 NCC spoke。在本部分中,您将使用 gcloud 命令将 VPC 配置为除 NCC 中心以外的独立项目的 NCC Spoke。

这样,自行管理 VPC 的项目所有者就可以参与与 NCC 中心的网络连接。

e1190fa898c5097d.png

跨项目:Google Cloud 控制台和 Cloud Shell

为了与 GCP 交互,我们将在整个实验中同时使用 Google Cloud 控制台和 Cloud Shell。

跨项目 Spoke Google Cloud 控制台

您可以访问 https://console.cloud.google.com 访问 Cloud 控制台。

在 Google Cloud 中设置以下各项,以便更轻松地配置 Network Connectivity Center:

在 Google Cloud Console 的“项目选择器”页面上,选择或创建一个 Google Cloud 项目。

启动 Cloud Shell。此 Codelab 利用 $variables 辅助在 Cloud Shell 中实现 gcloud 配置。

gcloud auth list
gcloud config list project
gcloud config set project [YOUR-CROSSPROJECT-NAME]
xprojname=[YOUR-CROSSPROJECT-NAME]
echo $xprojname
gcloud config set compute/zone us-central1-a
gcloud config set compute/region us-central1

IAM 角色

NCC 需要 IAM 角色才能访问特定 API。请务必根据需要为您的用户配置 NCC IAM 角色。

必须至少为跨项目 spoke 管理员授予 IAM 角色:“networkconnectivity.networkSpokeManager。”

下表列出了 NCC Hub and Spoke 管理员所需的 IAM 角色,以供参考。

角色/说明

权限

networkconnectivity.networkAdmin - 允许网络管理员管理 hub 和 spoke。

networkconnectivity.hubs.networkconnectivity.spokes

networkconnectivity.networkSpokeManager - 允许在 hub 中添加和管理 spoke。将在宿主项目拥有 Hub 的共享 VPC 中使用,但其他项目中的其他管理员可以为其连接向 Hub 添加 spoke。

networkconnectivity.spokes。**

networkconnectivity.networkUsernetworkconnectivity.networkViewer - 允许网络用户查看 hub 和 spoke 的不同属性。

networkconnectivity.hubs.getnetworkconnectivity.hubs.listnetworkconnectivity.spokes.getnetworkconnectivity.spokes.listnetworkconnectivity.spokes.aggregatedList

在跨项目中创建 VPC 和子网

VPC 网络包含您将安装 GCE 虚拟机来验证数据路径的子网

gcloud compute networks create xproject-vpc \
--subnet-mode custom

gcloud compute networks subnets create xprj-net-1 \
--network xproject-vpc \
--range 10.100.1.0/24 \
--region us-central1

NCC Hub 项目 URI

可以使用此 gcloud 命令查找 NCC Hub URI。在下一步中,您将需要该 URI 路径来配置跨项目 NCC spoke。

gcloud network-connectivity hubs describe ncc-hub

跨项目 Spoke VPC

登录 VPC 不属于 NCC Hub 项目的另一个项目。在 cloudshell 上,使用此命令将 VPC 配置为 NCC spoke。

  • HUB_URI 应该是其他项目中的 hub 的 URI。
  • VPC_URI 应与 spoke 位于同一项目中
  • VPC 网络指定此跨项目中的 VPC 将加入另一个项目中的 NCC 中心
gcloud network-connectivity spokes linked-vpc-network create xproj-spoke \
--hub=projects/[YOUR-PROJECT-NAME]/locations/global/hubs/ncc-hub \
--global \
--vpc-network=xproject-vpc

.

Create request issued for: [xproj-spoke]
Waiting for operation [projects/xproject/locations/global/operations/operation-1689790411247-600dafd351158-2b862329-19b747f1] to complete...done.                           
Created spoke [xproj-spoke].
createTime: '2023-07-19T18:13:31.388500663Z'
hub: projects/[YOUR-PROJECT-NAME]/locations/global/hubs/ncc-hub
linkedVpcNetwork:
  uri: https://www.googleapis.com/compute/v1/projects/xproject/global/networks/xproject-vpc
name: projects/xproject/locations/global/spokes/xproj-spoke
reasons:
- code: PENDING_REVIEW
  message: Spoke is Pending Review
spokeType: VPC_NETWORK
state: INACTIVE
uniqueId: 46b4d091-89e2-4760-a15d-c244dcb7ad69
updateTime: '2023-07-19T18:13:38.652800902Z'

跨项目 NCC spoke 的状态是什么?为什么?

6. 拒绝或接受跨项目 Spoke

概览

NCC Hub 管理员必须明确接受跨项目 spoke 才能加入 hub。这样可以防止项目所有者将恶意 NCC spoke 附加到 NCC 全局路由表。Spoke 被接受或拒绝后,随后可以通过运行上述命令根据需要多次拒绝或接受。

登录 Cloud Shell,返回 NCC 中心所在的项目。

确定要查看的跨项目 spoke

gcloud network-connectivity hubs list-spokes ncc-hub \
 --filter="reason:PENDING_REVIEW"

接受 spoke

gcloud network-connectivity spokes accept xproj-spoke --global

可选:拒绝 spoke

gcloud network-connectivity spokes reject xproj-spoke \
--global \
--details="some reason to reject"

在 Hub 上列出活跃 Spoke

gcloud network-connectivity hubs list-spokes ncc-hub \
 --filter="state:ACTIVE"
NAME            PROJECT          LOCATION  TYPE         STATE   STATE REASON
Xproj-spoke     xproj            global    VPC_NETWORK  ACTIVE
vpc4-spoke4     user-3p-dev      global    VPC_NETWORK  ACTIVE
vpc1-spoke1     user-3p-dev      global    VPC_NETWORK  ACTIVE
vpc2-spoke2     user-3p-dev      global    VPC_NETWORK  ACTIVE
vpc3-spoke3     user-3p-dev      global    VPC_NETWORK  ACTIVE

列出 Hub 上的子网路由

在输出中,您可以看到来自跨 VPC spoke 的子网路由吗?

gcloud network-connectivity hubs route-tables routes list \
--route_table=default \
--hub=ncc-hub \
--filter="NEXT_HOP:xprj-vpc"
IP_CIDR_RANGE  STATE   TYPE                NEXT_HOP  HUB      ROUTE_TABLE
10.100.0.0/16  ACTIVE  VPC_PRIMARY_SUBNET  xprj-vpc  ncc-hub  default

7. VPC 之间的专用 NAT

概览

在本部分中,您将为两个 VPC 之间重叠的子网范围配置专用 NAT。请注意,VPC 之间的专用 NAT 需要 NCC。

在上一部分中,VPC2 和 VPC3 配置了重叠的子网范围“10.3.3.0/24”。这两个 VPC 都配置为 NCC spoke,以防止重叠的子网插入 NCC 中心路由表,这意味着没有第 3 层数据路径可用于访问位于该子网上的主机。

在 NCC 中心项目中使用以下命令查找重叠的子网范围。

gcloud compute networks subnets list --network vpc2-ncc

gcloud compute networks subnets list --network vpc3-ncc

在 vpc2-ncc 上,包含重叠 IP 范围的子网名称是什么?

*记下子网名称并将其保存在某个位置。您将为此范围配置源 NAT。

配置专用 NAT

指定一个可路由的子网范围,以从 VPC2 的重叠子网中获取 NAT 流量。使用“–Purpose=PRIVATE_NAT”配置互不重叠的子网范围标记。

gcloud compute networks subnets create ncc2-spoke-nat \
--network=vpc2-ncc \
--region=us-central1 \
--range=10.10.10.0/29 \
--purpose=PRIVATE_NAT

创建专用 Cloud Router 路由器以执行专用 NAT

gcloud compute routers create private-nat-cr \
--network vpc2-ncc \
--region us-central1

配置 Cloud Router 路由器,以将 vpc2-ncc 的重叠范围 10.3.3.0/24 作为 NAT 来源。在下面的配置示例中,“override-vpc3”是重叠子网的名称。“ALL”关键字指定子网中的所有 IP 范围都将进行源 NAT。

gcloud compute routers nats create ncc2-nat \
--router=private-nat-cr \
--type=PRIVATE \
--nat-custom-subnet-ip-ranges=overlapping-vpc3:ALL \
--router-region=us-central1

前面的步骤创建了一个 NAT IP 范围池和要转换的特定子网。在此步骤中,创建 NAT 规则“1”-

gcloud compute routers nats rules create 1 \
--router=private-nat-cr \
--region=us-central1 \
--match='nexthop.hub == "//networkconnectivity.googleapis.com/projects/$projectname/locations/global/hubs/ncc-hub"' \
--source-nat-active-ranges=ncc2-spoke-nat \
--nat=ncc2-nat

验证专用 NAT

gcloud compute routers nats describe ncc2-nat --router=private-nat-cr

输出示例

enableDynamicPortAllocation: true
enableEndpointIndependentMapping: false
endpointTypes:
- ENDPOINT_TYPE_VM
name: ncc2-nat
rules:
- action:
    sourceNatActiveRanges:
    - https://www.googleapis.com/compute/projects/yueri-3p-dev/regions/us-central1/subnetworks/ncc2-spoke-nat
  match: nexthop.hub == "//networkconnectivity.googleapis.com/projects/yueri-3p-dev/locations/global/hubs/ncc-hub"
  ruleNumber: 1
sourceSubnetworkIpRangesToNat: LIST_OF_SUBNETWORKS
subnetworks:
- name: https://www.googleapis.com/compute/projects/yueri-3p-dev/regions/us-central1/subnetworks/overlapping-vpc3
  sourceIpRangesToNat:
  - ALL_IP_RANGES
type: PRIVATE

(可选)

  • 切换到网页版控制台
  • 请前往“网络服务 >Cloud NAT >ncc2-nat

验证默认情况下,动态端口分配处于启用状态。

114050bb65e0c4e2

接下来,您将验证使用为 VPC2 配置的专用 NAT 路径的数据路径。

5035b181aeaa30a8

打开与 "vm1-vpc1-ncc" 的 SSH 会话,并使用下面的 tcpdump 命令捕获来自 NAT 池范围 "10.10.10.0/29" 的数据包。

vm1-vpc1-ncc

sudo tcpdump -i any net 10.10.10.0/29 -n

在编写此 Codelab 时,专用 NAT 不支持 ICMP 数据包。通过 SSH 连接到“pNat-vm-vpc2”并使用 curl 命令(如下所示)连接到端口 TCP 80 上的“vm1-vpc1-ncc”

pnat-vm-vpc2

curl 10.1.1.2 -v 

"vm1-vpc1-ncc" 上检查 tcpdump 的输出。发起与我们网络服务器(位于 "vm1-vpc1-ncc")上的 TCP 会话的来源 IP 地址是什么。

tcpdump: data link type LINUX_SLL2
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
19:05:27.504761 ens4  In  IP 10.10.10.2.1024 > 10.1.1.2:80: Flags [S], seq 2386228656, win 65320, options [mss 1420,sackOK,TS val 3955849029 ecr 0,nop,wscale 7], length 0
19:05:27.504805 ens4  Out IP 10.1.1.2:80 > 10.10.10.2.1024: Flags [S.], seq 48316785, ack 2386228657, win 64768, options [mss 1420,sackOK,TS val 1815983704 ecr 3955849029,nop,wscale 7], length 0
<output snipped>

8. 验证数据路径连接

请参考下图,验证每个虚拟机之间的数据路径。

424df0ebe4510ebb.png

通过 SSH 连接到“vm1-vpc1-ncc”并启动 TCP 转储以跟踪来自 的 ICMP 数据包vm2-vpc2-ncc”。提醒一下,此虚拟机位于 VPC2 上。

vm1-vpc1-ncc

sudo tcpdump -i any icmp -v -e -n

建立与“vm1-vpc2-ncc”和“ping”的 SSH 会话“vm1-vpc1-ncc”的 IP 地址。

vm1-vpc2-ncc

ping 10.1.1.2

建立与“vm1-vpc2-ncc”和“ping”的 SSH 连接“vm1-vpc4-ncc”的 IP 地址。

vm1-vpc2-ncc

ping 240.0.0.2

9. 清理

登录 Cloud Shell 并删除中心和分支站点网络中的虚拟机实例

删除专用 VPC NAT 配置

gcloud compute routers nats rules delete 1 \
--nat=ncc2-nat \
--router=private-nat-cr \
--region=us-central1 \
--quiet

gcloud compute routers nats delete ncc2-nat \
--router=private-nat-cr \
--router-region=us-central1 \
--quiet

gcloud compute routers delete private-nat-cr \
--region=us-central1 \
--quiet

删除 NCC spoke

gcloud network-connectivity spokes delete vpc1-spoke1 --global --quiet

gcloud network-connectivity spokes delete vpc2-spoke2 --global --quiet

gcloud network-connectivity spokes delete vpc3-spoke3 --global --quiet

gcloud network-connectivity spokes delete vpc4-spoke4 --global --quiet

拒绝跨项目 spoke

拒绝来自 NCC hub 的跨项目 VPC spoke。

gcloud network-connectivity spokes reject projects/$xprojname/locations/global/spokes/xproj-spoke \--details="cleanup" \
--global

删除 NCC Hub

gcloud network-connectivity hubs delete ncc-hub --quiet

删除防火墙规则

gcloud compute firewall-rules delete ncc1-vpc-internal --quiet
gcloud compute firewall-rules delete ncc2-vpc-internal --quiet
gcloud compute firewall-rules delete ncc3-vpc-internal --quiet
gcloud compute firewall-rules delete ncc4-vpc-internal --quiet
gcloud compute firewall-rules delete ncc1-vpc-iap --quiet
gcloud compute firewall-rules delete ncc2-vpc-iap --quiet
gcloud compute firewall-rules delete ncc3-vpc-iap --quiet
gcloud compute firewall-rules delete ncc4-vpc-iap --quiet

删除 GCE 实例

gcloud compute instances delete vm1-vpc1-ncc --zone=us-central1-a --quiet
gcloud compute instances delete vm2-vpc2-ncc --zone=us-central1-a --quiet
gcloud compute instances delete pnat-vm-vpc2 --zone=us-central1-a --quiet
gcloud compute instances delete vm1-vpc4-ncc --zone=us-east1-b --quiet

删除 VPC 子网

gcloud compute networks subnets delete ncc2-spoke-nat --region us-central1 --quiet
gcloud compute networks subnets delete vpc1-ncc-subnet1 --region us-central1 --quiet
gcloud compute networks subnets delete vpc1-ncc-subnet2 --region us-central1 --quiet
gcloud compute networks subnets delete vpc1-ncc-subnet3 --region us-central1 --quiet
gcloud compute networks subnets delete vpc2-ncc-subnet1 --region us-central1 --quiet
gcloud compute networks subnets delete overlapping-vpc2 --region us-central1 --quiet 
gcloud compute networks subnets delete overlapping-vpc3 --region us-central1 --quiet

gcloud compute networks subnets delete benchmark-testing-rfc2544 --region us-east1 --quiet
gcloud compute networks subnets delete class-e-rfc5735 --region us-east1 --quiet
gcloud compute networks subnets delete ietf-protcol-assignment-rfc6890 --region us-east1 --quiet
gcloud compute networks subnets delete ipv6-4-relay-rfc7526 --region us-east1 --quiet
gcloud compute networks subnets delete pupi --region us-east1 --quiet
gcloud compute networks subnets delete test-net-1-rfc5737 --region us-east1 --quiet
gcloud compute networks subnets delete test-net-2-rfc5737 --region us-east1 --quiet
gcloud compute networks subnets delete test-net-3-rfc5737 --region us-east1 --quiet

删除 VPC

gcloud compute networks delete vpc1-ncc vpc2-ncc vpc3-ncc vpc4-ncc 
--quiet 

10. 恭喜!

您已完成 Network Connectivity Center 实验!

所学内容

  • 已配置带有 NCC 中心的全网状 VPC 对等互连网络
  • NCC Spoke 排除过滤器
  • 跨项目 Spoke 支持
  • VPC 之间的专用 NAT

后续步骤

©Google, LLC 或其关联公司。保留所有权利。请勿散布。