我尝试了 Aurora for PostgreSQL 的全托管的蓝/绿部署
首先
从2022年的re:invent宣布MySQL的Blue/Green部署开始,大约一年后,PostgreSQL(Aurora/RDS)也宣布推出了全托管的Blue/Green GA。
-
- Amazon RDS Blue/Green Deployments now supports Aurora and RDS PostgreSQL
- New – Fully managed Blue/Green Deployment in Amazon Aurora PostgreSQL and Amazon RDS for PostgreSQL
蓝/绿部署是什么意思?
一般的的蓝/绿色部署是指以下含义。
蓝绿部署是指在当前生产环境(蓝色)之外,建立一个新的生产环境(绿色),然后通过切换负载均衡器的连接目标等方式来发布新的生产环境的运维方法。
对于数据库的情况,切换和回滚时不能有数据丢失,因此必须进行数据同步等操作,需要一些高级技术。
根据以下描述,在Aurora PostgreSQL中进行手动的蓝/绿部署需要创建克隆数据库来进行数据同步和切换等操作。
- Amazon Aurora PostgreSQL blue/green deployment using fast database cloning
在PostgreSQL的蓝/绿部署中的限制事项。
限制事项可以在以下指南中找到,但重要的是,为了使用PostgreSQL的逻辑复制将数据从Blue环境复制到Green环境,受逻辑复制限制的功能将不被允许,或者需要重新创建Green环境。例如,不允许对没有主键的表进行更新或删除操作。
ブルー/グリーンデプロイの制限事項(2023/10/28時点では英語版のみ)
搭建针对Aurora for PostgreSQL的蓝/绿部署环境。
我們將嘗試在Aurora for PostgreSQL上建立Blue/Green部署環境。
在开始之前,需要进行一些准备工作(确认前提条件)。
蓝/绿部署基于PostgreSQL,并使用逻辑复制,因此蓝色一侧需要设置以下参数。
- rds.logical_replication

蓝/绿色的部署环境建立

綠側的引擎版本可以選擇藍側的版本以上(本次藍側為13.12)。(需要事先創建綠側版本的參數組)此次選擇15.4版本。

执行后,将为Green一侧的群集进行配置。



确认绿色环境
确认数据在绿色环境中的复制
当在Blue一侧添加数据时,可以确认数据也被复制到Green一侧。
$ psql -h bgdb01.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com -p 5432 -U postgres -d bgdb01
psql (13.7, server 13.12)
SSL connection (protocol: TLSv1.2, cipher: AES128-SHA256, bits: 128, compression: off)
Type "help" for help.
bgdb01=> select * from test01;
id | name | testcol
------+----------+---------
1 | hogehoge | 1
2 | hogehoge | 2
(2 rows)
bgdb01=> insert into test01 values (0003,'hogehogehoge',03);
INSERT 0 1
bgdb01=> commit;
WARNING: there is no transaction in progress
COMMIT
bgdb01=>
bgdb01=> select * from test01;
id | name | testcol
------+--------------+---------
1 | hogehoge | 1
2 | hogehoge | 2
3 | hogehogehoge | 3
(3 rows)
$ psql -h bgdb01-instance-1-green-xxxxxx.xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com -p 5432 -U postgres -d bgdb01
psql (13.7, server 15.4)
WARNING: psql major version 13, server major version 15.
Some psql features might not work.
SSL connection (protocol: TLSv1.2, cipher: AES128-SHA256, bits: 128, compression: off)
Type "help" for help.
bgdb01=> select * from testb10;
ERROR: relation "testb10" does not exist
LINE 1: select * from testb10;
^
bgdb01=> select * from test01;
id | name | testcol
------+--------------+---------
1 | hogehoge | 1
2 | hogehoge | 2
3 | hogehogehoge | 3
(3 rows)
执行对蓝色环境的DDL操作
当在Blue环境执行DDL时,会输出WARNING。
由于此状态下尝试执行Blue/Green切换会导致回滚,因此需要重新创建Green环境。
$ psql -h bgdb01.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com -p 5432 -U postgres -d bgdb01
psql (13.7, server 13.12)
SSL connection (protocol: TLSv1.2, cipher: AES128-SHA256, bits: 128, compression: off)
Type "help" for help.
bgdb01=> create table test02 (id char(4) not null, name text not null, testcol integer, primary key(id));
WARNING: command will not be replicated to the green instance: "CREATE TABLE"
CREATE TABLE
在针对没有主键的表的情况下,执行对蓝色环境的更新/删除操作。
当你试图对没有主键的Blue环境表进行更新/删除操作时,会发生错误并无法执行。
$ psql -h bgdb01.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com -p 5432 -U postgres -d bgdb01
psql (13.7, server 13.12)
SSL connection (protocol: TLSv1.2, cipher: AES128-SHA256, bits: 128, compression: off)
Type "help" for help.
bgdb01=> insert into test03 values(1,current_timestamp);
INSERT 0 1
bgdb01=> commit;
WARNING: there is no transaction in progress
COMMIT
bgdb01=> delete from test03;
ERROR: cannot delete from table "test03" because it does not have a replica identity and publishes deletes
HINT: To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE.
Aurora for PostgreSQL切换到蓝/绿部署





切换时的事件

切换日志
在切换至Aurora的Endpoint时,我们执行了一个每秒插入的脚本。第65次插入由于超时而导致错误,并且接下来大约有2分钟的间隔。(由于等待超时,实际的切换时间可能会稍短)
INSERT 0 1
WARNING: there is no transaction in progress
COMMIT
63
INSERT 0 1
WARNING: there is no transaction in progress
COMMIT
64
psql: error: could not connect to server: Connection timed out
Is the server running on host "bgdb01.cluster-xxxxxxxxxxxx.ap-northeast-1.rds.amazonaws.com" (10.12.52.122) and accepting
TCP/IP connections on port 5432?
65
INSERT 0 1
WARNING: there is no transaction in progress
COMMIT
66
63 | 2023-10-28 11:08:32.167017
64 | 2023-10-28 11:08:33.195218
66 | 2023-10-28 11:10:44.495658
总结
我试用了Aurora for PostgreSQL的全托管的蓝绿部署切换。与手动构建环境并进行切换相比,我认为它更易于使用。
然而,由于本次确认仅在小规模环境下进行,我仅限于功能确认,实际上,由于使用了逻辑复制,性能方面需要注意同步确认等方面的支持。关于这方面的注意事项也在功能介绍博客的最佳实践中进行了说明,希望在考虑使用时,请确认一下。