LoginSignup
1
1

RDSのBlue/Green Deploymentをやってみた

Posted at

Agenda

今回は、以下のことを解説します。

  • RDS Blue/Green Deploymentの概要
  • RDS Blue/Green Deploymentデプロイメントの使い方

本動画は以下の方を対象にしています

  • RDS Blue/Green Deploymentデプロイを使うメリットが知りたい
  • RDS Blue/Green Deploymentデプロイのやり方を知りたい

RDS Blue/Green Deploymentとは?

rds-bg-ex01.png

まず、Blue/Green Deploymentとは、本番環境(Blue)とステージング環境(Green)を使って、
システム変更作業をダウンタイムなしに行うことができるデプロイ方式です。

Blue/Green Deploymentはとても便利なシステムです。

本番環境(Blue)はそのままにして、ステージング環境(Green)で変更を行えます。
このやり方だと、テスト中に本番環境への影響を防げます。

この機能をデータベースにも応用したのが、今回紹介するRDS Blue/Green Deploymentです。

rds-bg-ex02.png

たとえば、新しいデータベースのアップグレードを考えているとき、まずGreen環境でアップグレードを行って、問題がないか確認する。
確認が終わったら、Greenを新しい本番環境として切り替えます。
これで、アップグレード中のダウンタイムがほとんどなくなり、リスクも低減されます。

なのでAmazon AuroraのBlue/Green Deploymentは、データベースの変更を安全かつ迅速に行うのにとても有効な手段です。

データベースをBlue/Green Deploymentするというのが発表された時は衝撃を覚えました。
去年発表され、最近正式にGAされてたので、データベースを運用している方は是非とも押さえておきたい機能のひとつです。

RDSのBlue/Greenデプロイの仕組み

では、どのようにRDS上でBlue/Green Deploymentを実現しているのでしょうか。

rds-bg-ex03.png

まず、AWS上でBlue/Green Deploymentを作成すると、
現在の本番環境(Blue)とGreenがステージング環境(Green)が作られます。

rds-bg-ex04.png

Green環境は本番環境のコピーで、レプリケーションを使って設定がコピーされてます。
この時、Green環境のクラスター名とインスタンス名の末尾にランダムな文字列が追加されます。
Blue/Green Deployment作成時に、Green環境でデータベースクラスターの変更やアップグレード、パラメーターの変更を行うことが可能です。
もちろん、この時点で本番環境には影響ありません。
デフォルトでは、Blue環境のReaderインスタンスから設定をコピーしますが、
Blue環境のプライマリDBからGreen環境のプライマリDBに設定をコピーすることも可能です。

rds-bg-ex05.png

Blue/Green Deployment作成後はステージング環境作業をしていきます。
DBスキーマや、インスタンスクラスの変更、動作テストなどができます。
この時、Green環境のDBクラスターはデフォルトで書き込み処理が許可されているので、
必要に応じてReadOnlyに設定しておくことが公式で推奨されています。

rds-bg-ex06.png

十分にテストして問題なければ、Greenを新しい本番環境として切り替えます。
切り替えは、だいたい1分未満で完了するので、従来のリリース作業よりもダウンタイムを短く抑えられます。

rds-bg-ex07.png

これでGreen環境が新本番環境となり、Blue/Green Deploymentが完了しました。

旧本番環境のBlue環境は手動で削除しない限りはそのまま残ります。
万が一、不具合が発生した場合はすぐにBlue環境にロールバックが可能です。
すぐに利用しない場合は一時停止しておくことでコストを抑えられますし、不要であればそのまま手動削除してしまっても良いです。

以上がBlue/Green Deploymentの流れとなります。

Blue/Green Deploymentの制限事項

Blue/Green Deploymentを利用するにあたって、いくつか注意事項があります。

たとえば、今回のハンズオンではAurora MySQLを使っていくわけですが、
MySQLバージョン8.0.11〜8.0.13はバグの影響でBlueグリーンデプロイが使えないようです。

また、Blueグリーンデプロイは、以下の機能ではサポートしていません

  • RDS Proxy
  • クロスリージョンリードレプリカ
  • AWS CloudFormation
  • マルチAZクラスターデプロイ etc.

PostgreSQLを利用する場合でも、使えない機能もいくつかあるようですので、
実際に業務で利用する場合は公式ドキュメントを確認しましょう。

ハンズオン概要

それでは今回のハンズオンについて説明します。

今回はサンプルのデータベースを作成したAurora MySQLのバージョンアップをBlue/ブリーんデプロイでやってみようと思います。

まず、以下を前提条件にハンズオンを進めていきます

  • AWSアカウントは作成済みであること
  • 簡単なLinux、MySQLコマンドが使えること(知らなくてもハンズオンは一応できます)
  • AWS CloudFormationの使い方を知っていること(動画を見れば一応できます)

rds-bg-ex08.png

流れは以下の通りです

  1. CloudFormationでハンズオン環境構築
  2. EC2からAuroraに対してサンプルDBを作成
  3. Blue/Green Deployment作成
  4. 切り替え
  5. 後片付け

それではやっていきましょう。

ハンズオン編

スタック作成

CloudFormtaionテンプレートからスタックを作成します。
利用するCloudFormationテンプレートは以下のaurora-bg-cfn.ymlに記載ありますのでそちらを利用ください。

※リージョンはap-northeast-1(東京リージョン)を指定するのを忘れないでください

rds-bg-01.png

作成完了後、AuroraのWriterエンドポイントをコピーしておく

rds-bg-02.png

サンプルDB作成

EC2にセッションマネージャーで接続をしましょう。
EC2コンソール画面からaurora-bg-dev-ec2を選択し、セッションマネージャーに接続。

rds-bg-03.png
rds-bg-04.png

デフォルトのec2-userで作業をしていく

sh-5.2$ bash
[ssm-user@ip-192-168-10-100 bin]$ sudo su -
[root@ip-192-168-10-100 ~]# su - ec2-user
[ec2-user@ip-192-168-10-100 ~]$

EC2にセッションマネージャー接続し、SQLファイル作成
サンプルのSQLファイルはGitHubリポジトリにおいています。
create-sample-db.sqlです。

[ec2-user@ip-192-168-10-100 ~]$ vi create-sample-db.sql
[ec2-user@ip-192-168-10-100 ~]$ cat create-sample-db.sql
-- データベースの作成
CREATE DATABASE `test-db`;

-- 新しいデータベースに接続
USE `test-db`;

-- 社員情報を格納するテーブルの作成
CREATE TABLE `employees` (
    `employee_id` INT NOT NULL AUTO_INCREMENT,
    `employee_name` VARCHAR(100) NOT NULL,
    PRIMARY KEY (`employee_id`)
);

-- テストデータの挿入(オプション)
INSERT INTO `employees` (`employee_name`) VALUES ('hogehoge');
INSERT INTO `employees` (`employee_name`) VALUES ('fugafuga');

作成したSQLファイルをDBに投入しましょう。

$ mysql -h <AuroraのWriterエンドポイント名> -u rdsroot -pPassW0rd < ./create-sample-db.sql

[ec2-user@ip-192-168-10-100 ~]$ mysql -h dev-aurora-bg-db.cluster-cedmf6a17cts.ap-northeast-1.rds.amazonaws.com -u rdsroot -pPassW0rd < ./create-sample-db.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

Auroraに接続し、サンプルデータを確認

mysql> USE `test-db`;
Database changed
mysql> SELECT * FROM employees;
+-------------+---------------+
| employee_id | employee_name |
+-------------+---------------+
|           1 | hogehoge      |
|           2 | fugafuga      |
+-------------+---------------+
2 rows in set (0.01 sec)

Blueグリーンデプロイ作成

RDSのデータベース画面を選択し、クラスターを選択し、
[Actions]-[Create Blue/Green Deployment]を選択

rds-bg-05.png

エンジンバージョンを変えて[Create staging environment]を選択

rds-bg-06.png

以下のようにGreen環境を作成し始める。
しばらく待つ。

rds-bg-07.png

以下でダウンタイムが発生しないか確認しておきましょう。

$ while :; do mysqladmin ping -u rdsroot -pPassW0rd -h ${DBホスト名}; date; sleep 1; done

グリーン環境が完成

rds-bg-08.png

グリン環境の確認

作成したBlueグリーンを選択すると、
Blue環境とグリーン環境の情報が確認できる

rds-bg-09.png

rds-bg-10.png

rds-bg-11.png

グリーン環境のDBに試しに接続してみる

[ec2-user@ip-192-168-10-100 ~]$ mysql -h dev-aurora-bg-db-green-ajyguq.cluster-cedmf6a17cts.ap-northeast-1.rds.amazonaws.com -u rdsroot -pPassW0rd
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 372
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test-db            |
+--------------------+
5 rows in set (0.00 sec)

mysql> USE test-db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SHOW TABLES;
+-------------------+
| Tables_in_test-db |
+-------------------+
| employees         |
+-------------------+
1 row in set (0.01 sec)

mysql> SELECT * FROM employees;
+-------------+---------------+
| employee_id | employee_name |
+-------------+---------------+
|           1 | hogehoge      |
|           2 | fugafuga      |
+-------------+---------------+
2 rows in set (0.00 sec)

また、リアルタイムでレプリケーション同期しているので、
Blue環境のDBを変更すれば、即時でグリーン環境にも設定は変更される。

試しにBlue環境のDBにデータを追加してみる。

sh-5.2$ mysql -h dev-aurora-bg-db.cluster-cedmf6a17cts.ap-northeast-1.rds.amazonaws.com -u rdsroot -pPassW0rd
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 666
Server version: 8.0.28 Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test-db            |
+--------------------+
5 rows in set (0.01 sec)

mysql> USE test-db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SELECT * FROM employees;
+-------------+---------------+
| employee_id | employee_name |
+-------------+---------------+
|           1 | hogehoge      |
|           2 | fugafuga      |
+-------------+---------------+
2 rows in set (0.00 sec)

mysql> INSERT INTO `employees` (`employee_name`) VALUES ('morimori');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM employees;
+-------------+---------------+
| employee_id | employee_name |
+-------------+---------------+
|           1 | hogehoge      |
|           2 | fugafuga      |
|           3 | morimori      |
+-------------+---------------+
3 rows in set (0.00 sec)

Green環境にデータが追加されたかを確認

ec2-user@ip-192-168-10-100 ~]$ mysql -h dev-aurora-bg-db-green-ajyguq.cluster-cedmf6a17cts.ap-northeast-1.rds.amazonaws.com -u rdsroot -pPassW0rd
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 372
Server version: 8.0.32 Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> USE test-db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

mysql> SELECT * FROM employees;
+-------------+---------------+
| employee_id | employee_name |
+-------------+---------------+
|           1 | hogehoge      |
|           2 | fugafuga      |
+-------------+---------------+
2 rows in set (0.00 sec)

mysql>
mysql> SELECT * FROM employees;
+-------------+---------------+
| employee_id | employee_name |
+-------------+---------------+
|           1 | hogehoge      |
|           2 | fugafuga      |
|           3 | morimori      |
+-------------+---------------+
3 rows in set (0.00 sec)

グリーン環境に切り替え

Blueグリーンを選択し、[Actions]-[Switch over]を選択

rds-bg-12.png

Switch over画面で、タイムアウト値を設定
タイムアウト値は30秒〜1時間まで設定可能。
タイムアウト値までに切り替えが完了しない場合、変更が全てロールバックされる

rds-bg-13.png

スイッチオーバー開始

rds-bg-14.png

グリーン環境のDB名がBlueに統一される

rds-bg-15.png

切り替えが完了
約2分で完了

rds-bg-16.png

切り替え後のグリーン環境を確認すると
エンドポイント名などのBlue環境のものが引きつがれる

rds-bg-17.png

Blue環境は切り替え後もそのまま残るので、不要になったら手動で削除する。
エンドポイント名に-oldがつく

rds-bg-18.png

CloudFormationパスワードが変更で切るか確認

rds-bg-19.png

hogehoge

Passwordも変更され始めた。
Blueグリーン後も、CLoudFormationは問題なく利用できる。

rds-bg-20.png

後片付け

Stackを削除しておく。

BlueグリーンとBlueグリーンデプロイは手動で削除しておく。

rds-bg-21.png

まとめ

終わり

参考リンク

Using Amazon RDS Blue/Green Deployments for database updates

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1