实验:NCC VPC 作为 Spoke

关于此 Codelab
schedule7 分钟
subject上次更新时间:2024年7月26日
account_circleEric Yu, David Tu 编写

在本实验中,用户将探索如何使用 Network Connectivity Center(NCC) 通过支持 VPC spoke 来大规模建立 VPC 网间连接。当用户将 VPC 定义为 VPC spoke 时,他们可以通过 NCC Hub 将其与多个 VPC 网络连接在一起。采用 VPC spoke 配置的 NCC 通过使用集中式连接管理模型,降低了通过 VPC 对等互连管理一对 VPC 间连接的操作复杂性。

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

构建内容

在此 Codelab 中,您将使用 NCC 中心构建逻辑的中心辐射型拓扑,该拓扑将跨三个不同的 VPC 实现全网状 VPC 连接网络。

  • 使用 NCC 实现全网状 VPC 连接
  • 跨 VPC 的专用 NAT
  • 熟悉 GCP VPC 网络
  • 了解 Cloud Router 和 BGP 路由
  • 两个单独的 GCP 项目
  • 此 Codelab 需要 5 个 VPC。其中一个 VPC 必须位于与 NCC 枢纽不同的项目中
  • 请查看您的配额:网络,并在需要时申请额外的网络(屏幕截图如下):

6d1b99c6da87fd84.png

目标

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

准备工作

Google Cloud 控制台和 Cloud Shell

在本实验中,我们将使用 Google Cloud 控制台和 Cloud Shell 与 GCP 进行交互。

NCC 中心项目 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 - 允许网络管理员管理中心和 spoke。

networkconnectivity.hubs.networkconnectivity.spokes.

networkconnectivity.networkSpokeManager - 允许在 hub 中添加和管理 spoke。适用于共享 VPC 场景,其中宿主项目拥有 Hub,但其他项目中的其他管理员可以为其关联添加 spoke。

networkconnectivity.spokes.**

networkconnectivity.networkUsernetworkconnectivity.networkViewer - 允许影音平台用户查看中心和辐射点的不同属性。

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

2. 设置网络环境

概览

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

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

245f1002db33ca98.png

创建 VPC 和子网

VPC 网络包含您将用于安装 GCE VM 以进行数据路径验证的子网

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 Hub

概览

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

8acc7651f52e251e.png

启用 API 服务

如果尚未启用 Network Connectivity API,请启用该 API:

gcloud services enable networkconnectivity.googleapis.com

创建 NCC Hub

使用 gcloud 命令创建 NCC 中心

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 集线器的默认路由表的内容。注意* 在 spoke 被

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 语音 API 时需要指定位置。标志“-global”可简化 gcloud 语法,让用户在配置新的 NCC 边缘时避免指定完整的 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”关键字过滤出该特定范围内的 /24 汇总路由,以防止这对 /25 子网导入到中心路由表。

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 集线器的默认路由表的内容。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 检测到与 VPC2 重叠的 IP 范围。回想一下,VPC2 和 VPC3 都使用相同的 10.3.3.0/24 IP 子网进行设置。

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

在撰写此 Codelab 时,有一个已知问题:用户必须删除并重新创建 NCC 轮辐,才能对导出过滤条件进行配置更改。

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 添加为 NCC 的 spoke 应该会成功。

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 中心路由表支持所有有效的 IPv4 有效范围类型,但以非公开方式使用的公共 IP 地址 (PUPI) 除外。

5. 包含跨项目 spoke 的 NCC

概览

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

这样,管理自己的 VPC 的项目所有者便可以参与 NCC Hub 的网络连接。

e1190fa898c5097d.png

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

在本实验中,我们将使用 Google Cloud 控制台和 Cloud Shell 与 GCP 进行交互。

跨项目 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 中心和 Spoke 管理员所需的 IAM 角色,供您参考。

角色/说明

权限

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

networkconnectivity.hubs.networkconnectivity.spokes.

networkconnectivity.networkSpokeManager - 允许在 hub 中添加和管理 spoke。适用于共享 VPC 场景,其中宿主项目拥有 Hub,但其他项目中的其他管理员可以为其关联添加 spoke。

networkconnectivity.spokes.**

networkconnectivity.networkUsernetworkconnectivity.networkViewer - 允许影音平台用户查看中心和辐射点的不同属性。

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

在跨项目中创建 VPC 和子网

VPC 网络包含您将用于安装 GCE VM 以进行数据路径验证的子网

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 中心 URI。您将需要该 URI 路径来配置下一步中的跨项目 NCC 分支。

gcloud network-connectivity hubs describe ncc-hub

跨项目 Spoke VPC

登录另一个 VPC 不属于 NCC Hub 项目的项目。在 Cloud Shell 中,使用以下命令将 VPC 配置为 NCC spoke。

  • HUB_URI 应为其他项目中的某个 hub 的 URI。
  • VPC_URI 应与此 spoke 位于同一项目中
  • VPC-network 用于指定此跨项目中的 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 中心管理员必须明确接受跨项目 spoke 才能加入该中心。这样可以防止项目所有者将不法 NCC 辐条附加到 NCC 全局路由表。接受或拒绝 Spoke 后,您可以通过运行上述命令来根据需要拒绝或接受该 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 之间的 Private NAT

概览

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

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

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

gcloud compute networks subnets list --network vpc2-ncc

gcloud compute networks subnets list
--network vpc3-ncc

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

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

配置 Private 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 源 NAT 重叠范围 10.3.3.0/24。在以下示例配置中,“overlapping-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”,用于转换与来自重叠子网范围的流量匹配的网络数据包(如果目标网络采用 NCC 中心路由表中的路径)。

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

可选:

  • 切换到 Web 控制台
  • 依次选择“网络服务 > Cloud NAT > ncc2-nat

验证动态端口分配是否默认处于启用状态。

114050bb65e0c4e2.png

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

5035b181aeaa30a8.png

打开与 "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" 上的 Web 服务器建立 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 转储以跟踪来自“vm2-vpc2-ncc”的 ICMP 数据包。请注意,此虚拟机位于 VPC2 中。

vm1-vpc1-ncc

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

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

vm1-vpc2-ncc

ping 10.1.1.2

建立到“vm1-vpc2-ncc”的 SSH 连接,并“ping”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 中心拒绝跨项目 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 之间的 Private NAT

后续步骤

©Google, LLC 或其关联公司。保留所有权利。不得分发。