使用 RPM Orchestrator 在 GCE VM 上部署 AlloyDB Omni 高可用性叢集

1. 簡介

本程式碼實驗室將說明如何在 Google Compute Engine (GCE) 虛擬機器 (VM) 上部署 AlloyDB Omni 高可用性 (HA) 叢集。完成本程式碼研究室後,您將佈建高可用性參考架構,其中包含 3 個資料庫節點和 2 個 HAProxy 節點,以及用於管理作業的控制節點。

必要條件

  • 從 Cloud 控制台存取 Google Cloud 專案和 Cloud Shell。
  • 已安裝並設定 Google Cloud SDK (gcloud)。如要瞭解如何安裝 gcloud,請參閱 gcloud-install
  • 已安裝 Terraform v1.9.8

課程內容

  • 如何建立及準備 GCE VM,以供 AlloyDB Omni 使用。
  • 如何安裝及執行 AlloyDB Omni RPM 自動調度管理工具。
  • 如何為高可用性參考架構安裝及設定 AlloyDB Omni RPM。

軟硬體需求

  • 取得 AlloyDB Omni RPM 和 RPM 自動調度管理工具的網址連結。如要申請,請填寫 AlloyDB Omni 註冊表單。連結會傳送至您電子郵件的存放區網址伺服器。初始化下列環境變數,方便您在程式碼研究室中使用網址。
    export ORCHESTRATOR_ANSIBLE_COLLECTION_PATH="..."
    export ALLOYDB_OMNI_REPOSITORY_URL="..."
    export ALLOYDB_OMNI_ORCHESTRATOR_REPOSITORY_URL="..."
    export ALLOYDB_OMNI_COMMON_REPOSITORY_URL=....
    export ETCD_REPOSITORY_URL="..."
    
  • 可存取 AlloyDB Omni 部署堆疊的終端機。您可以使用 Cloud Shell 達成這個目的。

2. 設定和需求條件

專案設定

建立 Google Cloud 專案

  1. Google Cloud 控制台的專案選取器頁面中,選取或建立 Google Cloud 專案
  2. 確認 Cloud 專案已啟用計費功能。瞭解如何檢查專案是否已啟用計費功能

啟動 Cloud Shell

雖然可以透過筆電遠端操作 Google Cloud,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是可在雲端執行的指令列環境。

Google Cloud 控制台中,點選右上角工具列的 Cloud Shell 圖示:

啟用 Cloud Shell 的圖示

你也可以依序按下 G 和 S 鍵。如果您位於 Google Cloud 控制台中,或使用這個連結,這個序列會啟用 Cloud Shell。

佈建並連線至環境的作業需要一些時間才能完成。完成後,您應該會看到如下的內容:

Google Cloud Shell 終端機顯示環境已連線

這部虛擬機器搭載各種您需要的開發工具,並提供永久的 5GB 主目錄,而且可在 Google Cloud 運作,大幅提升網路效能並強化驗證功能。您可以在瀏覽器中完成本程式碼研究室的所有作業。無須安裝任何軟體。

3. 建立 Google Cloud Compute Engine 虛擬機器

準備 Terraform 指令碼

  1. 定義專案 ID 和所需叢集名稱的環境變數。您會在整個程式碼研究室中使用這些功能。
    export PROJECT="your-project-id"
    export CLUSTER="your-cluster-name"
    
    注意:Google Cloud 專案 ID 長度必須介於 6 至 30 個字元之間。詳情請參閱 https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects#before_you_begin
  2. 確認您已登入 Google Cloud 帳戶使用者。
    gcloud auth login
    
  3. 為部署作業建立工作目錄,並從存放區來源複製必要的 Terraform 設定檔。
    mkdir -p ~/alloydb-omni/$PROJECT/$CLUSTER
    cd ~/alloydb-omni/$PROJECT/$CLUSTER
    gcloud storage cp gs://alloydb-omni-install/rpm-orchestrator/gce/terraform/*.tf .
    
  4. 建立 terraform.tfvars 檔案,根據參考架構指定必要參數,如下所示:
    cat > terraform.tfvars <<EOF
    # Required instance counts for reference architecture
    db_instance_count      = 3
    haproxy_instance_count = 2
    control_instance_count = 1
    
    # Optional: Customize if needed with the below variables
    # os_image             = "rocky-linux-cloud/rocky-linux-9"
    # zone                 = "us-west4-c"
    # region_1             = "us-west4"
    # db_instance_type     = "c4-highmem-4"
    # db_disk_type         = "hyperdisk-balanced"
    # db_data_size         =  "50" # GB
    # data_dir             =  "/data"
    EOF
    

執行 Terraform 指令碼並驗證

  1. 現在可以佈建 VM。
    terraform init
    
  2. 建立 VM 前,請確認您具備建立 VM 和其他資源的必要存取權。總而言之,您需要下列權限。
    roles/compute.admin
    roles/iam.roleAdmin
    roles/compute.osAdminLogin
    roles/iam.serviceAccountCreator
    roles/iam.serviceAccountUser
    roles/artifactregistry.repoAdmin
    roles/storage.objectUser
    roles/resourcemanager.projectIamAdmin
    roles/serviceusage.serviceUsageAdmin
    
  3. 驗證設定,並套用設定來佈建資源。
    terraform validate
    terraform apply --auto-approve
    
    注意:請確認您使用的是部署設定預期的 Terraform v1.9.8
  4. Terraform 完成後,請使用 gcloud 列出已建立的執行個體,確認虛擬機器已成功建立。
    gcloud compute instances list --filter="name~$CLUSTER" --project=$PROJECT
    
    您應該會看到對應於 3 個資料庫節點、2 個 HAProxy 節點和控制 VM 的執行個體,以及對應的區域。記錄控制節點的 ZONE。稍後會在本程式碼研究室中用到。
    export ZONE=$(gcloud compute instances list \
      --filter="name=$CLUSTER-control"          \
      --format="value(zone)" --project=$PROJECT)
    

4. 準備要部署的 VM

您需要建立控制節點的 SSH 工作階段,並執行步驟,啟用所有 VM (也稱為節點) 的 SSH 存取權。

  1. 確認已定義專案和叢集的環境變數。
    export PROJECT="your-project-id"
    export CLUSTER="your-cluster-name"
    
  2. 建立 SSH 金鑰,並新增該金鑰以登入控制項 VM。
    # Replace values with your specific GCP project, cluster and zone details
    ssh-keygen -t ed25519 -f $HOME/.ssh/google_compute_engine
    gcloud compute os-login ssh-keys add --key-file=$HOME/.ssh/google_compute_engine.pub --project=$PROJECT
    
  3. 建立防火牆規則,允許 SSH 連線和 VRRP 流量。
    gcloud compute config-ssh --project=$PROJECT
    gcloud compute firewall-rules create $CLUSTER-allow-ssh --network=$CLUSTER --project=$PROJECT --direction=INGRESS --action=allow --rules=tcp:22 --source-ranges="0.0.0.0/0"
    gcloud compute firewall-rules create $CLUSTER-allow-vrrp --network=$CLUSTER --project=$PROJECT --allow=112 --source-ranges="0.0.0.0/0"
    
  4. 透過 SSH 連線至控制項 VM:
    gcloud compute ssh "$CLUSTER-control" --zone=$ZONE --project=$PROJECT
    
  5. 在控制節點上建立 Linux 群組,名稱與使用者名稱相同:
    sudo groupadd $(id -un)
    sudo usermod -aG $(id -un) $(id -un)
    
  6. Terraform 設定會建立幾個設定指令碼,並在控制 VM 的 /tmp/ 目錄中暫存這些指令碼,包括叢集名稱、服務帳戶、專案名稱等。使用控制節點上的設定指令碼,從控制節點建立無密碼的 SSH 存取權,連線至所有叢集節點。
    /tmp/setup-ssh-for-cluster.sh
    
    重要事項:請記下 service_account 使用者。稍後在本程式碼研究室中,我們會將這個值做為 SSH_USER。
  7. 在本程式碼研究室中,我們可以停用所有節點的 SELinux。Terraform 指令碼會新增「/tmp/run-all.sh」,可用於此目的。
    /tmp/run-all.sh sudo setenforce 0
    
  8. 如果您已註冊 AlloyDB Omni 並取得連結,現在也該將這些網址新增至環境變數。
    cat >> ~/.codelab.env <<EOF
    export ORCHESTRATOR_ANSIBLE_COLLECTION_PATH="..."
    export ALLOYDB_OMNI_REPOSITORY_URL="..."
    export ALLOYDB_OMNI_ORCHESTRATOR_REPOSITORY_URL="..."
    export ALLOYDB_OMNI_COMMON_REPOSITORY_URL=....
    export ETCD_REPOSITORY_URL="..."
    EOF
    
    terraform/variables.tf 檔案的 cidr_range 輸入變數中,為環境決定虛擬 IP,確保不會與其他節點衝突,如下列範例所示:
    cat >> ~/.codelab.env <<EOF
    export VIRTUAL_IP="10.1.0.50"
    EOF
    

5. 在所有 VM 上安裝必要的軟體元件

下一步是在 VM 上安裝必要的軟體元件。這可以在控制節點上協調。下列所有指令都必須在控制節點上執行。

  1. 如果不在控制節點上,請透過 SSH 連線至控制 VM。
    gcloud compute ssh "$CLUSTER-control" --zone=$ZONE --project=$PROJECT
    
    進入控制 VM 的 ssh 提示後,請提供環境檔案來源。
    source ~/.codelab.env
    
  2. 在控制節點上安裝 Ansible 和必要的 Python 程式庫。
    sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y
    sudo dnf install -y ansible
    sudo dnf install -y python3-grpcio python3-protobuf python3-googleapis-common-protos python3-grpcio-status
    
  3. 接著,下載 RPM Orchestrator Ansible 集合 tar 檔案,並安裝該檔案。
    gcloud storage cp "gs://${ORCHESTRATOR_ANSIBLE_COLLECTION_PATH#https://storage.googleapis.com/}google-alloydbomni_orchestrator-*.tar.gz" .
    ansible-galaxy collection install google-alloydbomni_orchestrator-0.1.0-6.tar.gz
    ansible-galaxy collection list | grep alloydbomni_orchestrator
    
    注意:請確認 ORCHESTRATOR_ANSIBLE_COLLECTION_PATH 變數結尾有「/」。
  4. 協調器會使用 Ansible 目錄格式的部署規格檔案,瞭解叢集拓撲。在控制節點上建立名為 deployment_spec.yaml 的檔案,並填入節點詳細資料。
    cat > deployment_spec.yaml <<EOF
    alloydbomni:
      vars:
        cluster_manager:
          name: "$CLUSTER"
        etcd:
          setup: true
          config_forcewrite: true
        alloydbomni:
          major_version: "18"
          repo_url: $ALLOYDB_OMNI_REPOSITORY_URL
        alloydbomni_monitor:
          repo_url: $ALLOYDB_OMNI_COMMON_REPOSITORY_URL
        alloydbomni_cluster_manager:
          repo_url: $ALLOYDB_OMNI_ORCHESTRATOR_REPOSITORY_URL
        alloydbomni_node_manager:
          repo_url: $ALLOYDB_OMNI_ORCHESTRATOR_REPOSITORY_URL
        pgbouncer:
          repo_url: $ALLOYDB_OMNI_COMMON_REPOSITORY_URL
        pgbackrest:
          repo_url: $ALLOYDB_OMNI_COMMON_REPOSITORY_URL
    
      children:
        primary_instance_nodes:
          hosts:
            $CLUSTER-db1:
            $CLUSTER-db2:
            $CLUSTER-db3:
        load_balancer_nodes:
          hosts:
            $CLUSTER-haproxy1:
            $CLUSTER-haproxy2:
    EOF
    
    注意:請檢查產生的檔案,確認檔案參照的值是否有效。
  5. 建立名為 install.yaml 的劇本,其中會參照協調器集合中的安裝角色。
    cat > install.yaml <<EOF
    - name: Install AlloyDB Omni cluster components
      hosts: all
      vars:
        ansible_become: true
        ansible_user: $SSH_USER
        ansible_ssh_private_key_file: $HOME/ssh-key-cluster-sa
      roles:
        - role: google.alloydbomni_orchestrator.install
    EOF
    
    注意:請檢查產生的檔案,確認檔案參照的值是否有效。
  6. 使用廣告空間檔案執行劇本,在所有指定節點中下載並安裝 RPM。
    ansible-playbook -i deployment_spec.yaml install.yaml
    

6. 啟動 AlloyDB Omni 叢集

在這個階段,我們已在所有節點上安裝所有必要元件。我們已準備好啟動 AlloyDB Omni 叢集。

  1. 如果不在控制節點上,請透過 SSH 連線至控制 VM。
    gcloud compute ssh "$CLUSTER-control" --zone=$ZONE --project=$PROJECT
    
    進入控制 VM 的 ssh 提示後,請提供環境檔案來源。
    source ~/.codelab.env
    
  2. 產生密碼的雜湊值,並記下該值。
    encoded_password=$(echo -n "your unique password" | base64)
    
  3. 如要建立叢集,AlloyDB Omni 必須知道如何設定。為資料庫叢集規格建立檔案名稱 dbcluster.yaml
    cat > dbcluster.yaml <<EOF
    Secret:
      metadata:
        name: db-pw-$CLUSTER
      spec:
        type: Opaque
        data:
          $CLUSTER: $encoded_password
    ---
    DBCluster:
      metadata:
        name: $CLUSTER
      spec:
        databaseVersion: 18.1.0
        mode: ""
        availability:
          numberOfStandbys: 2
          enableAutoFailover: true
          enableAutoHeal: true
          autoFailoverTriggerThreshold: 2
          autoHealTriggerThreshold: 2
          healthcheckPeriodSeconds: 5
          replayReplicationSlotsOnStandbys: false
        primarySpec:
          adminUser:
            passwordRef:
              name: db-pw-$CLUSTER
          resources:
            cpu: 4
            memory: 32Gi
            disks:
            - name: DataDisk
              path: $PGDATA
          dbLoadBalancerOptions:
            gcp:
              loadBalancerIP: "$VIRTUAL_IP"
              loadBalancerType: "internal"
              loadBalancerInterface: "eth0"
    EOF
    
  4. 建立名為 bootstrap.yaml 的劇本,該劇本會參照啟動 Ansible 角色,建立 AlloyDB Omni 叢集。
    cat > bootstrap.yaml <<EOF
    - name: Create DBCluster
      hosts: localhost
      vars:
        ansible_become: true
        ansible_user: $SSH_USER
        ansible_ssh_private_key_file: $HOME/ssh-key-cluster-sa
      roles:
      - role: google.alloydbomni_orchestrator.bootstrap
    EOF
    
  5. 執行劇本來建立叢集
    ansible-playbook bootstrap.yaml -i deployment_spec.yaml -e resource_spec=dbcluster.yaml
    

7. (選用) 設定 PgBouncer 連線集區

AlloyDB Omni 支援使用 PgBouncer 進行輕量型連線集區化。叢集佈建完成後,您就能設定及啟動 PgBouncer。

  1. 建立名為 pgbouncer.yaml 的資源規格檔案,將連線集區與資料庫叢集建立關聯:
    cat > pgbouncer.yaml <<EOF
    PgBouncer:
      metadata:
        name: pgbouncer-pooler
      spec:
        dbclusterRef: $CLUSTER
        allowSuperUserAccess: true
        accessMode: "rw"
        port: 6432
    EOF
    
  2. 使用先前建立的相同 bootstrap.yaml 劇本,啟動 PgBouncer 集區,並傳遞新的規格檔案:
    ansible-playbook bootstrap.yaml -i deployment_spec.yaml -e resource_spec=pgbouncer.yaml
    

8. 驗證 AlloyDB Omni 叢集

如要確認叢集是否正常運作,以及是否可透過負載平衡器存取,您可以從控制節點使用標準 PostgreSQL 用戶端連線至叢集。

  1. 在控制節點上安裝 PostgreSQL 18 用戶端存放區和套件:
    sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    sudo dnf install -y postgresql18
    
  2. 使用先前保留的虛擬 IP 連線至叢集。系統會提示您輸入在 dbcluster.yaml 中編碼的密碼:
    /usr/pgsql-18/bin/psql -h $VIRTUAL_IP -U postgres -W
    
  3. 連線後,您可以執行基本 SQL 查詢來驗證叢集狀態,例如檢查資料庫版本:
    postgres=# SELECT version();
    
    您應該會看到輸出內容,指出 PostgreSQL 18 正在透過 AlloyDB Omni 執行。輸入 \q 即可退出提示。

9. (選用) 使用 pgBackRest 備份資料

AlloyDB Omni 會整合 pgBackRest,直接將備份內容管理至 Cloud Storage。您可以設定備份方案,並觸發按照需求執行的備份作業,將資料備份到 Terraform 設定建立的 GCS 值區。

  1. 建立名為 backup_plan.yaml 的備份方案規格檔案,指向您佈建的 GCS 儲存空間:
    cat > backup_plan.yaml <<EOF
    BackupPlan:
      metadata:
        name: pgb-plan
      spec:
        dbclusterRef: $CLUSTER
        backupLocation:
          type: GCS
          gcsOptions:
            bucket: $CLUSTER-gcs-backups
            key: /backups
    EOF
    
  2. 建立名為 backup.yaml 的 Ansible Playbook,其中會參照備份管理角色:
    cat > backup.yaml <<EOF
    - name: Manage AlloyDB Omni Backups
      hosts: localhost
      vars:
        ansible_become: true
        ansible_user: $SSH_USER
        ansible_ssh_private_key_file: $HOME/ssh-key-cluster-sa
      roles:
        - role: google.alloydbomni_orchestrator.backup
    EOF
    
  3. 使用 backup.yaml 劇本套用備份方案:
    ansible-playbook backup.yaml -i deployment_spec.yaml -e resource_spec=backup_plan.yaml
    
  4. 成功建立方案後,請建立名為 create_backup.yaml 的隨需備份資源檔案。
    cat > create_backup.yaml <<EOF
    Backup:
      metadata:
        name: on-demand-backup
      spec:
        backupPlanRef: pgb-plan
        dbclusterRef: $CLUSTER
    EOF
    
  5. 執行應對手冊,啟動備份:
    ansible-playbook backup.yaml -i deployment_spec.yaml -e resource_spec=create_backup.yaml
    
  6. 如要驗證備份狀態,請建立 status.yaml 劇本:
- name: Fetch AlloyDB Omni Resource Status
hosts: localhost
vars:
  ansible_become: true
  ansible_user: $SSH_USER
  ansible_ssh_private_key_file: $HOME/ssh-key-cluster-sa
roles:
  - role: google.alloydbomni_orchestrator.status
EOF
  1. 執行應對手冊,列出所有備份
ansible-playbook status.yaml -i deployment_spec.yaml -e resource_type=Backup
  1. 或者,如要取得先前建立的特定備份檔詳細資訊,請傳遞 -e resource_name=on-demand-backup
ansible-playbook status.yaml -i deployment_spec.yaml \
  -e resource_type=Backup \
  -e resource_name=on-demand-backup

10. 清除資源

部署完成後,您可以銷毀已佈建的資源,以免產生費用。

  1. 建立名為 teardown.yaml 的應對手冊:
    cat > teardown.yaml <<EOF
    - name: Tear down AlloyDB Omni cluster
      hosts: localhost
      vars:
        ansible_become: true
        ansible_user: $SSH_USER
        ansible_ssh_private_key_file: $HOME/ssh-key-cluster-sa
      roles:
      - role: google.alloydbomni_orchestrator.delete
    EOF
    
  2. 使用 ansible-playbook 執行應對手冊。如果已設定備份,請先刪除 Backup 和 BackupPlan 資源:
    ansible-playbook teardown.yaml -i deployment_spec.yaml  -e "resource_type=Backup" -e "resource_name=on-demand-backup"
    ansible-playbook teardown.yaml -i deployment_spec.yaml  -e "resource_type=BackupPlan" -e "resource_name=pgb-plan"
    
    如果您已設定 PgBouncer,請接著刪除連線集區資源:
    ansible-playbook teardown.yaml -i deployment_spec.yaml  -e "resource_type=PgBouncer" -e "resource_name=pgbouncer-pooler"
    
    接著,將 DBCluster 指定為 resource_type,然後刪除資料庫叢集本身:
    ansible-playbook teardown.yaml -i deployment_spec.yaml  -e "resource_type=DBCluster" -e "resource_name=$CLUSTER"
    
  3. 刪除叢集後,請登出控制節點,切換回終端機並前往 Terraform 工作目錄。
    注意:如果您剛登入 Cloud Shell,請記得設定以下項目:
    export PROJECT="your-project-id"
    export CLUSTER="your-cluster-name"
    
  4. 刪除 VRRP 防火牆規則,並刪除 Terraform 管理的資源:
    gcloud compute firewall-rules delete -q "$CLUSTER-allow-vrrp" --project="$PROJECT"
    gcloud compute firewall-rules delete -q "$CLUSTER-allow-ssh" --project="$PROJECT"
    terraform destroy
    
  5. 系統提示時,請確認要銷毀裝置。由於網路資源或路徑有時會殘留,導致無法完全拆除,請執行下列安全清除指令,確保所有相關聯的防火牆規則、NAT 閘道、路由器、路徑、子網路和網路都已完全移除。將 REGION 換成您的特定部署區域,例如「us-central1」:
    # Delete any remaining firewall rules associated with the cluster
    gcloud compute firewall-rules list --project=$PROJECT 2> /dev/null | grep ^$CLUSTER- | cut -f1 -d' ' | \
      while read rule; do gcloud compute firewall-rules delete --project=$PROJECT --quiet $rule; done
    
    # Delete the Cloud NAT gateway and router if they still exist
    gcloud compute routers nats describe $CLUSTER-nat-gw --router=$CLUSTER-router --region=REGION --project=$PROJECT 2>/dev/null \
      && gcloud compute routers nats delete $CLUSTER-nat-gw --router=$CLUSTER-router --region=REGION --project=$PROJECT --quiet
    
    gcloud compute routers describe $CLUSTER-router --region=REGION --project=$PROJECT 2>/dev/null \
      && gcloud compute routers delete $CLUSTER-router --region=REGION --project=$PROJECT --quiet
    
    # Delete any remaining custom routes
    gcloud compute routes list --project=$PROJECT --filter="network:$CLUSTER" --format="value(name)" 2>/dev/null | \
      while read route; do gcloud compute routes delete --project=$PROJECT --quiet $route 2>/dev/null || true; done
    
    # Delete the subnet and network if they still exist
    gcloud compute networks subnets describe $CLUSTER --region=REGION --project=$PROJECT 2>/dev/null \
      && gcloud compute networks subnets delete $CLUSTER --region=REGION --project=$PROJECT --quiet
    
    gcloud compute networks describe $CLUSTER --project $PROJECT 2> /dev/null \
      && gcloud compute networks delete $CLUSTER --project $PROJECT --quiet
    

11. 恭喜

恭喜您完成本程式碼研究室。

涵蓋內容

  • 如何建立及準備 GCE VM,以供 AlloyDB Omni 使用。
  • 如何安裝及執行 AlloyDB Omni RPM 自動調度管理工具。
  • 如何為高可用性參考架構安裝及設定 AlloyDB Omni RPM。

如要進一步瞭解 AlloyDB Omni,請參閱說明文件

12. 問卷調查

您會如何使用本教學課程?

僅閱讀內容 閱讀內容並完成練習