1. บทนำ
ในโค้ดแล็บนี้ คุณจะได้เรียนรู้วิธีทำให้การเชื่อมต่อบริการส่วนตัวสำหรับ AlloyDB พร้อมใช้งาน และวิธีเข้าถึงบริการ AlloyDB โดยใช้ Private Service Connect ที่ติดตั้งใช้งาน
ดูข้อมูลเพิ่มเติมเกี่ยวกับ Private Service Connect ได้ที่นี่
ข้อกำหนดเบื้องต้น
- ความเข้าใจพื้นฐานเกี่ยวกับ Google Cloud Console
- ทักษะพื้นฐานในอินเทอร์เฟซบรรทัดคำสั่งและ Google Cloud Shell
สิ่งที่คุณจะได้เรียนรู้
- วิธีทำให้คลัสเตอร์และอินสแตนซ์ AlloyDB ใช้งานได้
- วิธีทำให้ Private Service Connect ใช้งานได้
- วิธีเชื่อมต่อจาก VM ไปยังอินสแตนซ์ AlloyDB ผ่าน Private Service Connect
สิ่งที่ต้องมี
- บัญชี Google Cloud และโปรเจ็กต์ Google Cloud
- เว็บเบราว์เซอร์ เช่น Chrome
2. การตั้งค่าและข้อกําหนด
การตั้งค่าสภาพแวดล้อมด้วยตนเอง
- ลงชื่อเข้าใช้ Google Cloud Console แล้วสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี
- ชื่อโปรเจ็กต์คือชื่อที่แสดงสำหรับผู้เข้าร่วมโปรเจ็กต์นี้ ซึ่งเป็นสตริงอักขระที่ Google APIs ไม่ได้ใช้ โดยคุณจะอัปเดตได้ทุกเมื่อ
- รหัสโปรเจ็กต์ต้องไม่ซ้ำกันในโปรเจ็กต์ Google Cloud ทั้งหมดและจะเปลี่ยนแปลงไม่ได้ (เปลี่ยนแปลงไม่ได้หลังจากตั้งค่าแล้ว) คอนโซล Cloud จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ซึ่งปกติแล้วคุณไม่จำเป็นต้องสนใจว่าสตริงนั้นจะเป็นอะไร ในโค้ดแล็บส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (โดยปกติจะระบุเป็น
PROJECT_ID
) หากไม่ชอบรหัสที่สร้างขึ้น คุณก็สร้างรหัสอื่นแบบสุ่มได้ หรือจะลองใช้อุปกรณ์ของคุณเองเพื่อดูว่าฟีเจอร์นี้พร้อมใช้งานหรือไม่ก็ได้ คุณจะเปลี่ยนแปลงชื่อหลังจากขั้นตอนนี้ไม่ได้ และชื่อจะยังคงอยู่ตลอดระยะเวลาของโปรเจ็กต์ - โปรดทราบว่ามีค่าที่ 3 ซึ่งเป็นหมายเลขโปรเจ็กต์ที่ API บางรายการใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 รายการนี้ได้ในเอกสารประกอบ
ข้อควรระวัง: รหัสโปรเจ็กต์จะซ้ำกันไม่ได้ทั่วโลก และบุคคลอื่นจะใช้รหัสที่คุณเลือกไม่ได้ คุณเป็นผู้ใช้รหัสดังกล่าวเพียงคนเดียว แม้ว่าโปรเจ็กต์จะถูกลบไปแล้ว แต่รหัสดังกล่าวก็ไม่สามารถนํามาใช้ซ้ำได้
- ถัดไป คุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร/API ของ Cloud การทำตามโค้ดแล็บนี้จะไม่เสียค่าใช้จ่ายมากนัก หากต้องการปิดทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินหลังจากบทแนะนำนี้ คุณลบทรัพยากรที่สร้างไว้หรือลบโปรเจ็กต์ได้ ผู้ใช้ Google Cloud รายใหม่มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรีมูลค่า$300 USD
เริ่ม Cloud Shell
แม้ว่าคุณจะดำเนินการกับ Google Cloud จากระยะไกลจากแล็ปท็อปได้ แต่ในโค้ดแล็บนี้ คุณจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมบรรทัดคำสั่งที่ทำงานในระบบคลาวด์
จากคอนโซล Google Cloud ให้คลิกไอคอน Cloud Shell ในแถบเครื่องมือด้านขวาบน
การจัดสรรและเชื่อมต่อกับสภาพแวดล้อมจะใช้เวลาเพียงไม่กี่นาที เมื่อดำเนินการเสร็จแล้ว คุณควรเห็นข้อมูลดังต่อไปนี้
เครื่องเสมือนนี้โหลดเครื่องมือการพัฒนาทั้งหมดที่คุณต้องการ ซึ่งจะมีไดเรกทอรีหลักขนาด 5 GB ถาวรและทำงานบน Google Cloud ซึ่งจะช่วยเพิ่มประสิทธิภาพเครือข่ายและการรับรองได้อย่างมีประสิทธิภาพ คุณทํางานทั้งหมดในโค้ดแล็บนี้ได้ภายในเบราว์เซอร์ คุณไม่จำเป็นต้องติดตั้งอะไรเลย
3. ก่อนเริ่มต้น
เปิดใช้ API
โปรดทราบว่าทรัพยากรบางอย่างที่คุณเปิดใช้จะมีค่าใช้จ่ายหากคุณไม่ได้ใช้ระดับโปรโมชัน ในกรณีปกติ หากทรัพยากรทั้งหมดถูกทำลายเมื่อทำ Lab เสร็จแล้ว ค่าใช้จ่ายของทรัพยากรทั้งหมดจะไม่เกิน $5 เราขอแนะนำให้ตรวจสอบการเรียกเก็บเงินและดูว่าคุณยอมรับการออกกำลังหรือไม่
ใน Cloud Shell ให้ตรวจสอบว่าได้ตั้งค่ารหัสโปรเจ็กต์แล้ว โดยทำดังนี้
โดยปกติแล้ว รหัสโปรเจ็กต์จะแสดงในวงเล็บในพรอมต์คำสั่งในเชลล์ระบบคลาวด์ดังที่แสดงในภาพ
gcloud config set project [YOUR-PROJECT-ID]
หน้าต่างจะปรากฏขึ้นเพื่อขอสิทธิ์เข้าถึง Cloud Shell โปรดคลิกให้สิทธิ์
จากนั้นตั้งค่าตัวแปรสภาพแวดล้อม PROJECT_ID เป็นรหัสโปรเจ็กต์ Google Cloud ดังนี้
PROJECT_ID=$(gcloud config get-value project)
ตั้งค่าตัวแปรสภาพแวดล้อม REGION และ ZONE เป็นภูมิภาคและโซนที่ต้องการ ดังนี้
REGION=europe-west4
ZONE=europe-west4-a
เปิดใช้บริการที่จำเป็นทั้งหมด
gcloud services enable compute.googleapis.com \
alloydb.googleapis.com \
dns.googleapis.com \
servicenetworking.googleapis.com \
iam.googleapis.com
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud services enable compute.googleapis.com \ alloydb.googleapis.com \ dns.googleapis.com \ servicenetworking.googleapis.com \ iam.googleapis.com Operation "operations/acat.p2-981315804223-09b8112e-2c2c-4a30-9018-c27e6a06c199" finished successfully.
4. ติดตั้งใช้งานคลัสเตอร์ AlloyDB
ใน Cloud Shell คุณสามารถใช้บรรทัดคำสั่งเพื่อสร้างคลัสเตอร์ AlloyDB ใหม่โดยเปิดใช้ Private Service Connect ได้โดยทำดังนี้
gcloud alloydb clusters create alloydb-cluster-01 \
--password=changeme \
--region=$REGION \
--project=$PROJECT_ID \
--enable-private-service-connect
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud alloydb clusters create alloydb-cluster-01 \ --password=changeme \ --region=$REGION \ --project=$PROJECT_ID \ --enable-private-service-connect Operation ID: operation-1739367760591-62df21d80fae8-9aa7c3ab-64604bae Creating cluster...done.
ตอนนี้ให้สร้างอินสแตนซ์หลักของ AlloyDB โดยทำดังนี้
gcloud alloydb instances create alloydb-instance-01 \
--instance-type=PRIMARY \
--cpu-count=2 \
--availability-type=ZONAL \
--region=$REGION \
--cluster=alloydb-cluster-01 \
--project=$PROJECT_ID \
--allowed-psc-projects=$PROJECT_ID
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud alloydb instances create alloydb-instance-01 \ --instance-type=PRIMARY \ --cpu-count=2 \ --availability-type=ZONAL \ --region=$REGION \ --cluster=alloydb-cluster-01 \ --project=$PROJECT_ID \ --allowed-psc-projects=$PROJECT_ID Operation ID: operation-1739368280521-62df23c7e7bda-7e52597c-8c7d9d79 Creating instance...done.
5. ตั้งค่า Private Service Connect
สําหรับงานที่เกี่ยวข้องกับเครือข่ายต่อไปนี้ เราจะถือว่ามี VPC ที่ชื่อ default อยู่แล้ว
จองที่อยู่ IP ภายใน
ค้นหาช่วง CIDR ของเครือข่ายย่อย VPC ในภูมิภาค GCP ที่อ้างอิงในตัวแปรสภาพแวดล้อม REGION แล้วเลือกที่อยู่ IP ว่างในช่วง CIDR นี้สำหรับปลายทาง Privat Service Connect
gcloud compute networks subnets describe default \
--region=$REGION --project=$PROJECT_ID \
--format="value(ipCidrRange)"
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-cloud-sql-test)$ gcloud compute networks subnets describe default \ --region=$REGION --project=$PROJECT_ID \ --format="value(ipCidrRange)" 10.164.0.0/20
จองที่อยู่ IP ภายในสำหรับปลายทาง Private Service Connect ในช่วงที่อยู่ CIDR ของซับเน็ต VPC ที่ดึงข้อมูลข้างต้น
gcloud compute addresses create alloydb-psc \
--project=$PROJECT_ID \
--region=$REGION \
--subnet=default \
--addresses=10.164.0.10
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute addresses create alloydb-psc \ --project=$PROJECT_ID \ --region=$REGION \ --subnet=default \ --addresses=10.164.0.10 Created [https://www.googleapis.com/compute/v1/projects/psc-alloydb-test/regions/europe-west4/addresses/alloydb-psc].
ยืนยันว่ามีการสำรองที่อยู่ IP ภายในและสถานะ "สงวนไว้" ปรากฏขึ้นสำหรับที่อยู่ IP
gcloud compute addresses list --project=$PROJECT_ID \
--filter="name=alloydb-psc"
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute addresses list --project=$PROJECT_ID \ --filter="name=alloydb-psc" NAME: alloydb-psc ADDRESS/RANGE: 10.164.0.10 TYPE: INTERNAL PURPOSE: GCE_ENDPOINT NETWORK: REGION: europe-west4 SUBNET: default STATUS: RESERVED
รับ URI ของไฟล์แนบบริการ
หลังจากสร้างอินสแตนซ์ AlloyDB ที่เปิดใช้ Private Service Connect แล้ว ให้รับ URI ของไฟล์แนบบริการและใช้เพื่อสร้างปลายทาง Private Service Connect ด้วยที่อยู่ IP ภายในที่สงวนไว้ด้านบน
gcloud alloydb instances describe alloydb-instance-01 \
--cluster=alloydb-cluster-01 \
--region="$REGION" \
--format="value(pscInstanceConfig.serviceAttachmentLink)" | \
sed 's|.*/projects/|projects/|'
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud alloydb instances describe alloydb-instance-01 \ --cluster=alloydb-cluster-01 \ --region="$REGION" \ --format="value(pscInstanceConfig.serviceAttachmentLink)" | \ sed 's|.*/projects/|projects/|' projects/se8c7a64d55e2e1f9p-tp/regions/europe-west4/serviceAttachments/alloydb-69f67b56-9fa-alloydb-instance-sa
สร้าง Private Service Connect
สร้างปลายทาง Private Service Connect และชี้ปลายทางไปยัง URI ไฟล์แนบของบริการ AlloyDB โดยทำดังนี้
gcloud compute forwarding-rules create alloydb-psc-ep \
--address=alloydb-psc \
--project=$PROJECT_ID \
--region=$REGION \
--network=default \
--target-service-attachment=projects/se8c7a64d55e2e1f9p-tp/regions/europe-west4/serviceAttachments/alloydb-69f67b56-9fa-alloydb-instance-sa \
--allow-psc-global-access
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute forwarding-rules create alloydb-psc-ep \ --address=alloydb-psc \ --project=$PROJECT_ID \ --region=$REGION \ --network=default \ --target-service-attachment=projects/se8c7a64d55e2e1f9p-tp/regions/europe-west4/serviceAttachments/alloydb-69f67b56-9fa-alloydb-instance-sa \ --allow-psc-global-access Created [https://www.googleapis.com/compute/v1/projects/psc-alloydb-test/regions/europe-west4/forwardingRules/alloydb-psc-ep].
ตรวจสอบว่าอุปกรณ์ปลายทางเชื่อมต่อกับไฟล์แนบของบริการได้ โดยทำดังนี้
gcloud compute forwarding-rules describe alloydb-psc-ep \
--project=$PROJECT_ID \
--region=$REGION \
--format="value(pscConnectionStatus)"
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute forwarding-rules describe alloydb-psc-ep \ --project=$PROJECT_ID \ --region=$REGION \ --format="value(pscConnectionStatus)" ACCEPTED
กำหนดค่าโซน DNS ที่มีการจัดการ
หากต้องการเพิ่มชื่อ DNS ที่แนะนำสำหรับอินสแตนซ์ AlloyDB วิธีที่ดีที่สุดคือสร้างโซน DNS ส่วนตัวในเครือข่าย VPC ที่เกี่ยวข้อง โดยทำดังนี้
gcloud dns managed-zones create alloydb-dns \
--project=$PROJECT_ID \
--description="DNS zone for the AlloyDB instances" --dns-name=$REGION.alloydb-psc.goog. \
--networks=default \
--visibility=private
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud dns managed-zones create alloydb-dns \ --project=$PROJECT_ID \ --description="DNS zone for the AlloyDB instances" --dns-name=$REGION.alloydb-psc.goog. \ --networks=default \ --visibility=private Created [https://dns.googleapis.com/dns/v1/projects/psc-alloydb-test/managedZones/alloydb-dns].
เพิ่มระเบียน DNS สำหรับ Private Service Connect
รับระเบียน DNS ที่แนะนำสำหรับอินสแตนซ์ AlloyDB โดยทำดังนี้
gcloud alloydb instances describe alloydb-instance-01 \
--cluster=alloydb-cluster-01 --region=$REGION --project=$PROJECT_ID \
--format="value(pscInstanceConfig.pscDnsName)"
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud alloydb instances describe alloydb-instance-01 \ --cluster=alloydb-cluster-01 --region=$REGION --project=$PROJECT_ID \ --format="value(pscInstanceConfig.pscDnsName)" 69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog.
เพิ่มระเบียน DNS ที่แนะนําลงในโซน DNS ที่มีการจัดการ
gcloud dns record-sets create 69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog. \
--project=$PROJECT_ID \
--type=A \
--rrdatas=10.164.0.10 \
--zone=alloydb-dns
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud dns record-sets create 69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog. \ --project=$PROJECT_ID \ --type=A \ --rrdatas=10.164.0.10 \ --zone=alloydb-dns NAME: 69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog. TYPE: A TTL: 0 DATA: 10.164.0.10
6. เตรียมเครื่องเสมือน Google Compute Engine
ทำให้เครื่องเสมือน Google Compute Engine ใช้งานได้
ระบบจะใช้เครื่องเสมือน (VM) เพื่อเชื่อมต่อกับอินสแตนซ์ AlloyDB
gcloud compute instances create alloydb-client \
--zone=$ZONE \
--create-disk=auto-delete=yes,boot=yes,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" --format="value(name)") \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--shielded-secure-boot \
--network-interface=no-address
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute instances create alloydb-client \ --zone=$ZONE \ --create-disk=auto-delete=yes,boot=yes,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" --format="value(name)") \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --network-interface=no-address Created [https://www.googleapis.com/compute/v1/projects/psc-alloydb-test/zones/europe-west4-a/instances/alloydb-client]. NAME: alloydb-client ZONE: europe-west4-a MACHINE_TYPE: n1-standard-1 PREEMPTIBLE: INTERNAL_IP: 10.164.0.2 EXTERNAL_IP: STATUS: RUNNING
ติดตั้งไคลเอ็นต์ Postgres
สร้าง Cloud NAT สําหรับการรับส่งข้อมูลขาออกไปยังอินเทอร์เน็ตที่ VM สามารถเข้าถึงที่เก็บ Linux ได้ ดังนี้
gcloud compute routers create cloud-nat-router \
--network=default \
--region=$REGION
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute routers create cloud-nat-router \ --network=default \ --region=$REGION Creating router [cloud-nat-router]...done. NAME: cloud-nat-router REGION: europe-west4 NETWORK: default
gcloud compute routers nats create cloud-nat \
--router=cloud-nat-router \
--region=$REGION \
--nat-all-subnet-ip-ranges \
--auto-allocate-nat-external-ips
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute routers nats create cloud-nat \ --router=cloud-nat-router \ --region=$REGION \ --nat-all-subnet-ip-ranges \ --auto-allocate-nat-external-ips Creating NAT [cloud-nat] in router [cloud-nat-router]...done.
ติดตั้งซอฟต์แวร์ไคลเอ็นต์ PostgreSQL ใน VM ที่ติดตั้งใช้งาน
เชื่อมต่อกับ VM
gcloud compute ssh --zone $ZONE "alloydb-client" --tunnel-through-iap --project $PROJECT_ID
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute ssh --zone $ZONE "alloydb-client" --tunnel-through-iap --project $PROJECT_ID WARNING: To increase the performance of the tunnel, consider installing NumPy. For instructions, please see https://cloud.google.com/iap/docs/using-tcp-forwarding#increasing_the_tcp_upload_bandwidth Warning: Permanently added 'compute.3358765012415130370' (ED25519) to the list of known hosts. Linux alloydb-client 6.1.0-29-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.123-1 (2025-01-02) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Creating directory '/home/student_tampferl_altostrat_com'.
ติดตั้งคำสั่งเรียกใช้ซอฟต์แวร์ภายใน VM โดยทำดังนี้
sudo apt-get update
sudo apt-get install --yes postgresql-client
ผลลัพธ์ที่คาดหวัง
sudo apt-get update sudo apt-get install --yes postgresql-client Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B] Get:5 file:/etc/apt/mirrors/debian-security.list Mirrorlist [39 B] Get:7 https://packages.cloud.google.com/apt google-compute-engine-bookworm-stable InRelease [1321 B] Get:2 https://deb.debian.org/debian bookworm InRelease [151 kB] Get:3 https://deb.debian.org/debian bookworm-updates InRelease [55.4 kB] Get:4 https://deb.debian.org/debian bookworm-backports InRelease [59.0 kB] ...redacted... update-alternatives: using /usr/share/postgresql/15/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode Setting up postgresql-client (15+248) ... Processing triggers for man-db (2.11.2-2) ... Processing triggers for libc-bin (2.36-9+deb12u9) ...
7. เชื่อมต่อกับอินสแตนซ์ AlloyDB for Postgres ผ่าน Private Service Connect
เชื่อมต่อกับอินสแตนซ์
psql "dbname=postgres user=postgres host=69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog."
ผลลัพธ์ที่คาดหวัง
student_tampferl_altostrat_com@alloydb-client:~$ psql "dbname=postgres user=postgres host=69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog." Password for user postgres: psql (15.10 (Debian 15.10-0+deb12u1), server 15.7) SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off) Type "help" for help. postgres=>
สร้างและทดสอบฐานข้อมูล
สร้างฐานข้อมูล
CREATE DATABASE company;
ผลลัพธ์ที่คาดหวัง
postgres=> CREATE DATABASE company; CREATE DATABASE postgres=>
แสดงรายการฐานข้อมูลทั้งหมด
\l
ผลลัพธ์ที่คาดหวัง
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges -----------------+------------------+----------+---------+-------+------------+-----------------+--------------------------------------- alloydbadmin | alloydbadmin | UTF8 | C | C | und-x-icu | icu | alloydbmetadata | alloydbadmin | UTF8 | C | C | und-x-icu | icu | alloydbadmin=CTc/alloydbadmin + | | | | | | | alloydbmetadata=c/alloydbadmin company | postgres | UTF8 | C | C | und-x-icu | icu | postgres | alloydbsuperuser | UTF8 | C | C | und-x-icu | icu | template0 | alloydbadmin | UTF8 | C | C | und-x-icu | icu | =c/alloydbadmin + | | | | | | | alloydbadmin=CTc/alloydbadmin template1 | alloydbsuperuser | UTF8 | C | C | und-x-icu | icu | =c/alloydbsuperuser + | | | | | | | alloydbsuperuser=CTc/alloydbsuperuser (6 rows)
เชื่อมต่อกับฐานข้อมูลพนักงาน
\c company
ผลลัพธ์ที่คาดหวัง
postgres=> \c company psql (15.10 (Debian 15.10-0+deb12u1), server 15.7) SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off) You are now connected to database "company" as user "postgres". company=>
สร้างตารางในฐานข้อมูลของบริษัท
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
first VARCHAR(255) NOT NULL,
last VARCHAR(255) NOT NULL,
salary DECIMAL (10, 2)
);
ผลลัพธ์ที่คาดหวัง
company=> CREATE TABLE employees ( id SERIAL PRIMARY KEY, first VARCHAR(255) NOT NULL, last VARCHAR(255) NOT NULL, salary DECIMAL (10, 2) ); CREATE TABLE
แทรกข้อมูลลงในตาราง employees ของฐานข้อมูลบริษัท
INSERT INTO employees (first, last, salary) VALUES
('Max', 'Mustermann', 5000.00),
('Anna', 'Schmidt', 7000.00),
('Peter', 'Mayer', 6000.00);
ผลลัพธ์ที่คาดหวัง
company=> INSERT INTO employees (first, last, salary) VALUES ('Max', 'Mustermann', 5000.00), ('Anna', 'Schmidt', 7000.00), ('Peter', 'Mayer', 6000.00); INSERT 0 3 company=>
ค้นหาตารางพนักงาน
SELECT * FROM employees;
ผลลัพธ์ที่คาดหวัง
company=> SELECT * FROM employees; id | first | last | salary ----+-------+------------+--------- 1 | Max | Mustermann | 5000.00 2 | Anna | Schmidt | 7000.00 3 | Peter | Mayer | 6000.00 (3 rows) company=>
ออกจากฐานข้อมูล Postgres และ VM แล้วกลับไปที่ Cloud Shell โดยทำดังนี้
\q
exit
ผลลัพธ์ที่คาดหวัง
company=> \q student_tampferl_altostrat_com@alloydb-client:~$ exit logout Connection to compute.3358765012415130370 closed. student@cloudshell:~ (psc-alloydb-test)$
8. ล้างสภาพแวดล้อม
ทำลายทรัพยากร Google Cloud ทั้งหมดในโปรเจ็กต์เมื่อคุณใช้งานห้องทดลองเสร็จแล้ว
ลบคลัสเตอร์ AlloyDB
ใน Cloud Shell ให้ลบคลัสเตอร์ AlloyDB for Postgres โดยทำดังนี้
gcloud alloydb clusters delete alloydb-cluster-01 --region=$REGION --force --quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud alloydb clusters delete alloydb-cluster-01 --region=$REGION --force --quiet Operation ID: operation-1739488287386-62e0e2d75ead0-f8ffd91f-9c0f56c1 Deleting cluster...done.
ลบเครื่องเสมือน Google Compute Engine
ลบ VM ใน Cloud Shell โดยทำดังนี้
gcloud compute instances delete alloydb-client \
--zone=$ZONE \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute instances delete alloydb-client \ --zone=$ZONE \ --quiet Deleted [https://www.googleapis.com/compute/v1/projects/psc-alloydb-test/zones/europe-west4-a/instances/alloydb-client].
ลบคอมโพเนนต์เครือข่าย
ลบคอมโพเนนต์ที่เกี่ยวข้องกับเครือข่าย ได้แก่ Cloud NAT, Cloud Router, ปลายทาง Private Service Connect, ที่อยู่ IP ภายในที่สงวนไว้, ระเบียน DNS และโซน DNS ที่มีการจัดการ
ลบ Cloud NAT
gcloud compute routers nats delete cloud-nat \
--router=cloud-nat-router \
--region=$REGION \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute routers nats delete cloud-nat \ --router=cloud-nat-router \ --region=$REGION \ --quiet Updated [https://www.googleapis.com/compute/v1/projects/psc-alloydb-test/regions/europe-west4/routers/cloud-nat-router].
ลบ Cloud Router
gcloud compute routers delete cloud-nat-router \
--region=$REGION \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute routers delete cloud-nat-router \ --region=$REGION \ --quiet Deleted [https://www.googleapis.com/compute/v1/projects/psc-alloydb-test/regions/europe-west4/routers/cloud-nat-router].
ลบปลายทาง Private Service Connect
gcloud compute forwarding-rules delete alloydb-psc-ep \
--project=$PROJECT_ID \
--region=$REGION \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute forwarding-rules delete alloydb-psc-ep \ --project=$PROJECT_ID \ --region=$REGION \ --quiet Deleted [https://www.googleapis.com/compute/v1/projects/psc-alloydb-test/regions/europe-west4/forwardingRules/alloydb-psc-ep].
ปล่อยที่อยู่ IP ภายในที่สงวนไว้
gcloud compute addresses delete alloydb-psc \
--project=$PROJECT_ID \
--region=$REGION \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud compute addresses delete alloydb-psc \ --project=$PROJECT_ID \ --region=$REGION \ --quiet Deleted [https://www.googleapis.com/compute/v1/projects/psc-alloydb-test/regions/europe-west4/addresses/alloydb-psc].
ลบระเบียน DNS
gcloud dns record-sets delete 69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog. \
--project=$PROJECT_ID \
--type=A \
--zone=alloydb-dns
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud dns record-sets delete 69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog. \ --project=$PROJECT_ID \ --type=A \ --zone=alloydb-dns Deleted [https://dns.googleapis.com/dns/v1/projects/psc-alloydb-test/managedZones/alloydb-dns/rrsets/69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog./A].
ลบโซน DNS ที่มีการจัดการ
gcloud dns managed-zones delete alloydb-dns \
--project=$PROJECT_ID \
--quiet
ผลลัพธ์ที่คาดหวัง
student@cloudshell:~ (psc-alloydb-test)$ gcloud dns managed-zones delete alloydb-dns \ --project=$PROJECT_ID \ --quiet Deleted [https://dns.googleapis.com/dns/v1/projects/psc-alloydb-test/managedZones/alloydb-dns].
9. ขอแสดงความยินดี
ขอแสดงความยินดีที่ทํา Codelab จนเสร็จสมบูรณ์
สิ่งที่เราได้พูดถึง
- วิธีทำให้คลัสเตอร์และอินสแตนซ์ AlloyDB ใช้งานได้
- วิธีทำให้ Private Service Connect ใช้งานได้
- วิธีเชื่อมต่อจาก VM ไปยังอินสแตนซ์ AlloyDB ผ่าน Private Service Connect