Aurora 数据库集群由多个 Aurora 数据库实例组成,其中一个实例被指定为主要写入器,其他所有实例被指定为备份读取器。为了确保在可用区发生故障时的高可用性,Aurora 允许跨单个 AWS 区域的多个可用区部署数据库实例。如果托管主数据库实例的可用区发生故障,Aurora 会自动修复自身,并将一个读取器实例从未发生故障的可用区提升为新的写入器实例。
有关 Aurora 数据库提供的语义的更多详细信息,请参阅 AWS Aurora 文档。
本文档遵循 AWS 最佳实践,并创建了一个私有 Aurora 数据库,该数据库不会公开到 Internet。要从 ROSA 集群访问数据库,请 建立数据库与 ROSA 集群之间的对等连接。
以下流程包含两个部分
在 eu-west-1 中创建名为“keycloak-aurora”的 Aurora 多 AZ 数据库集群。
创建 ROSA 集群与 Aurora VPC 之间的对等连接,以允许部署在 ROSA 集群上的应用程序建立与数据库的连接。
为 Aurora 集群创建 VPC
aws ec2 create-vpc \
--cidr-block 192.168.0.0/16 \
--tag-specifications "ResourceType=vpc, Tags=[{Key=AuroraCluster,Value=keycloak-aurora}]" \(1)
--region eu-west-1
1 | 我们添加了一个可选的标签,其中包含 Aurora 集群的名称,以便我们可以轻松地检索 VPC。 |
{
"Vpc": {
"CidrBlock": "192.168.0.0/16",
"DhcpOptionsId": "dopt-0bae7798158bc344f",
"State": "pending",
"VpcId": "vpc-0b40bd7c59dbe4277",
"OwnerId": "606671647913",
"InstanceTenancy": "default",
"Ipv6CidrBlockAssociationSet": [],
"CidrBlockAssociationSet": [
{
"AssociationId": "vpc-cidr-assoc-09a02a83059ba5ab6",
"CidrBlock": "192.168.0.0/16",
"CidrBlockState": {
"State": "associated"
}
}
],
"IsDefault": false
}
}
为 Aurora 将要部署到的每个可用区创建子网,使用新创建的 VPC 的 VpcId
。
为每个可用区指定的 cidr-block 范围不得重叠。 |
区域 A
aws ec2 create-subnet \
--availability-zone "eu-west-1a" \
--vpc-id vpc-0b40bd7c59dbe4277 \
--cidr-block 192.168.0.0/19 \
--region eu-west-1
{
"Subnet": {
"AvailabilityZone": "eu-west-1a",
"AvailabilityZoneId": "euw1-az3",
"AvailableIpAddressCount": 8187,
"CidrBlock": "192.168.0.0/19",
"DefaultForAz": false,
"MapPublicIpOnLaunch": false,
"State": "available",
"SubnetId": "subnet-0d491a1a798aa878d",
"VpcId": "vpc-0b40bd7c59dbe4277",
"OwnerId": "606671647913",
"AssignIpv6AddressOnCreation": false,
"Ipv6CidrBlockAssociationSet": [],
"SubnetArn": "arn:aws:ec2:eu-west-1:606671647913:subnet/subnet-0d491a1a798aa878d",
"EnableDns64": false,
"Ipv6Native": false,
"PrivateDnsNameOptionsOnLaunch": {
"HostnameType": "ip-name",
"EnableResourceNameDnsARecord": false,
"EnableResourceNameDnsAAAARecord": false
}
}
}
区域 B
aws ec2 create-subnet \
--availability-zone "eu-west-1b" \
--vpc-id vpc-0b40bd7c59dbe4277 \
--cidr-block 192.168.32.0/19 \
--region eu-west-1
{
"Subnet": {
"AvailabilityZone": "eu-west-1b",
"AvailabilityZoneId": "euw1-az1",
"AvailableIpAddressCount": 8187,
"CidrBlock": "192.168.32.0/19",
"DefaultForAz": false,
"MapPublicIpOnLaunch": false,
"State": "available",
"SubnetId": "subnet-057181b1e3728530e",
"VpcId": "vpc-0b40bd7c59dbe4277",
"OwnerId": "606671647913",
"AssignIpv6AddressOnCreation": false,
"Ipv6CidrBlockAssociationSet": [],
"SubnetArn": "arn:aws:ec2:eu-west-1:606671647913:subnet/subnet-057181b1e3728530e",
"EnableDns64": false,
"Ipv6Native": false,
"PrivateDnsNameOptionsOnLaunch": {
"HostnameType": "ip-name",
"EnableResourceNameDnsARecord": false,
"EnableResourceNameDnsAAAARecord": false
}
}
}
获取 Aurora VPC 路由表的 ID
aws ec2 describe-route-tables \
--filters Name=vpc-id,Values=vpc-0b40bd7c59dbe4277 \
--region eu-west-1
{
"RouteTables": [
{
"Associations": [
{
"Main": true,
"RouteTableAssociationId": "rtbassoc-02dfa06f4c7b4f99a",
"RouteTableId": "rtb-04a644ad3cd7de351",
"AssociationState": {
"State": "associated"
}
}
],
"PropagatingVgws": [],
"RouteTableId": "rtb-04a644ad3cd7de351",
"Routes": [
{
"DestinationCidrBlock": "192.168.0.0/16",
"GatewayId": "local",
"Origin": "CreateRouteTable",
"State": "active"
}
],
"Tags": [],
"VpcId": "vpc-0b40bd7c59dbe4277",
"OwnerId": "606671647913"
}
]
}
将 Aurora VPC 路由表与每个可用区的子网关联
区域 A
aws ec2 associate-route-table \
--route-table-id rtb-04a644ad3cd7de351 \
--subnet-id subnet-0d491a1a798aa878d \
--region eu-west-1
区域 B
aws ec2 associate-route-table \
--route-table-id rtb-04a644ad3cd7de351 \
--subnet-id subnet-057181b1e3728530e \
--region eu-west-1
创建 Aurora 子网组
aws rds create-db-subnet-group \
--db-subnet-group-name keycloak-aurora-subnet-group \
--db-subnet-group-description "Aurora DB Subnet Group" \
--subnet-ids subnet-0d491a1a798aa878d subnet-057181b1e3728530e \
--region eu-west-1
创建 Aurora 安全组
aws ec2 create-security-group \
--group-name keycloak-aurora-security-group \
--description "Aurora DB Security Group" \
--vpc-id vpc-0b40bd7c59dbe4277 \
--region eu-west-1
{
"GroupId": "sg-0d746cc8ad8d2e63b"
}
创建 Aurora DB 集群
aws rds create-db-cluster \
--db-cluster-identifier keycloak-aurora \
--database-name keycloak \
--engine aurora-postgresql \
--engine-version ${properties["aurora-postgresql.version"]} \
--master-username keycloak \
--master-user-password secret99 \
--vpc-security-group-ids sg-0d746cc8ad8d2e63b \
--db-subnet-group-name keycloak-aurora-subnet-group \
--region eu-west-1
您应该替换 --master-username 和 --master-user-password 值。在配置 Keycloak 数据库凭据时必须使用此处指定的这些值。 |
{
"DBCluster": {
"AllocatedStorage": 1,
"AvailabilityZones": [
"eu-west-1b",
"eu-west-1c",
"eu-west-1a"
],
"BackupRetentionPeriod": 1,
"DatabaseName": "keycloak",
"DBClusterIdentifier": "keycloak-aurora",
"DBClusterParameterGroup": "default.aurora-postgresql15",
"DBSubnetGroup": "keycloak-aurora-subnet-group",
"Status": "creating",
"Endpoint": "keycloak-aurora.cluster-clhthfqe0h8p.eu-west-1.rds.amazonaws.com",
"ReaderEndpoint": "keycloak-aurora.cluster-ro-clhthfqe0h8p.eu-west-1.rds.amazonaws.com",
"MultiAZ": false,
"Engine": "aurora-postgresql",
"EngineVersion": "15.5",
"Port": 5432,
"MasterUsername": "keycloak",
"PreferredBackupWindow": "02:21-02:51",
"PreferredMaintenanceWindow": "fri:03:34-fri:04:04",
"ReadReplicaIdentifiers": [],
"DBClusterMembers": [],
"VpcSecurityGroups": [
{
"VpcSecurityGroupId": "sg-0d746cc8ad8d2e63b",
"Status": "active"
}
],
"HostedZoneId": "Z29XKXDKYMONMX",
"StorageEncrypted": false,
"DbClusterResourceId": "cluster-IBWXUWQYM3MS5BH557ZJ6ZQU4I",
"DBClusterArn": "arn:aws:rds:eu-west-1:606671647913:cluster:keycloak-aurora",
"AssociatedRoles": [],
"IAMDatabaseAuthenticationEnabled": false,
"ClusterCreateTime": "2023-11-01T10:40:45.964000+00:00",
"EngineMode": "provisioned",
"DeletionProtection": false,
"HttpEndpointEnabled": false,
"CopyTagsToSnapshot": false,
"CrossAccountClone": false,
"DomainMemberships": [],
"TagList": [],
"AutoMinorVersionUpgrade": true,
"NetworkType": "IPV4"
}
}
创建 Aurora DB 实例
创建区域 A 写入器实例
aws rds create-db-instance \
--db-cluster-identifier keycloak-aurora \
--db-instance-identifier "keycloak-aurora-instance-1" \
--db-instance-class db.t4g.large \
--engine aurora-postgresql \
--region eu-west-1
创建区域 B 读取器实例
aws rds create-db-instance \
--db-cluster-identifier keycloak-aurora \
--db-instance-identifier "keycloak-aurora-instance-2" \
--db-instance-class db.t4g.large \
--engine aurora-postgresql \
--region eu-west-1
等待所有写入器和读取器实例准备就绪
aws rds wait db-instance-available --db-instance-identifier keycloak-aurora-instance-1 --region eu-west-1
aws rds wait db-instance-available --db-instance-identifier keycloak-aurora-instance-2 --region eu-west-1
aws rds describe-db-clusters \
--db-cluster-identifier keycloak-aurora \
--query 'DBClusters[*].Endpoint' \
--region eu-west-1 \
--output text
[
"keycloak-aurora.cluster-clhthfqe0h8p.eu-west-1.rds.amazonaws.com"
]
对包含 Keycloak 部署的每个 ROSA 集群执行这些步骤。
检索 Aurora VPC
aws ec2 describe-vpcs \
--filters "Name=tag:AuroraCluster,Values=keycloak-aurora" \
--query 'Vpcs[*].VpcId' \
--region eu-west-1 \
--output text
vpc-0b40bd7c59dbe4277
检索 ROSA 集群 VPC
使用 oc
登录 ROSA 集群
检索 ROSA VPC
NODE=$(kubectl get nodes --selector=node-role.kubernetes.io/worker -o jsonpath='{.items[0].metadata.name}')
aws ec2 describe-instances \
--filters "Name=private-dns-name,Values=${NODE}" \
--query 'Reservations[0].Instances[0].VpcId' \
--region eu-west-1 \
--output text
vpc-0b721449398429559
创建对等连接
aws ec2 create-vpc-peering-connection \
--vpc-id vpc-0b721449398429559 \(1)
--peer-vpc-id vpc-0b40bd7c59dbe4277 \(2)
--peer-region eu-west-1 \
--region eu-west-1
1 | ROSA 集群 VPC |
2 | Aurora VPC |
{
"VpcPeeringConnection": {
"AccepterVpcInfo": {
"OwnerId": "606671647913",
"VpcId": "vpc-0b40bd7c59dbe4277",
"Region": "eu-west-1"
},
"ExpirationTime": "2023-11-08T13:26:30+00:00",
"RequesterVpcInfo": {
"CidrBlock": "10.0.17.0/24",
"CidrBlockSet": [
{
"CidrBlock": "10.0.17.0/24"
}
],
"OwnerId": "606671647913",
"PeeringOptions": {
"AllowDnsResolutionFromRemoteVpc": false,
"AllowEgressFromLocalClassicLinkToRemoteVpc": false,
"AllowEgressFromLocalVpcToRemoteClassicLink": false
},
"VpcId": "vpc-0b721449398429559",
"Region": "eu-west-1"
},
"Status": {
"Code": "initiating-request",
"Message": "Initiating Request to 606671647913"
},
"Tags": [],
"VpcPeeringConnectionId": "pcx-0cb23d66dea3dca9f"
}
}
等待对等连接存在
aws ec2 wait vpc-peering-connection-exists --vpc-peering-connection-ids pcx-0cb23d66dea3dca9f
接受对等连接
aws ec2 accept-vpc-peering-connection \
--vpc-peering-connection-id pcx-0cb23d66dea3dca9f \
--region eu-west-1
{
"VpcPeeringConnection": {
"AccepterVpcInfo": {
"CidrBlock": "192.168.0.0/16",
"CidrBlockSet": [
{
"CidrBlock": "192.168.0.0/16"
}
],
"OwnerId": "606671647913",
"PeeringOptions": {
"AllowDnsResolutionFromRemoteVpc": false,
"AllowEgressFromLocalClassicLinkToRemoteVpc": false,
"AllowEgressFromLocalVpcToRemoteClassicLink": false
},
"VpcId": "vpc-0b40bd7c59dbe4277",
"Region": "eu-west-1"
},
"RequesterVpcInfo": {
"CidrBlock": "10.0.17.0/24",
"CidrBlockSet": [
{
"CidrBlock": "10.0.17.0/24"
}
],
"OwnerId": "606671647913",
"PeeringOptions": {
"AllowDnsResolutionFromRemoteVpc": false,
"AllowEgressFromLocalClassicLinkToRemoteVpc": false,
"AllowEgressFromLocalVpcToRemoteClassicLink": false
},
"VpcId": "vpc-0b721449398429559",
"Region": "eu-west-1"
},
"Status": {
"Code": "provisioning",
"Message": "Provisioning"
},
"Tags": [],
"VpcPeeringConnectionId": "pcx-0cb23d66dea3dca9f"
}
}
更新 ROSA 集群 VPC 路由表
ROSA_PUBLIC_ROUTE_TABLE_ID=$(aws ec2 describe-route-tables \
--filters "Name=vpc-id,Values=vpc-0b721449398429559" "Name=association.main,Values=true" \(1)
--query "RouteTables[*].RouteTableId" \
--output text \
--region eu-west-1
)
aws ec2 create-route \
--route-table-id ${ROSA_PUBLIC_ROUTE_TABLE_ID} \
--destination-cidr-block 192.168.0.0/16 \(2)
--vpc-peering-connection-id pcx-0cb23d66dea3dca9f \
--region eu-west-1
1 | ROSA 集群 VPC |
2 | 这必须与创建 Aurora VPC 时使用的 cidr-block 相同 |
更新 Aurora 安全组
AURORA_SECURITY_GROUP_ID=$(aws ec2 describe-security-groups \
--filters "Name=group-name,Values=keycloak-aurora-security-group" \
--query "SecurityGroups[*].GroupId" \
--region eu-west-1 \
--output text
)
aws ec2 authorize-security-group-ingress \
--group-id ${AURORA_SECURITY_GROUP_ID} \
--protocol tcp \
--port 5432 \
--cidr 10.0.17.0/24 \(1)
--region eu-west-1
1 | ROSA 集群的“machine_cidr” |
{
"Return": true,
"SecurityGroupRules": [
{
"SecurityGroupRuleId": "sgr-0785d2f04b9cec3f5",
"GroupId": "sg-0d746cc8ad8d2e63b",
"GroupOwnerId": "606671647913",
"IsEgress": false,
"IpProtocol": "tcp",
"FromPort": 5432,
"ToPort": 5432,
"CidrIpv4": "10.0.17.0/24"
}
]
}
验证 ROSA 集群和 Aurora DB 集群之间是否可以建立连接的最简单方法是在 Openshift 集群上部署 psql
并尝试连接到写入器端点。
以下命令在默认命名空间中创建一个 Pod,如果可能,则与 Aurora 集群建立 psql
连接。退出 Pod shell 后,Pod 将被删除。
USER=keycloak (1)
PASSWORD=secret99 (2)
DATABASE=keycloak (3)
HOST=$(aws rds describe-db-clusters \
--db-cluster-identifier keycloak-aurora \(4)
--query 'DBClusters[*].Endpoint' \
--region eu-west-1 \
--output text
)
kubectl run -i --tty --rm debug --image=postgres:15 --restart=Never -- psql postgresql://${USER}:${PASSWORD}@${HOST}/${DATABASE}
1 | Aurora DB 用户,这可以与创建 DB 时使用的 --master-username 相同。 |
2 | Aurora DB 用户密码,这可以与创建 DB 时使用的 --master—user-password 相同。 |
3 | Aurora DB 的名称,例如 --database-name 。 |
4 | Aurora DB 集群的名称。 |
现在 Aurora 数据库已建立并与您所有的 ROSA 集群链接,以下列出了将 Aurora 数据库与 Keycloak 连接的相关 Keycloak CR 选项。这些更改将在 使用 Keycloak 运算符部署 Keycloak 以实现高可用性 指南中需要。JDBC url 被配置为使用 Aurora 数据库写入器端点。
将 spec.db.url
更新为 jdbc:aws-wrapper:postgresql://$HOST:5432/keycloak
,其中 $HOST
是 Aurora 写入器端点 URL。
确保 spec.db.usernameSecret
和 spec.db.passwordSecret
引用的 Secrets 包含创建 Aurora 时定义的用户名和密码。
成功部署 Aurora 数据库后,继续执行 使用 Infinispan 运算符部署 Infinispan 以实现高可用性