AWSマルチリージョンにおける高可用性方式(ルーティング切替編)

こんにちは、SCSKの茂木です。

前回ご紹介したAWSマルチリージョンにおける高可用性方式で記事に書ききれなかったルーティング切替の実装について、
今回は詳しく解説していきます。

 

背景と目的

前回の記事でも触れていましたが、マルチリージョンでの冗長化構成においては
障害や災害が発生した際のダウンタイム最小化が重要なポイントの1つとなります。

ダウンタイムを最小化するためにはHAクラスタソフトでデータやミドルウェアを冗長化しておくことと合わせて、
障害や災害の発生時にクライアント通信がセカンダリサーバに向くようルーティングを切り替える仕組みが必要となります。

実装方針

ダウンタイム最小化のためルーティング切替は自動で行えるようにします。
切替の流れは以下を想定しており、AWS CLIを使用したスクリプトでルーティングの切替を行います。

①障害または災害が発生しプライマリサーバがダウン
②ダウンを検知し、セカンダリサーバでスクリプトをキック
③スクリプト内でルートテーブルのエントリ差し替えを行い、仮想IPをセカンダリサーバまでルーティング
④セカンダリサーバでサービス継続

ルーティングの切替は稼働しているサービスのフェイルオーバーと合わせて行う必要があります。
非同期での切り替わりによって意図しない挙動になる可能性があります。

環境設定

設定項目 説明
AWS CLIインストール ドキュメントを参考にEC2全台にAWS CLIをインストールします。

IAMポリシーの作成と権限の割り当て ルートテーブルとTransitGateway ルートテーブルに関する許可ポリシーを作成し、EC2に権限を割り当てます。
今回は以下4つのアクションを許可しました。
“ec2:CreateRoute”
“ec2:DeleteRoute”
“ec2:CreateTransitGatewayRoute”
“ec2:DeleteTransitGatewayRoute”
AWS CLIの設定(任意) スクリプト実行ユーザに応じてaws configureを適宜設定ください。

 

ルーティング切替実装

スクリプト概要

今回はbashスクリプトを使用しました。
東京-大阪間の双方で切替が必要になるため実行したEC2のリージョンでルーティングの方向を決めています。
ルートテーブルやリージョンの数に応じて分岐や処理を書き換える必要があります。

以下の記事を参考にしています。

スクリプト詳細

#!/bin/bash

rm -rf ~/.aws/cli/cache

# EC2メタデータサービスからアベイラビリティゾーンを取得する
availability_zone=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)

# アベイラビリティゾーンからリージョンを抽出(末尾の文字を取り除く)
current_region=${availability_zone::-1}

echo "現在のAWSリージョン: $current_region"

# リージョンを確認して処理を切り替える
if [ "$current_region" == "ap-northeast-3" ]; then
    echo "大阪リージョンです。特定の処理を実行します。"

    # rtb-oya-public(tokyo)
    aws ec2 delete-route \
        --route-table-id rtb-0cf7a64e23924df35 \
        --destination-cidr-block 20.1.1.1/32 \
        --region ap-northeast-1

    aws ec2 create-route \
        --route-table-id rtb-0cf7a64e23924df35 \
        --destination-cidr-block 20.1.1.1/32 \
        --gateway-id tgw-0068638c0345e3bf5 \
        --region ap-northeast-1

    # rtb-oya-private(tokyo)
    aws ec2 delete-route \
        --route-table-id rtb-0b947251e47673818 \
        --destination-cidr-block 20.1.1.1/32 \
        --region ap-northeast-1

    # tgw-rtb-tokyo-oya(tokyo)
    aws ec2 delete-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-033d37d8d27c1a288 \
        --region ap-northeast-1

    aws ec2 create-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-033d37d8d27c1a288 \
        --transit-gateway-attachment-id tgw-attach-08df5bd6f7102ce5d \
        --region ap-northeast-1

    # tgw-rtb-tokyo-osaka-oya(tokyo)
    aws ec2 delete-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-0903b5f96664d99ad \
        --region ap-northeast-1

    # tgw-rtb-osaka-tokyo-oya(osaka)
    aws ec2 create-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-08f3755f4400837a5 \
        --transit-gateway-attachment-id tgw-attach-0734a75c43c8dedd9 \
        --region ap-northeast-3

    # tgw-rtb-osaka-oya(osaka)
    aws ec2 create-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-0a005c5db6c404746 \
        --transit-gateway-attachment-id tgw-attach-0734a75c43c8dedd9 \
        --region ap-northeast-3

    # rtb-oya-private(osaka)
    aws ec2 create-route \
        --route-table-id rtb-05b5fd18535d843ca \
        --destination-cidr-block 20.1.1.1/32 \
        --network-interface-id eni-082d40701b0d371ec \
        --region ap-northeast-3

else
    echo "東京リージョンです。通常の処理を実行します。"

    # rtb-oya-public(tokyo)
        aws ec2 delete-route \
            --route-table-id rtb-0cf7a64e23924df35 \
            --destination-cidr-block 20.1.1.1/32 \
            --region ap-northeast-1

    aws ec2 create-route \
        --route-table-id rtb-0cf7a64e23924df35 \
        --destination-cidr-block 20.1.1.1/32 \
        --network-interface-id eni-06102ba74656de21a \
        --region ap-northeast-1

    # rtb-oya-private(tokyo)
    aws ec2 create-route \
        --route-table-id rtb-0b947251e47673818 \
        --destination-cidr-block 20.1.1.1/32 \
        --network-interface-id eni-06102ba74656de21a \
        --region ap-northeast-1

    # tgw-rtb-tokyo-oya(tokyo)
    aws ec2 delete-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-033d37d8d27c1a288 \
        --region ap-northeast-1

    aws ec2 create-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-033d37d8d27c1a288 \
        --transit-gateway-attachment-id tgw-attach-0da655bb73febede7 \
        --region ap-northeast-1

    # tgw-rtb-tokyo-osaka-oya(tokyo)
    aws ec2 create-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-0903b5f96664d99ad \
        --transit-gateway-attachment-id tgw-attach-0da655bb73febede7 \
        --region ap-northeast-1

    # tgw-rtb-osaka-tokyo-oya(osaka)
    aws ec2 delete-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-08f3755f4400837a5 \
        --region ap-northeast-3

    # tgw-rtb-osaka-oya(osaka)
    aws ec2 delete-transit-gateway-route \
        --destination-cidr-block 20.1.1.1/32 \
        --transit-gateway-route-table-id tgw-rtb-0a005c5db6c404746 \
        --region ap-northeast-3

    # rtb-oya-private(osaka)
    aws ec2 delete-route \
        --route-table-id rtb-05b5fd18535d843ca \
        --destination-cidr-block 20.1.1.1/32 \
        --region ap-northeast-3

fi

echo "All routes have been created successfully."

補足(LifeKeeperへの組み込み)

LifeKeeperのGenericARKを利用することでプライマリサーバのダウン検知をトリガーにスクリプトをキックすることができます。

タイトルとURLをコピーしました