LoginSignup
18
2

AWS Aurora PostgreSQLのメジャーバージョンアップ

Last updated at Posted at 2023-11-30

はじめに

メタップスアドベントカレンダー第一日目の記事です。

Aurora PostgreSQL11系がそろそろEOLを迎えるため、14系へメジャーバージョンアップを実施
https://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/AuroraUserGuide/Aurora.VersionPolicy.html?utm_source=pocket_mylist#Aurora.VersionPolicy.MajorVersionLifetime

目次

  • 事前準備
  • メンテナンス化
  • バージョンアップ
  • Anylyze実行
  • メンテナンス解除

事前準備

パラメータグループ作成

  • Postgre14系のパラメーターグループ作成(terraform実行)
aws_rds_cluster_parameter_group.tf
# aws_rds_cluster_parameter_group.foo-aurora-postgresql14:
resource "aws_rds_cluster_parameter_group" "foo-aurora-postgresql14" {
    description = "foo-aurora-postgresql14"
    family      = "aurora-postgresql14"
    name        = "foo-aurora-postgresql14"
    tags        = {}
    parameter {
        apply_method = "immediate"
        name         = "log_min_duration_statement"
        value        = "-1"
    }
    parameter {
        apply_method = "immediate"
        name         = "log_statement"
        value        = "none"
    }
}
aws_db_parameter_group.tf
# aws_db_parameter_group.foo-aurora-postgresql14:
resource "aws_db_parameter_group" "foo-aurora-postgresql14" {
    description = "foo-aurora-postgresql14"
    family      = "aurora-postgresql14"
    name        = "foo-aurora-postgresql14"
    tags        = {}
}

ダウンタイム確認用スクリプト作成

  • どの程度DB接続がダウンするか確認するため、ダウンタイム確認用のスクリプト作成
psqlping.sh
#!/bin/bash

if [ -z "${1}" ]; then
    echo "Specify hostname."
    exit
fi

pg_isready -h $1 > /dev/null 2>&1
if [ "${?}" = "0" ]; then
    echo -n "[pg_isready] OK"
fi

psql -c "SELECT 1" -h $1 -U foo > /dev/null 2>&1
if [ "${?}" = "0" ]; then
    echo  " [psql query execute] OK"
fi
  • スクリプトファイルへ実行権限付与
コマンド
chmod +x psqlping.sh

メンテナンス化

  • メンテ用の503リスナールールを上位に移動(CLI実行)
コマンド
ALB_LISTENER_RULE_ARN=arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxxxxxxxx
aws elbv2 set-rule-priorities --rule-priorities RuleArn=${ALB_LISTENER_RULE_ARN},Priority=1 --no-cli-pager

バージョンアップ

スナップショット取得

コマンド
CLUSTER_NAME_LIST=$(cat << EOS
foo-aurora-postgresql-cluster
bar-aurora-postgresql-cluster
baz-aurora-postgresql-cluster
EOS
)

for CLUSTER_NAME in ${CLUSTER_NAME_LIST}
do
aws rds create-db-cluster-snapshot --db-cluster-identifier ${CLUSTER_NAME} --db-cluster-snapshot-identifier ${CLUSTER_NAME}-20231113 --no-cli-pager
done

ダウンタイム確認スクリプト実行

コマンド
while true; do echo -n "$(date) "; ./psqlping.sh foo.ap-northeast-1.rds.amazonaws.com; sleep 0.5; done

バージョンアップ

今回は14.9を選択
image.png

事前作成したパラメータグループを指定
image.png

ダウンタイム結果

約12分 環境にもよる想定

結果
2023年  9月 22日 金曜日 02:44:49 UTC [pg_isready] OK [psql query execute] OK
2023年  9月 22日 金曜日 02:44:50 UTC 2023年  9月 22日 金曜日 02:44:50 UTC 2023年  9月 22日 金曜日 02:44:51 UTC 2023年  9月 22日
2023年  9月 22日 金曜日 02:56:47 UTC [pg_isready] OK [psql query execute] OK

Anylyze実行

DB接続

コマンド
psql -h foo.ap-northeast-1.rds.amazonaws.com -U foo
コマンド
\c foo_db

Analyze

コマンド、結果
select current_timestamp;ANALYZE VERBOSE;select current_timestamp;

       current_timestamp       
 2023-09-22 13:03:21.496542+09
(1 )
 2023-09-22 13:13:12.62939+09
(1 )

メンテナンス解除

  • メンテ用の503リスナールールを下位に移動(CLI実行)
コマンド
ALB_LISTENER_RULE_ARN=arn:aws:elasticloadbalancing:ap-northeast-1:xxxxxxxxxxxxxxxxxxxx
aws elbv2 set-rule-priorities --rule-priorities RuleArn=${ALB_LISTENER_RULE_ARN},Priority=1000 --no-cli-pager
18
2
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
18
2