วิธีสร้าง Private Service Connect สำหรับ AlloyDB

1. บทนำ

ใน Codelab นี้ คุณจะได้เรียนรู้วิธีติดตั้งใช้งานการเชื่อมต่อบริการส่วนตัวสำหรับ AlloyDB และวิธีเข้าถึงบริการ AlloyDB โดยใช้ Private Service Connect ที่ติดตั้งใช้งาน

1cd460fc0abeee2c.png

ดูข้อมูลเพิ่มเติมเกี่ยวกับ Private Service Connect ได้ที่นี่

ข้อกำหนดเบื้องต้น

  • ความเข้าใจพื้นฐานเกี่ยวกับคอนโซล Google Cloud
  • ทักษะพื้นฐานในอินเทอร์เฟซบรรทัดคำสั่งและ Google Cloud Shell

สิ่งที่คุณจะได้เรียนรู้

  • วิธีติดตั้งใช้งานคลัสเตอร์และอินสแตนซ์ AlloyDB
  • วิธีติดตั้งใช้งาน Private Service Connect
  • วิธีเชื่อมต่อจาก VM ไปยังอินสแตนซ์ AlloyDB ผ่าน Private Service Connect

สิ่งที่คุณต้องมี

  • บัญชี Google Cloud และโปรเจ็กต์ Google Cloud
  • เว็บเบราว์เซอร์ เช่น Chrome

2. การตั้งค่าและข้อกำหนด

การตั้งค่าสภาพแวดล้อมแบบเรียนรู้ด้วยตนเอง

  1. ลงชื่อเข้าใช้ คอนโซล Google Cloud แล้วสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี

295004821bab6a87.png 37d264871000675d.png 5e3ff691252acf41.png

  • ชื่อโปรเจ็กต์คือชื่อที่แสดงสำหรับผู้เข้าร่วมโปรเจ็กต์นี้ ซึ่งเป็นสตริงอักขระที่ Google APIs ไม่ได้ใช้ โดยคุณจะอัปเดตได้ทุกเมื่อ
  • รหัสโปรเจ็กต์ต้องไม่ซ้ำกันในโปรเจ็กต์ Google Cloud ทั้งหมดและเปลี่ยนแปลงไม่ได้ (เปลี่ยนไม่ได้หลังจากตั้งค่าแล้ว) Cloud Console จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ซึ่งโดยปกติแล้วคุณไม่จำเป็นต้องสนใจว่าสตริงนั้นคืออะไร ใน Codelab ส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (โดยปกติจะระบุเป็น PROJECT_ID) หากไม่ชอบรหัสที่สร้างขึ้น คุณก็สร้างรหัสแบบสุ่มอีกรหัสหนึ่งได้ หรือคุณจะลองใช้ชื่อของคุณเองเพื่อดูว่ามีชื่อนั้นหรือไม่ก็ได้ คุณจะเปลี่ยนแปลงรหัสนี้หลังจากขั้นตอนนี้ไม่ได้ และรหัสจะยังคงอยู่ตลอดระยะเวลาของโปรเจ็กต์
  • โปรดทราบว่ายังมีค่าที่ 3 ซึ่งคือหมายเลขโปรเจ็กต์ที่ API บางตัวใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 นี้ได้ในเอกสารประกอบ

ข้อควรระวัง: รหัสโปรเจ็กต์จะไม่ซ้ำกันทั่วโลกและบุคคลอื่นจะใช้รหัสนี้ไม่ได้หลังจากที่คุณเลือกแล้ว คุณเป็นผู้ใช้รหัสดังกล่าวเพียงคนเดียว แม้ว่าจะลบโปรเจ็กต์ไปแล้ว แต่ก็ไม่สามารถใช้รหัสซ้ำได้

  1. จากนั้นคุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร/API ของ Cloud การทำตาม Codelab นี้จะไม่เสียค่าใช้จ่ายมากนัก หรืออาจไม่เสียเลย หากต้องการปิดทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินนอกเหนือจากบทแนะนำนี้ คุณสามารถลบทรัพยากรที่สร้างขึ้นหรือลบโปรเจ็กต์ได้ ผู้ใช้ Google Cloud รายใหม่มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรีมูลค่า$300 USD

เริ่มต้น Cloud Shell

แม้ว่าคุณจะใช้งาน Google Cloud จากระยะไกลในแล็ปท็อปได้ แต่ใน Codelab นี้คุณจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมบรรทัดคำสั่งที่ทำงานในระบบคลาวด์

จาก คอนโซล Google Cloud ให้คลิกไอคอน Cloud Shell ในแถบเครื่องมือด้านขวาบน

55efc1aaa7a4d3ad.png

การจัดสรรและเชื่อมต่อกับสภาพแวดล้อมจะใช้เวลาเพียงไม่กี่นาที เมื่อเสร็จแล้ว คุณควรเห็นข้อความคล้ายกับตัวอย่างต่อไปนี้

7ffe5cbb04455448.png

เครื่องเสมือนนี้มาพร้อมเครื่องมือพัฒนาซอฟต์แวร์ทั้งหมดที่คุณต้องการ โดยมีไดเรกทอรีหลักแบบถาวรขนาด 5 GB และทำงานบน Google Cloud ซึ่งช่วยเพิ่มประสิทธิภาพเครือข่ายและการตรวจสอบสิทธิ์ได้อย่างมาก คุณสามารถทำงานทั้งหมดใน Codelab นี้ได้ภายในเบราว์เซอร์ คุณไม่จำเป็นต้องติดตั้งอะไร

3. ก่อนเริ่มต้น

เปิดใช้ API

โปรดทราบว่าทรัพยากรบางอย่างที่คุณเปิดใช้จะทำให้เกิดค่าใช้จ่ายหากคุณไม่ได้ใช้ระดับโปรโมชัน ในสถานการณ์ปกติ หากคุณทำลายทรัพยากรทั้งหมดเมื่อ Lab เสร็จสมบูรณ์ ค่าใช้จ่ายของทรัพยากรทั้งหมดจะไม่เกิน $5 เราขอแนะนำให้ตรวจสอบการเรียกเก็บเงินและดูว่าการดำเนินการนี้เหมาะกับคุณหรือไม่

ใน Cloud Shell ให้ตรวจสอบว่าได้ตั้งค่ารหัสโปรเจ็กต์แล้ว

โดยปกติแล้ว รหัสโปรเจ็กต์จะแสดงในวงเล็บใน Command Prompt ใน Cloud Shell ดังที่แสดงในรูปภาพ

edbf16c09a01fbab.png

gcloud config set project [YOUR-PROJECT-ID]

หน้าต่างจะปรากฏขึ้นมาเพื่อขอการให้สิทธิ์ Cloud Shell โปรดคลิกให้สิทธิ์

33fbeb082b3aac19.png

จากนั้นตั้งค่าตัวแปรสภาพแวดล้อม 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 นี้สำหรับปลายทาง Private 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 ของ Service Attachment และใช้เพื่อสร้างปลายทาง 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 ของ Service Attachment ของ 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

แทรกข้อมูลลงในตารางพนักงานของฐานข้อมูลบริษัท

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 สำหรับ 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

10. แบบสำรวจ

คุณจะใช้บทแนะนำนี้อย่างไร

อ่านอย่างเดียว อ่านและทำแบบฝึกหัดให้เสร็จ