Private Service Connect 接口

1. 简介

Private Service Connect 接口是一种资源,允许提供方 Virtual Private Cloud (VPC) 网络发起与使用方 VPC 网络中的各种目的地的连接。提供方网络与使用方网络可以位于不同的项目和组织中。

如果网络连接接受来自 Private Service Connect 接口的连接,Google Cloud 会为该接口分配一个由网络连接指定的使用方子网的 IP 地址。使用方和提供方网络已连接,可以使用内部 IP 地址进行通信。

网络连接与 Private Service Connect 接口之间的连接类似于 Private Service Connect 端点与服务连接之间的连接,但存在以下两个主要区别:

  • 网络连接允许提供方网络发起与使用方网络的连接(代管式服务出站流量),而端点则允许使用方网络发起与提供方网络的连接(代管式服务入站流量)。
  • Private Service Connect 接口连接具有传递性。这意味着提供方网络可以与连接到使用方网络的其他网络通信。

构建内容

在本教程中,您将构建一个全面的 Private Service Connect (PSC) 接口架构,该架构利用 Cloud 防火墙规则来允许和拒绝从提供方到使用方的计算资源的连接,如图 1 所示。

图 1

d39bf35e55bdf9e6.png

您将在使用方 VPC 中创建一个 psc-network-attachment,从而实现以下用例:

  1. 创建 Cloud Firewall 规则,以允许从 bear 访问 lion
  2. 创建一条 Cloud 防火墙规则,禁止 bear 访问 tiger
  3. 创建 Cloud Firewall 规则,以允许 cosmo 访问 bear

学习内容

  • 如何创建网络连接
  • 提供方如何使用网络附件创建 PSC 接口
  • 如何建立生产者与使用方之间的通信
  • 如何允许从提供方虚拟机 (bear) 访问使用方虚拟机 (lion)
  • 如何阻止从提供方虚拟机 (bear) 访问使用方虚拟机 (tiger)
  • 如何允许从使用方虚拟机 (cosmo) 访问提供方虚拟机 (bear)

所需条件

2. 准备工作

更新项目以支持本教程

本教程使用 $variables 来帮助在 Cloud Shell 中实现 gcloud 配置。

在 Cloud Shell 中,执行以下操作:

gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
projectid=YOUR-PROJECT-NAME
echo $projectid

3. 使用方设置

创建使用方 VPC

在 Cloud Shell 中,执行以下操作:

gcloud compute networks create consumer-vpc --project=$projectid --subnet-mode=custom

创建使用方子网

在 Cloud Shell 中,执行以下操作:

gcloud compute networks subnets create lion-subnet-1 --project=$projectid --range=192.168.20.0/28 --network=consumer-vpc --region=us-central1

在 Cloud Shell 中,执行以下操作:

gcloud compute networks subnets create tiger-subnet-1 --project=$projectid --range=192.168.30.0/28 --network=consumer-vpc --region=us-central1

在 Cloud Shell 中,执行以下操作:

gcloud compute networks subnets create cosmo-subnet-1 --project=$projectid --range=192.168.40.0/28 --network=consumer-vpc --region=us-central1

创建 Private Service Connect 网络连接子网

在 Cloud Shell 中,执行以下操作:

gcloud compute networks subnets create intf-subnet --project=$projectid --range=192.168.10.0/28 --network=consumer-vpc --region=us-central1

Cloud Router 和 NAT 配置

本教程中使用 Cloud NAT 进行软件包安装,因为虚拟机实例没有公共 IP 地址。Cloud NAT 支持具有专用 IP 地址的虚拟机访问互联网。

在 Cloud Shell 中,创建 Cloud Router 路由器。

gcloud compute routers create cloud-router-for-nat --network consumer-vpc --region us-central1

在 Cloud Shell 中,创建 NAT 网关。

gcloud compute routers nats create cloud-nat-us-central1 --router=cloud-router-for-nat --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --region us-central1

4. 启用 IAP

如需允许 IAP 连接到您的虚拟机实例,请创建一个防火墙规则,该规则:

  • 适用于您希望使用 IAP 可访问的所有 VM 实例。
  • 允许来自 IP 地址范围 35.235.240.0/20 的入站流量。此范围包含 IAP 用于 TCP 转发的所有 IP 地址。

在 Cloud Shell 中,创建 IAP 防火墙规则。

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

5. 创建使用方虚拟机实例

在 Cloud Shell 中,创建使用方虚拟机实例 lion。

gcloud compute instances create lion \
    --project=$projectid \
    --machine-type=e2-micro \
    --image-family debian-11 \
    --no-address \
    --image-project debian-cloud \
    --zone us-central1-a \
    --subnet=lion-subnet-1 \
    --metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install tcpdump
      sudo apt-get install apache2 -y
      sudo service apache2 restart
      echo 'Welcome to the lion app server !!' | tee /var/www/html/index.html
      EOF"

在 Cloud Shell 中,创建使用方虚拟机实例 tiger。

gcloud compute instances create tiger \
    --project=$projectid \
    --machine-type=e2-micro \
    --image-family debian-11 \
    --no-address \
    --image-project debian-cloud \
    --zone us-central1-a \
    --subnet=tiger-subnet-1 \
    --metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install tcpdump
      sudo apt-get install apache2 -y
      sudo service apache2 restart
      echo 'Welcome to the tiger app server !!' | tee /var/www/html/index.html
      EOF"

在 Cloud Shell 中,创建使用方虚拟机实例 cosmo。

gcloud compute instances create cosmo \
    --project=$projectid \
    --machine-type=e2-micro \
    --image-family debian-11 \
    --no-address \
    --image-project debian-cloud \
    --zone us-central1-a \
    --subnet=cosmo-subnet-1 \
    --metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install tcpdump
      sudo apt-get install apache2 -y
      sudo service apache2 restart
      echo 'Welcome to the cosmo app server !!' | tee /var/www/html/index.html
      EOF"

获取并存储实例的 IP 地址:

在 Cloud Shell 中,针对 lion 和 tiger 虚拟机实例执行 describe 操作。

gcloud compute instances describe lion --zone=us-central1-a | grep  networkIP:

gcloud compute instances describe tiger --zone=us-central1-a | grep  networkIP:

gcloud compute instances describe cosmo --zone=us-central1-a | grep  networkIP:

6. Private Service Connect 网络连接

网络连接是区域级资源,表示 Private Service Connect 接口的使用方端。您将单个子网与一个网络连接关联,而提供方将该子网的 IP 分配给 Private Service Connect 接口。子网必须与网络连接位于同一区域。网络连接必须与提供方服务位于同一区域。

创建网络连接

在 Cloud Shell 中,创建网络连接。

gcloud compute network-attachments create psc-network-attachment \
    --region=us-central1 \
    --connection-preference=ACCEPT_MANUAL \
    --producer-accept-list=$projectid \
    --subnets=intf-subnet

列出网络连接

在 Cloud Shell 中,列出网络连接。

gcloud compute network-attachments list

描述网络连接

在 Cloud Shell 中,描述网络连接。

gcloud compute network-attachments describe psc-network-attachment --region=us-central1

记下提供方在创建 Private Service Connect 接口时将使用的 psc-network-attachment URI。示例如下:

user@cloudshell$ gcloud compute network-attachments describe psc-network-attachment --region=us-central1 
connectionPreference: ACCEPT_MANUAL
creationTimestamp: '2023-06-06T20:57:12.623-07:00'
fingerprint: 4Yq6xAfaRO0=
id: '3235195049527328503'
kind: compute#networkAttachment
name: psc-network-attachment
network: https://www.googleapis.com/compute/v1/projects/$projectid/global/networks/consumer-vpc
producerAcceptLists:
- $projectid
region: https://www.googleapis.com/compute/v1/projects/$projectid/regions/us-central1
selfLink: https://www.googleapis.com/compute/v1/projects/$projectid/regions/us-central1/networkAttachments/psc-network-attachment
subnetworks:
- https://www.googleapis.com/compute/v1/projects/$projectid/regions/us-central1/subnetworks/intf-subnet

7. 提供方设置

创建提供方 VPC 网络

在 Cloud Shell 中,执行以下操作:

gcloud compute networks create producer-vpc --project=$projectid --subnet-mode=custom

创建提供方子网

在 Cloud Shell 中,创建用于 psc 接口的 vNIC0 的子网。

gcloud compute networks subnets create prod-subnet --project=$projectid --range=10.20.1.0/28 --network=producer-vpc --region=us-central1

8. 启用 IAP

如需允许 IAP 连接到您的虚拟机实例,请创建一个防火墙规则,该规则:

  • 适用于您希望使用 IAP 可访问的所有 VM 实例。
  • 允许来自 IP 地址范围 35.235.240.0/20 的入站流量。此范围包含 IAP 用于 TCP 转发的所有 IP 地址。

在 Cloud Shell 中,创建 IAP 防火墙规则。

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

9. 创建 Private Service Connect 接口

Private Service Connect 接口是一种资源,允许提供方 Virtual Private Cloud (VPC) 网络发起与使用方 VPC 网络中的各种目的地的连接。提供方网络与使用方网络可以位于不同的项目和组织中。

如果网络连接接受来自 Private Service Connect 接口的连接,Google Cloud 会为该接口分配一个由网络连接指定的使用方子网的 IP 地址。使用方和提供方网络已连接,可以使用内部 IP 地址进行通信。

在 Cloud Shell 中,创建 Private Service Connect 接口 (bear),然后插入网络连接描述输出中之前识别出的 psc-network-attachment URI。

gcloud compute instances create bear --zone us-central1-a --machine-type=f1-micro --can-ip-forward --network-interface subnet=prod-subnet,network=producer-vpc,no-address --network-interface network-attachment=https://www.googleapis.com/compute/v1/projects/$projectid/regions/us-central1/networkAttachments/psc-network-attachment

多 NIC 验证

验证 PSC 接口是否配置了适当的 IP 地址。vNIC0 将使用生产方 prod-subnet (10.20.1.0/28),vNIC1 将使用使用方 intf-subnet (192.168.10.0/28)。

gcloud compute instances describe bear --zone=us-central1-a | grep networkIP:

示例:

user$ gcloud compute instances describe bear --zone=us-central1-a | grep networkIP:
  networkIP: 10.20.1.2
  networkIP: 192.168.10.2

10. 更新使用方防火墙规则

创建 Cloud Firewall 规则,以允许 bear 访问 lion

在 Cloud Shell 中,创建优先级较高的规则,允许从 attachment-subnet (intf-subnet) 的 IP 地址范围到 lion-subnet-1 地址范围内的目的地的出站流量。

gcloud compute firewall-rules create allow-limited-egress-to-lion \
    --network=consumer-vpc \
    --action=ALLOW \
    --rules=ALL \
    --direction=EGRESS \
    --priority=1000 \
    --source-ranges="192.168.10.0/28" \
    --destination-ranges="192.168.20.0/28" \
    --enable-logging

在 Cloud Shell 中,创建一个入站流量允许规则,用于替换针对来自 psc-network-attachment 子网的流量的隐式拒绝入站规则。

gcloud compute firewall-rules create allow-ingress \
--network=consumer-vpc \
--action=ALLOW \
--rules=ALL \
--direction=INGRESS \
--priority=1000 \
--source-ranges="192.168.10.0/28" \
--enable-logging

创建一条 Cloud 防火墙规则,拒绝 bear 访问所有网段(包括 tiger)

在 Cloud Shell 中,创建一个低优先级规则,用于拒绝来自网络连接子网 intf-subnet 的 IP 地址范围的所有出站流量。

gcloud compute firewall-rules create deny-all-egress \
    --network=consumer-vpc \
    --action=DENY \
    --rules=ALL \
    --direction=EGRESS \
    --priority=65534 \
    --source-ranges="192.168.10.0/28" \
    --destination-ranges="0.0.0.0/0" \
    --enable-logging

创建 Cloud 防火墙规则,以允许从 cosmo 访问 bear

在 Cloud Shell 中,创建一个入站流量允许规则,用于替换针对来自 psc-network-attachment 子网的流量的隐式拒绝入站规则。

gcloud compute firewall-rules create vm-subnet-allow-ingress \
    --network=consumer-vpc \
    --action=ALLOW \
    --rules=ALL \
    --direction=INGRESS \
    --priority=1000 \
    --source-ranges="192.168.40.0/28" \
    --destination-ranges="192.168.10.0/28" \
    --enable-logging

11. 为 PSC 接口创建 Linux 路由

在 PSC 接口实例中,配置 Linux 路由以允许提供方与使用方子网通信。

查找 Private Service Connect 接口的客机操作系统名称

如需配置路由,您需要知道 Private Service Connect 接口的客机操作系统名称,该名称与 Google Cloud 中的接口名称不同。

在 Cloud Shell 中,打开一个新标签页,然后执行以下操作:

gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
projectid=YOUR-PROJECT-NAME
echo $projectid

在 Cloud Shell 中使用 IAP 登录 psc-interface 虚拟机 bear。

gcloud compute ssh bear --project=$projectid --zone=us-central1-a --tunnel-through-iap

在 Cloud Shell 中,获取 psc-interface 实例的 IP 地址

ip a

示例:

user@bear:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 42:01:0a:14:01:02 brd ff:ff:ff:ff:ff:ff
    altname enp0s4
    inet 10.20.1.2/32 brd 10.20.1.2 scope global dynamic ens4
       valid_lft 85991sec preferred_lft 85991sec
    inet6 fe80::4001:aff:fe14:102/64 scope link 
       valid_lft forever preferred_lft forever
3: ens5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 42:01:c0:a8:0a:02 brd ff:ff:ff:ff:ff:ff
    altname enp0s5
    inet 192.168.10.2/32 brd 192.168.10.2 scope global dynamic ens5
       valid_lft 85991sec preferred_lft 85991sec
    inet6 fe80::4001:c0ff:fea8:a02/64 scope link 
       valid_lft forever preferred_lft forever

查找 PSC 接口的网关 IP

在网络接口列表中,找到并存储与您的 Private Service Connect 接口的 IP 地址关联的接口名称,例如 ens5 (vNIC1)

如需配置路由,您需要知道 Private Service Connect 接口的默认网关的 IP 地址

在 Cloud Shell 中,我们将使用 1,因为 PSC 接口与 vNIC1 相关联。

curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/1/gateway -H "Metadata-Flavor: Google" && echo

示例会生成默认网关 192.168.10.1

user@bear:~$ curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/1/gateway -H "Metadata-Flavor: Google" && echo
192.168.10.1

为使用方子网添加路由

对于连接到 Private Service Connect 接口的每个使用方子网,您必须向 Private Service Connect 接口的默认网关添加路由。这样可以确保前往使用方网络的流量来自 Private Service Connect 接口。

在 bear 实例中,将路由添加到使用方子网。

sudo ip route add 192.168.20.0/28 via 192.168.10.1 dev ens5
sudo ip route add 192.168.30.0/28 via 192.168.10.1 dev ens5
sudo ip route add 192.168.40.0/28 via 192.168.10.1 dev ens5

验证路由表

在 Cloud Shell 中,验证新添加的路由。

ip route show

Example.

user@bear:~$ ip route show
default via 10.20.1.1 dev ens4 
10.20.1.0/28 via 10.20.1.1 dev ens4 
10.20.1.1 dev ens4 scope link 
192.168.10.0/28 via 192.168.10.1 dev ens5 
192.168.10.1 dev ens5 scope link 
192.168.20.0/28 via 192.168.10.1 dev ens5 
192.168.30.0/28 via 192.168.10.1 dev ens5 
192.168.40.0/28 via 192.168.10.1 dev ens5 

12. 验证 Bear 与 Lion 之间的连接是否成功

我们来确认一下,提供方虚拟机实例 bear 是否可以通过执行 curl 命令与使用方实例 lion 通信。

在 bear 实例中,对本教程前面在 bear 实例中确定的 lion 的 IP 地址执行 curl 命令。

curl -v <lions IP Address>

示例:

user@bear:~$ curl -v 192.168.20.2
*   Trying 192.168.20.2:80...
* Connected to 192.168.20.2 (192.168.20.2) port 80 (#0)
> GET / HTTP/1.1
> Host: 192.168.20.2
> User-Agent: curl/7.74.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Tue, 06 Jun 2023 03:53:08 GMT
< Server: Apache/2.4.56 (Debian)
< Last-Modified: Mon, 05 Jun 2023 19:41:26 GMT
< ETag: "1e-5fd6716a1e11b"
< Accept-Ranges: bytes
< Content-Length: 30
< Content-Type: text/html
< 
Welcome to lion app server !!
* Connection #0 to host 192.168.20.2 left intact

13. 验证 bear 到 tiger 的连接是否已屏蔽

我们来查看防火墙日志,确认出站流量防火墙规则是否阻止了 bear 对 tiger 的访问。

在新的 Cloud 控制台会话中,依次前往“日志记录”→“日志浏览器”→“选择查询”

2ae597e6d970cddf.png

将以下查询字符串粘贴到搜索字段中,然后选择流式传输

jsonPayload.rule_details.reference="network:consumer-vpc/firewall:deny-all-egress"

30d7bfae315f2ee3.png

在 bear 实例中,对本教程前面标识的 tiger 实例的 IP 地址执行 curl 命令。curl 最终会超时。

curl -v <tiger's IP Address>

示例:

user@bear:~$ curl -v 192.168.30.2 
*   Trying 192.168.30.2:80...
* connect to 192.168.30.2 port 80 failed: Connection timed out
* Failed to connect to 192.168.30.2 port 80: Connection timed out
* Closing connection 0
curl: (28) Failed to connect to 192.168.30.2 port 80: Connection timed out

验证 Logs Explorer 是否已捕获被拒绝的防火墙日志。选择日志条目并展开嵌套字段以查看元数据。

5c42a6587300be55.png

14. 验证 cosmo 与 bear 的连接是否成功

打开新的 Cloud Shell 标签页,然后更新您的项目设置。

在 Cloud Shell 中,执行以下操作:

gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
projectid=YOUR-PROJECT-NAME
echo $projectid

在 Cloud Shell 中使用 IAP 登录 Cosmo 实例。

gcloud compute ssh cosmo --project=$projectid --zone=us-central1-a --tunnel-through-iap

在 Cloud Shell 中,对本教程前面标识的熊的 IP 地址 vNIV1 执行 ping 命令

ping <bears vNIC1 IP Address>

示例:

user@cosmo:~$ ping 192.168.10.2 -c 5
PING 192.168.10.2 (192.168.10.2) 56(84) bytes of data.
64 bytes from 192.168.10.2: icmp_seq=1 ttl=64 time=0.277 ms
64 bytes from 192.168.10.2: icmp_seq=2 ttl=64 time=0.288 ms
64 bytes from 192.168.10.2: icmp_seq=3 ttl=64 time=0.265 ms
64 bytes from 192.168.10.2: icmp_seq=4 ttl=64 time=0.264 ms
64 bytes from 192.168.10.2: icmp_seq=5 ttl=64 time=0.366 ms

15. 清理

在 Cloud Shell 中,删除教程组件。

gcloud compute instances delete bear --zone=us-central1-a --quiet

gcloud compute instances delete lion --zone=us-central1-a --quiet

gcloud compute instances delete tiger --zone=us-central1-a --quiet

gcloud compute instances delete cosmo --zone=us-central1-a --quiet

gcloud compute network-attachments delete psc-network-attachment --region=us-central1 --quiet

gcloud compute firewall-rules delete allow-ingress allow-limited-egress-to-lion deny-all-egress ssh-iap-consumer ssh-iap-producer vm-subnet-allow-ingress --quiet

gcloud compute networks subnets delete cosmo-subnet-1 intf-subnet lion-subnet-1 prod-subnet tiger-subnet-1 --region=us-central1 --quiet

gcloud compute routers delete cloud-router-for-nat --region=us-central1 --quiet 

gcloud compute networks delete consumer-vpc --quiet

gcloud compute networks delete producer-vpc --quiet

16. 恭喜

恭喜,您已成功通过实现防火墙规则来配置和验证 Private Service Connect 接口以及使用方和提供方连接。

您创建了使用方基础架构,并添加了网络附件,以允许提供方创建多 NIC 虚拟机来桥接使用方和提供方通信。您了解了如何在使用方 VPC 网络中创建防火墙规则,以允许与使用方和提供方 VPC 中的实例建立连接。

Cosmopup 认为教程很棒!

e6d3675ca7c6911f.jpeg

后续操作

查看下列教程…

深入阅读和视频

参考文档