Agent Engine PSC Explicit Proxy

1. 簡介

Private Service Connect 介面是一種資源,可讓供應商虛擬私有雲 (VPC) 網路啟動與用戶虛擬私有雲網路中各種目的地的連線。供應商和用戶網路可以位於不同專案和機構中。

如果網路連結接受來自 Private Service Connect 介面的連線,Google Cloud 會從網路連結指定的消費者子網路,為介面分配 IP 位址。消費者和生產者網路已連線,可使用內部 IP 位址通訊。

網路連結與 Private Service Connect 介面之間的連線,類似於 Private Service Connect 端點與服務連結之間的連線,但有兩項主要差異:

  • 網路連結可讓供應商網路啟動與消費者網路的連線 (代管服務輸出),而端點則可讓消費者網路啟動與供應商網路的連線 (代管服務輸入)。
  • Private Service Connect 介面連線是可遞移的。也就是說,生產端網路可以與連線至消費端網路的其他網路通訊。

Vertex AI PSC 介面可連線性考量

  • PSC 介面可將流量轉送至 RFC1918 位址區塊內的 VPC 或內部部署目的地。
  • 如果 PSC 介面指定非 RFC-1918 位址區塊,則必須在消費者 VPC 中部署具有 RFC-1918 位址的明確 Proxy。在 Vertex AI 部署作業中,必須定義 Proxy,以及目標端點的 FQDN。
  • 如果您只使用 PSC 介面設定部署作業,系統會保留預設的網際網路存取權。這類傳出流量會直接從 Google 代管的安全租戶網路輸出。

Vertex AI PSC 介面 VPC-SC 注意事項

  • 如果專案屬於 VPC Service Controls 範圍,範圍會封鎖 Google 代管的租戶預設網際網路存取權,防止資料外洩。
  • 如要允許部署作業在此情境中存取公開網際網路,您必須明確設定安全輸出路徑,透過 VPC 傳送流量。
  • 建議您在 VPC 邊界內設定具有 RFC1918 位址的 Proxy 伺服器,並建立 Cloud NAT 閘道,允許 Proxy VM 存取網際網路。

如需更多資訊,請參閱下列資源:

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

為 Vertex AI 資源設定 Private Service Connect 介面 | Google Cloud

建構項目

在本教學課程中,您將建構透過 Private Service Connect (PSC) 介面部署的完整 Agent Engine,以便透過部署在消費者虛擬私有雲的 Proxy VM (具有 RFC1918 位址),連線至公開網站 (https://api.frankfurter.app/)。這個範例部署作業適用於啟用 VPC-SC 的專案,或適用於需要透過客戶網路 (而非租戶虛擬私有雲) 傳輸網際網路輸出流量的管理員。

圖 1

f42f2db921f6d5af.png

您會在用戶端 VPC 中建立單一 psc-network-attachment,利用 DNS 對接來解析代管 Agent Engine 的租戶專案中的用戶端網路 proxy-vm,進而實現下列用途:

部署 Agent Engine,並設定 Proxy VM 做為明確的 Proxy,允許其連線至公開網址 https://api.frankfurter.app

課程內容

  • 如何建立網路連結
  • 生產者如何使用網路連結建立 PSC 介面
  • 如何使用 DNS 對接,建立從生產端到消費端的通訊
  • 如何部署及使用 Proxy VM 進行網際網路輸出

軟硬體需求

Google Cloud 專案

IAM 權限

2. 事前準備

更新專案以支援教學課程

本教學課程會使用 $variables,協助您在 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. 消費者設定

建立 Consumer VPC

這個虛擬私有雲位於客戶專案中。這個虛擬私有雲中會建立下列資源

  • 消費者子網路
  • 網路連結子網路
  • Cloud Router (Cloud NAT 必備)
  • Cloud NAT

在 Cloud Shell 中執行下列操作:

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

建立消費者子網路

在 Cloud Shell 中,為 Proxy VM 建立子網路:

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 位址的 Proxy VM 提供網際網路存取權。有了 Cloud NAT,只有私人 IP 位址的 VM 也能連上網際網路,執行安裝軟體套件等工作。

在 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 連線至您的 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

5. 建立消費者 VM 執行個體

在 Cloud Shell 中建立用戶端 VM 執行個體 proxy-vm,做為 Agent Engine 的明確 Proxy。我們會使用 tinyproxy 做為 Proxy 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 區域,並填入指向 proxy-vm IP 位址的 A 記錄。稍後,系統會在 Agent Engine 中部署 DNS 對等互連,允許存取消費者的 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 中,對 VM 執行個體執行說明。

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

在 Cloud Shell 中,為 VM (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"

建立 Cloud Firewall 規則,允許從 PSC 介面存取

在下一節中,請建立防火牆規則,允許來自 PSC 網路附件的流量存取 Consumer 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 Notebook

下一節將引導您建立 Jupyter Notebook。這個筆記本將用於部署 Agent Engine,以明確的 Proxy 做為網際網路輸出目標。

建立使用者管理的服務帳戶

在下一節中,您將建立與本教學課程所用 Vertex AI Workbench 執行個體相關聯的服務帳戶。

在本教學課程中,服務帳戶會套用下列角色:

在 Cloud Shell 中建立服務帳戶。

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

在 Cloud Shell 中,將服務帳戶更新為 Storage 管理員角色。

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

在 Cloud Shell 中,使用 Vertex AI 使用者角色更新服務帳戶。

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

在 Cloud Shell 中,更新服務帳戶,並指派 Artifact Registry 管理員角色。

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. 更新明確 Proxy

在下一節中,您需要透過 SSH 連線至明確的 Proxy,更新 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