Agent Engine PSC 显式代理

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 接口连接具有传递性。这意味着提供方网络可以与连接到使用方网络的其他网络通信。

Vertex AI PSC 接口可达性注意事项

  • PSC 接口能够将流量路由到 RFC1918 地址块内的 VPC 或本地目标。
  • 以非 rfc-1918 地址块为目标的 PSC 接口需要在使用方的 VPC 中部署一个具有 rfc-1918 地址的显式代理。在 Vertex AI 部署中,必须定义代理以及目标端点的 FQDN。
  • 如果您仅为部署配置了 PSC 接口,则部署会保留其默认的互联网访问权限。出站流量会直接从安全的 Google 管理的租户网络中出站。

Vertex AI PSC 接口 VPC-SC 注意事项

  • 如果您的项目属于 VPC Service Controls 边界的一部分,该边界会阻止 Google 管理的租户的默认互联网访问权限,以防止数据渗漏。
  • 如要在此场景中允许部署访问公共互联网,您必须明确配置一条通过您的 VPC 路由流量的安全出站路径。
  • 实现此目标的推荐方法是,在您的 VPC 边界内设置一个具有 RFC1918 地址的代理服务器,并创建一个 Cloud NAT 网关以允许代理虚拟机访问互联网。

如需了解详情,请参阅以下资源:

部署代理 | Vertex AI 上的生成式 AI | Google Cloud

为 Vertex AI 资源设置 Private Service Connect 接口 | Google Cloud

构建内容

在本教程中,您将构建一个全面的 Agent Engine,该引擎部署了 Private Service Connect (PSC) 接口,以允许通过部署在使用方 VPC 中的代理虚拟机(具有 RFC1918 地址)连接到公共网站 (https://api.frankfurter.app/)。此部署示例适用于已启用 VPC-SC 的项目,或适用于需要通过客户网络(而非租户 VPC)进行互联网出站流量的管理员。

图 1

f42f2db921f6d5af.png

您将在使用方 VPC 中创建一个 psc-network-attachment,利用 DNS 对等互连来解析托管 Agent Engine 的租户项目中的使用方网络 proxy-vm,从而实现以下使用情形:

部署 Agent Engine 并配置代理虚拟机以充当显式代理,从而使其能够访问公共网址 https://api.frankfurter.app

学习内容

  • 如何创建网络连接
  • 提供方如何使用网络连接创建 PSC 接口
  • 如何使用 DNS 对等互连建立从提供方到使用方的通信
  • 如何部署和使用代理虚拟机进行互联网出站流量

所需条件

Google Cloud 项目

IAM 权限

2. 准备工作

更新项目以支持本教程

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

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

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

API 启用

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

gcloud services enable "compute.googleapis.com"
gcloud services enable "aiplatform.googleapis.com"
gcloud services enable "dns.googleapis.com"
gcloud services enable "notebooks.googleapis.com"
gcloud services enable "storage.googleapis.com"
gcloud services enable "iap.googleapis.com"

验证 API 是否已成功启用

gcloud services list --enabled

3. 使用方设置

创建使用方 VPC

此 VPC 位于客户项目中。系统将在此 VPC 中创建以下资源

  • 消费类子网
  • 网络连接子网
  • Cloud Router(Cloud NAT 必需)
  • Cloud NAT

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

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

创建使用方子网

在 Cloud Shell 中,为代理虚拟机创建子网:

gcloud compute networks subnets create rfc1918-subnet1 --project=$projectid --range=10.10.10.0/28 --network=consumer-vpc --region=us-central1

创建 Private Service Connect 网络连接子网

在 Cloud Shell 中,为 PSC 网络连接创建子网:

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 网关。我们将使用日志记录来验证对 Frankfurter API (https://api.frankfurter.app/) 的公共 IP 的访问权限。

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 --enable-logging --log-filter=ALL

4. 启用 IAP

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

  • 适用于您希望使用 IAP 可访问的所有虚拟机实例。
  • 允许来自 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 中,创建使用方虚拟机实例 proxy-vm,该实例将充当 Agent Engine 的显式代理。我们将使用 tinyproxy 作为代理 HTTP 流量的应用。

gcloud compute instances create proxy-vm \
    --project=$projectid \
    --machine-type=e2-micro \
    --image-family debian-11 \
    --no-address \
    --can-ip-forward \
    --image-project debian-cloud \
    --zone us-central1-a \
    --subnet=rfc1918-subnet1 \
    --shielded-secure-boot \
    --metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install tcpdump
      sudo apt-get install tinyproxy -y
      sudo apt-get install apache2 -y
      sudo service apache2 restart
      echo 'proxy server !!' | tee /var/www/html/index.html
      EOF"

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_AUTOMATIC \
    --subnets=intf-subnet

列出网络连接

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

gcloud compute network-attachments list

描述网络连接

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

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

记下 PSC 网络连接名称 psc-network-attachment,提供方在创建 Private Service Connect 接口时将使用该名称。

如需在 Cloud 控制台中查看 PSC 网络附加网址,请前往以下位置:

网络服务 → Private Service Connect → 网络连接 → psc-network-attachment

8eec51cb197da218.png

7. 专用 DNS 区域

您将为 demo.com 创建一个 Cloud DNS 区域,并使用指向代理虚拟机的 IP 地址的 A 记录填充该区域。之后,DNS 对等互连将部署在 Agent Engine 中,从而允许访问使用方的 DNS 记录。

在 Cloud Shell 中,执行以下操作以创建 DNS 名称 demo.com。

gcloud dns --project=$projectid managed-zones create private-dns-codelab --description="" --dns-name="demo.com." --visibility="private" --networks="https://compute.googleapis.com/compute/v1/projects/$projectid/global/networks/consumer-vpc"

获取并存储用于 DNS A 记录的实例的 IP 地址。

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

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

在 Cloud Shell 中,为虚拟机 proxy-vm.demo.com 创建记录集,确保根据您环境的输出更新 IP 地址。

gcloud dns --project=$projectid record-sets create proxy-vm.demo.com. --zone="private-dns-codelab" --type="A" --ttl="300" --rrdatas="10.10.10.2"

创建云防火墙规则以允许从 PSC 接口进行访问

在下一部分中,创建一个防火墙规则,以允许源自 PSC 网络连接的流量访问使用方 VPC 中的 proxy-vm。

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

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

8. 创建 Jupyter 笔记本

以下部分将指导您创建 Jupyter 笔记本。此笔记本将用于部署以明确代理为目标的 Agent Engine,以实现互联网出站流量。

创建用户管理的托管式服务账号

在下一部分中,您将创建一个服务账号,该账号将与本教程中使用的 Vertex AI Workbench 实例相关联。

在本教程中,服务账号将具有以下角色:

在 Cloud Shell 中,创建服务账号。

gcloud iam service-accounts create notebook-sa \
    --display-name="notebook-sa"

在 Cloud Shell 中,使用 Storage Admin 角色更新服务账号。

gcloud projects add-iam-policy-binding $projectid --member="serviceAccount:notebook-sa@$projectid.iam.gserviceaccount.com" --role="roles/storage.admin"

在 Cloud Shell 中,使用 Vertex AI User 角色更新服务账号。

gcloud projects add-iam-policy-binding $projectid --member="serviceAccount:notebook-sa@$projectid.iam.gserviceaccount.com" --role="roles/aiplatform.user"

在 Cloud Shell 中,使用 Artifact Registry Admin 角色更新服务账号。

gcloud projects add-iam-policy-binding $projectid --member="serviceAccount:notebook-sa@$projectid.iam.gserviceaccount.com" --role="roles/artifactregistry.admin"

在 Cloud Shell 中,允许笔记本服务账号使用 Compute Engine 默认服务账号。

gcloud iam service-accounts add-iam-policy-binding \
    $(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')-compute@developer.gserviceaccount.com \
    --member="serviceAccount:notebook-sa@$projectid.iam.gserviceaccount.com" \
    --role="roles/iam.serviceAccountUser"

9. 更新显式代理

在下一部分中,您需要通过 SSH 连接到显式代理,更新 tinyproxy.conf 配置文件,然后执行重置。

从 Cloud Shell

gcloud compute ssh --zone us-central1-a "proxy-vm" --tunnel-through-iap --project $projectid

打开 tinyproxy 配置文件,使用您选择的编辑器进行更新。以下是使用 VIM 的示例。

sudo vim /etc/tinyproxy/tinyproxy.conf

# Locate the "Listen" configuration line to restrict listening to only its private IP address of the Proxy-VM, rather than all interfaces. 

Listen 10.10.10.2

# Locate the "Allow" configuration line to allow requests ONLY from the PSC Network Attachment Subnet

Allow 192.168.10.0/24

Save the configs by the following steps:
1. Press the `ESC` key to enter Command Mode.
2. Type `:wq` to save (w) and quit (q).
3. Press `Enter`

Restart the tinyproxy service to apply the changes:
sudo systemctl restart tinyproxy

Validate the tinyproxy service is running:
sudo systemctl status tinyproxy

Perform an exit returning to cloud shell
exit

10. 创建 Vertex AI Workbench 实例

在下一部分中,创建一个包含之前创建的服务账号 notebook-sa 的 Vertex AI Workbench 实例。

在 Cloud Shell 中,创建 private-client 实例。

gcloud workbench instances create workbench-tutorial --vm-image-project=cloud-notebooks-managed --vm-image-family=workbench-instances --machine-type=n1-standard-4 --location=us-central1-a --subnet-region=us-central1 --subnet=rfc1918-subnet1 --disable-public-ip --shielded-secure-boot=true --shielded-integrity-monitoring=true --shielded-vtpm=true --service-account-email=notebook-sa@$projectid.iam.gserviceaccount.com

11. Vertex AI Service Agent 更新

Vertex AI 会代表您执行操作,例如从用于创建 PSC 接口的 PSC 网络连接子网获取 IP 地址。为此,Vertex AI 使用需要 Network Admin 权限的服务代理(如下所示):

service-$projectnumber@gcp-sa-aiplatform.iam.gserviceaccount.com

在 Cloud Shell 中,获取您的项目编号。

gcloud projects describe $projectid | grep projectNumber

在 Cloud Shell 中,设置您的项目编号。

projectnumber=YOUR-PROJECT-Number

在 Cloud Shell 中,为 AI Platform 创建服务账号。如果您的项目中有现有服务账号,请跳过此步骤。

gcloud beta services identity create --service=aiplatform.googleapis.com --project=$projectnumber

在 Cloud Shell 中,使用角色 compute.networkAdmin 更新服务代理账号。

gcloud