使用 Amazon Global Aurora PostgreSQL 叢集

Amazon Global Aurora PostgreSQL 叢集可用於 Keycloak 的底層資料庫,無論是單站點或多站點組態。目前僅支援在 ROSA 上部署的 Keycloak。

Aurora 全域資料庫跨越多個 AWS 區域,可從 AWS 區域中的中斷進行災害復原。Aurora 會自動處理將所有資料和更新從主要 AWS 區域複製到每個次要區域。

部署 Aurora 叢集

可以透過執行 ./provision/aws/rds/aurora_create_global_db.sh 並使用以下環境變數,在多個 AWS 區域中部署 Aurora 叢集

AURORA_GLOBAL_REGIONS= # A list of AWS regions for the Aurora cluster to span. The first region in the list is where the Primary cluster is hosted.
AURORA_GLOBAL_CLUSTER= # The name of the Global Aurora cluster
AURORA_INSTANCES= # The number of Aurora db instances to create in each region, defaults to 1.

這會針對每個區域建立一個 Aurora 叢集,並將它們與全域 Aurora 叢集 $AURORA_GLOBAL_CLUSTER 相關聯。此腳本會等到所有區域叢集及其執行個體都可用後,才會傳回。如果全域叢集已存在,則會顯示訊息指出此情況,且腳本將會失敗並顯示結束代碼 1。

Aurora 全域資料庫叢集由多個區域叢集組成,每個叢集都有其專用的寫入器和讀取器端點。為了抽象化這一點,我們建立了一個 Route53 CNAME 項目,Keycloak 執行個體必須使用該項目才能連線到資料庫。Route53 項目會在 $AURORA_GLOBAL_CLUSTER.aurora-global.keycloak-benchmark.com 公開 Aurora 主要叢集的寫入器端點。

為了確保上述 Route53 項目在故障轉移後反映主要叢集的寫入器端點,我們會在每個 $AURORA_GLOBAL_REGIONS 中部署一個 AWS Lambda 函數。此函數會在全域故障轉移事件完成時觸發,位於新主要叢集的區域中,並更新 CNAME 項目以指向最新的寫入器端點。

指定的 AURORA_GLOBAL_CLUSTER 在每個 AWS 帳戶中必須是唯一的,並且必須遵循 「資料庫叢集識別碼」中概述的慣例。

將 ROSA 叢集連線至 Aurora 叢集

必須在 ROSA 叢集和每個個別的 Aurora 叢集之間建立對等連線。

若要設定此類連線,請使用以下環境執行 ./provision/aws/rds/aurora_create_global_peering_connections.sh

AURORA_GLOBAL_CLUSTER= # The name of the Global Aurora cluster
CLUSTER_NAME= # The name of the ROSA cluster to establish the peering connectin with

部署 Keycloak

透過各種工作檔案部署 Keycloak 時,必須設定以下環境變數,以確保設定正確的資料庫端點。

AURORA_GLOBAL_CLUSTER= # The name of the Global Aurora cluster
KC_DATABASE_URL=$AURORA_GLOBAL_CLUSTER.aurora-global.keycloak-benchmark.com
KC_DATABASE=aurora-postgres

模擬叢集故障轉移

可以透過執行 failover-global-cluster 命令,從主要 Aurora 叢集觸發故障轉移至次要 Aurora 叢集

aws rds failover-global-cluster \
  --global-cluster-identifier ${AURORA_GLOBAL_CLUSTER} \
  --target-db-cluster-identifier ${AURORA_CLUSTER_IDENTIFIER} \
  --allow-data-loss

其中 AURORA_CLUSTER_IDENTIFIER 是您希望成為主要叢集的次要叢集的 arn。以下命令會輸出全域 Aurora 叢集所有成員的 arn

aws rds describe-global-clusters \
  --query "GlobalClusters[?GlobalClusterIdentifier=='${AURORA_GLOBAL_CLUSTER}'].GlobalClusterMembers[*].DBClusterArn"

從 Aurora 叢集斷開 ROSA 叢集的連線

若要移除 ROSA 和 Aurora VPC 之間的對等連線,請使用以下環境執行 ./provision/aws/rds/aurora_delete_global_peering_connection.sh

AURORA_GLOBAL_CLUSTER= # The name of the Global Aurora cluster
CLUSTER_NAME= # The name of the ROSA cluster to remove the peering connection from

刪除 Aurora 叢集

在刪除 Aurora 叢集之前,必須先移除所有與 ROSA 叢集建立的對等連線。

若要移除 Aurora 叢集,請使用以下環境執行 ./provision/aws/rds/aurora_delete_global_db.sh

AURORA_GLOBAL_CLUSTER= # The name of the Global Aurora cluster