1. บทนำ
อินเทอร์เฟซ Private Service Connect คือทรัพยากรที่ช่วยให้เครือข่าย Virtual Private Cloud (VPC) ของผู้ผลิตเริ่มการเชื่อมต่อกับปลายทางต่างๆ ในเครือข่ายของผู้บริโภคได้ เครือข่ายผู้ผลิตและเครือข่ายผู้บริโภคอาจอยู่ในโปรเจ็กต์และองค์กรที่แตกต่างกัน
การเชื่อมต่อระหว่างไฟล์แนบเครือข่ายกับอินเทอร์เฟซ Private Service Connect คล้ายกับการเชื่อมต่อระหว่างปลายทาง Private Service Connect กับไฟล์แนบบริการ แต่มีข้อแตกต่างที่สำคัญ 2 ข้อดังนี้
- ไฟล์แนบเครือข่ายช่วยให้เครือข่ายของผู้ผลิตเริ่มการเชื่อมต่อกับเครือข่ายของผู้บริโภคได้ (การรับส่งข้อมูลขาออกของบริการที่มีการจัดการ) ส่วนปลายทางช่วยให้เครือข่ายของผู้บริโภคเริ่มการเชื่อมต่อกับเครือข่ายของผู้ผลิตได้ (การรับส่งข้อมูลขาเข้าของบริการที่มีการจัดการ)
- การเชื่อมต่ออินเทอร์เฟซ Private Service Connect เป็นแบบทรานซิทีฟ ซึ่งหมายความว่าเครือข่ายผู้ผลิตสามารถสื่อสารกับเครือข่ายอื่นๆ ที่เชื่อมต่อกับเครือข่ายผู้บริโภคได้
สิ่งที่คุณจะสร้าง
ไปป์ไลน์ Vertex AI ที่ติดตั้งใช้งานในโปรเจ็กต์ไคลเอ็นต์ที่ Google จัดการจะใช้ไฟล์แนบเครือข่าย PSC เพื่อสร้างอินสแตนซ์หลาย NIC ระหว่างเครือข่ายผู้ผลิตและเครือข่ายผู้บริโภค เนื่องจากมีการติดตั้งใช้งานการแนบเครือข่าย PSC ด้วย NIC หลายตัวจากเครือข่ายผู้บริโภค Vertex AI Pipelines จึงเข้าถึงเส้นทางที่พร้อมใช้งานจากเครือข่ายผู้บริโภคได้
ในบทแนะนํานี้ คุณจะได้สร้างสถาปัตยกรรมอินเทอร์เฟซ Private Service Connect (PSC) ที่ครอบคลุมสําหรับไปป์ไลน์ Vertex AI ที่ใช้กฎไฟร์วอลล์ของ Cloud เพื่ออนุญาตหรือปฏิเสธการเชื่อมต่อจากผู้ผลิตไปยังอินสแตนซ์ทดสอบของผู้บริโภคดังที่แสดงในรูปที่ 1
รูปที่ 1
คุณจะต้องสร้าง psc-network-attachment รายการเดียวใน VPC ของผู้บริโภค ซึ่งจะทำให้เกิดกรณีการใช้งานต่อไปนี้
- สร้างกฎไฟร์วอลล์ขาเข้าใน consumer-vpc ที่อนุญาตให้ซับเน็ต Vertex AI Pipeline (192.168.10.0/28) เข้าถึง test-svc-1 ยืนยันว่า PING ที่สร้างขึ้นจากงานไปป์ไลน์ไปยัง test-svc-1 สำเร็จโดยใช้ TCPDUMP
- สร้างกฎไฟร์วอลล์ขาเข้าใน consumer-vpc ที่ปฏิเสธเครือข่ายย่อยของ Vertex AI Pipeline (192.168.10.0/28) ไปยัง test-svc-2 ยืนยันความล้มเหลวของ PING ตามบันทึกของไฟร์วอลล์ที่ Logs Explorer สร้างขึ้น
สิ่งที่คุณจะได้เรียนรู้
- วิธีสร้างไฟล์แนบเครือข่าย
- วิธีที่ไปป์ไลน์ Vertex AI สามารถใช้ไฟล์แนบเครือข่ายเพื่อสร้างอินเทอร์เฟซ PSC
- วิธีสร้างการสื่อสารจากผู้ผลิตไปยังผู้บริโภค
- วิธีอนุญาตให้เข้าถึงจาก Verex AI Pipelines ไปยัง VM ของผู้บริโภค test-svc-1
- วิธีปฏิเสธการเข้าถึงจาก Verex AI Pipelines ไปยัง VM ของผู้บริโภค test-svc-2 โดยใช้ Cloud Firewall
สิ่งที่ต้องมี
- โปรเจ็กต์ Google Cloud
- สิทธิ์ IAM
- ผู้ดูแลระบบอินสแตนซ์ Compute (roles/compute.instanceAdmin)
- ผู้ดูแลระบบเครือข่าย Compute (roles/compute.networkAdmin)
- ผู้ดูแลระบบความปลอดภัยของ Compute (roles/compute.securityAdmin)
- ผู้ใช้อุโมงค์ข้อมูลที่รักษาความปลอดภัยด้วย IAP (roles/iap.tunnelResourceAccessor)
- ผู้ดูแลระบบการบันทึก (roles/logging.admin)
- ผู้ดูแลระบบสมุดบันทึก (roles/notebooks.admin)
- ผู้ดูแลระบบ IAM ของโปรเจ็กต์ (roles/resourcemanager.projectIamAdmin)
- ผู้ดูแลระบบโควต้า (roles/servicemanagement.quotaAdmin)
- ผู้ดูแลระบบบัญชีบริการ (roles/iam.serviceAccountAdmin)
- ผู้ใช้บัญชีบริการ (roles/iam.serviceAccountUser)
- ผู้ดูแลระบบ Vertex AI (roles/aiplatform.admin)
2. ก่อนเริ่มต้น
บทแนะนำนี้ใช้ตัวแปร $เพื่อช่วยให้ใช้งานการกำหนดค่า gcloud ใน Cloud Shell ได้
ใน Cloud Shell ให้ทําดังนี้
gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
projectid=YOUR-PROJECT-NAME
echo $projectid
อัปเดตโปรเจ็กต์เพื่อรองรับบทแนะนำ
ใน Cloud Shell ให้ทําดังนี้
gcloud services enable notebooks.googleapis.com
gcloud services enable aiplatform.googleapis.com
gcloud services enable compute.googleapis.com
gcloud services enable cloudresourcemanager.googleapis.com
3. การตั้งค่าสำหรับผู้บริโภค
สร้าง VPC ของผู้บริโภค
ใน Cloud Shell ให้ทําดังนี้
gcloud compute networks create consumer-vpc --project=$projectid --subnet-mode=custom
สร้างซับเน็ตของผู้บริโภค
ใน Cloud Shell ให้ทําดังนี้
gcloud compute networks subnets create test-subnet-1 --project=$projectid --range=192.168.20.0/28 --network=consumer-vpc --region=us-central1
ใน Cloud Shell ให้ทําดังนี้
gcloud compute networks subnets create test-subnet-2 --project=$projectid --range=192.168.30.0/28 --network=consumer-vpc --region=us-central1
ใน Cloud Shell ให้ทําดังนี้
gcloud compute networks subnets create workbench-subnet --project=$projectid --range=192.168.40.0/28 --network=consumer-vpc --region=us-central1 --enable-private-ip-google-access
การกำหนดค่าเราเตอร์ระบบคลาวด์และ NAT
บทแนะนำสำหรับการดาวน์โหลดแพ็กเกจซอฟต์แวร์โน้ตบุ๊กจะใช้การเปลี่ยนค่าที่อยู่เครือข่าย (NAT) ของ Cloud เนื่องจากอินสแตนซ์โน้ตบุ๊กไม่มีที่อยู่ IP ภายนอก Cloud NAT มีความสามารถ NAT ขาออก ซึ่งหมายความว่าไม่อนุญาตให้โฮสต์อินเทอร์เน็ตเริ่มการสื่อสารกับโน้ตบุ๊กที่มีการจัดการโดยผู้ใช้ ซึ่งทำให้โน้ตบุ๊กมีความปลอดภัยมากขึ้น
สร้าง Cloud Router ระดับภูมิภาคใน Cloud Shell
gcloud compute routers create cloud-router-us-central1 --network consumer-vpc --region us-central1
สร้างเกตเวย์ NAT ของ Cloud ตามภูมิภาคใน Cloud Shell
gcloud compute routers nats create cloud-nat-us-central1 --router=cloud-router-us-central1 --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --region us-central1
สร้างซับเน็ตการแนบเครือข่าย Private Service Connect
สร้างซับเน็ตของไฟล์แนบเครือข่ายที่ Vertex AI Pipelines ใช้ใน Cloud Shell
gcloud compute networks subnets create intf-subnet --project=$projectid --range=192.168.10.0/28 --network=consumer-vpc --region=us-central1
4. เปิดใช้ Identity-Aware Proxy (IAP)
หากต้องการอนุญาตให้ IAP เชื่อมต่อกับอินสแตนซ์ VM ให้สร้างกฎไฟร์วอลล์ที่มีลักษณะดังนี้
- มีผลกับอินสแตนซ์ VM ทั้งหมดที่คุณต้องการเข้าถึงโดยใช้ IAP
- อนุญาตการรับส่งข้อมูลขาเข้าจากช่วง IP 35.235.240.0/20 ช่วงนี้มีที่อยู่ IP ทั้งหมดที่ IAP ใช้สำหรับการส่งต่อ TCP
สร้างกฎไฟร์วอลล์ IAP ใน Cloud Shell
gcloud compute firewall-rules create ssh-iap-consumer \
--network consumer-vpc \
--allow tcp:22 \
--source-ranges=35.235.240.0/20
5. สร้างอินสแตนซ์ VM ของผู้บริโภค
สร้างอินสแตนซ์ VM ของผู้บริโภค test-svc-1 ใน Cloud Shell
gcloud compute instances create test-svc-1 \
--project=$projectid \
--machine-type=e2-micro \
--image-family debian-11 \
--no-address \
--image-project debian-cloud \
--zone us-central1-a \
--subnet=test-subnet-1 \
--shielded-secure-boot
สร้างอินสแตนซ์ VM ของผู้บริโภค test-svc-2 ใน Cloud Shell
gcloud compute instances create test-svc-2 \
--project=$projectid \
--machine-type=e2-micro \
--image-family debian-11 \
--no-address \
--image-project debian-cloud \
--zone us-central1-a \
--subnet=test-subnet-2 \
--shielded-secure-boot
รับและจัดเก็บที่อยู่ IP ของอินสแตนซ์ ดังนี้
ใน Cloud Shell ให้ใช้คำสั่ง describe กับอินสแตนซ์ VM ทดสอบ
gcloud compute instances describe test-svc-1 --zone=us-central1-a | grep networkIP:
gcloud compute instances describe test-svc-2 --zone=us-central1-a | grep networkIP:
ตัวอย่าง
user@cloudshell(psc-vertex)$ gcloud compute instances describe test-svc-1 --zone=us-central1-a | grep networkIP:
gcloud compute instances describe test-svc-2 --zone=us-central1-a | grep networkIP:
networkIP: 192.168.20.2
networkIP: 192.168.30.2
6. ไฟล์แนบเครือข่าย Private Service Connect
ไฟล์แนบเครือข่ายคือทรัพยากรระดับภูมิภาคที่แสดงถึงฝั่งผู้บริโภคของอินเทอร์เฟซ Private Service Connect คุณเชื่อมโยงซับเน็ตเดียวกับการแนบเครือข่าย และผู้ผลิต (Vertex AI Pipelines) จะกำหนด IP ให้กับอินเทอร์เฟซ Private Service Connect
สร้างไฟล์แนบเครือข่าย
สร้างไฟล์แนบเครือข่ายใน Cloud Shell
gcloud compute network-attachments create psc-network-attachment \
--region=us-central1 \
--connection-preference=ACCEPT_MANUAL \
--subnets=intf-subnet
แสดงรายการไฟล์แนบของเครือข่าย
ใน Cloud Shell ให้แสดงรายการไฟล์แนบของเครือข่าย
gcloud compute network-attachments list
อธิบายไฟล์แนบของเครือข่าย
อธิบายไฟล์แนบเครือข่ายใน Cloud Shell
gcloud compute network-attachments describe psc-network-attachment --region=us-central1
จด URI ของ psc-network-attachment ที่ผู้ผลิตจะใช้เมื่อสร้างอินเทอร์เฟซ Private Service Connect
ในตัวอย่างด้านล่าง URI ไฟล์แนบของเครือข่าย psc มีดังนี้
projects/psc-vertex/regions/us-central1/networkAttachments/psc-network-attachment
user@cloudshell$ gcloud compute network-attachments describe psc-network-attachment --region=us-central1
connectionPreference: ACCEPT_MANUAL
creationTimestamp: '2025-01-21T12:25:25.385-08:00'
fingerprint: m9bHc9qnosY=
id: '56224423547354202'
kind: compute#networkAttachment
name: psc-network-attachment
network: https://www.googleapis.com/compute/v1/projects/psc-vertex/global/networks/consumer-vpc
region: https://www.googleapis.com/compute/v1/projects/psc-vertex/regions/us-central1
selfLink: https://www.googleapis.com/compute/v1/projects/psc-vertex/regions/us-central1/networkAttachments/psc-network-attachment
subnetworks:
- https://www.googleapis.com/compute/v1/projects/psc-vertex/regions/us-central1/subnetworks/intf-subnet
7. การตั้งค่า Vertex AI Workbench
ส่วนต่อไปนี้จะแนะนำการสร้าง Jupyter Notebook ระบบจะใช้โน้ตบุ๊กนี้เพื่อทำให้งานไปป์ไลน์ใช้งานได้ ซึ่งจะส่ง PING จากไปป์ไลน์ Vertex AI ไปยังอินสแตนซ์ทดสอบ เส้นทางข้อมูลระหว่างไปป์ไลน์ Vertex AI กับเครือข่ายของผู้บริโภคที่มีอินสแตนซ์ใช้อินเทอร์เฟซเครือข่าย Private Service Connect
สร้างบัญชีบริการที่ผู้ใช้จัดการ
ในส่วนต่อไปนี้ คุณจะต้องสร้างบัญชีบริการที่จะเชื่อมโยงกับอินสแตนซ์ Vertex AI Workbench ที่ใช้ในบทแนะนำ
ในบทแนะนำ บัญชีบริการจะมีบทบาทต่อไปนี้
สร้างบัญชีบริการใน Cloud Shell
gcloud iam service-accounts create notebook-sa \
--display-name="notebook-sa"
ใน Cloud Shell ให้อัปเดตบัญชีบริการที่มีบทบาทผู้ดูแลระบบพื้นที่เก็บข้อมูล
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 ให้อัปเดตบัญชีบริการที่มีบทบาทผู้ดูแลระบบที่เก็บอาร์ติแฟกต์
gcloud projects add-iam-policy-binding $projectid --member="serviceAccount:notebook-sa@$projectid.iam.gserviceaccount.com" --role="roles/artifactregistry.admin"
ใน Cloud Shell ให้อนุญาตให้บัญชีบริการของ Notebook ใช้บัญชีบริการเริ่มต้นของ 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"
สร้างอินสแตนซ์ Vertex AI Workbench
ในส่วนต่อไปนี้ ให้สร้างอินสแตนซ์ Vertex AI Workbench ที่รวมบัญชีบริการ notebook-sa ที่สร้างไว้ก่อนหน้านี้
สร้างอินสแตนซ์ไคลเอ็นต์ส่วนตัวใน Cloud Shell
gcloud workbench instances create workbench-tutorial --vm-image-project=deeplearning-platform-release --vm-image-family=common-cpu-notebooks --machine-type=n1-standard-4 --location=us-central1-a --subnet-region=us-central1 --subnet=workbench-subnet --disable-public-ip --shielded-secure-boot=true --service-account-email=notebook-sa@$projectid.iam.gserviceaccount.com
8. การเชื่อมต่อ Vertex AI Pipelines กับ test-svc-1
เปิดแท็บ Cloud Shell ใหม่และอัปเดตการตั้งค่าโปรเจ็กต์
ใน Cloud Shell ให้ทําดังนี้
gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
projectid=YOUR-PROJECT-NAME
echo $projectid
หากต้องการอนุญาตการเชื่อมต่อจาก Vertex AI Pipelines ไปยัง test-svc-1 ให้สร้างกฎไฟร์วอลล์ขาเข้าที่ระบุไฟล์แนบเครือข่าย PSC เป็นต้นทาง (192.168.10.0/28) และที่อยู่ IP ของ test-svc-1 เป็นปลายทาง
ใน Cloud Shell ให้อัปเดต destination-range ให้ตรงกับที่อยู่ IP ของ test-svc-1
gcloud compute --project=$projectid firewall-rules create allow-icmp-vertex-pipelines-to-test-svc1-vm --direction=INGRESS --priority=1000 --network=consumer-vpc --action=ALLOW --source-ranges=192.168.10.0/28 --destination-ranges=<your-test-svc-1-vm-ip> --rules=icmp
ตัวอย่าง
gcloud compute --project=$projectid firewall-rules create allow-icmp-vertex-pipelines-to-test-svc1-vm --direction=INGRESS --priority=1000 --network=consumer-vpc --action=ALLOW --source-ranges=192.168.10.0/28 --destination-ranges=192.168.20.2 --rules=icmp
เข้าสู่ระบบอินสแตนซ์ test-svc-1 โดยใช้ IAP ใน Cloud Shell
gcloud compute ssh test-svc-1 --project=$projectid --zone=us-central1-a --tunnel-through-iap
ในระบบปฏิบัติการ ให้เรียกใช้ tcpdump เพื่อบันทึกการรับส่งข้อมูล ICMP ระบบจะใช้เซสชันระบบปฏิบัติการนี้เพื่อตรวจสอบการสื่อสารระหว่าง Vertex AI Pipeline กับ VM
sudo tcpdump -i any icmp -nn
9. การอัปเดตตัวแทนบริการ Vertex AI
Vertex AI Pipelines จะดําเนินการในนามของคุณ เช่น รับที่อยู่ IP จากซับเน็ตไฟล์แนบเครือข่าย PSC ที่ใช้สร้างอินเทอร์เฟซ PSC โดย Vertex AI Pipelines จะใช้ตัวแทนบริการ (แสดงอยู่ด้านล่าง) ซึ่งต้องมีสิทธิ์ผู้ดูแลระบบเครือข่าย
service-$projectnumber@gcp-sa-aiplatform.iam.gserviceaccount.com
รับหมายเลขโปรเจ็กต์ใน Cloud Shell
gcloud projects describe $projectid | grep projectNumber
ตัวอย่าง
gcloud projects describe $projectid | grep projectNumber:
projectNumber: '795057945528'
ใน Cloud Shell ให้อัปเดตบัญชีตัวแทนบริการที่มีบทบาท compute.networkAdmin
gcloud projects add-iam-policy-binding $projectid --member="serviceAccount:service-<your-projectnumber>@gcp-sa-aiplatform.iam.gserviceaccount.com" --role="roles/compute.networkAdmin"
ตัวอย่าง
gcloud projects add-iam-policy-binding $projectid --member="serviceAccount:service-795057945528@gcp-sa-aiplatform.iam.gserviceaccount.com" --role="roles/compute.networkAdmin"
10. การอัปเดตบัญชีบริการเริ่มต้น
เปิดใช้ Compute Engine API และให้สิทธิ์บัญชีบริการเริ่มต้นเข้าถึง Vertex AI โปรดทราบว่าระบบอาจใช้เวลาสักครู่ในการแสดงการเปลี่ยนแปลงการเข้าถึง
ใน Cloud Shell ให้อัปเดตบัญชีบริการเริ่มต้นที่มีบทบาท aiplatform.user
gcloud projects add-iam-policy-binding $projectid \
--member="serviceAccount:<your-projectnumber>-compute@developer.gserviceaccount.com" \
--role="roles/aiplatform.user"
ตัวอย่าง
gcloud projects add-iam-policy-binding $projectid \
--member="serviceAccount:795057945528-compute@developer.gserviceaccount.com" \
--role="roles/aiplatform.user"
11. ติดตั้งใช้งานงาน Vertex AI Pipelines
ในส่วนต่อไปนี้ คุณจะต้องสร้างโน้ตบุ๊กเพื่อทำการ PING ไปยังอินสแตนซ์ test-svc-1 ของผู้บริโภคให้สำเร็จ
เรียกใช้งานการฝึกในอินสแตนซ์ Vertex AI Workbench
- ในคอนโซล Google Cloud ให้ไปที่แท็บอินสแตนซ์ในหน้าเวิร์กเบนช์ของ Vertex AI
- คลิกเปิด JupyterLab ข้างชื่ออินสแตนซ์ Vertex AI Workbench (workbench-tutorial) อินสแตนซ์ Vertex AI Workbench จะเปิดขึ้นใน JupyterLab
- เลือกไฟล์ > ใหม่ > สมุดบันทึก
- เลือกเคอร์เนล > Python 3
- ในเซลล์โน้ตบุ๊กใหม่ ให้เรียกใช้คำสั่งต่อไปนี้เพื่อให้แน่ใจว่าคุณมี pip เวอร์ชันล่าสุด
! pip3 install --upgrade --quiet google-cloud-aiplatform \
kfp \
google-cloud-pipeline-components
- ตั้งค่าตัวแปรโปรเจ็กต์ในเซลล์สมุดบันทึกใหม่
PROJECT_ID = "<your-projectid>"
REGION = "<your-region>"
NETWORK_ATTACHMENT_NAME = "psc-network-attachment"
ตัวอย่าง
PROJECT_ID = "psc-vertex"
REGION = "us-central1"
NETWORK_ATTACHMENT_NAME = "psc-network-attachment"
- กําหนดชื่อที่เก็บข้อมูลที่ไม่ซ้ำกันทั่วโลกเป็นตัวแปรในเซลล์โน้ตบุ๊กใหม่
BUCKET_URI = f"gs://<your-bucket-name>"
ตัวอย่าง
BUCKET_URI = f"gs://psc-vertex-bucket"
- สร้างที่เก็บข้อมูลในเซลล์โน้ตบุ๊กใหม่
! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}
ในส่วนต่อไปนี้ คุณจะต้องกำหนดบัญชีบริการ Compute Engine เริ่มต้นที่จะใช้เรียกใช้งานไปป์ไลน์ รวมถึงให้สิทธิ์ที่เหมาะสมแก่บัญชีดังกล่าว
shell_output = ! gcloud projects describe $PROJECT_ID
PROJECT_NUMBER = shell_output[-1].split(":")[1].strip().replace("'", "")
SERVICE_ACCOUNT = f"{PROJECT_NUMBER}-compute@developer.gserviceaccount.com"
print(f"Project Number: {PROJECT_NUMBER}")
print(f"Service Account: {SERVICE_ACCOUNT}")
ระบบจะพิมพ์บัญชีบริการและหมายเลขโปรเจ็กต์เพื่อยืนยันว่าการดำเนินการเสร็จสมบูรณ์
- ในเซลล์โน้ตบุ๊กใหม่ ให้มอบสิทธิ์บัญชีบริการในการอ่านและเขียนอาร์ติแฟกต์ไปป์ไลน์ในที่เก็บข้อมูลที่สร้างขึ้นในขั้นตอนก่อนหน้า
! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectCreator {BUCKET_URI}
! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectViewer {BUCKET_URI}
- ในเซลล์โน้ตบุ๊กใหม่ ให้กําหนดพารามิเตอร์ไปป์ไลน์ โปรดทราบว่า NETWORK_ATTACHMENT_NAME คือไฟล์แนบเครือข่าย PSC จึงต้องตรงกัน
PIPELINE_ROOT = f"{BUCKET_URI}/pipeline_root/psc_test"
NETWORK_ATTACHMENT_URI = f"projects/{PROJECT_NUMBER}/regions/{REGION}/networkAttachments/{NETWORK_ATTACHMENT_NAME}"
- ในเซลล์ใหม่ของ Notebook ให้เริ่มต้น Vertex AI SDK
from kfp import dsl
from google.cloud import aiplatform, aiplatform_v1beta1
import time
from google.cloud.aiplatform_v1.types import pipeline_state
import yaml
from datetime import datetime
import logging
aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
- ในเซลล์โน้ตบุ๊กใหม่ ให้กําหนดคอมโพเนนต์ทดสอบ
@dsl.container_component
def ping_check(network_address: str):
"""Pings a network address
Args:
network_address: The IP address to ping
"""
return dsl.ContainerSpec(
image="ubuntu:22.04",
command=["sh", "-c"],
args=[
f"""
# Use sed for regex replacement, cleaner than bash parameter expansion for this
cleaned_address=$(echo "{network_address}" | sed 's/[^0-9.]//g')
apt-get update && apt-get install inetutils-traceroute inetutils-ping -y
echo "Will ping $cleaned_address"
if ! ping -c 3 $cleaned_address; then
echo "Ping failed"
traceroute -w 1 -m 7 $cleaned_address
exit 1
fi
"""
],
)
- กําหนดไปป์ไลน์ในเซลล์สมุดบันทึกใหม่
@dsl.pipeline(name="check-connectivity")
def pipeline(ip_address: str):
"""Pings an IP address. Facilitated by a Private Service Connect Interface
Args:
ip_address: The IP address to ping
"""
ping_check(network_address=ip_address).set_caching_options(False)
return
- ในเซลล์สมุดบันทึกใหม่ ให้เรียกใช้ฟังก์ชันยูทิลิตี แล้วรอให้ไปป์ไลน์ทำงานเสร็จ
def wait_for_pipeline(
project_id: str,
region: str,
pipeline_job_resource_name: str,
timeout: int = 20 * 60, # Default timeout of 20 minutes (in seconds)
) -> bool:
"""
Waits for a Vertex AI pipeline to finish, with a timeout.
Args:
project_id (str): The Google Cloud project ID.
region (str): The region where the pipeline is running.
pipeline_job_resource_name (str): The resource name of the pipeline job.
timeout (int): The maximum time to wait for the pipeline to finish, in seconds.
Defaults to 20 minutes (1200 seconds).
Returns:
bool: True if the pipeline succeeded, False otherwise.
Raises:
TimeoutError: If the pipeline does not finish within the specified timeout.
"""
# Initialize the AIPlatform client
aiplatform.init(project=project_id, location=region)
# Get the pipeline job
pipeline_job = aiplatform.PipelineJob.get(resource_name=pipeline_job_resource_name)
logging.info(
f"Vertex AI Console Link: https://console.cloud.google.com/vertex-ai/pipelines/locations/{region}/runs/{pipeline_job.resource_name.split('/')[-1]}?project={project_id}"
)
start_time = time.time()
while True:
status = pipeline_job.state
logging.info(f"Pipeline Job status: {status.name}")
if status in [
pipeline_state.PipelineState.PIPELINE_STATE_SUCCEEDED,
pipeline_state.PipelineState.PIPELINE_STATE_FAILED,
pipeline_state.PipelineState.PIPELINE_STATE_CANCELLED,
]:
break # Exit the loop if the job is finished
if time.time() - start_time > timeout:
logging.error(f"Pipeline timed out after {timeout} seconds.")
raise TimeoutError(f"Pipeline timed out after {timeout} seconds.")
# Wait for a short time before checking again
time.sleep(10) # Adjust the wait time as needed
# Do something based on the final status
if status == pipeline_state.PipelineState.PIPELINE_STATE_SUCCEEDED:
logging.info("Pipeline succeeded")
return True
elif status == pipeline_state.PipelineState.PIPELINE_STATE_CANCELLED:
logging.error("Pipeline cancelled")
raise Exception("Pipeline cancelled")
elif status == pipeline_state.PipelineState.PIPELINE_STATE_FAILED:
logging.error("Pipeline failed")
raise Exception("Pipeline failed")
- ในเซลล์ใหม่ของสมุดบันทึก ให้เรียกใช้ฟังก์ชันยูทิลิตีเพื่อเรียกใช้ไปป์ไลน์
def run_job_with_psc_interface_config(
project_id: str,
region: str,
pipeline_root: str,
network_attachment_name: str,
ip_address: str,
local_pipeline_file: str = "pipeline.yaml",
):
"""
Compiles, submits, and monitors a Vertex AI pipeline.
"""
parameter_values = {"ip_address": ip_address}
pipeline_root = f"{pipeline_root}/{datetime.now().strftime('%Y%m%d%H%M%S')}"
logging.info("Compiling pipeline")
try:
with open(local_pipeline_file, "r") as stream:
pipeline_spec = yaml.safe_load(stream)
logging.info(f"Pipeline Spec: {pipeline_spec}")
except yaml.YAMLError as exc:
logging.error(f"Error loading pipeline yaml file: {exc}")
raise
logging.info(f"Will use pipeline root: {pipeline_root}")
# Initialize the Vertex SDK using PROJECT_ID and LOCATION
aiplatform.init(project=project_id, location=region)
# Create the API endpoint
client_options = {"api_endpoint": f"{region}-aiplatform.googleapis.com"}
# Initialize the PipelineServiceClient
client = aiplatform_v1beta1.PipelineServiceClient(client_options=client_options)
# Construct the request
request = aiplatform_v1beta1.CreatePipelineJobRequest(
parent=f"projects/{project_id}/locations/{region}",
pipeline_job=aiplatform_v1beta1.PipelineJob(
display_name="pipeline-with-psc-interface-config",
pipeline_spec=pipeline_spec,
runtime_config=aiplatform_v1beta1.PipelineJob.RuntimeConfig(
gcs_output_directory=pipeline_root, parameter_values=parameter_values
),
psc_interface_config=aiplatform_v1beta1.PscInterfaceConfig(
network_attachment=network_attachment_name
),
),
)
# Make the API call
response = client.create_pipeline_job(request=request)
# Print the response
logging.info(f"Pipeline job created: {response.name}")
return response.name
- ในเซลล์โน้ตบุ๊กใหม่ ให้คอมไพล์ไปป์ไลน์
from kfp import compiler
compiler.Compiler().compile(pipeline_func=pipeline, package_path='pipeline.yaml')
- ในเซลล์โน้ตบุ๊กใหม่ ให้อัปเดต TARGET_IP_ADDRESS ให้ตรงกับที่อยู่ IP ที่ได้ในขั้นตอนก่อนหน้าสำหรับ test-svc-1 และดูสถานะงานของไปป์ไลน์
TARGET_IP_ADDRESS = "<your-test-svc-1-ip>"
try:
job_name = run_job_with_psc_interface_config(
project_id=PROJECT_ID,
region=REGION,
pipeline_root=PIPELINE_ROOT,
network_attachment_name=NETWORK_ATTACHMENT_URI,
ip_address=TARGET_IP_ADDRESS,
)
wait_for_pipeline(
project_id=PROJECT_ID,
region=REGION,
pipeline_job_resource_name=job_name,
)
except Exception as e:
logging.error(f"An error occurred: {e}")
ตัวอย่าง
TARGET_IP_ADDRESS = "192.168.20.2"
try:
job_name = run_job_with_psc_interface_config(
project_id=PROJECT_ID,
region=REGION,
pipeline_root=PIPELINE_ROOT,
network_attachment_name=NETWORK_ATTACHMENT_URI,
ip_address=TARGET_IP_ADDRESS,
)
wait_for_pipeline(
project_id=PROJECT_ID,
region=REGION,
pipeline_job_resource_name=job_name,
)
except Exception as e:
logging.error(f"An error occurred: {e}")
เมื่อทำขั้นตอนที่ 17 แล้ว ไปป์ไลน์จะใช้เวลาประมาณ 8 นาทีจึงจะเสร็จสมบูรณ์
12. ตรวจสอบการเชื่อมต่อกับ test-svc-1
ในเซลล์ที่ใช้ดำเนินการขั้นตอนที่ 17 ให้สังเกตการเปลี่ยนแปลงสถานะงานของไปป์ไลน์จาก PIPELINE_STATE_PENDING เป็น PIPELINE_STATE_RUNNING และสุดท้ายเป็น PIPELINE_STATE_SUCCEEDED ซึ่งบ่งบอกว่ามีการเรียก ping จากไปป์ไลน์ Vertex AI และคำตอบจาก test-svc-1 สำเร็จ
หากต้องการตรวจสอบการรับส่งข้อมูล ICMP ระหว่าง Vertex AI Pipeline กับ test-svc-1 ให้ดูเซสชัน tcpdump ที่สร้างขึ้นก่อนหน้านี้ซึ่งทำงานในระบบปฏิบัติการ test-svc-1 ที่มีบันทึกที่ระบุการรับส่งข้อมูลแบบ 2 ทิศทาง
ในตัวอย่าง tcpdump นี้ Vertex AI Pipelines ดึงที่อยู่ IP 192.168.10.3 จากซับเน็ต 192.168.10.0/28 ส่วน 192.168.20.2 คือที่อยู่ IP ของ test-svc-1 โปรดทราบว่าที่อยู่ IP ในสภาพแวดล้อมของคุณอาจแตกต่างออกไป
user@test-svc-1:~$ sudo tcpdump -i any icmp -nn
tcpdump: data link type LINUX_SLL2
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
18:57:54.737490 ens4 In IP 192.168.10.3 > 192.168.20.2: ICMP echo request, id 257, seq 0, length 64
18:57:54.737523 ens4 Out IP 192.168.20.2 > 192.168.10.3: ICMP echo reply, id 257, seq 0, length 64
13. การเชื่อมต่อ AI ของ Vertex AI Pipelines กับ test-svc-2
ในส่วนต่อไปนี้ คุณจะต้องสร้างกฎไฟร์วอลล์ขาเข้าเพื่อปฏิเสธการรับส่งข้อมูลจากซับเน็ตของไปป์ไลน์ Vertex AI (192.168.10.0/28) ไปยัง test-svc-2 ตามด้วยการอัปเดตโน้ตบุ๊คให้แสดงที่อยู่ IP ของ test-svc-2 จากนั้นจึงเรียกใช้การเรียกใช้งานไปป์ไลน์
ในเซลล์โน้ตบุ๊ก สถานะงานของไปป์ไลน์จะแสดงเป็น "ข้อผิดพลาด - ไปป์ไลน์ไม่สําเร็จ" นอกจากนี้ การบันทึกของไฟร์วอลล์จะให้ข้อมูลเชิงลึกเกี่ยวกับการเชื่อมต่อที่ไม่สําเร็จ
สร้างกฎไฟร์วอลล์การปฏิเสธข้อมูลขาเข้า
หากต้องการปฏิเสธการเชื่อมต่อจาก Vertex AI Pipelines ไปยัง test-svc-2 ให้สร้างกฎไฟร์วอลล์ขาเข้าที่ระบุไฟล์แนบเครือข่าย PSC เป็นต้นทาง (192.168.10.0/28) และที่อยู่ IP ของ test-svc-2 เป็นปลายทาง
ใน Cloud Shell ให้อัปเดต destination-range ให้ตรงกับที่อยู่ IP ของ test-svc-2
gcloud compute --project=$projectid firewall-rules create deny-icmp-vertex-pipelines-to-test-svc2-vm --direction=INGRESS --priority=1000 --network=consumer-vpc --action=DENY --source-ranges=192.168.10.0/28 --rules=ALL --destination-ranges=<your-test-svc-2-vm-ip> --rules=icmp --enable-logging
ตัวอย่าง
gcloud compute --project=$projectid firewall-rules create deny-icmp-vertex-pipelines-to-test-svc2-vm --direction=INGRESS --priority=1000 --network=consumer-vpc --action=DENY --source-ranges=192.168.10.0/28 --rules=ALL --destination-ranges=192.168.30.2 --enable-logging
เรียกใช้งานไปป์ไลน์จากเซลล์ในสมุดบันทึก
ในเซลล์โน้ตบุ๊กใหม่ ให้อัปเดต TARGET_IP_ADDRESS ให้ตรงกับที่อยู่ IP ที่ได้ในขั้นตอนก่อนหน้าสำหรับ test-svc-2 และดูสถานะงานของไปป์ไลน์
TARGET_IP_ADDRESS = "<your-test-svc-2-ip>"
try:
job_name = run_job_with_psc_interface_config(
project_id=PROJECT_ID,
region=REGION,
pipeline_root=PIPELINE_ROOT,
network_attachment_name=NETWORK_ATTACHMENT_URI,
ip_address=TARGET_IP_ADDRESS,
)
wait_for_pipeline(
project_id=PROJECT_ID,
region=REGION,
pipeline_job_resource_name=job_name,
)
except Exception as e:
logging.error(f"An error occurred: {e}")
ตัวอย่าง
TARGET_IP_ADDRESS = "192.168.30.2"
try:
job_name = run_job_with_psc_interface_config(
project_id=PROJECT_ID,
region=REGION,
pipeline_root=PIPELINE_ROOT,
network_attachment_name=NETWORK_ATTACHMENT_URI,
ip_address=TARGET_IP_ADDRESS,
)
wait_for_pipeline(
project_id=PROJECT_ID,
region=REGION,
pipeline_job_resource_name=job_name,
)
except Exception as e:
logging.error(f"An error occurred: {e}")
เมื่อดำเนินการแล้ว งานในไปป์ไลน์จะใช้เวลาประมาณ 8 นาทีจึงจะเสร็จสมบูรณ์
14. ตรวจสอบการเชื่อมต่อกับ test-svc-2 ไม่สำเร็จ
ในเซลล์ที่ใช้เรียกใช้งานไปป์ไลน์ ให้สังเกตการเปลี่ยนสถานะจาก PIPELINE_STATE_PENDING เป็น PIPELINE_STATE_FAILED ซึ่งบ่งบอกว่าการ ping จาก Vertex AI Pipelines และการตอบกลับจาก test-svc-2 ไม่สำเร็จ
เมื่อใช้เครื่องมือสำรวจบันทึก คุณสามารถดูรายการการบันทึกของไฟร์วอลล์ที่ตรงกับกฎการปฏิเสธ Ingress ซึ่งประกอบด้วยซับเน็ตของ Vertex AI Pipelines (192.168.10.0/28) และที่อยู่ IP test-svc-2
เลือก "แสดงการค้นหา" แล้วแทรกตัวกรองด้านล่าง "15 นาทีที่ผ่านมา" ตามด้วย "เรียกใช้การค้นหา"
jsonPayload.rule_details.reference:("network:consumer-vpc/firewall:deny-icmp-vertex-pipelines-to-test-svc2-vm")
เลือกรายการบันทึก แล้วขยายช่องที่ฝังอยู่เพื่อแสดงองค์ประกอบข้อมูลซึ่งประกอบด้วยไปป์ไลน์ Vertex AI และที่อยู่ IP test-svc-2 ที่ตรวจสอบกฎไฟร์วอลล์ขาเข้าที่ถูกปฏิเสธ
15. ล้างข้อมูล
ลบคอมโพเนนต์บทแนะนำจาก Cloud Shell
gcloud compute instances delete test-svc-1 test-svc-2 --zone=us-central1-a --quiet
gcloud workbench instances delete workbench-tutorial --location=us-central1-a --quiet
gcloud compute firewall-rules delete deny-icmp-vertex-pipelines-to-test-svc2-vm allow-icmp-vertex-pipelines-to-test-svc1-vm ssh-iap-consumer --quiet
gcloud compute routers nats delete cloud-nat-us-central1 --router=cloud-router-us-central1 --region us-central1 --quiet
gcloud compute routers delete cloud-router-us-central1 --region=us-central1 --quiet
16. ขอแสดงความยินดี
ยินดีด้วย คุณได้กําหนดค่าและตรวจสอบอินเทอร์เฟซ Private Service Connect รวมถึงการเชื่อมต่อของผู้บริโภคและผู้ผลิตเรียบร้อยแล้วด้วยการใช้ไฟร์วอลล์ที่อนุญาตและปฏิเสธ Ingress
คุณได้สร้างโครงสร้างพื้นฐานสำหรับผู้บริโภคและเพิ่มไฟล์แนบเครือข่ายที่อนุญาตให้บริการ Vertex AI Pipelines สร้าง VM อินเทอร์เฟซ PSC เพื่อเชื่อมต่อการสื่อสารระหว่างผู้บริโภคและผู้ผลิต คุณได้เรียนรู้วิธีสร้างกฎไฟร์วอลล์ในเครือข่าย VPC ของผู้บริโภคที่อนุญาตและปฏิเสธการเชื่อมต่อกับอินสแตนซ์ในเครือข่ายของผู้บริโภค
Cosmopup คิดว่าบทแนะนำเจ๋งสุดๆ