下記のように、EC2(VPC)のIPの付け替え(Floating IPパターン)を試してきましたが、今回は、Corosync & Pacemakerで
自動的に付け替える(フェイルオーバ)部分を紹介します。

High Availability NATの作成(CentOS6)の記事の「corosyncとpacemakerの導入と設定」までの手順は
同様となります。

そして、フェイルオーバに使うスクリプトは下記となります。

# cat /etc/init.d/associate-private-ip 
#!/bin/sh
#
# chkconfig: 2345 99 10
# description: Associate EIP

# Source Function Library
. /etc/init.d/functions

# System Variable
prog=${0##*/}
lock=/var/lock/subsys/$prog

# User Variavle
PRIVATE_IP=0.0.0.0

# Source Config
if [ -f /etc/sysconfig/$prog ] ; then
. /etc/sysconfig/$prog
fi

#
case "$1" in
start)
touch $lock
AZ=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
REGION=`echo $AZ | cut -c 1-$((${#AZ} - 1))`
MAC=`curl -s http://169.254.169.254/latest/meta-data/mac`
INTERFACE_ID=`curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/$MAC/interface-id`
aws --region $REGION ec2 assign-private-ip-addresses
--network-interface-id $INTERFACE_ID
--private-ip-addresses $PRIVATE_IP
--allow-reassignment
| logger -s -i -t $prog
exit ${PIPESTATUS[0]}
;;
stop)
rm -f $lock
exit 0
;;
status)
if [ -f $lock ] ; then
exit 0
else
exit 3
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac

可変情報は、次のファイルに設定します。

# cat /etc/sysconfig/associate-private-ip
PRIVATE_IP=10.10.8.200

Pacemakerへの登録は下記の通りです。

crm configure property no-quorum-policy="ignore" stonith-enabled="false"
crm configure rsc_defaults resource-stickiness="INFINITY" migration-threshold="1"
crm configure primitive httpd lsb:httpd op monitor interval="5s"
crm configure primitive associate-private-ip lsb:associate-private-ip
crm configure group ha-web httpd associate-private-ip

※Apacheに障害が発生したらフェイルオーバー(IPの切り替え)を行うようにしています。

以上で、下記のような状態になっていると思います。

# crm_mon
============
Last updated: Wed Jan 30 20:46:08 2013
Last change: Wed Jan 30 20:46:01 2013 via cibadmin on ip-10-10-8-114
Stack: openais
Current DC: ip-10-10-8-122 - partition with quorum
Version: 1.1.7-6.el6-148fccfd5985c5590cc601123c6c16e966b85d14
2 Nodes configured, 2 expected votes
2 Resources configured.
============

Online: [ ip-10-10-8-122 ip-10-10-8-114 ]

Resource Group: ha-web
httpd (lsb:httpd): Started ip-10-10-8-114
associate-private-ip (lsb:associate-private-ip): Started ip-10-10-8-114

アクティブなEC2の方でApacheを停止します。

# service httpd stop
httpd を停止中: [ OK ]

そうすると、下記のようにフェイルオーバーされ、IPアドレスが切り替わります。

============
Last updated: Wed Jan 30 20:48:08 2013
Last change: Wed Jan 30 20:46:01 2013 via cibadmin on ip-10-10-8-114
Stack: openais
Current DC: ip-10-10-8-122 - partition with quorum
Version: 1.1.7-6.el6-148fccfd5985c5590cc601123c6c16e966b85d14
2 Nodes configured, 2 expected votes
2 Resources configured.
============

Online: [ ip-10-10-8-122 ip-10-10-8-114 ]

Resource Group: ha-web
httpd (lsb:httpd): Started ip-10-10-8-122
associate-private-ip (lsb:associate-private-ip): Started ip-10-10-8-122

Failed actions:
httpd_monitor_5000 (node=ip-10-10-8-114, call=4, rc=7, status=complete): not running

※AWSコンソールで実際のIPアドレスの切り替わりは確認できます。

尚、二つ目のIPアドレスをCentOS6で利用するためには、ENIにSecondary IPを付与してCentOS(6)で利用
記事で紹介したような手順が実際には必要になります。

実際にApacheのフェイルオーバー時の停止時間を確認したところ、下記のように約10秒程度でした。

connected to 54.249.50.18:80 (198 bytes), seq=272 time=2.40 ms
connected to 54.249.50.18:80 (198 bytes), seq=273 time=4.78 ms
connected to 54.249.50.18:80 (198 bytes), seq=274 time=2.65 ms
could not connect (Connection refused)
could not connect (Connection refused)
could not connect (Connection refused)
could not connect (Connection refused)
could not connect (Connection refused)
could not connect (Connection refused)
connected to 54.249.50.18:80 (198 bytes), seq=281 time=8201.49 ms
connected to 54.249.50.18:80 (198 bytes), seq=282 time=3.06 ms
connected to 54.249.50.18:80 (198 bytes), seq=283 time=2.86 ms

こちらの記事はなかの人(suz-lab)監修のもと掲載しています。
元記事は、こちら