Cách tạo Private Service Connect cho AlloyDB

1. Giới thiệu

Trong lớp học lập trình này, bạn sẽ tìm hiểu cách triển khai một Private Service Connect cho AlloyDB và cách truy cập vào dịch vụ AlloyDB bằng Private Service Connect đã triển khai.

1cd460fc0abeee2c.png

Bạn có thể xem thêm thông tin về Private Service Connect tại đây.

Điều kiện tiên quyết

  • Có kiến thức cơ bản về Google Cloud Console
  • Kỹ năng cơ bản về giao diện dòng lệnh và Google Cloud Shell

Kiến thức bạn sẽ học được

  • Cách triển khai một cụm và thực thể AlloyDB
  • Cách triển khai Private Service Connect
  • Cách kết nối từ một VM đến một thực thể AlloyDB thông qua Private Service Connect

Bạn cần có

  • Tài khoản Google Cloud và dự án trên Google Cloud
  • Một trình duyệt web như Chrome

2. Thiết lập và yêu cầu

Thiết lập môi trường theo tốc độ của riêng bạn

  1. Đăng nhập vào Google Cloud Console rồi tạo một dự án mới hoặc sử dụng lại một dự án hiện có. Nếu chưa có tài khoản Gmail hoặc Google Workspace, bạn phải tạo một tài khoản.

295004821bab6a87.png37d264871000675d.png5e3ff691252acf41.png

  • Tên dự án là tên hiển thị của những người tham gia dự án này. Đây là một chuỗi ký tự mà các API của Google không sử dụng. Bạn có thể cập nhật thông tin này bất cứ lúc nào.
  • Mã dự án phải là duy nhất trên tất cả các dự án trên Google Cloud và không thể thay đổi (bạn không thể thay đổi sau khi đã đặt). Cloud Console sẽ tự động tạo một chuỗi duy nhất; thường thì bạn không cần quan tâm đến chuỗi này. Trong hầu hết các lớp học lập trình, bạn sẽ cần tham chiếu đến Mã dự án (thường được xác định là PROJECT_ID). Nếu không thích mã nhận dạng được tạo, bạn có thể tạo một mã nhận dạng ngẫu nhiên khác. Hoặc bạn có thể thử tên người dùng của riêng mình để xem tên đó có dùng được hay không. Bạn không thể thay đổi thông tin này sau bước này và thông tin này sẽ giữ nguyên trong suốt thời gian diễn ra dự án.
  • Để bạn biết, có một giá trị thứ ba là Số dự án mà một số API sử dụng. Tìm hiểu thêm về cả 3 giá trị này trong tài liệu.

Thận trọng: Mã dự án là mã duy nhất trên toàn cầu và không ai khác có thể sử dụng mã này sau khi bạn chọn. Bạn là người dùng duy nhất của mã nhận dạng đó. Ngay cả khi một dự án bị xoá, bạn cũng không thể sử dụng lại mã nhận dạng đó

  1. Tiếp theo, bạn cần bật tính năng thanh toán trong Cloud Console để sử dụng các tài nguyên/API trên Cloud. Việc thực hiện lớp học lập trình này sẽ không tốn nhiều chi phí, nếu có. Để tắt các tài nguyên nhằm tránh bị tính phí ngoài phạm vi hướng dẫn này, bạn có thể xoá các tài nguyên đã tạo hoặc xoá dự án. Người dùng mới của Google Cloud đủ điều kiện tham gia chương trình Dùng thử miễn phí trị giá 300 USD.

Khởi động Cloud Shell

Mặc dù có thể vận hành Google Cloud từ xa trên máy tính xách tay, nhưng trong lớp học lập trình này, bạn sẽ sử dụng Google Cloud Shell, một môi trường dòng lệnh chạy trên Cloud.

Trên Bảng điều khiển Google Cloud, hãy nhấp vào biểu tượng Cloud Shell trên thanh công cụ ở trên cùng bên phải:

55efc1aaa7a4d3ad.png

Quá trình này chỉ mất vài phút để cung cấp và kết nối với môi trường. Khi quá trình này kết thúc, bạn sẽ thấy như sau:

7ffe5cbb04455448.png

Máy ảo này được trang bị tất cả các công cụ phát triển mà bạn cần. Nó cung cấp một thư mục chính có dung lượng 5 GB và chạy trên Google Cloud, giúp tăng cường đáng kể hiệu suất mạng và hoạt động xác thực. Bạn có thể thực hiện mọi thao tác trong lớp học lập trình này trong trình duyệt. Bạn không cần cài đặt bất cứ thứ gì.

3. Trước khi bắt đầu

Bật API

Xin lưu ý rằng một số tài nguyên mà bạn bật sẽ phát sinh chi phí nếu bạn không sử dụng bậc khuyến mãi. Trong trường hợp bình thường, nếu tất cả các tài nguyên bị huỷ sau khi hoàn thành phòng thí nghiệm, thì chi phí của tất cả các tài nguyên sẽ không vượt quá 5 USD. Bạn nên kiểm tra thông tin thanh toán và đảm bảo rằng bạn chấp nhận việc này.

Trong Cloud Shell, hãy đảm bảo bạn đã thiết lập mã dự án:

Thông thường, mã dự án sẽ xuất hiện trong dấu ngoặc đơn trong dấu nhắc lệnh trong trình bao trên đám mây như trong hình:

edbf16c09a01fbab.png

gcloud config set project [YOUR-PROJECT-ID]

Một cửa sổ sẽ bật lên yêu cầu Uỷ quyền Cloud Shell. Vui lòng nhấp vào Uỷ quyền

33fbeb082b3aac19.png

Sau đó, hãy đặt biến môi trường PROJECT_ID thành mã dự án Google Cloud của bạn:

PROJECT_ID=$(gcloud config get-value project)

Đặt biến môi trường REGION và ZONE thành khu vực và vùng bạn muốn:

REGION=europe-west4
ZONE=europe-west4-a

Bật tất cả các dịch vụ cần thiết:

gcloud services enable compute.googleapis.com \
                       alloydb.googleapis.com \
                       dns.googleapis.com \
                       servicenetworking.googleapis.com \
                       iam.googleapis.com 

Kết quả đầu ra dự kiến:

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. Triển khai một Cụm AlloyDB

Trong Cloud Shell, bạn có thể dùng dòng lệnh để tạo một cụm AlloyDB mới có bật Private Service Connect:

gcloud alloydb clusters create alloydb-cluster-01 \
    --password=changeme \
    --region=$REGION \
    --project=$PROJECT_ID \
    --enable-private-service-connect

Kết quả đầu ra dự kiến

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.

Bây giờ, hãy tạo một phiên bản chính 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

Kết quả đầu ra dự kiến

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. Thiết lập Private Service Connect

Đối với các tác vụ liên quan đến mạng sau đây, giả định là bạn đã có một VPC có tên là default.

Đặt trước một địa chỉ IP nội bộ

Tìm dải CIDR của mạng con VPC trong khu vực GCP được đề cập trong biến môi trường REGION và chọn một địa chỉ IP trống trong dải CIDR này cho điểm cuối Privat Service Connect:

gcloud compute networks subnets describe default \
             --region=$REGION --project=$PROJECT_ID \
             --format="value(ipCidrRange)"

Kết quả đầu ra dự kiến

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

Dành riêng một địa chỉ IP nội bộ cho điểm cuối Private Service Connect trong dải CIDR mạng con VPC dẫn xuất ở trên:

gcloud compute addresses create alloydb-psc \
--project=$PROJECT_ID \
--region=$REGION \
--subnet=default \
--addresses=10.164.0.10

Kết quả đầu ra dự kiến

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].

Xác minh rằng địa chỉ IP nội bộ đã được đặt trước và trạng thái ĐÃ ĐẶT TRƯỚC xuất hiện cho địa chỉ IP đó.

gcloud compute addresses list --project=$PROJECT_ID \
     --filter="name=alloydb-psc"

Kết quả đầu ra dự kiến

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

Lấy URI tệp đính kèm dịch vụ

Sau khi tạo một thực thể AlloyDB có bật Private Service Connect, hãy lấy URI của tệp đính kèm dịch vụ rồi dùng URI đó để tạo điểm cuối Private Service Connect bằng địa chỉ IP nội bộ đã đặt trước ở trên.

gcloud alloydb instances describe alloydb-instance-01 \
    --cluster=alloydb-cluster-01 \
    --region="$REGION" \
    --format="value(pscInstanceConfig.serviceAttachmentLink)" | \
    sed 's|.*/projects/|projects/|'

Kết quả đầu ra dự kiến

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

Tạo Kết nối dịch vụ riêng tư

Tạo điểm cuối Private Service Connect và trỏ điểm cuối đó đến URI đính kèm dịch vụ 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

Kết quả đầu ra dự kiến

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].

Xác minh rằng điểm cuối có thể kết nối với tệp đính kèm dịch vụ:

gcloud compute forwarding-rules describe alloydb-psc-ep \
--project=$PROJECT_ID \
--region=$REGION \
--format="value(pscConnectionStatus)"

Kết quả đầu ra dự kiến

student@cloudshell:~ (psc-alloydb-test)$ gcloud compute forwarding-rules describe alloydb-psc-ep \
--project=$PROJECT_ID \
--region=$REGION \
--format="value(pscConnectionStatus)"
ACCEPTED

Định cấu hình một vùng DNS được quản lý

Để thêm tên DNS được đề xuất cho thực thể AlloyDB, tốt nhất là bạn nên tạo một Vùng DNS riêng trong mạng VPC tương ứng:

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

Kết quả đầu ra dự kiến

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].

Thêm bản ghi DNS cho Private Service Connect

Lấy bản ghi DNS được đề xuất cho phiên bản AlloyDB:

gcloud alloydb instances describe alloydb-instance-01 \
--cluster=alloydb-cluster-01 --region=$REGION --project=$PROJECT_ID \
--format="value(pscInstanceConfig.pscDnsName)"

Kết quả đầu ra dự kiến

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.

Thêm bản ghi DNS được đề xuất vào vùng DNS được quản lý

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

Kết quả đầu ra dự kiến

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. Chuẩn bị Máy ảo Google Compute Engine

Triển khai máy ảo Google Compute Engine

Máy ảo (VM) sẽ được dùng để kết nối với thực thể 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

Kết quả đầu ra dự kiến

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

Cài đặt ứng dụng Postgres

Tạo một Cloud NAT cho lưu lượng truy cập đi ra Internet mà VM có thể truy cập vào các kho lưu trữ Linux:

gcloud compute routers create cloud-nat-router \
    --network=default \
    --region=$REGION

Kết quả đầu ra dự kiến

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

Kết quả đầu ra dự kiến

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.

Cài đặt phần mềm máy khách PostgreSQL trên máy ảo đã triển khai

Kết nối với máy ảo:

gcloud compute ssh --zone $ZONE "alloydb-client" --tunnel-through-iap --project $PROJECT_ID

Kết quả đầu ra dự kiến

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'.

Cài đặt lệnh chạy phần mềm bên trong máy ảo:

sudo apt-get update
sudo apt-get install --yes postgresql-client

Kết quả đầu ra dự kiến

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. Kết nối với phiên bản AlloyDB for Postgres thông qua Private Service Connect

Kết nối với thực thể

psql "dbname=postgres user=postgres host=69f67b56-9fa2-4480-89f6-3f1a38ee51cb.6a495301-4802-4ec1-bb9b-69b57d2616ec.europe-west4.alloydb-psc.goog."

Kết quả đầu ra dự kiến

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=> 

Tạo và kiểm thử cơ sở dữ liệu

Tạo cơ sở dữ liệu

CREATE DATABASE company;

Kết quả đầu ra dự kiến

postgres=> CREATE DATABASE company;
CREATE DATABASE
postgres=> 

Liệt kê tất cả cơ sở dữ liệu

\l

Kết quả đầu ra dự kiến

     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)

Kết nối với cơ sở dữ liệu nhân viên

\c company

Kết quả đầu ra dự kiến

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=>

Tạo bảng trong cơ sở dữ liệu của công ty

CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    first VARCHAR(255) NOT NULL,
    last VARCHAR(255) NOT NULL,
    salary DECIMAL (10, 2)
);

Kết quả đầu ra dự kiến

company=> CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    first VARCHAR(255) NOT NULL,
    last VARCHAR(255) NOT NULL,
    salary DECIMAL (10, 2)
);
CREATE TABLE

Chèn dữ liệu vào bảng nhân viên của cơ sở dữ liệu công ty

INSERT INTO employees (first, last, salary) VALUES
    ('Max', 'Mustermann', 5000.00),
    ('Anna', 'Schmidt', 7000.00),
    ('Peter', 'Mayer', 6000.00);

Kết quả đầu ra dự kiến

company=> INSERT INTO employees (first, last, salary) VALUES
    ('Max', 'Mustermann', 5000.00),
    ('Anna', 'Schmidt', 7000.00),
    ('Peter', 'Mayer', 6000.00);
INSERT 0 3
company=>

Truy vấn bảng nhân viên

SELECT * FROM employees;

Kết quả đầu ra dự kiến

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=> 

Thoát khỏi cơ sở dữ liệu Postgres và VM rồi quay lại Cloud Shell:

\q
exit

Kết quả đầu ra dự kiến

company=> \q
student_tampferl_altostrat_com@alloydb-client:~$ exit
logout
Connection to compute.3358765012415130370 closed.
student@cloudshell:~ (psc-alloydb-test)$ 

8. Dọn dẹp môi trường

Huỷ tất cả tài nguyên trên Google Cloud trong dự án khi bạn hoàn tất bài thực hành.

Xoá cụm AlloyDB

Trong Cloud Shell, hãy xoá cụm AlloyDB cho Postgres:

gcloud alloydb clusters delete alloydb-cluster-01 --region=$REGION --force --quiet

Kết quả đầu ra dự kiến

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.

Xoá máy ảo Google Compute Engine

Trong Cloud Shell, hãy xoá VM:

gcloud compute instances delete alloydb-client \
    --zone=$ZONE \
    --quiet

Kết quả đầu ra dự kiến

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].

Xoá các thành phần mạng

Xoá các thành phần liên quan đến mạng: Cloud NAT, Cloud Router, điểm cuối Private Service Connect, địa chỉ IP nội bộ được dự phòng, bản ghi DNS và vùng được quản lý DNS.

Xoá Cloud NAT:

gcloud compute routers nats delete cloud-nat \
    --router=cloud-nat-router \
    --region=$REGION \
    --quiet

Kết quả đầu ra dự kiến

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].

Xoá Cloud Router:

gcloud compute routers delete cloud-nat-router \
    --region=$REGION \
    --quiet 

Kết quả đầu ra dự kiến

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].

Xoá điểm cuối Private Service Connect:

gcloud compute forwarding-rules delete alloydb-psc-ep \
    --project=$PROJECT_ID \
    --region=$REGION \
    --quiet

Kết quả đầu ra dự kiến

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].

Hủy đặt trước địa chỉ IP nội bộ được đặt trước::

gcloud compute addresses delete alloydb-psc \
   --project=$PROJECT_ID \
   --region=$REGION \
   --quiet

Kết quả đầu ra dự kiến

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].

Xoá bản ghi 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

Kết quả đầu ra dự kiến

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].

Xoá vùng DNS được quản lý:

gcloud dns managed-zones delete alloydb-dns \
   --project=$PROJECT_ID \
   --quiet

Kết quả đầu ra dự kiến

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. Xin chúc mừng

Chúc mừng bạn đã hoàn thành lớp học lập trình này.

Nội dung đã đề cập

  • Cách triển khai cụm và thực thể AlloyDB
  • Cách triển khai Private Service Connect
  • Cách kết nối từ một VM đến một thực thể AlloyDB thông qua Private Service Connect

10. Khảo sát

Bạn sẽ sử dụng hướng dẫn này như thế nào?

Chỉ đọc qua Đọc và hoàn thành bài tập