8.3. AWS設定の例

このチュートリアルでは、AWS上で"Watchdog"を使う簡単な例を示します。 この例では、高可用性のためにElastic IP Addressを仮想IPとして使います。

注意: watchdogは、Pgpool-IIのすべてのモード、すなわちレプリケーションモード、マスタ/スレーブモード、ローモードのいずれでも使えます。

8.3.1. AWSのセットアップ

この例では、2ノードのPgpool-II watchdogクラスタを使います。 そこで、2つのAmazon Linux EC2インスタンスを設定し、ひとつのElastic IPアドレスを使用します。 以下のステップを実施してください。

8.3.2. Pgpool-IIの設定

この例の設定は項8.2とほとんど同じになりますが、delegate_IPを設定せず、代わりにwd_escalation_commandwd_de_escalation_commandを使ってleader/Active Pgpool-IIノードのElastic IPアドレスを切り替えるのが異なります。

use_watchdog = on
delegate_IP = ''
...
wd_escalation_command = '$path_to_script/aws-escalation.sh'
wd_de_escalation_command = '$path_to_script/aws-de-escalation.sh'
  

8.3.3. エスカレーションおよびディエスカレーション用のスクリプト

aws-escalation.shとaws-de-escalation.shスクリプトを2つのインスタンス上に作成し、wd_escalation_commandwd_de_escalation_commandがそれぞれそれらを指すようにしてください。

注意: AWSインスタンス上でwd-escalation.shとwd-de-escalation.shで使用するコマンドが実行できるようにするために、AWS CLIの設定が必要になるかもしれません。 configure AWS CLIを参照してください。

8.3.3.1. エスカレーションスクリプト

このスクリプトは、watchdogがactive/leaderノードになったときに、Elastic IPをアサインするためにwatchdogが実行します。

aws-escalation.sh:

     #! /bin/sh

     ELASTIC_IP=35.163.178.3
     # replace it with the Elastic IP address you
     # allocated from the aws console
     INSTANCE_ID=i-0a9b64e449b17ed4b
     # replace it with the instance id of the Instance
     # this script is installed on

     echo "Assigning Elastic IP $ELASTIC_IP to the instance $INSTANCE_ID"
     # bring up the Elastic IP
     aws ec2 associate-address --instance-id $INSTANCE_ID --public-ip $ELASTIC_IP

     exit 0
    

8.3.3.2. ディエスカレーションスクリプト

このスクリプトは、watchdogがactive/leaderノードを退任するときに、Elastic IPのアサインを解除するためにwatchdogが実行します。

aws-de-escalation.sh:

     #! /bin/sh

     ELASTIC_IP=35.163.178.3
     # replace it with the Elastic IP address you
     # allocated from the aws console

     echo "disassociating the Elastic IP $ELASTIC_IP from the instance"
     # bring down the Elastic IP
     aws ec2 disassociate-address --public-ip $ELASTIC_IP
     exit 0
    

AWSコマンドリファレンス

"Configure AWS CLI", AWS Documentation: Configuring the AWS Command Line Interface.

"associate-address", AWS Documentation: associate-address reference.

"disassociate-address", AWS Documentation: disassociate-address reference.

8.3.4. 実行してみる

それぞれのサーバ上でPgpool-IIを"-n"スイッチ付きで起動し、pgpool.logにログメッセージをリダイレクトします。 leader/active Pgpool-IIノードは、Elastic IPのアサインメッセージを表示します。

    LOG:  I am the cluster leader node. Starting escalation process
    LOG:  escalation process started with PID:23543
    LOG:  watchdog: escalation started
         Assigning Elastic IP 35.163.178.3 to the instance i-0a9b64e449b17ed4b
     {
     "AssociationId": "eipassoc-39853c42"
     }
    
    LOG:  watchdog escalation successful
    LOG:  watchdog escalation process with pid: 23543 exit with SUCCESS.
   

Elastic IPアドレスにpingが通ることを確かめます。

    [user@someserver]$ ping 35.163.178.3
    PING 35.163.178.3 (35.163.178.3) 56(84) bytes of data.
    64 bytes from 35.163.178.3: icmp_seq=1 ttl=64 time=0.328 ms
    64 bytes from 35.163.178.3: icmp_seq=2 ttl=64 time=0.264 ms
    64 bytes from 35.163.178.3: icmp_seq=3 ttl=64 time=0.412 ms
   

"psql -h ELASTIC_IP -p port"でPostgreSQLに接続してみます。

    [user@someserver]$ psql -h 35.163.178.3 -p 9999 -l
   

8.3.5. Elastic IPの切換

アクティブなサーバが使用できなくなった時にスタンバイサーバがElastic IPを獲得できることを確認するために、アクティブサーバ上のPgpool-IIを停止します。 すると、スタンバイサーバはElastic IPアドレスを使い始めるはずです。 Pgpool-IIのログには以下のようなメッセージが表示されます。

         LOG:  remote node "172.31.2.94:9999 [Linux ip-172-31-2-94]" is shutting down
     LOG:  watchdog cluster has lost the coordinator node
    
    LOG:  watchdog node state changed from [STANDBY] to [JOINING]
    LOG:  watchdog node state changed from [JOINING] to [INITIALIZING]
    LOG:  I am the only alive node in the watchdog cluster
    HINT:  skipping stand for coordinator state
    LOG:  watchdog node state changed from [INITIALIZING] to [LEADER]
    LOG:  I am announcing my self as leader/coordinator watchdog node
    LOG:  I am the cluster leader node
    DETAIL:  our declare coordinator message is accepted by all nodes
    LOG:  I am the cluster leader node. Starting escalation process
    LOG:  escalation process started with PID:23543
    LOG:  watchdog: escalation started
         Assigning Elastic IP 35.163.178.3 to the instance i-0dd3e60734a6ebe14
     {
     "AssociationId": "eipassoc-39853c42"
     }
    
    LOG:  watchdog escalation successful
    LOG:  watchdog escalation process with pid: 61581 exit with SUCCESS.
   

Elastic IPアドレスにpingが通ることを再度確かめます。

    [user@someserver]$ ping 35.163.178.3
    PING 35.163.178.3 (35.163.178.3) 56(84) bytes of data.
    64 bytes from 35.163.178.3: icmp_seq=1 ttl=64 time=0.328 ms
    64 bytes from 35.163.178.3: icmp_seq=2 ttl=64 time=0.264 ms
    64 bytes from 35.163.178.3: icmp_seq=3 ttl=64 time=0.412 ms
   

"psql -h ELASTIC_IP -p port"でPostgreSQLに接続してみます。

    [user@someserver]$ psql -h 35.163.178.3 -p 9999 -l