使用 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 集群,并将它们与 Global Aurora 集群 $AURORA_GLOBAL_CLUSTER 关联。脚本会等待直到所有区域集群及其实例都可用后才返回。如果全局集群已存在,则会显示一条消息指示这一点,并且脚本将失败并返回退出代码 1。

一个 Aurora Global DB 集群由多个区域集群组成,每个集群都有自己专用的 Writer 和 Reader 端点。为了抽象这一点,我们创建了一个 Route53 CNAME 条目,Keycloak 实例必须使用该条目连接到数据库。 Route53 条目在 $AURORA_GLOBAL_CLUSTER.aurora-global.keycloak-benchmark.com 暴露 Aurora 主集群的 writer 端点。

为了确保上述 Route53 条目在故障转移后反映主集群的 writer 端点,我们在每个 $AURORA_GLOBAL_REGIONS 部署了一个 AWS Lambda 函数。此函数在全局故障转移事件完成时被触发,在新主集群的区域中,并更新 CNAME 条目以指向最新的 writer 端点。

指定的 AURORA_GLOBAL_CLUSTER 在每个 AWS 账户中必须是唯一的,并遵循 "DB cluster identifier" 中概述的约定。

将 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

当通过各种 task 文件部署 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。以下命令输出 Global Aurora 集群所有成员的 ARN

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

断开 ROSA 集群与 Aurora 集群的连接

要删除 ROSA 和 Aurora VPCS 之间的对等连接,请使用以下环境执行 ./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