Kết nối AlloyDB với Oracle thông qua VPN của Google

1. Giới thiệu

Đôi khi, bạn không thể chuyển khối lượng công việc từ cơ sở dữ liệu Oracle sang AlloyDB vì nhiều lý do. Trong những trường hợp như vậy, nếu muốn cung cấp dữ liệu trong AlloyDB để báo cáo hoặc xử lý thêm, chúng ta có thể tận dụng FDW (Trình bao bọc dữ liệu bên ngoài) của Oracle. FDW của Oracle cho phép truy vấn dữ liệu từ một cơ sở dữ liệu Oracle từ xa và trình bày dữ liệu từ xa thông qua các khung hiển thị, khiến dữ liệu đó xuất hiện như thể nằm trong AlloyDB.

Trong lớp học lập trình này, bạn sẽ tìm hiểu cách sử dụng Oracle FDW để kết nối cơ sở dữ liệu AlloyDB với cơ sở dữ liệu Oracle được triển khai trong một mạng riêng biệt bằng dịch vụ VPN.

f920b8427b6fccb3.png

Biểu đồ ở trên cho thấy cụm AlloyDB và thực thể GCE instance-1 ở bên trái được triển khai trong một VPC có mạng mặc định và thực thể GCE ora-xe-01 được triển khai trong một VPC khác có tên mạng là $PROJECT_ID-vpc-02. VPC thứ nhất và thứ hai được kết nối bằng VPN trên đám mây với các tuyến đường đã thiết lập, cho phép các phiên bản Oracle và AlloyDB giao tiếp với nhau.

AlloyDB đến Oracle qua VPN AlloyDB đến Oracle qua VPN

Bạn có thể xem thêm thông tin về tiện ích FDW của Oracle tại đây.

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

  • Hiểu biết cơ bản về Google Cloud Console
  • Kỹ năng cơ bản về giao diện dòng lệnh và Google Shell
  • Có kiến thức cơ bản về cơ sở dữ liệu PostgreSQL và Oracle

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

  • Cách triển khai Cụm AlloyDB
  • Cách kết nối với AlloyDB
  • Cách định cấu hình và triển khai cơ sở dữ liệu Oracle mẫu
  • Cách thiết lập VPN giữa hai mạng VPC
  • Cách định cấu hình tiện ích FDW của Oracle

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.

fbef9caa1602edd0.png

a99b7ace416376c4.png

5e3ff691252acf41.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 luôn có thể cập nhật thông tin này.
  • Mã dự án là mã 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 mã này 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ó được chấp nhận hay không. Bạn không thể thay đổi tên này sau bước này và tên này sẽ tồn tại trong suốt thời gian của dự án.
  • Để bạn nắm được thông tin, 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.
  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 đám mây. 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

Kết quả:

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

gcloud config set project [YOUR-PROJECT-ID]
PROJECT_ID=$(gcloud config get-value project)

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

gcloud services enable alloydb.googleapis.com \
                       compute.googleapis.com \
                       cloudresourcemanager.googleapis.com \
                       servicenetworking.googleapis.com \
                       vpcaccess.googleapis.com

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

student@cloudshell:~ (gleb-test-short-004)$ gcloud services enable alloydb.googleapis.com \
                       compute.googleapis.com \
                       cloudresourcemanager.googleapis.com \
                       servicenetworking.googleapis.com \
                       vpcaccess.googleapis.com 
Operation "operations/acf.p2-404051529011-664c71ad-cb2b-4ab4-86c1-1f3157d70ba1" finished successfully.

Định cấu hình khu vực mặc định thành us-central1 hoặc bất kỳ khu vực nào khác mà bạn muốn. Trong phòng thí nghiệm này, chúng ta sẽ sử dụng khu vực us-central1.

gcloud config set compute/region us-central1

4. Triển khai Cụm AlloyDB

Trước khi tạo một cụm AlloyDB, chúng ta cần phân bổ một dải IP riêng tư trong VPC để dùng cho phiên bản AlloyDB trong tương lai. Sau đó, chúng ta sẽ có thể tạo cụm và phiên bản.

Tạo dải IP riêng tư

Chúng ta cần định cấu hình chế độ cài đặt Private Service Access trong VPC cho AlloyDB. Giả định ở đây là chúng ta có mạng VPC "mặc định" trong dự án và mạng này sẽ được dùng cho mọi hành động.

Tạo dải IP riêng tư:

gcloud compute addresses create psa-range \
    --global \
    --purpose=VPC_PEERING \
    --prefix-length=16 \
    --description="VPC private service access" \
    --network=default

Tạo kết nối riêng tư bằng dải IP được phân bổ:

gcloud services vpc-peerings connect \
    --service=servicenetworking.googleapis.com \
    --ranges=psa-range \
    --network=default

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ gcloud compute addresses create psa-range \
    --global \
    --purpose=VPC_PEERING \
    --prefix-length=16 \
    --description="VPC private service access" \
    --network=default
Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/addresses/psa-range].

student@cloudshell:~ (test-project-402417)$ gcloud services vpc-peerings connect \
    --service=servicenetworking.googleapis.com \
    --ranges=psa-range \
    --network=default
Operation "operations/pssn.p24-4470404856-595e209f-19b7-4669-8a71-cbd45de8ba66" finished successfully.

student@cloudshell:~ (test-project-402417)$

Tạo cụm AlloyDB

Tạo một cụm AlloyDB ở khu vực mặc định:

export PGPASSWORD=`openssl rand -hex 12`
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
gcloud alloydb clusters create $ADBCLUSTER \
    --password=$PGPASSWORD \
    --network=default \
    --region=$REGION

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ export PGPASSWORD=`openssl rand -base64 12`
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
gcloud alloydb clusters create $ADBCLUSTER \
    --password=$PGPASSWORD \
    --network=default \
    --region=$REGION
Operation ID: operation-1697655441138-6080235852277-9e7f04f5-2012fce4
Creating cluster...done.                                                                                                                                                                                                                                                           

Ghi lại mật khẩu PostgreSQL để sử dụng sau này:

echo $PGPASSWORD

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ echo $PGPASSWORD
bbefbfde7601985b0dee5723

Tạo phiên bản chính của AlloyDB

Tạo một phiên bản chính AlloyDB cho cụm của chúng ta:

export REGION=us-central1
gcloud alloydb instances create $ADBCLUSTER-pr \
    --instance-type=PRIMARY \
    --cpu-count=2 \
    --region=$REGION \
    --cluster=$ADBCLUSTER

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ gcloud alloydb instances create $ADBCLUSTER-pr \
    --instance-type=PRIMARY \
    --cpu-count=2 \
    --region=$REGION \
    --availability-type ZONAL \
    --cluster=$ADBCLUSTER
Operation ID: operation-1697659203545-6080315c6e8ee-391805db-25852721
Creating instance...done.                                                                                                                                                                                                                                                     

5. Kết nối với AlloyDB

Để làm việc với AlloyDB và chạy các lệnh như tạo cơ sở dữ liệu, bật tiện ích và các lệnh khác, chúng ta cần có một môi trường phát triển. Đó có thể là bất kỳ công cụ tiêu chuẩn nào hoạt động với PostgreSQL. Trong trường hợp này, chúng ta sẽ sử dụng một ứng dụng tiêu chuẩn cho PostgreSQL được cài đặt trên một hộp Linux.

AlloyDB được triển khai bằng cách chỉ sử dụng kết nối riêng tư, vì vậy, chúng ta cần một VM đã cài đặt ứng dụng PostgreSQL để làm việc với cơ sở dữ liệu.

Triển khai máy ảo GCE

Tạo một máy ảo GCE trong cùng một khu vực và VPC với cụm AlloyDB.

Trong Cloud Shell, hãy thực thi:

export ZONE=us-central1-a
gcloud compute instances create instance-1 \
    --zone=$ZONE \
    --scopes=https://www.googleapis.com/auth/cloud-platform

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ export ZONE=us-central1-a
student@cloudshell:~ (test-project-402417)$ gcloud compute instances create instance-1 \
    --zone=$ZONE \
    --scopes=https://www.googleapis.com/auth/cloud-platform
Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/zones/us-central1-a/instances/instance-1].
NAME: instance-1
ZONE: us-central1-a
MACHINE_TYPE: n1-standard-1
PREEMPTIBLE: 
INTERNAL_IP: 10.128.0.2
EXTERNAL_IP: 34.71.192.233
STATUS: RUNNING

Cài đặt Postgres Client (Máy khách Postgres)

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 instance-1 --zone=us-central1-a

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ gcloud compute ssh instance-1 --zone=us-central1-a
Updating project ssh metadata...working..Updated [https://www.googleapis.com/compute/v1/projects/test-project-402417].                                                                                                                                                         
Updating project ssh metadata...done.                                                                                                                                                                                                                                              
Waiting for SSH key to propagate.
Warning: Permanently added 'compute.5110295539541121102' (ECDSA) to the list of known hosts.
Linux instance-1 5.10.0-26-cloud-amd64 #1 SMP Debian 5.10.197-1 (2023-09-29) 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.
student@instance-1:~$ 

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 trên bảng điều khiển:

student@instance-1:~$ sudo apt-get update
sudo apt-get install --yes postgresql-client
Get:1 https://packages.cloud.google.com/apt google-compute-engine-bullseye-stable InRelease [5146 B]
Get:2 https://packages.cloud.google.com/apt cloud-sdk-bullseye InRelease [6406 B]   
Hit:3 https://deb.debian.org/debian bullseye InRelease  
Get:4 https://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
Get:5 https://packages.cloud.google.com/apt google-compute-engine-bullseye-stable/main amd64 Packages [1930 B]
Get:6 https://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
Get:7 https://deb.debian.org/debian bullseye-backports InRelease [49.0 kB]
...redacted...
update-alternatives: using /usr/share/postgresql/13/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
Setting up postgresql-client (13+225) ...
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for libc-bin (2.31-13+deb11u7) ...

Kết nối với phiên bản

Kết nối với phiên bản chính từ máy ảo bằng psql.

Trong cùng một thẻ Cloud Shell có phiên SSH đã mở đến VM instance-1.

Sử dụng giá trị mật khẩu AlloyDB (PGPASSWORD) đã ghi chú và mã nhận dạng cụm AlloyDB để kết nối với AlloyDB từ máy ảo GCE:

export PGPASSWORD=<Noted password>
ADBCLUSTER=<your AlloyDB cluster name>
REGION=us-central1
INSTANCE_IP=$(gcloud alloydb instances describe $ADBCLUSTER-pr --cluster=$ADBCLUSTER --region=$REGION --format="value(ipAddress)")
export INSTANCE_IP=<AlloyDB Instance IP>
psql "host=$INSTANCE_IP user=postgres sslmode=require"

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@instance-1:~$ export PGPASSWORD=CQhOi5OygD4ps6ty
student@instance-1:~$ ADBCLUSTER=alloydb-aip-01
student@instance-1:~$ REGION=us-central1
student@instance-1:~$ INSTANCE_IP=$(gcloud alloydb instances describe $ADBCLUSTER-pr --cluster=$ADBCLUSTER --region=$REGION --format="value(ipAddress)")
gleb@instance-1:~$ psql "host=$INSTANCE_IP user=postgres sslmode=require"
psql (13.13 (Debian 13.13-0+deb11u1), server 14.7)
WARNING: psql major version 13, server major version 14.
         Some psql features might not work.
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=> 

6. Tạo cơ sở dữ liệu Oracle mẫu

Cơ sở dữ liệu Oracle mẫu của chúng tôi sẽ được triển khai trong một VPC riêng biệt để phản ánh các trường hợp khi cơ sở dữ liệu này được triển khai tại chỗ hoặc trong bất kỳ môi trường nào khác tách biệt với VPC của cụm AlloyDB.

Tạo VPC thứ hai

Sử dụng một thẻ đang mở của Cloud Shell hoặc một thiết bị đầu cuối dòng lệnh có Google Cloud SDK, hãy thực thi:

PROJECT_ID=$(gcloud config get-value project)
REGION=us-central1
gcloud compute networks create $PROJECT_ID-vpc-02 --project=$PROJECT_ID --description=Custom\ VPC\ for\ $PROJECT_ID\ project --subnet-mode=custom --mtu=1460 --bgp-routing-mode=regional
gcloud compute networks subnets create $PROJECT_ID-vpc-02-$REGION --project=$PROJECT_ID --range=10.110.0.0/24 --stack-type=IPV4_ONLY --network=$PROJECT_ID-vpc-02 --region=$REGION

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ PROJECT_ID=$(gcloud config get-value project)
REGION=us-central1
gcloud compute networks create $PROJECT_ID-vpc-02 --project=$PROJECT_ID --description=Custom\ VPC\ for\ $PROJECT_ID\ project --subnet-mode=custom --mtu=1460 --bgp-routing-mode=regional
gcloud compute networks subnets create $PROJECT_ID-vpc-02-$REGION --project=$PROJECT_ID --range=10.110.0.0/24 --stack-type=IPV4_ONLY --network=$PROJECT_ID-vpc-02 --region=$REGION
Your active configuration is: [cloudshell-3726]
Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/networks/test-project-402417-vpc-02].
NAME: test-project-402417-vpc-02
SUBNET_MODE: CUSTOM
BGP_ROUTING_MODE: REGIONAL
IPV4_RANGE: 
GATEWAY_IPV4: 

Instances on this network will not be reachable until firewall rules
are created. As an example, you can allow all internal traffic between
instances as well as SSH, RDP, and ICMP by running:

$ gcloud compute firewall-rules create <FIREWALL_NAME> --network test-project-402417-vpc-02 --allow tcp,udp,icmp --source-ranges <IP_RANGE>
$ gcloud compute firewall-rules create <FIREWALL_NAME> --network test-project-402417-vpc-02 --allow tcp:22,tcp:3389,icmp

Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/regions/us-central1/subnetworks/test-project-402417-vpc-02-us-central1].
NAME: test-project-402417-vpc-02-us-central1
REGION: us-central1
NETWORK: test-project-402417-vpc-02
RANGE: 10.110.0.0/24
STACK_TYPE: IPV4_ONLY
IPV6_ACCESS_TYPE: 

Định cấu hình các quy tắc tối thiểu về tường lửa trong VPC thứ hai để chẩn đoán cơ bản và kết nối SSH.

gcloud compute firewall-rules create $PROJECT_ID-vpc-02-allow-icmp --project=$PROJECT_ID --network=$PROJECT_ID-vpc-02 --description=Allows\ ICMP\ connections\ from\ any\ source\ to\ any\ instance\ on\ the\ network. --direction=INGRESS --priority=65534 --source-ranges=0.0.0.0/0 --action=ALLOW --rules=icmp
gcloud compute firewall-rules create $PROJECT_ID-vpc-02-allow-ssh --project=$PROJECT_ID --network=$PROJECT_ID-vpc-02 --description=Allows\ TCP\ connections\ from\ any\ source\ to\ any\ instance\ on\ the\ network\ using\ port\ 22. --direction=INGRESS --priority=65534 --source-ranges=0.0.0.0/0 --action=ALLOW --rules=tcp:22

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ gcloud compute firewall-rules create $PROJECT_ID-vpc-02-allow-icmp --project=$PROJECT_ID --network=$PROJECT_ID-vpc-02 --description=Allows\ ICMP\ connections\ from\ any\ source\ to\ any\ instance\ on\ the\ network. --direction=INGRESS --priority=65534 --source-ranges=0.0.0.0/0 --action=ALLOW --rules=icmp
gcloud compute firewall-rules create $PROJECT_ID-vpc-02-allow-ssh --project=$PROJECT_ID --network=$PROJECT_ID-vpc-02 --description=Allows\ TCP\ connections\ from\ any\ source\ to\ any\ instance\ on\ the\ network\ using\ port\ 22. --direction=INGRESS --priority=65534 --source-ranges=0.0.0.0/0 --action=ALLOW --rules=tcp:22
Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/firewalls/test-project-402417-vpc-02-allow-icmp].                                                                                         
Creating firewall...done.                                                                                                                                                                                                                     
NAME: test-project-402417-vpc-02-allow-icmp
NETWORK: test-project-402417-vpc-02
DIRECTION: INGRESS
PRIORITY: 65534
ALLOW: icmp
DENY: 
DISABLED: False
Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/firewalls/test-project-402417-vpc-02-allow-ssh].                                                                                          
Creating firewall...done.                                                                                                                                                                                                                     
NAME: test-project-402417-vpc-02-allow-ssh
NETWORK: test-project-402417-vpc-02
DIRECTION: INGRESS
PRIORITY: 65534
ALLOW: tcp:22
DENY: 
DISABLED: False

Định cấu hình các quy tắc tường lửa trong VPC mặc định và VPC thứ hai để cho phép kết nối giữa các mạng con nội bộ với các cổng cơ sở dữ liệu.

ALLOYDB_NET_IP_RANGES=$(gcloud compute networks subnets list --network=default --filter="region:us-central1" --format="value(ipCidrRange)"),$(gcloud compute addresses list --filter="name:psa-range AND network:default" --format="value(address_range())")
gcloud compute firewall-rules create default-allow-postgres --project=$PROJECT_ID --network=default --description=Allows\ Postgres\ connections\ from\ local\ networks\ to\ postgres\ instance\ on\ the\ network\ using\ port\ 5432. --direction=INGRESS --priority=65534 --source-ranges=10.110.0.0/24 --action=ALLOW --rules=tcp:5432
gcloud compute firewall-rules create $PROJECT_ID-vpc-02-allow-oracle --project=$PROJECT_ID --network=$PROJECT_ID-vpc-02 --description=Allows\ Oracle\ connections\ from\ local\ networks\ to\ Oracle\ instance\ on\ the\ network\ using\ port\ 1521. --direction=INGRESS --priority=65534 --source-ranges=$ALLOYDB_NET_IP_RANGES --action=ALLOW --rules=tcp:1521

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ ALLOYDB_NET_IP_RANGES=$(gcloud compute networks subnets list --network=default --filter="region:us-central1" --format="value(ipCidrRange)"),$(gcloud compute addresses list --filter="name:psa-range AND network:default" --format="value(address_range())")
gcloud compute firewall-rules create default-allow-postgres --project=$PROJECT_ID --network=default --description=Allows\ Postgres\ connections\ from\ local\ networks\ to\ postgres\ instance\ on\ the\ network\ using\ port\ 5432. --direction=INGRESS --priority=65534 --source-ranges=10.110.0.0/24 --action=ALLOW --rules=tcp:5432
gcloud compute firewall-rules create $PROJECT_ID-vpc-02-allow-oracle --project=$PROJECT_ID --network=$PROJECT_ID-vpc-02 --description=Allows\ Oracle\ connections\ from\ local\ networks\ to\ Oracle\ instance\ on\ the\ network\ using\ port\ 1521. --direction=INGRESS --priority=65534 --source-ranges=$ALLOYDB_NET_IP_RANGES --action=ALLOW --rules=tcp:1521
Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/firewalls/default-allow-postgres].                                                                                                  
Creating firewall...done.                                                                                                                                                                                                                     
NAME: default-allow-postgres
NETWORK: default
DIRECTION: INGRESS
PRIORITY: 65534
ALLOW: tcp:5432
DENY: 
DISABLED: False
Creating firewall...working..Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/firewalls/test-project-402417-vpc-02-allow-oracle].                                                                                       
Creating firewall...done.                                                                                                                                                                                                                     
NAME: test-project-402417-vpc-02-allow-oracle
NETWORK: test-project-402417-vpc-02
DIRECTION: INGRESS
PRIORITY: 65534
ALLOW: tcp:1521
DENY: 
DISABLED: False

Triển khai máy ảo GCE trong VPC thứ hai

Triển khai máy ảo GCE bằng mạng con VPC thứ hai cho môi trường Oracle mẫu của chúng tôi. Phiên bản điện toán này sẽ được dùng làm môi trường Oracle thử nghiệm của chúng ta. Các tệp nhị phân cơ sở dữ liệu Oracle XE sẽ được cài đặt tại đây và phiên bản này sẽ được dùng làm máy chủ và làm ứng dụng cho các tác vụ cụ thể của Oracle.

Sử dụng cùng một cloud shell hoặc thiết bị đầu cuối, hãy chạy lệnh để tạo VM:

SERVER_NAME=ora-xe-01
PROJECT_ID=$(gcloud config get-value project)
REGION=us-central1
ZONE=us-central1-a
MACHINE_TYPE=e2-standard-2
SUBNET=$PROJECT_ID-vpc-02-$REGION
DISK_SIZE=50
gcloud compute instances create $SERVER_NAME --project=$PROJECT_ID --zone=$ZONE --machine-type=$MACHINE_TYPE --network-interface=subnet=$SUBNET --create-disk=auto-delete=yes,boot=yes,size=$DISK_SIZE,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-11 AND family!=debian-11-arm64" \
    --format="value(name)"),type=pd-ssd

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ SERVER_NAME=ora-xe-01
PROJECT_ID=$(gcloud config get-value project)
REGION=us-central1
ZONE=us-central1-a
MACHINE_TYPE=e2-standard-2
SUBNET=$PROJECT_ID-vpc-02-$REGION
DISK_SIZE=50
gcloud compute instances create $SERVER_NAME --project=$PROJECT_ID --zone=$ZONE --machine-type=$MACHINE_TYPE --network-interface=subnet=$SUBNET --create-disk=auto-delete=yes,boot=yes,size=$DISK_SIZE,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-11 AND family!=debian-11-arm64" \
    --format="value(name)"),type=pd-ssd
Your active configuration is: [cloudshell-3726]
Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/zones/us-central1-a/instances/ora-xe-01].
WARNING: Some requests generated warnings:
 - Disk size: '50 GB' is larger than image size: '10 GB'. You might need to resize the root repartition manually if the operating system does not support automatic resizing. See https://cloud.google.com/compute/docs/disks/add-persistent-disk#resize_pd for details.

NAME: ora-xe-01
ZONE: us-central1-a
MACHINE_TYPE: e2-standard-2
PREEMPTIBLE: 
INTERNAL_IP: 10.110.0.2
EXTERNAL_IP: 34.121.117.216
STATUS: RUNNING

Tạo Oracle XE

Oracle Express Edition (XE) sẽ được dùng cho các bài kiểm thử. Xin lưu ý rằng việc sử dụng vùng chứa Oracle XE phải tuân theo các điều khoản của giấy phép Điều khoản và điều kiện sử dụng miễn phí của Oracle.

Kết nối với máy ảo đã tạo bằng ssh:

gcloud compute ssh $SERVER_NAME --zone=$ZONE

Trong phiên SSH, hãy triển khai các gói bắt buộc cho loại triển khai docker của Oracle XE. Bạn có thể đọc về các lựa chọn triển khai khác trong tài liệu của Oracle.

sudo apt-get update
sudo apt-get -y install ca-certificates curl gnupg lsb-release
sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $USER

Kết quả đầu ra dự kiến trên bảng điều khiển (đã chỉnh sửa):

student@ora-xe-01:~$ sudo apt-get update
sudo apt-get -y install ca-certificates curl gnupg lsb-release
sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $USER
Get:1 https://packages.cloud.google.com/apt google-compute-engine-bullseye-stable InRelease [5146 B]
Get:2 https://packages.cloud.google.com/apt cloud-sdk-bullseye InRelease [6406 B] 
...
Setting up git (1:2.30.2-1+deb11u2) ...
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for libc-bin (2.31-13+deb11u7) ...
student@ora-xe-01:~$ 

Kết nối lại với máy ảo bằng cách đăng xuất rồi kết nối lại.

exit
gcloud compute ssh $SERVER_NAME --zone=$ZONE

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@ora-xe-01:~$ exit
logout
Connection to 34.132.87.73 closed.
student@cloudshell:~ (test-project-002-410214)$ gcloud compute ssh $SERVER_NAME --zone=$ZONE
Linux ora-xe-01 5.10.0-26-cloud-amd64 #1 SMP Debian 5.10.197-1 (2023-09-29) 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.
Last login: Thu Jan  4 14:51:25 2024 from 34.73.112.191
student@ora-xe-01:~$

Trong phiên SSH mới đến ora-xe-01, hãy thực thi:

ORACLE_PWD=`openssl rand -hex 12`
echo $ORACLE_PWD
docker run -d --name oracle-xe -p 1521:1521 -e ORACLE_PWD=$ORACLE_PWD container-registry.oracle.com/database/express:21.3.0-xe

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@ora-xe-01:~$ ORACLE_PWD=`openssl rand -hex 12`
echo $ORACLE_PWD
docker run -d --name oracle-xe -p 1521:1521 -e ORACLE_PWD=$ORACLE_PWD container-registry.oracle.com/database/express:21.3.0-xe

e36e191b6c298ce6e02a614c
Unable to find image 'container-registry.oracle.com/database/express:21.3.0-xe' locally
21.3.0-xe: Pulling from database/express
2318ff572021: Pull complete 
c6250726c822: Pull complete 
33ac5ea7f7dd: Pull complete 
753e0fae7e64: Pull complete 
Digest: sha256:dcf137aab02d5644aaf9299aae736e4429f9bfdf860676ff398a1458ab8d23f2
Status: Downloaded newer image for container-registry.oracle.com/database/express:21.3.0-xe
9f74e2d37bf49b01785338495e02d79c55c92e4ddd409eddcf45e754fd9044d9

Ghi lại giá trị ORACLE_PWD để sử dụng sau này cho việc kết nối với cơ sở dữ liệu với tư cách là người dùng hệ thống và sys.

Cài đặt giản đồ mẫu

Tạo một tập dữ liệu mẫu trong cơ sở dữ liệu đã cài đặt bằng cách sử dụng giản đồ mẫu tiêu chuẩn của bộ phận Nhân sự.

Trong cùng một phiên SSH:

git clone https://github.com/oracle-samples/db-sample-schemas.git
curl -O https://download.oracle.com/otn_software/java/sqldeveloper/sqlcl-latest.zip
sudo apt-get install -y unzip openjdk-17-jdk
unzip sqlcl-latest.zip
echo 'export PATH=$HOME/sqlcl/bin:$PATH' >>.bashrc 
exec $SHELL

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@ora-xe-01:~$ git clone https://github.com/oracle-samples/db-sample-schemas.git
curl -O https://download.oracle.com/otn_software/java/sqldeveloper/sqlcl-latest.zip
sudo apt-get install -y unzip openjdk-17-jdk
unzip sqlcl-latest.zip
echo 'export PATH=$HOME/sqlcl/bin:$PATH' >>.bashrc 
exec $SHELL
Cloning into 'db-sample-schemas'...
remote: Enumerating objects: 624, done.
remote: Counting objects: 100% (104/104), done.
...
  inflating: sqlcl/lib/sshd-contrib.jar  
  inflating: sqlcl/lib/sshd-putty.jar  
  inflating: sqlcl/lib/dbtools-sqlcl-distribution.jar  
student@ora-xe-01:~$

Trong cùng một phiên SSH:

ORACLE_PWD=<your noted password from the previous step>
cd db-sample-schemas/human_resources/
sql system/$ORACLE_PWD@localhost/XEPDB1 @hr_install.sql

Nhập mật khẩu cho lược đồ HR (bạn có thể dùng mật khẩu mạnh bất kỳ) và chấp nhận các bảng mặc định được đề xuất trong lời nhắc.

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@ora-xe-01:~$ ORACLE_PWD=e36e191b6c298ce6e02a614c
student@ora-xe-01:~$ cd db-sample-schemas/human_resources/
sql system/$ORACLE_PWD@localhost/XEPDB1 @hr_install.sql


SQLcl: Release 23.3 Production on Wed Jan 03 14:38:14 2024

Copyright (c) 1982, 2024, Oracle.  All rights reserved.

Last Successful login time: Wed Jan 03 2024 14:38:17 +00:00

Connected to:
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0


Thank you for installing the Oracle Human Resources Sample Schema.
This installation script will automatically exit your database session
at the end of the installation or if any error is encountered.
The entire installation will be logged into the 'hr_install.log' log file.

Enter a password for the user HR: ************************


Enter a tablespace for HR [USERS]: 
Do you want to overwrite the schema, if it already exists? [YES|no]: 
******  Creating REGIONS table ....

Table REGIONS created.
... 
Table             provided    actual 
______________ ___________ _________ 
regions                  5         5 
countries               25        25 
departments             27        27 
locations               23        23 
employees              107       107 
jobs                    19        19 
job_history             10        10 

Thank you!                                                  
___________________________________________________________ 
The installation of the sample schema is now finished.      
Please check the installation verification output above.    
                                                            
You will now be disconnected from the database.             
                                                            
Thank you for using Oracle Database!                        
                                                            
Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0

Tạo người dùng cho FDW Oracle

Để đơn giản, chúng ta sẽ tạo một người dùng POSTGRES – giống như người dùng PostgreSQL mặc định.

Kết nối với cơ sở dữ liệu Oracle với tư cách là người dùng SYS:

sql sys/$ORACLE_PWD@localhost/XEPDB1 as sysdba

Trong phiên SQL với tư cách là người dùng SYS, hãy thực thi (thay thế POSTGRES_ORA_PWD bằng mật khẩu của bạn cho người dùng):

create user postgres identified by POSTGRES_ORA_PWD;

Và cấp cho người dùng các đặc quyền cần thiết:

grant connect, select any table to postgres;
grant select on v_$SQL_PLAN to postgres;
grant select on v_$SQL to postgres;

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@ora-xe-01:~/db-sample-schemas/human_resources$ sql sys/$ORACLE_PWD@localhost/XEPDB1 as sysdba


SQLcl: Release 23.3 Production on Wed Jan 03 14:42:37 2024

Copyright (c) 1982, 2024, Oracle.  All rights reserved.

Connected to:
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0

SQL> create user postgres identified by VeryStrongPassword0011##;

User POSTGRES created.

SQL> grant connect, select any table to postgres;

Grant succeeded.

SQL> grant select on v_$SQL_PLAN to postgres;

Grant succeeded.

SQL> grant select on v_$SQL to postgres;

Grant succeeded.

SQL> 

7. Tạo VPN giữa VPC

Các tài nguyên được triển khai trong mỗi VPC chỉ có thể thấy địa chỉ IP nội bộ cho mạng cụ thể đó và không thể kết nối với bất kỳ tài nguyên nội bộ nào trong một VPC khác. Google Cloud VPN tạo một cầu nối giữa hai VPC và cho phép VM có cơ sở dữ liệu Oracle kết nối và được kết nối từ phiên bản AlloyDB. Kết nối VPN bao gồm một số thành phần như Bộ định tuyến đám mây, Cổng, Đường hầm VPN và Phiên BGP. Ở đây, chúng ta sẽ xem xét quá trình tạo và định cấu hình tất cả các thành phần cần thiết cho một VPN có tính sẵn sàng cao giữa hai VPC. Bạn có thể đọc thêm về Google Cloud VPN trong tài liệu.

Tạo bộ định tuyến và cổng

Tạo Bộ định tuyến đám mây và cổng VPN trong cả hai VPC.

Trong Cloud Shell hoặc một cửa sổ dòng lệnh có Cloud SDK, hãy thực thi:

PROJECT_ID=$(gcloud config get-value project)
REGION=us-central1
gcloud compute routers create $PROJECT_ID-vpc-01-router --project=$PROJECT_ID --region=$REGION --network=default --asn=64520 --advertisement-mode=custom --set-advertisement-groups=all_subnets
gcloud compute routers create $PROJECT_ID-vpc-02-router --project=$PROJECT_ID --region=$REGION --network=$PROJECT_ID-vpc-02 --asn=64521 --advertisement-mode=custom --set-advertisement-groups=all_subnets

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ PROJECT_ID=$(gcloud config get-value project)
REGION=us-central1
gcloud compute routers create $PROJECT_ID-vpc-01-router --project=$PROJECT_ID --region=$REGION --network=default --asn=64520 --advertisement-mode=custom --set-advertisement-groups=all_subnets
gcloud compute routers create $PROJECT_ID-vpc-02-router --project=$PROJECT_ID --region=$REGION --network=$PROJECT_ID-vpc-02 --asn=64521 --advertisement-mode=custom --set-advertisement-groups=all_subnets
Your active configuration is: [cloudshell-18870]
Creating router [test-project-402417-vpc-01-router]...done.                                                                                                                                                                                         
NAME: test-project-402417-vpc-01-router
REGION: us-central1
NETWORK: default
Creating router [test-project-402417-vpc-02-router]...done.                                                                                                                                                                                         
NAME: test-project-402417-vpc-02-router
REGION: us-central1
NETWORK: test-project-402417-vpc-02

Tạo cổng VPN ở cả hai VPC:

PROJECT_ID=$(gcloud config get-value project)
gcloud compute vpn-gateways create $PROJECT_ID-vpc-01-vpn-gtw --project=$PROJECT_ID --region=$REGION --network=default
gcloud compute vpn-gateways create $PROJECT_ID-vpc-02-vpn-gtw --project=$PROJECT_ID --region=$REGION --network=$PROJECT_ID-vpc-02

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ gcloud compute vpn-gateways create $PROJECT_ID-vpc-01-vpn-gtw --project=$PROJECT_ID --region=$REGION --network=default
gcloud compute vpn-gateways create $PROJECT_ID-vpc-02-vpn-gtw --project=$PROJECT_ID --region=$REGION --network=$PROJECT_ID-vpc-02
Creating VPN Gateway...done.                                                                                                                                                                                                                  
NAME: test-project-402417-vpc-01-vpn-gtw
INTERFACE0: 35.242.106.28
INTERFACE1: 35.220.86.122
INTERFACE0_IPV6: 
INTERFACE1_IPV6: 
NETWORK: default
REGION: us-central1
Creating VPN Gateway...done.                                                                                                                                                                                                                  
NAME: test-project-402417-vpc-02-vpn-gtw
INTERFACE0: 35.242.116.197
INTERFACE1: 35.220.68.53
INTERFACE0_IPV6: 
INTERFACE1_IPV6: 
NETWORK: test-project-402417-vpc-02
REGION: us-central1
student@cloudshell:~ (test-project-402417)$ 

Tạo đường hầm VPN

Chúng tôi sẽ tạo một cấu hình HA với một cặp đường hầm VPN cho mỗi VPC.

Trong Cloud Shell hoặc một thiết bị đầu cuối có SDK, hãy thực thi:

PROJECT_ID=$(gcloud config get-value project)
SHARED_SECRET=`openssl rand -base64 24`
gcloud compute vpn-tunnels create $PROJECT_ID-vpc-01-vpn-tunnel-01 --shared-secret=$SHARED_SECRET  --peer-gcp-gateway=$PROJECT_ID-vpc-02-vpn-gtw --ike-version=2 --router=$PROJECT_ID-vpc-01-router --vpn-gateway=$PROJECT_ID-vpc-01-vpn-gtw --project=$PROJECT_ID --region=$REGION --interface=0
gcloud compute vpn-tunnels create $PROJECT_ID-vpc-01-vpn-tunnel-02 --shared-secret=$SHARED_SECRET  --peer-gcp-gateway=$PROJECT_ID-vpc-02-vpn-gtw --ike-version=2 --router=$PROJECT_ID-vpc-01-router --vpn-gateway=$PROJECT_ID-vpc-01-vpn-gtw --project=$PROJECT_ID --region=$REGION --interface=1
gcloud compute vpn-tunnels create $PROJECT_ID-vpc-02-vpn-tunnel-01 --shared-secret=$SHARED_SECRET  --peer-gcp-gateway=$PROJECT_ID-vpc-01-vpn-gtw --ike-version=2 --router=$PROJECT_ID-vpc-02-router --vpn-gateway=$PROJECT_ID-vpc-02-vpn-gtw --project=$PROJECT_ID --region=$REGION --interface=0
gcloud compute vpn-tunnels create $PROJECT_ID-vpc-02-vpn-tunnel-02 --shared-secret=$SHARED_SECRET  --peer-gcp-gateway=$PROJECT_ID-vpc-01-vpn-gtw --ike-version=2 --router=$PROJECT_ID-vpc-02-router --vpn-gateway=$PROJECT_ID-vpc-02-vpn-gtw --project=$PROJECT_ID --region=$REGION --interface=1

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ SHARED_SECRET=`openssl rand -base64 24`
gcloud compute vpn-tunnels create $PROJECT_ID-vpc-01-vpn-tunnel-01 --shared-secret=$SHARED_SECRET  --peer-gcp-gateway=$PROJECT_ID-vpc-02-vpn-gtw --ike-version=2 --router=$PROJECT_ID-vpc-01-router --vpn-gateway=$PROJECT_ID-vpc-01-vpn-gtw --project=$PROJECT_ID --region=$REGION --interface=0
gcloud compute vpn-tunnels create $PROJECT_ID-vpc-01-vpn-tunnel-02 --shared-secret=$SHARED_SECRET  --peer-gcp-gateway=$PROJECT_ID-vpc-02-vpn-gtw --ike-version=2 --router=$PROJECT_ID-vpc-01-router --vpn-gateway=$PROJECT_ID-vpc-01-vpn-gtw --project=$PROJECT_ID --region=$REGION --interface=1
gcloud compute vpn-tunnels create $PROJECT_ID-vpc-02-vpn-tunnel-01 --shared-secret=$SHARED_SECRET  --peer-gcp-gateway=$PROJECT_ID-vpc-01-vpn-gtw --ike-version=2 --router=$PROJECT_ID-vpc-02-router --vpn-gateway=$PROJECT_ID-vpc-02-vpn-gtw --project=$PROJECT_ID --region=$REGION --interface=0
gcloud compute vpn-tunnels create $PROJECT_ID-vpc-02-vpn-tunnel-02 --shared-secret=$SHARED_SECRET  --peer-gcp-gateway=$PROJECT_ID-vpc-01-vpn-gtw --ike-version=2 --router=$PROJECT_ID-vpc-02-router --vpn-gateway=$PROJECT_ID-vpc-02-vpn-gtw --project=$PROJECT_ID --region=$REGION --interface=1
Creating VPN tunnel...done.                                                                                                                                                                                                                   
NAME: test-project-402417-vpc-01-vpn-tunnel-01
REGION: us-central1
GATEWAY: test-project-402417-vpc-01-vpn-gtw
VPN_INTERFACE: 0
PEER_ADDRESS: 35.242.116.197
Creating VPN tunnel...done.                                                                                                                                                                                                                   
NAME: test-project-402417-vpc-01-vpn-tunnel-02
REGION: us-central1
GATEWAY: test-project-402417-vpc-01-vpn-gtw
VPN_INTERFACE: 1
PEER_ADDRESS: 35.220.68.53
Creating VPN tunnel...done.                                                                                                                                                                                                                   
NAME: test-project-402417-vpc-02-vpn-tunnel-01
REGION: us-central1
GATEWAY: test-project-402417-vpc-02-vpn-gtw
VPN_INTERFACE: 0
PEER_ADDRESS: 35.242.106.28
Creating VPN tunnel...done.                                                                                                                                                                                                                   
NAME: test-project-402417-vpc-02-vpn-tunnel-02
REGION: us-central1
GATEWAY: test-project-402417-vpc-02-vpn-gtw
VPN_INTERFACE: 1
PEER_ADDRESS: 35.220.86.122

Tạo phiên BGP

Các phiên Border Gateway Protocol (BGP) cho phép định tuyến động bằng cách trao đổi thông tin về các tuyến đường có sẵn giữa VPC.

Tạo cặp BGP ngang hàng đầu tiên trên mạng đầu tiên (mặc định). Địa chỉ IP của các thiết bị ngang hàng sẽ được dùng cho BGP được tạo trong VPC thứ hai.

Trong Cloud Shell hoặc một thiết bị đầu cuối có SDK, hãy thực thi:

PROJECT_ID=$(gcloud config get-value project)
gcloud compute routers add-interface $PROJECT_ID-vpc-01-router --interface-name=$PROJECT_ID-vpc-01-router-bgp-if-0 --vpn-tunnel=$PROJECT_ID-vpc-01-vpn-tunnel-01 --region=$REGION
gcloud compute routers add-bgp-peer $PROJECT_ID-vpc-01-router --peer-name=$PROJECT_ID-vpc-01-vpn-tunnel-01-bgp --interface=$PROJECT_ID-vpc-01-router-bgp-if-0 --peer-asn=64521 --region=$REGION
gcloud compute routers add-interface $PROJECT_ID-vpc-01-router --interface-name=$PROJECT_ID-vpc-01-router-bgp-if-1 --vpn-tunnel=$PROJECT_ID-vpc-01-vpn-tunnel-02 --region=$REGION
gcloud compute routers add-bgp-peer $PROJECT_ID-vpc-01-router --peer-name=$PROJECT_ID-vpc-01-vpn-tunnel-02-bgp --interface=$PROJECT_ID-vpc-01-router-bgp-if-1 --peer-asn=64521 --region=$REGION

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ gcloud compute routers add-interface $PROJECT_ID-vpc-01-router --interface-name=$PROJECT_ID-vpc-01-router-bgp-if-0 --vpn-tunnel=$PROJECT_ID-vpc-01-vpn-tunnel-01 --region=$REGION
gcloud compute routers add-bgp-peer $PROJECT_ID-vpc-01-router --peer-name=$PROJECT_ID-vpc-01-vpn-tunnel-01-bgp --interface=$PROJECT_ID-vpc-01-router-bgp-if-0 --peer-asn=64521 --region=$REGION
gcloud compute routers add-interface $PROJECT_ID-vpc-01-router --interface-name=$PROJECT_ID-vpc-01-router-bgp-if-1 --vpn-tunnel=$PROJECT_ID-vpc-01-vpn-tunnel-02 --region=$REGION
gcloud compute routers add-bgp-peer $PROJECT_ID-vpc-01-router --peer-name=$PROJECT_ID-vpc-01-vpn-tunnel-02-bgp --interface=$PROJECT_ID-vpc-01-router-bgp-if-1 --peer-asn=64521 --region=$REGION
Updated [https://www.googleapis.com/compute/v1/projects/test-project-402417/regions/us-central1/routers/test-project-402417-vpc-01-router].
Creating peer [test-project-402417-vpc-01-vpn-tunnel-01-bgp] in router [test-project-402417-vpc-01-router]...done.                                                                                                                                        
Updated [https://www.googleapis.com/compute/v1/projects/test-project-402417/regions/us-central1/routers/test-project-402417-vpc-01-router].
Creating peer [test-project-402417-vpc-01-vpn-tunnel-02-bgp] in router [test-project-402417-vpc-01-router]...done.                                                                                                                                        
student@cloudshell:~ (test-project-402417)$

Thêm BGP vào VPC thứ hai:

REGION=us-central1
PROJECT_ID=$(gcloud config get-value project)
gcloud compute routers add-interface $PROJECT_ID-vpc-02-router --interface-name=$PROJECT_ID-vpc-02-router-bgp-if-0 --vpn-tunnel=$PROJECT_ID-vpc-02-vpn-tunnel-01 --ip-address=$(gcloud compute routers describe $PROJECT_ID-vpc-01-router --format="value(bgpPeers[0].peerIpAddress)" --region=$REGION) --mask-length=30 --region=$REGION 
gcloud compute routers add-bgp-peer $PROJECT_ID-vpc-02-router --peer-name=$PROJECT_ID-vpc-02-vpn-tunnel-01-bgp --interface=$PROJECT_ID-vpc-01-router-bgp-if-0 --peer-ip-address=$(gcloud compute routers describe $PROJECT_ID-vpc-01-router --format="value(bgpPeers[0].ipAddress)" --region=$REGION) --peer-asn=64520 --region=$REGION
gcloud compute routers add-interface $PROJECT_ID-vpc-02-router --interface-name=$PROJECT_ID-vpc-02-router-bgp-if-1 --vpn-tunnel=$PROJECT_ID-vpc-02-vpn-tunnel-02 --ip-address=$(gcloud compute routers describe $PROJECT_ID-vpc-01-router --format="value(bgpPeers[1].peerIpAddress)" --region=$REGION) --mask-length=30 --region=$REGION
gcloud compute routers add-bgp-peer $PROJECT_ID-vpc-02-router --peer-name=$PROJECT_ID-vpc-02-vpn-tunnel-02-bgp --interface=$PROJECT_ID-vpc-01-router-bgp-if-1 --peer-ip-address=$(gcloud compute routers describe $PROJECT_ID-vpc-01-router --format="value(bgpPeers[1].ipAddress)" --region=$REGION) --peer-asn=64520 --region=$REGION

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ REGION=us-central1
PROJECT_ID=$(gcloud config get-value project)
gcloud compute routers add-interface $PROJECT_ID-vpc-02-router --interface-name=$PROJECT_ID-vpc-02-router-bgp-if-0 --vpn-tunnel=$PROJECT_ID-vpc-02-vpn-tunnel-01 --ip-address=$(gcloud compute routers describe $PROJECT_ID-vpc-01-router --format="value(bgpPeers[0].peerIpAddress)" --region=$REGION) --mask-length=30 --region=$REGION 
gcloud compute routers add-bgp-peer $PROJECT_ID-vpc-02-router --peer-name=$PROJECT_ID-vpc-02-vpn-tunnel-01-bgp --interface=$PROJECT_ID-vpc-01-router-bgp-if-0 --peer-ip-address=$(gcloud compute routers describe $PROJECT_ID-vpc-01-router --format="value(bgpPeers[0].ipAddress)" --region=$REGION) --peer-asn=64520 --region=$REGION
gcloud compute routers add-interface $PROJECT_ID-vpc-02-router --interface-name=$PROJECT_ID-vpc-02-router-bgp-if-1 --vpn-tunnel=$PROJECT_ID-vpc-02-vpn-tunnel-02 --ip-address=$(gcloud compute routers describe $PROJECT_ID-vpc-01-router --format="value(bgpPeers[1].peerIpAddress)" --region=$REGION) --mask-length=30 --region=$REGION
gcloud compute routers add-bgp-peer $PROJECT_ID-vpc-02-router --peer-name=$PROJECT_ID-vpc-02-vpn-tunnel-02-bgp --interface=$PROJECT_ID-vpc-01-router-bgp-if-1 --peer-ip-address=$(gcloud compute routers describe $PROJECT_ID-vpc-01-router --format="value(bgpPeers[1].ipAddress)" --region=$REGION) --peer-asn=64520 --region=$REGION
Your active configuration is: [cloudshell-18870]
Updated [https://www.googleapis.com/compute/v1/projects/test-project-402417/regions/us-central1/routers/test-project-402417-vpc-02-router].
Creating peer [test-project-402417-vpc-02-vpn-tunnel-01-bgp] in router [test-project-402417-vpc-02-router]...done.                                                                                                                                        
Updated [https://www.googleapis.com/compute/v1/projects/test-project-402417/regions/us-central1/routers/test-project-402417-vpc-02-router].
Creating peer [test-project-402417-vpc-02-vpn-tunnel-02-bgp] in router [test-project-402417-vpc-02-router]...done.

Thêm các tuyến tuỳ chỉnh vào BGP

Giờ chúng ta cần quảng cáo dải IP riêng tư của dịch vụ cho BGP

Trong Cloud Shell, hãy thực thi:

PROJECT_ID=$(gcloud config get-value project)
gcloud compute routers update $PROJECT_ID-vpc-01-router --add-advertisement-ranges=$(gcloud compute addresses list --filter="name:psa-range AND network:default" --format="value(address_range())") --region=$REGION
gcloud compute networks peerings update servicenetworking-googleapis-com --network=default --import-custom-routes --export-custom-routes

Kết quả đầu ra dự kiến trên bảng điều khiển (đã chỉnh sửa):

PROJECT_ID=$(gcloud config get-value project)
gcloud compute routers update $PROJECT_ID-vpc-01-router --add-advertisement-ranges=$(gcloud compute addresses list --filter="name:psa-range AND network:default" --format="value(address_range())") --region=$REGION
gcloud compute networks peerings update servicenetworking-googleapis-com --network=default --import-custom-routes --export-custom-routes
...

8. Định cấu hình FDW Oracle trong AlloyDB

Bây giờ, chúng ta có thể tạo một cơ sở dữ liệu kiểm thử và định cấu hình tiện ích FDW của Oracle. Chúng ta sẽ sử dụng VM instance-1 mà chúng ta đã tạo ở bước thứ hai và mật khẩu đã ghi chú cho cụm AlloyDB.

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

Kết nối với máy ảo GCE instance-1:

ZONE=us-central1-a
gcloud compute ssh instance-1 --zone=$ZONE  

Trong phiên SSH, hãy thực thi:

export PGPASSWORD=<Noted password>
REGION=us-central1
ADBCLUSTER=alloydb-aip-01
INSTANCE_IP=$(gcloud alloydb instances describe $ADBCLUSTER-pr --cluster=$ADBCLUSTER --region=$REGION --format="value(ipAddress)")
psql "host=$INSTANCE_IP user=postgres" -c "CREATE DATABASE quickstart_db"

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@instance-1:~$ export PGPASSWORD=6dd7fKHnMId8RM97
student@instance-1:~$ REGION=us-central1
ADBCLUSTER=alloydb-aip-01
INSTANCE_IP=$(gcloud alloydb instances describe $ADBCLUSTER-pr --cluster=$ADBCLUSTER --region=$REGION --format="value(ipAddress)")
psql "host=$INSTANCE_IP user=postgres" -c "CREATE DATABASE quickstart_db"
CREATE DATABASE
student@instance-1:~$  

Định cấu hình tiện ích FDW của Oracle

Bật tiện ích FDW của Oracle trong cơ sở dữ liệu mới tạo.

Trong cùng một phiên SSH, hãy thực thi:

psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "create extension if not exists oracle_fdw cascade"

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres dbname=quickstart_db" -c "create extension if not exists oracle_fdw cascade"
CREATE EXTENSION
student@instance-1:~$ 

Định cấu hình FDW của Oracle

Chúng ta sẽ tiếp tục làm việc trong cùng một phiên SSH để định cấu hình Oracle FDW nhằm hoạt động với lược đồ mẫu HR trong cơ sở dữ liệu Oracle.

Tạo cấu hình máy chủ FDW bằng địa chỉ IP nội bộ cho máy ảo Oracle XE trong VPC thứ hai.

Trong cùng một phiên SSH đến instance-1, hãy thực thi:

ORACLE_SERVER_IP=$(gcloud compute instances list --filter="name=(ora-xe-01)" --format="value(networkInterfaces[0].networkIP)")
psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "create server ora_xe foreign data wrapper oracle_fdw options (dbserver '$ORACLE_SERVER_IP:1521/xepdb1')"

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@instance-1:~$ ORACLE_SERVER_IP=$(gcloud compute instances list --filter="name=(ora-xe-01)" --format="value(networkInterfaces[0].networkIP)")
psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "create server ora_xe foreign data wrapper oracle_fdw options (dbserver '$ORACLE_SERVER_IP:1521/xepdb1')"
CREATE SERVER 

Cấp quyền sử dụng cho người dùng postgres trên máy chủ đã tạo.

psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "grant usage on foreign server ora_xe to postgres"

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@instance-1:~$ psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "grant usage on foreign server ora_xe to postgres"
GRANT

Tạo mối liên kết giữa người dùng trong PostgreSQL và người dùng cơ sở dữ liệu Oracle bằng mật khẩu mà chúng ta thiết lập cho người dùng trong cơ sở dữ liệu Oracle. Sử dụng mật khẩu đã ghi chú cho người dùng Oracle POSTGRES mà chúng ta đã tạo trước đó (POSTGRES_ORA_PWD).

POSTGRES_ORA_PWD=<your password for the oracle user postgres>
psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "CREATE USER MAPPING FOR postgres SERVER ora_xe OPTIONS ( USER 'postgres', PASSWORD '$POSTGRES_ORA_PWD')"

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@instance-1:~$ POSTGRES_ORA_PWD=VeryStrongPassword0011##
student@instance-1:~$ psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "CREATE USER MAPPING FOR postgres SERVER ora_xe OPTIONS ( USER 'postgres', PASSWORD '$POSTGRES_ORA_PWD')"
CREATE USER MAPPING

9. Sử dụng FDW của Oracle với lược đồ HR

Giờ đây, chúng ta có thể dùng FDW của Oracle với dữ liệu trong cơ sở dữ liệu Oracle.

Nhập định nghĩa lược đồ HR của Oracle

Chúng tôi tiếp tục làm việc trên máy ảo ứng dụng Postgres bằng cách sử dụng psql để làm việc với AlloyDB.

Trong phiên SSH của máy ảo, hãy thực thi:

psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "create schema ora_exe_hr";
psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "import foreign schema \"HR\" from server ora_xe into ora_exe_hr";

Kết quả đầu ra dự kiến (đã chỉnh sửa):

student@instance-1:~$ psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "create schema ora_exe_hr";
psql -h $INSTANCE_IP -U postgres -d quickstart_db -c "import foreign schema \"HR\" from server ora_xe into ora_exe_hr";
CREATE SCHEMA
IMPORT FOREIGN SCHEMA
student@instance-1:~$ 

Kiểm thử FDW của Oracle

Hãy chạy một câu lệnh SQL mẫu.

Kết nối với cơ sở dữ liệu. Trong phiên SSH của máy ảo, hãy thực thi:

psql -h $INSTANCE_IP -U postgres -d quickstart_db

Trong phiên PSQL, hãy chạy một câu lệnh chọn.

select * from ora_exe_hr.countries limit 5;

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

student@instance-1:~$ psql -h $INSTANCE_IP -U postgres -d quickstart_db
psql (13.13 (Debian 13.13-0+deb11u1), server 14.7)
WARNING: psql major version 13, server major version 14.
         Some psql features might not work.
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

quickstart_db=> select * from ora_exe_hr.countries limit 5;
 country_id | country_name | region_id 
------------+--------------+-----------
 AR         | Argentina    |        20
 AU         | Australia    |        40
 BE         | Belgium      |        10
 BR         | Brazil       |        20
 CA         | Canada       |        20
(5 rows)

quickstart_db=>

Thao tác này sẽ trả về kết quả, tức là cấu hình của chúng ta hoạt động chính xác.

Bạn có thể thử nghiệm với các bảng còn lại.

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

Giờ đây, khi tất cả các tác vụ đã hoàn tất, chúng ta có thể dọn dẹp môi trường bằng cách huỷ các thành phần để tránh bị tính phí không cần thiết.

Huỷ các phiên bản và cụm AlloyDB khi bạn hoàn tất lớp học

Xoá cụm AlloyDB và tất cả các phiên bản

Cụm bị huỷ bằng lựa chọn force, thao tác này cũng sẽ xoá tất cả các phiên bản thuộc cụm.

Trong cloud shell, hãy xác định dự án và các biến môi trường nếu bạn bị ngắt kết nối và mất tất cả các chế độ cài đặt trước đó:

gcloud config set project <your project id>
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
export PROJECT_ID=$(gcloud config get-value project)

Xoá cụm:

gcloud alloydb clusters delete $ADBCLUSTER --region=$REGION --force

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-001-402417)$ gcloud alloydb clusters delete $ADBCLUSTER --region=$REGION --force
All of the cluster data will be lost when the cluster is deleted.

Do you want to continue (Y/n)?  Y

Operation ID: operation-1697820178429-6082890a0b570-4a72f7e4-4c5df36f
Deleting cluster...done.   

Xoá bản sao lưu AlloyDB

Xoá tất cả bản sao lưu AlloyDB cho cụm:

for i in $(gcloud alloydb backups list --filter="CLUSTER_NAME: projects/$PROJECT_ID/locations/$REGION/clusters/$ADBCLUSTER" --format="value(name)" --sort-by=~createTime) ; do gcloud alloydb backups delete $(basename $i) --region $REGION --quiet; done

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-001-402417)$ for i in $(gcloud alloydb backups list --filter="CLUSTER_NAME: projects/$PROJECT_ID/locations/$REGION/clusters/$ADBCLUSTER" --format="value(name)" --sort-by=~createTime) ; do gcloud alloydb backups delete $(basename $i) --region $REGION --quiet; done
Operation ID: operation-1697826266108-60829fb7b5258-7f99dc0b-99f3c35f
Deleting backup...done.                                                                                                                                                                                                                                                            

Giờ đây, chúng ta có thể hủy bỏ máy ảo

Xoá máy ảo GCE

Trong Cloud Shell, hãy thực thi:

export GCEVM=instance-1
export ZONE=us-central1-a
gcloud compute instances delete $GCEVM \
    --zone=$ZONE \
    --quiet

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-001-402417)$ export GCEVM=instance-1
export ZONE=us-central1-a
gcloud compute instances delete $GCEVM \
    --zone=$ZONE \
    --quiet
Deleted 

Xoá máy ảo bằng Oracle XE

Trong Cloud Shell, hãy thực thi:

SERVER_NAME=ora-xe-01
ZONE=us-central1-a
gcloud compute instances delete $SERVER_NAME \
    --zone=$ZONE \
    --quiet

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ SERVER_NAME=ora-xe-01
ZONE=us-central1-a
gcloud compute instances delete $SERVER_NAME \
    --zone=$ZONE \
    --quiet
Deleted [https://www.googleapis.com/compute/v1/projects/test-project-402417/zones/us-central1-a/instances/ora-xe-01].
student@cloudshell:~ (test-project-402417)$

Xoá VPN

Trong Cloud Shell, hãy thực thi:

REGION=us-central1
PROJECT_ID=$(gcloud config get-value project)
gcloud compute vpn-tunnels delete $PROJECT_ID-vpc-01-vpn-tunnel-01 --region=$REGION --quiet
gcloud compute vpn-tunnels delete $PROJECT_ID-vpc-01-vpn-tunnel-02 --region=$REGION --quiet
gcloud compute vpn-tunnels delete $PROJECT_ID-vpc-02-vpn-tunnel-01 --region=$REGION --quiet
gcloud compute vpn-tunnels delete $PROJECT_ID-vpc-02-vpn-tunnel-02 --region=$REGION --quiet
gcloud compute routers delete $PROJECT_ID-vpc-02-router --region=$REGION --quiet
gcloud compute routers delete $PROJECT_ID-vpc-01-router --region=$REGION --quiet
gcloud compute vpn-gateways delete $PROJECT_ID-vpc-01-vpn-gtw --region=$REGION --quiet
gcloud compute vpn-gateways delete $PROJECT_ID-vpc-02-vpn-gtw --region=$REGION --quiet

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ REGION=us-central1
PROJECT_ID=$(gcloud config get-value project)
gcloud compute vpn-tunnels delete $PROJECT_ID-vpc-01-vpn-tunnel-01 --region=$REGION --quiet
gcloud compute vpn-tunnels delete $PROJECT_ID-vpc-01-vpn-tunnel-02 --region=$REGION --quiet
gcloud compute vpn-tunnels delete $PROJECT_ID-vpc-02-vpn-tunnel-01 --region=$REGION --quiet
gcloud compute vpn-tunnels delete $PROJECT_ID-vpc-02-vpn-tunnel-02 --region=$REGION --quiet
gcloud compute routers delete $PROJECT_ID-vpc-02-router --region=$REGION --quiet
gcloud compute routers delete $PROJECT_ID-vpc-01-router --region=$REGION --quiet
gcloud compute vpn-gateways delete $PROJECT_ID-vpc-01-vpn-gtw --region=$REGION --quiet
gcloud compute vpn-gateways delete $PROJECT_ID-vpc-02-vpn-gtw --region=$REGION --quiet
Your active configuration is: [cloudshell-18870]
Deleting VPN tunnel...done.                                                                                                                                                                                                                   
Deleting VPN tunnel...done.                                                                                                                                                                                                                   
Deleting VPN tunnel...done.                                                                                                                                                                                                                   
Deleting VPN tunnel...done.                                                                                                                                                                                                                   
Deleted [https://www.googleapis.com/compute/v1/projects/test-project-402417/regions/us-central1/routers/test-project-402417-vpc-02-router].
Deleted [https://www.googleapis.com/compute/v1/projects/test-project-402417/regions/us-central1/routers/test-project-402417-vpc-01-router].
Deleting VPN Gateway...done.                                                                                                                                                                                                                  
Deleting VPN Gateway...done.
student@cloudshell:~ (test-project-402417)$ 

Xoá VPC thứ hai

Trong Cloud Shell, hãy thực thi:

REGION=us-central1
PROJECT_ID=$(gcloud config get-value project)
gcloud compute firewall-rules delete $PROJECT_ID-vpc-02-allow-icmp --quiet
gcloud compute firewall-rules delete $PROJECT_ID-vpc-02-allow-ssh --quiet
gcloud compute firewall-rules delete default-allow-postgres --quiet
gcloud compute firewall-rules delete $PROJECT_ID-vpc-02-allow-oracle --quiet
gcloud compute networks subnets delete $PROJECT_ID-vpc-02-$REGION --region=$REGION --quiet
gcloud compute networks delete $PROJECT_ID-vpc-02 --quiet

Kết quả đầu ra dự kiến trên bảng điều khiển:

student@cloudshell:~ (test-project-402417)$ gcloud compute firewall-rules delete $PROJECT_ID-vpc-02-allow-icmp --quiet
gcloud compute firewall-rules delete $PROJECT_ID-vpc-02-allow-ssh --quiet
gcloud compute firewall-rules delete default-allow-postgres --quiet
gcloud compute firewall-rules delete $PROJECT_ID-vpc-02-allow-oracle --quiet
gcloud compute networks subnets delete $PROJECT_ID-vpc-02-$REGION --region=$REGION --quiet
gcloud compute networks delete $PROJECT_ID-vpc-02 --quiet
Deleted [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/firewalls/test-project-402417-vpc-02-allow-icmp].
Deleted [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/firewalls/test-project-402417-vpc-02-allow-ssh].
Deleted [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/firewalls/default-allow-postgres].
Deleted [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/firewalls/test-project-402417-vpc-02-allow-oracle].
gcloud compute networks subnets delete $PROJECT_ID-vpc-02-$REGION --region=$REGION --quiet
gcloud compute networks delete $PROJECT_ID-vpc-02 --quiet

11. 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 AlloyDB
  • Cách kết nối với AlloyDB
  • Cách định cấu hình và triển khai cơ sở dữ liệu Oracle mẫu
  • Cách thiết lập VPN giữa hai mạng VPC
  • Cách định cấu hình tiện ích FDW của Oracle

12. Khảo sát

Kết quả:

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