1. 简介
利用 Private Service Connect 为从 Vertex AI Model Garden 部署的模型建立安全、私密的访问通道。此方法不会公开公共端点,而是允许您将模型部署到仅可在虚拟私有云 (VPC) 中访问的专用 Vertex AI 端点。
Private Service Connect 会在您的 VPC 内创建一个具有内部 IP 地址的端点,该端点直接连接到托管模型的 Google 管理的 Vertex AI 服务。这样一来,VPC 和本地环境中的应用(通过 Cloud VPN 或 Interconnect)便可以使用专用 IP 发送推理请求。所有网络流量都保留在 Google 的网络中,这有助于提高安全性、缩短延迟时间,并完全隔离模型的服务端点与公共互联网。

构建内容
在本教程中,您将从 Model Garden 下载 Gemma 3,该模型托管在 Vertex AI 在线推理中,作为可通过 Private Service Connect 访问的专用端点。端到端设置将包括:
- Model Garden 模型:您将从 Vertex AI Model Garden 中选择 Gemma 3,并将其部署到 Private Service Connect 端点。
- Private Service Connect:您将在自己的虚拟私有云 (VPC) 中配置一个使用内部 IP 地址的使用方端点。
- 与 Vertex AI 的安全连接:PSC 端点将以 Vertex AI 为您的私有模型部署自动生成的服务连接为目标。这样一来,系统会建立专用连接,确保 VPC 与模型服务端点之间的流量不会通过公共互联网。
- VPC 中的客户端配置:您将设置一个客户端(例如,Compute Engine 虚拟机)使用 PSC 端点的内部 IP 地址向已部署的模型发送推理请求。
最后,您将获得一个可正常运行的示例,其中 Model Garden 模型以不公开方式提供,只能从您指定的 VPC 网络内访问。
学习内容
在本教程中,您将学习如何部署 Vertex AI Model Garden 中的模型,并使用 Private Service Connect (PSC) 从 Virtual Private Cloud (VPC) 安全地访问该模型。借助此方法,VPC(使用方)中的应用可以私密地连接到 Vertex AI 模型端点(提供方服务),而无需遍历公共互联网。
具体而言,您将学习:
- 了解 Vertex AI 的 PSC:PSC 如何实现私密且安全的使用方到提供方连接。您的 VPC 可以使用内部 IP 地址访问已部署的 Model Garden 模型。
- 部署具有私密访问权限的模型:如何为 Model Garden 模型配置 Vertex AI 端点以使用 PSC,从而使其成为专用端点。
- 服务连接的角色:当您将模型部署到私有 Vertex AI 端点时,Google Cloud 会在 Google 管理的租户项目中自动创建服务连接。此服务连接会将模型服务服务公开给使用方网络。
- 在 VPC 中创建 PSC 端点:
- 如何从已部署的 Vertex AI 端点详细信息中获取唯一的服务连接 URI。
- 如何在 VPC 中所选的子网内预留内部 IP 地址。
- 如何在 VPC 中创建充当 PSC 端点的转发规则,以 Vertex AI 服务连接为目标。此端点通过预留的内部 IP 地址使模型可供访问。
- 建立专用连接:VPC 中的 PSC 端点如何连接到服务连接,从而安全地将您的网络与 Vertex AI 服务桥接起来。
- 以私密方式发送推理请求:如何从 VPC 内的资源(例如 Compute Engine 虚拟机)向 PSC 端点的内部 IP 地址发送预测请求。
- 验证:用于测试和确认您可以通过专用连接成功将推理请求从 VPC 发送到已部署的 Model Garden 模型的步骤。
完成此操作后,您将能够托管 Model Garden 中的模型,这些模型只能从您的专用网络基础架构访问。
所需条件
Google Cloud 项目
IAM 权限
- AI Platform Admin (roles/ml.Admin)
- Compute Network Admin (roles/compute.networkAdmin)
- Compute Instance Admin (roles/compute.instanceAdmin)
- Compute Security Admin (roles/compute.securityAdmin)
- DNS Administrator (roles/dns.admin)
- IAP-secured Tunnel User (roles/iap.tunnelResourceAccessor)
- Logging Admin (roles/logging.admin)
- Notebooks Admin (roles/notebooks.admin)
- Project IAM Admin (roles/resourcemanager.projectIamAdmin)
- Service Account Admin (roles/iam.serviceAccountAdmin)
- Service Usage Admin (roles/serviceusage.serviceUsageAdmin)
2. 准备工作
更新项目以支持教程
本教程使用 $变量来帮助在 Cloud Shell 中实现 gcloud 配置。
在 Cloud Shell 中,执行以下操作:
gcloud config list project
gcloud config set project [YOUR-PROJECT-ID]
projectid=[YOUR-PROJECT-ID]
echo $projectid
API 启用
在 Cloud Shell 中,执行以下操作:
gcloud services enable "compute.googleapis.com"
gcloud services enable "aiplatform.googleapis.com"
gcloud services enable "serviceusage.googleapis.com"
3. 部署模型
按照以下步骤从 Model Garden 部署模型
在 Google Cloud 控制台中,前往 Model Garden,然后搜索并选择 Gemma 3

点击“部署选项”,然后选择“Vertex AI”

在“在 Vertex AI 上部署”窗格中,选择“高级”。预填充的区域和机器配置是根据可用容量选择的。您可以更改这些值,但此 Codelab 专为 us-central1 区域量身打造。

在“在 Vertex AI 上部署”窗格中,确保“端点访问权限”配置为“Private Service Connect”,然后选择您的项目。

将其他选项都保留为默认值,然后选择底部的“部署”,并查看通知中的部署状态。

在 Model Garden 中,选择提供 Gemma 3 模型和端点的区域 us-central1。模型部署大约需要 5 分钟。

30 分钟后,端点将在完成后转换为“有效”状态

选择端点,获取并记下端点 ID。

打开 Cloud Shell 并执行以下操作,以获取 Private Service Connect 服务连接 URI。使用方在部署 PSC 使用方端点时会使用此 URI 字符串。
在 Cloud Shell 中,更新端点 ID,然后发出以下命令。
gcloud ai endpoints describe [Endpoint ID] --region=us-central1 | grep -i serviceAttachment:
具体流程请参阅以下示例:
user@cloudshell:$ gcloud ai endpoints describe 2124795225560842240 --region=us-central1 | grep -i serviceAttachment:
Using endpoint [https://us-central1-aiplatform.googleapis.com/]
serviceAttachment: projects/o9457b320a852208e-tp/regions/us-central1/serviceAttachments/gkedpm-52065579567eaf39bfe24f25f7981d
将 serviceAttachment 之后的内容复制到一个名为“Service_attachment”的变量中,您稍后在创建 PSC 连接时需要用到该变量。
user@cloudshell:$ Service_attachment=projects/o9457b320a852208e-tp/regions/us-central1/serviceAttachments/gkedpm-52065579567eaf39bfe24f25f7981d
4. 使用方设置
创建使用方 VPC
在 Cloud Shell 中,执行以下操作:
gcloud compute networks create consumer-vpc --project=$projectid --subnet-mode=custom
创建使用方虚拟机子网
在 Cloud Shell 中,执行以下操作:
gcloud compute networks subnets create consumer-vm-subnet --project=$projectid --range=192.168.1.0/24 --network=consumer-vpc --region=us-central1 --enable-private-ip-google-access
创建 PSC 端点子网
gcloud compute networks subnets create pscendpoint-subnet --project=$projectid --range=10.10.10.0/28 --network=consumer-vpc --region=us-central1
5. 启用 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
6. 创建使用方虚拟机实例
在 Cloud Shell 中,创建消费方虚拟机实例 consumer-vm。
gcloud compute instances create consumer-vm \
--project=$projectid \
--machine-type=e2-micro \
--image-family debian-11 \
--no-address \
--shielded-secure-boot \
--image-project debian-cloud \
--zone us-central1-a \
--subnet=consumer-vm-subnet
7. Private Service Connect 端点
使用方在其 VPC 内创建具有内部 IP 地址的使用方端点(转发规则)。此 PSC 端点以提供方的服务连接为目标。使用方 VPC 或混合网络中的客户端可以将流量发送到此内部 IP 地址,以访问提供方的服务。
为个人用户端点预留 IP 地址。
在 Cloud Shell 中,创建转发规则。
gcloud compute addresses create psc-address \
--project=$projectid \
--region=us-central1 \
--subnet=pscendpoint-subnet \
--addresses=10.10.10.6
验证 IP 地址是否已预留
在 Cloud Shell 中,列出预留的 IP 地址。
gcloud compute addresses list
您应该会看到 10.10.10.6 IP 地址已预留。

通过指定您在上一步(“部署模型”部分)中捕获的服务连接 URI (target-service-attachment) 来创建使用方端点。
在 Cloud Shell 中,描述网络连接。
gcloud compute forwarding-rules create psc-consumer-ep \
--network=consumer-vpc \
--address=psc-address \
--region=us-central1 \
--target-service-attachment=$Service_attachment \
--project=$projectid
验证服务连接是否接受端点
gcloud compute forwarding-rules describe psc-consumer-ep \
--project=$projectid \
--region=us-central1 \
在响应中,验证 pscConnectionStatus 字段是否显示“ACCEPTED”状态

8. 从 Consumer 虚拟机进行测试
在 Cloud Shell 中,执行以下步骤,以向 Consumer 虚拟机授予访问 Vertex Model Garden API 的权限
通过 SSH 连接到 Consumer 虚拟机

使用应用默认凭据重新进行身份验证,并指定 Vertex AI 范围。
gcloud auth application-default login
--scopes=https://www.googleapis.com/auth/cloud-platform
使用下表生成 C网址 命令,并根据您的环境进行调整
属性 | 值 |
协议 | HTTP |
位置 | us-central1 |
在线预测端点 | 2133539641536544768 |
项目 ID | test4-473419 |
型号 | gemma-3-12b-it |
Private Service Connect 端点 IP | 10.10.10.6 |
消息 | [{"role": "user","content": "What weighs more 1 pound of feathers or rocks?"}] |
根据您的环境详细信息更新并执行 curl 命令:
curl -k -v -X POST -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" -H "Content-Type: application/json" http://[PSC-IP]/v1/projects/[Project-ID]/locations/us-central1/endpoints/[Predictions Endpoint]/chat/completions -d '{"model": "google/gemma-3-12b-it", "messages": [{"role": "user","content": "What weighs more 1 pound of feathers or rocks?"}] }'
示例:
curl -k -v -X POST -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" -H "Content-Type: application/json" http://10.10.10.6/v1/projects/test4-473419/locations/us-central1/endpoints/2133539641536544768/chat/completions -d '{"model": "google/gemma-3-12b-it", "messages": [{"role": "user","content": "What weighs more 1 pound of feathers or rocks?"}] }'
最终结果 - 成功!
您应该会在输出底部看到来自 Gemma 3 的预测结果,这表明您能够通过 PSC 端点私密地访问 API 端点
Connection #0 to host 10.10.10.6 left intact
{"id":"chatcmpl-9e941821-65b3-44e4-876c-37d81baf62e0","object":"chat.completion","created":1759009221,"model":"google/gemma-3-12b-it","choices":[{"index":0,"message":{"role":"assistant","reasoning_content":null,"content":"This is a classic trick question! They weigh the same. One pound is one pound, regardless of the material. 😊\n\n\n\n","tool_calls":[]},"logprobs":null,"finish_reason":"stop","stop_reason":106}],"usage":{"prompt_tokens":20,"total_tokens":46,"completion_tokens":26,"prompt_tokens_details":null},"prompt_logprobs":null
9. 清理
从 Cloud Shell 中删除教程组件。
gcloud ai endpoints undeploy-model ENDPOINT_ID --deployed-model-id=DEPLOYED_MODEL_ID --region=us-central1 --quiet
gcloud ai endpoints delete $ENDPOINT_ID --project=$projectid --region=us-central1 --quiet
gcloud ai models delete $MODEL_ID --project=$projectid --region=us-central1 --quiet
gcloud compute instances delete consumer-vm --zone=us-central1-a --quiet
gcloud compute forwarding-rules delete psc-consumer-ep --region=us-central1 --project=$projectid --quiet
gcloud compute addresses delete psc-address --region=us-central1 --project=$projectid --quiet
gcloud compute networks subnets delete pscendpoint-subnet consumer-vm-subnet --region=us-central1 --quiet
gcloud compute firewall-rules delete ssh-iap-consumer --project=$projectid
gcloud compute networks delete consumer-vpc --project=$projectid --quiet
gcloud projects delete $projectid --quiet
10. 恭喜
恭喜!您已成功配置并验证了对 Vertex AI Prediction 上托管的 Gemma 3 API 的专用访问,所用的是 Private Service Connect 端点。
您已创建使用方基础架构,包括在 VPC 中预留内部 IP 地址和配置 Private Service Connect 端点(转发规则)。此端点通过定位与已部署的 Gemma 3 模型关联的服务连接,安全地连接到 Vertex AI 服务。此设置允许 VPC 或连接的网络中的应用使用内部 IP 地址以私密且安全的方式与 Gemma 3 API 互动,而无需流量通过公共互联网。