1. 簡介
運用 Private Service Connect,為從 Vertex AI Model Garden 部署的模型建立安全私密存取權。這個方法不會公開端點,而是將模型部署至私人 Vertex AI 端點,僅限虛擬私有雲 (VPC) 內存取。
Private Service Connect 會在虛擬私有雲中建立具有內部 IP 位址的端點,直接連線至 Google 管理的 Vertex AI 服務,該服務會代管您的模型。這項功能可讓虛擬私有雲和地端環境中的應用程式 (透過 Cloud VPN 或 Interconnect) 使用私人 IP 傳送推論要求。所有網路流量都會留在 Google 的網路中,因此可提升安全性、減少延遲,並完全隔離模型的服務端點與公開網際網路。

建構項目
在本教學課程中,您將從 Model Garden 下載 Gemma 3,並以 Private Service Connect 可存取的私人端點形式,將其代管於 Vertex AI Online Inference。端對端設定包括:
- Model Garden 模型:您將從 Vertex AI Model Garden 選取 Gemma 3,並部署至 Private Service Connect 端點。
- Private Service Connect:您會在虛擬私有雲 (VPC) 中設定消費者端點,其中包含您自有網路中的內部 IP 位址。
- 安全連線至 Vertex AI:PSC 端點會以 Vertex AI 為私有模型部署自動產生的服務附件為目標。這會建立私人連線,確保虛擬私有雲和模型服務端點之間的流量不會經過公用網際網路。
- 虛擬私有雲中的用戶端設定:您將設定用戶端 (例如 Compute Engine VM),透過 PSC 端點的內部 IP 位址,將推論要求傳送至已部署的模型。
完成後,您將擁有可正常運作的範例,以私密方式提供 Model Garden 模型,且只能從指定的虛擬私有雲網路存取。
課程內容
在本教學課程中,您將瞭解如何從 Vertex AI Model Garden 部署模型,並使用 Private Service Connect (PSC),從虛擬私有雲 (VPC) 安全存取模型。透過這個方法,虛擬私有雲 (消費者) 內的應用程式就能以私密方式連線至 Vertex AI 模型端點 (生產者服務),不必經過公開網際網路。
具體來說,您將學會:
- 瞭解 Vertex AI 的 PSC:瞭解 PSC 如何建立私密安全的消費者與生產者連線。您的 VPC 可以使用內部 IP 位址存取已部署的 Model Garden 模型。
- 使用私人存取權部署模型:瞭解如何為 Model Garden 模型設定 Vertex AI 端點,以使用 PSC,將其設為私人端點。
- 服務附件的角色:將模型部署至私有 Vertex AI 端點時,Google Cloud 會在 Google 管理的租戶專案中自動建立服務附件。這項服務附件會將模型服務服務公開給消費者網路。
- 在虛擬私有雲中建立 PSC 端點:
- 如何從已部署的 Vertex AI 端點詳細資料取得專屬服務附件 URI。
- 如何保留虛擬私有雲中選定子網路的內部 IP 位址。
- 如何在虛擬私有雲中建立轉送規則,做為 PSC 端點,並以 Vertex AI 服務連結為目標。這個端點可透過保留的內部 IP 存取模型。
- 建立私人連線:虛擬私有雲中的 PSC 端點如何連線至服務附件,安全地將您的網路與 Vertex AI 服務橋接。
- 私下傳送推論要求:如何從虛擬私有雲中的資源 (例如 Compute Engine VM) 將預測要求傳送至 PSC 端點的內部 IP 位址。
- 驗證:測試並確認您可以透過私有連線,從虛擬私有雲成功將推論要求傳送至已部署的 Model Garden 模型。
完成這項操作後,您就能從模型園地代管模型,且只能透過私人網路基礎架構存取。
軟硬體需求
Google Cloud 專案
IAM 權限
- AI Platform 管理員 (roles/ml.Admin)
- Compute 網路管理員 (roles/compute.networkAdmin)
- Compute 執行個體管理員 (roles/compute.instanceAdmin)
- Compute 安全性管理員 (roles/compute.securityAdmin)
- DNS 管理員 (roles/dns.admin)
- 受 IAP 保護的通道使用者 (roles/iap.tunnelResourceAccessor)
- 記錄管理員 (roles/logging.admin)
- 筆記本管理員 (roles/notebooks.admin)
- 專案 IAM 管理員 (roles/resourcemanager.projectIamAdmin)
- 服務帳戶管理員 (roles/iam.serviceAccountAdmin)
- 服務使用情形管理員 (roles/serviceusage.serviceUsageAdmin)
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"
3. 部署模型
請按照下列步驟,從 Model Garden 部署模型:
在 Google Cloud 控制台中,前往 Model Garden,然後搜尋並選取 Gemma 3

按一下「部署選項」,然後選取「Vertex AI」

在「Deploy on Vertex AI」窗格中,選取「Advanced」。系統會根據可用容量預先填入區域和機器規格。您可以變更這些值,但本程式碼研究室是為 us-central1 量身打造。

在「Deploy on Vertex AI」窗格中,確認「Endpoint Access」已設為「Private Service Connect」,然後選取專案。

其他選項均保留預設值,然後選取底部的「Deploy」,並查看通知中的部署狀態。

在 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. 消費者設定
建立消費者虛擬私有雲
在 Cloud Shell 中執行下列操作:
gcloud compute networks create consumer-vpc --project=$projectid --subnet-mode=custom
建立消費者 VM 子網路
在 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 連線至您的 VM 執行個體,請根據以下條件建立防火牆規則:
- 套用至所有您希望能透過 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. 建立消費者 VM 執行個體
在 Cloud Shell 中,建立取用者 VM 執行個體 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 端點
消費者會在虛擬私有雲中建立具有內部 IP 位址的消費者端點 (轉送規則)。這個 PSC 端點的目標是生產者的服務連結。消費者虛擬私有雲或混合式網路中的用戶端可以將流量傳送至這個內部 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. 從消費者 VM 進行測試
在 Cloud Shell 中執行下列步驟,授予 Consumer VM 存取 Vertex Model Garden API 的權限
透過 SSH 連線至 Consumer VM

使用應用程式預設憑證重新驗證,並指定 Vertex AI 範圍。
gcloud auth application-default login
--scopes=https://www.googleapis.com/auth/cloud-platform
使用下表產生 CURL 指令,並根據環境調整
屬性 | 值 |
通訊協定 | 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. 恭喜
恭喜!您已成功設定並驗證私人存取權,可透過 Private Service Connect 端點存取 Vertex AI Prediction 上代管的 Gemma 3 API。
您已建立消費者基礎架構,包括預留內部 IP 位址,以及在虛擬私有雲中設定 Private Service Connect 端點 (轉送規則)。這個端點會以與已部署的 Gemma 3 模型相關聯的服務附件為目標,安全地連線至 Vertex AI 服務。完成這項設定後,VPC 或連線網路中的應用程式就能使用內部 IP 位址,以私密且安全的方式與 Gemma 3 API 互動,不必透過公開網際網路傳輸流量。