我为您的问题开发了一个 terraform 配置文件 示例。它已准备好使用,但应仅作为学习和测试目的的示例。它在us-east-1 区域使用默认 VPC 和 terraform 0.13 和 AWS 提供商 3.6 进行了测试。
示例 terraform 配置文件创建的关键资源是:
- 公共 MySQL aurora 集群 1 个写入器和 2 个副本。
- Aurora 副本的应用程序自动扩展策略基于 CPU 利用率 (50%),最小和最大容量分别为 2 和 4。
- SNS 主题和 SQS 队列订阅了该主题。借助队列,无需配置电子邮件或 lambda,即可轻松查看 SNS 消息。
- 两个 RDS 事件订阅。一个(例如故障)用于集群级事件,第二个用于实例级事件。在这两种情况下,事件都会发布到 SNS 主题,然后在 SQS 中可供查看。
下面我将详细介绍所提出的问题和示例配置文件。
具有 1 个写入器和 2 个副本的 Aurora MySQL 集群
集群将配备 1 个写入器和 2 个副本。
副本的自动缩放策略
application-auto-scaling 是基于 TargetTrackingScaling 的 RDSReaderAverageCPUUtilization。扩展策略基于副本的整体 CPU 利用率 (50%),而不是其单个副本。
这是一个很好的做法,因为极光副本在连接级别自动负载平衡。这意味着新连接将大致平均分布在可用副本中,前提是您使用的是reader enpoint。
一旦副本被扩展/扩展活动或故障替换,您可能应用于单个副本的任何警报或扩展策略都将失效。这是因为任何扩展策略都将绑定到特定的数据库实例。一旦实例消失,警报将不起作用。
可以在 CLoudWatch 警报控制台中查看与 AWS 代表您创建的策略相关联的警报。
Aurora 数据库实例失败
如果任何数据库实例失败,Aurora 将自动继续修复问题,包括重新启动数据库实例、将只读副本提升为新的主实例、重新连接 MySQL 或完全替换失败的实例.
您可以在一定程度上自己模拟这些事件,如Testing Amazon Aurora Using Fault Injection Queries 中所述。
测试故障转移到只读副本
aws rds failover-db-cluster --db-cluster-identifier aurora-cluster-demo
主实例测试崩溃
这将导致实例自动重启
mysql -h <endpoint> -u root -e "ALTER SYSTEM CRASH INSTANCE;"
阅读器实例测试崩溃
这将导致 MySQL 重新启动。
mysql -h <endpoint> -u root -e "ALTER SYSTEM SIMULATE 100 PERCENT READ REPLICA FAILURE TO ALL FOR INTERVAL 10 MINUTE;"
阅读器更换测试
您可以通过手动删除阅读器实例来模拟完全失败
控制台。删除后,Aurora 将自动提供替换。
监控集群故障
您可以使用Amazon RDS Event Notification自动检测和响应与您的 Aurora 集群及其实例相关的各种事件。失败是 RDS 事件通知机制捕获的事件之一。
您可以订阅感兴趣的事件类别并接收到 SNS 的通知。一旦检测到事件并将其发布到 SNS 中,您就可以使用它做您想做的事情。例如,调用 lambda 事件来分析事件和 Aurora 集群的当前状态、执行纠正措施或发送电子邮件通知。
例如,当您像之前那样手动强制故障转移时,您会收到一条消息
带有以下信息(仅显示片段):
\"Event Message\":\"Started cross AZ failover to DB instance: aurora-cluster-demo-1\"
及以后:
\"Event Message\":\"Completed failover to DB instance: aurora-cluster-demo-1\"}"
示例 terraform 配置文件订阅了许多类别。因此,您必须将它们微调到您所需要的。您也可以订阅所有这些文件,并让 lambda 函数在它们发生时分析它们,并决定是否应该只归档它们,或者该函数应该执行一些自动化过程。
AppAutoScaling 或 AutoScaling
Aurora 读取复制使用application-auto-scaling 进行缩放,而不是 AutoScaling(我在这里假设您的意思是 EC2 AutoScaling)。 EC2 AutoScaling 仅用于常规 EC2 实例,不适用于 RDS。
示例 terraform 配置文件
provider "aws" {
# YOUR DATA
region = "us-east-1"
}
data "aws_vpc" "default" {
default = true
}
resource "aws_rds_cluster" "default" {
cluster_identifier = "aurora-cluster-demo"
engine = "aurora-mysql"
engine_version = "5.7.mysql_aurora.2.03.2"
database_name = "myauroradb"
master_username = "root"
master_password = "bar4343sfdf233"
vpc_security_group_ids = [aws_security_group.allow_mysql.id]
backup_retention_period = 1
skip_final_snapshot = true
}
resource "aws_rds_cluster_instance" "cluster_instances" {
count = 3
identifier = "aurora-cluster-demo-${count.index}"
cluster_identifier = aws_rds_cluster.default.id
instance_class = "db.t2.small"
publicly_accessible = true
engine = aws_rds_cluster.default.engine
engine_version = aws_rds_cluster.default.engine_version
}
resource "aws_security_group" "allow_mysql" {
name = "allow_mysql"
description = "Allow Mysql inbound Internet traffic"
vpc_id = data.aws_vpc.default.id
ingress {
description = "Mysql poert"
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_appautoscaling_target" "replicas" {
service_namespace = "rds"
scalable_dimension = "rds:cluster:ReadReplicaCount"
resource_id = "cluster:${aws_rds_cluster.default.id}"
min_capacity = 2
max_capacity = 4
}
resource "aws_appautoscaling_policy" "replicas" {
name = "cpu-auto-scaling"
service_namespace = aws_appautoscaling_target.replicas.service_namespace
scalable_dimension = aws_appautoscaling_target.replicas.scalable_dimension
resource_id = aws_appautoscaling_target.replicas.resource_id
policy_type = "TargetTrackingScaling"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "RDSReaderAverageCPUUtilization"
}
target_value = 50
scale_in_cooldown = 300
scale_out_cooldown = 300
}
}
resource "aws_sns_topic" "default" {
name = "rds-events"
}
resource "aws_sqs_queue" "default" {
name = "aurora-notifications"
}
resource "aws_sns_topic_subscription" "user_updates_sqs_target" {
topic_arn = aws_sns_topic.default.arn
protocol = "sqs"
endpoint = aws_sqs_queue.default.arn
}
resource "aws_sqs_queue_policy" "test" {
queue_url = aws_sqs_queue.default.id
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "sqspolicy",
"Statement": [
{
"Sid": "First",
"Effect": "Allow",
"Principal": "*",
"Action": "sqs:SendMessage",
"Resource": "${aws_sqs_queue.default.arn}",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "${aws_sns_topic.default.arn}"
}
}
}
]
}
POLICY
}
resource "aws_db_event_subscription" "cluster" {
name = "cluster-events"
sns_topic = aws_sns_topic.default.arn
source_type = "db-cluster"
event_categories = [
"failover", "failure", "deletion", "notification"
]
}
resource "aws_db_event_subscription" "instances" {
name = "instances-events"
sns_topic = aws_sns_topic.default.arn
source_type = "db-instance"
event_categories = [
"availability",
"deletion",
"failover",
"failure",
"low storage",
"maintenance",
"notification",
"read replica",
"recovery",
"restoration",
]
}
output "endpoint" {
value = aws_rds_cluster.default.endpoint
}
output "reader-endpoint" {
value = aws_rds_cluster.default.reader_endpoint
}