使用 TLS 通过 Vertex AI 实现安全的在线预测

1. 简介

利用 Private Service Connect (PSC) 为从 Vertex AI Model Garden 部署的模型建立高度安全、私密的访问通道。此方法不会公开端点,而是允许您将模型部署到仅在虚拟私有云 (VPC) 中可访问的专用 Vertex AI 端点。

Private Service Connect 会在 VPC 内创建一个具有内部 IP 地址的端点,该端点直接连接到托管模型的 Google 管理的 Vertex AI 服务。这样,VPC 和本地环境(通过 Cloud VPN 或 Interconnect)中的应用就可以使用专用 IP 发送推理请求。

至关重要的是,VPC 与专用 Vertex AI 端点之间的所有网络流量都保留在 Google 的专用网络上,与公共互联网完全隔离。此外,此专用连接在传输过程中使用 TLS 加密进行保护。这种端到端加密可确保预测请求和模型响应受到保护,从而提高数据机密性和完整性。通过 PSC 进行网络隔离与 TLS 加密相结合,可为在线预测提供稳健安全的环境,从而减少延迟并显著增强安全状况。

8bdac2f2d3dd88c9.png

构建内容

在本教程中,您将从 Model Garden 下载 Gemma 3,该模型托管在 Vertex AI Online Inference 中,作为可通过 Private Service Connect 访问的专用端点。 端到端设置将包括:

  1. Model Garden 模型:您将从 Vertex AI Model Garden 中选择 Gemma 3,并将其部署到 Private Service Connect 端点。
  2. Private Service Connect:您将在虚拟私有云 (VPC) 中配置一个使用方端点,该端点由您自己的网络中的内部 IP 地址组成。
  3. 与 Vertex AI 的安全连接:PSC 端点将以 Vertex AI 为专用模型部署自动生成的服务连接为目标。这样便会建立专用连接,确保 VPC 与模型部署端点之间的数据流量不会通过公共互联网传输。
  4. VPC 内的客户端配置:您将在 VPC 内设置一个客户端(例如 Compute Engine 虚拟机),以使用 PSC 端点的内部 IP 地址向已部署的模型发送推理请求。
  5. 验证 TLS 加密:在 VPC 内的客户端虚拟机中,您将使用标准工具 ( openssl s_client) 连接到 PSC 端点的内部 IP 地址。通过检查握手详细信息和提供的服务器证书,此步骤将允许您确认与 Vertex AI 服务的通信通道确实使用 TLS 加密。

最后,您将获得一个可正常运行的示例,其中 Model Garden 模型以私密方式提供,只能从指定的 VPC 网络内访问。

学习内容

在本教程中,您将学习如何从 Vertex AI Model Garden 部署模型,并使用 Private Service Connect (PSC) 使其可以从虚拟私有云 (VPC) 安全访问。此方法允许 VPC(使用方)中的应用以私密方式连接到 Vertex AI 模型端点(提供方服务),而无需通过公共互联网传输。

具体而言,您将学习:

  1. 了解 Vertex AI 的 PSC:PSC 如何实现私密且安全的使用方到提供方连接。您的 VPC 可以使用内部 IP 地址访问已部署的 Model Garden 模型。
  2. 部署具有私密访问权限的模型:如何为 Model Garden 模型配置 Vertex AI 端点以使用 PSC,使其成为专用端点。
  3. 服务连接的角色:当您将模型部署到专用 Vertex AI 端点时,Google Cloud 会在 Google 管理的租户项目中自动创建服务连接。此服务连接会将模型部署服务公开给使用方网络。
  4. 在 VPC 中创建 PSC 端点:
  • 如何从已部署的 Vertex AI 端点详细信息中获取唯一服务连接 URI。
  • 如何在 VPC 中所选的子网内预留内部 IP 地址。
  • 如何在 VPC 中创建充当 PSC 端点的转发规则,以 Vertex AI 服务连接为目标。此端点使模型可以通过预留的内部 IP 进行访问。
  1. 建立私密连接:VPC 中的 PSC 端点如何连接到服务连接,从而安全地将您的网络与 Vertex AI 服务桥接起来。
  2. 以私密方式发送推理请求:如何从 VPC 内的资源(例如 Compute Engine 虚拟机)向 PSC 端点的内部 IP 地址发送预测请求。
  3. 验证:测试并确认您可以通过专用连接成功地从 VPC 向已部署的 Model Garden 模型发送推理请求的步骤。
  4. 验证 TLS 加密: 如何使用 VPC 客户端(例如 Compute Engine 虚拟机)中的工具通过 TLS 连接到 PSC 端点的内部 IP 地址。

完成此操作后,您将能够托管 Model Garden 中的模型,这些模型只能从您的专用网络基础架构访问。

所需条件

Google Cloud 项目

IAM 权限

2. 准备工作

更新项目以支持本教程

本教程使用 $variables 来帮助在 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"
gcloud services enable dns.googleapis.com

3. 部署模型

按照以下步骤从 Model Garden 部署模型

在 Google Cloud 控制台中,前往 Model Garden,然后搜索并选择 Gemma 3

47c724030a846b25.png

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

a9a9f4aef7e52356.png

在“部署设置”部分底部选择“修改设置”

d5b449d2893ddf2f.png

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

c679aa90c3638e31.png

保留其他选项的所有默认值,然后选择底部的“部署”,并查看通知以了解部署状态。

d5eab71cbb43e2fe.png

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

fce9d5c9152255f1.png

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

d6cc741415213e1a.png

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

87d1756fa534619d.png

选择端点以检索其端点 ID 并更新变量。在显示的示例中,ID 为 1934769929467199488。

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

endpointID=<Enter_Your_Endpoint_ID>
region=us-central1

执行以下操作以获取 Private Service Connect 服务连接 URI。使用方在部署 PSC 使用方端点时会使用此 URI 字符串。

在 Cloud Shell 中,使用端点 ID/区域变量,然后发出以下命令:

gcloud ai endpoints describe $endpointID --region=$region  | grep -i serviceAttachment:

具体流程请参阅以下示例:

user@cloudshell:$ gcloud ai endpoints describe 1934769929467199488 --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=<Enter_Your_ServiceAttachment>

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=$region --enable-private-ip-google-access

创建 PSC 端点子网,在 Cloud Shell 中,执行以下操作**:**

gcloud compute networks subnets create pscendpoint-subnet --project=$projectid --range=10.10.10.0/28 --network=consumer-vpc --region=$region

5. 启用 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

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=$region \
    --subnet=pscendpoint-subnet \
    --addresses=10.10.10.6

验证 IP 地址是否已预留。

在 Cloud Shell 中,列出预留的 IP 地址。

gcloud compute addresses list 

您应该会看到预留的 10.10.10.6 IP 地址。

3029e97ddc9c192d.png

通过指定您在上一步(“部署模型”部分)中捕获的服务连接 URI target-service-attachment 来创建使用方端点。

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

 gcloud compute forwarding-rules create psc-consumer-ep \
    --network=consumer-vpc \
    --address=psc-address \
    --region=$region \
    --target-service-attachment=$Service_attachment \
    --project=$projectid

验证服务连接是否接受端点。

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

gcloud compute forwarding-rules describe psc-consumer-ep \
    --project=$projectid \
    --region=$region

在响应中,验证 pscConnectionStatus 字段是否显示“ACCEPTED”状态

e1debc2bdb6a1a44.png

8. 设置通过 TLS 连接到 Vertex HTTPS 端点

创建 DNS 专用区域,以便您无需指定 IP 地址即可获取在线推理。

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

DNS_NAME_SUFFIX="prediction.p.vertexai.goog."  

gcloud dns managed-zones create vertex \
--project=$projectid \
--dns-name=$DNS_NAME_SUFFIX \
--networks=consumer-vpc \
--visibility=private \
--description="A DNS zone for Vertex AI endpoints using Private Service Connect."

创建 A 记录以将网域映射到 PSC IP 地址。

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

gcloud dns record-sets create "*.prediction.p.vertexai.goog." \
  --zone=vertex \
  --type=A \
  --ttl=300 \
  --rrdatas="10.10.10.6"

创建 Cloud Router 实例作为 NAT 实例的前提条件。

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

gcloud compute routers create consumer-cr \
--region=$region --network=consumer-vpc \
--asn=65001 

创建用于下载 openssl 和 dnsutils 软件包的 Cloud NAT 实例。

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

gcloud compute routers nats create consumer-nat-gw \
  --router=consumer-cr \
  --region=$region \
  --nat-all-subnet-ip-ranges \
  --auto-allocate-nat-external-ips 

通过 SSH(控制台)连接到使用方虚拟机。在 Cloud Shell 中,执行以下操作:

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

更新以下软件包,安装 open-ssl 并安装 DNS 实用程序

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

sudo apt update 
sudo apt install openssl
sudo apt-get install -y dnsutils

您需要在下一步中提供项目编号。如需获取项目编号,请从 Cloud Shell 运行以下命令,并将其放入变量中:

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

gcloud projects describe $projectid --format="value(projectNumber)"
Example Output: 549538389202
projectNumber=549538389202

您需要在接下来的几个步骤中定义一些其他变量,首先从 Cloud Shell 中捕获这些变量(ENDPOINT_IDREGIONVERTEX_AI_PROJECT_ID),然后在虚拟机中创建相同的变量。

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

echo $projectNumber
echo $projectid 
echo $region
echo $endpointID

以下是输出示例:

549538389202
test4-473419
Us-central1
1934769929467199s

在您的使用方虚拟机中添加这些变量 - 以下是示例:

projectNumber=1934769929467199488  
projectid=test4-473419
region=us-central1
endpointID=1934769929467199488

通过在虚拟机的主目录中执行以下命令来下载 Vertex AI 证书。此命令会创建一个名为 vertex_certificate.crt 的文件。

sudo openssl s_client -showcerts -connect $endpointID-$region-$projectNumber.prediction.p.vertexai.goog:443 </dev/null | openssl x509 -outform pem -out vertex_certificate.crt

Output should look like below, error is expected.

2f2899141dde1af2.png

将证书移至系统受信任证书存储区。

sudo mv vertex_certificate.crt /usr/local/share/ca-certificates

更新证书管理器。

sudo update-ca-certificates

更新后应如下所示。

user@linux-vm:~$ sudo update-ca-certificates

Updating certificates in /etc/ssl/certs...

1 added, 0 removed; done.

Running hooks in /etc/ca-certificates/update.d...

Done.

9. 从使用方虚拟机进行最终测试

在使用方虚拟机上,使用应用默认凭证重新进行身份验证,并指定 Vertex AI 范围:

gcloud auth application-default login
--scopes=https://www.googleapis.com/auth/cloud-platform 

在使用方虚拟机中运行以下 curl 命令,以使用提示“What weighs more 1 pound of feathers or rocks?”测试对 Gemini 模型的预测

curl -v -X POST -H "Authorization: Bearer $(gcloud auth application-default print-access-token)"   -H "Content-Type: application/json" https://$endpointID-$region-$projectNumber.prediction.p.vertexai.goog/v1/projects/$projectid/locations/$region/endpoints/$endpointID/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

10. 清理

从 Cloud Shell 中,删除本教程的组件。

Get Deployed Model ID first with this command, you will need it to delete the Endpoint ID:

gcloud ai endpoints describe $endpointID \
  --region=$region \
  --project=$projectid \
  --format="table[no-heading](deployedModels.id)"

Example Output: 7389140900875599872

Put it in a Variable:

deployedModelID=7389140900875599872

Run following Commands:

gcloud ai endpoints undeploy-model $endpointID --deployed-model-id=$deployedModelID --region=$region --quiet

gcloud ai endpoints delete $endpointID --project=$projectid --region=$region --quiet

Run Following command to get $MODEL_ID to delete Model:

gcloud ai models list --project=$projectid --region=$region

Example Output:

Using endpoint [https://us-central1-aiplatform.googleapis.com/]
MODEL_ID: gemma-3-12b-it-1768409471942
DISPLAY_NAME: gemma-3-12b-it-1768409471942

Put MODEL_ID value in a variable:

MODEL_ID=gemma-3-12b-it-1768409471942

Run the follow command to delete Model:

gcloud ai models delete $MODEL_ID --project=$projectid --region=$region --quiet

Clean up rest of the lab:
gcloud compute instances delete consumer-vm --zone=us-central1-a --quiet

gcloud compute forwarding-rules delete psc-consumer-ep --region=$region --project=$projectid --quiet

gcloud compute addresses delete psc-address --region=$region --project=$projectid --quiet

gcloud compute networks subnets delete pscendpoint-subnet consumer-vm-subnet --region=$region --quiet

gcloud compute firewall-rules delete ssh-iap-consumer --project=$projectid

gcloud compute routers delete consumer-cr --region=$region

gcloud compute networks delete consumer-vpc --project=$projectid --quiet

11. 恭喜

恭喜!您已成功配置并验证对 Vertex AI Prediction 上托管的 Gemma 3 API 的私密访问权限,方法是使用从 Vertex AI 获取并部署到虚拟机受信任证书存储区的自签名证书的 Private Service Connect 端点。

您创建了使用方基础架构,包括预留内部 IP 地址、在 VPC 中配置 Private Service Connect 端点(转发规则)和专用 DNS,以匹配自签名证书 *prediction.p.vertexai.goog。此端点通过以与已部署的 Gemma 3 模型关联的服务连接为目标,安全地连接到 Vertex AI 服务。

此设置可确保 VPC 或连接的网络中的应用可以使用证书通过内部 IP 地址以私密方式与 Gemma 3 API 交互。所有流量都保留在 Google 的网络中,绝不会通过公共互联网传输。

后续操作

延伸阅读和视频

参考文档