labunix's blog

labunixのラボUnix

DRBDとapache2、pacemaker+heartbeatを連携する。

■DRBDとapache2、pacemaker+heartbeatを連携する。

 apache2はkeepalived+IPVSで使用したものと同じ。
 つまりデフォルト。
 コンテンツのコピーは後でも良い。

 IPVSのみでWebサーバをラウンドロビン構成にする。
 http://labunix.hateblo.jp/entry/20130619/1371652385

 Wheezy/SqueezeでKeepalived+LVS(IPVS)構成(NAT)にする。
 http://d.hatena.ne.jp/labunix/20130624

■「/server-status」
 ステータスモジュールが読み込まれていて、
 シンボリックリンクで有効になっている。

$ echo "" | sudo a2enmod | sed s/" "/"\n"/g | grep status
status

$ env LANG=C ls -l /etc/apache2/mods-*/status.load | sed s/".*[0-9] "//g
/etc/apache2/mods-available/status.load
/etc/apache2/mods-enabled/status.load -> ../mods-available/status.load

$ ls -l /etc/apache2/mods-*/status.conf | sed s/".*[0-9] "//g
/etc/apache2/mods-available/status.conf
/etc/apache2/mods-enabled/status.conf -> ../mods-available/status.conf

■要は以下が出力されればOK。

$ w3m -dump -no-proxy http://localhost/server-status | tail -16
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

 Srv  Child Server number - generation
 PID  OS process ID
 Acc  Number of accesses this connection / this child / this slot
  M   Mode of operation
 CPU  CPU usage, number of seconds
 SS   Seconds since beginning of most recent request
 Req  Milliseconds required to process most recent request
Conn  Kilobytes transferred this connection
Child Megabytes transferred this child
Slot  Total megabytes transferred this slot

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Apache/2.2.22 (Debian) Server at localhost Port 80

■DRBDの設定は以下。

 EXT3+DRBDの導入
 http://labunix.hateblo.jp/entry/20130723/1374515808

$ echo "Primary" >/dev/null;sudo drbdadm role drbd0
Primary/Secondary

$ echo "Secondary" > /dev/null;sudo drbdadm role drbd0
Secondary/Primary

■pacemaker+heartbeatの設定は以下。
 大したことはしてないので、下記の文中を読めば設定出来る。

 Wheezyにheartbeat+pacemakerを導入する
 http://labunix.hateblo.jp/entry/20130720/1374330540

 STONITHプラグイン「external/ssh」でシステムを自動的に再起動してみる。
 http://labunix.hateblo.jp/entry/20130722/1374496401

$ sudo crm_mon -A -1
============
Last updated: Tue Jul 23 21:12:07 2013
Last change: Tue Jul 23 02:18:31 2013 via cibadmin on xen-debian2
Stack: Heartbeat
Current DC: xen-debian1 (e0322e2c-d119-4b46-9be9-68cf992ce4d7) - partition with quorum
Version: 1.1.7-ee0730e13d124c3d58f00016c3376a1de5323cff
2 Nodes configured, unknown expected votes
2 Resources configured.
============

Online: [ xen-debian2 xen-debian1 ]

 Clone Set: fencing [st-ssh]
     Started: [ xen-debian2 xen-debian1 ]

Node Attributes:
* Node xen-debian2:
* Node xen-debian1:

■不用意に再起動しないよう、「st-ssh」の削除、「st-null」に戻す。
 2台構成なので「no-quorum-policy="ignore"」を確認

$ sudo cibadmin -Q > tmp.xml
--- st-ssh 関連を削除 ---
$ sudo cibadmin --cib_replace --xml-file tmp.xml

$ cat check.txt 
	configure
	show

$ sudo crm < check.txt 
node $id="e0322e2c-d119-4b46-9be9-68cf992ce4d7" xen-debian1
node $id="fbe03c12-747e-45d7-8a25-469bc9c28f2d" xen-debian2
primitive st-null stonith:null \
	params hostlist="xen-debian1 xen-debian2"
property $id="cib-bootstrap-options" \
	dc-version="1.1.7-ee0730e13d124c3d58f00016c3376a1de5323cff" \
	cluster-infrastructure="Heartbeat" \
	no-quorum-policy="ignore"

$ sudo crm_mon -A -1 | tail -7
Online: [ xen-debian2 xen-debian1 ]

 st-null	(stonith:null):	Started xen-debian2

Node Attributes:
* Node xen-debian2:
* Node xen-debian1:

■stonith自体を明示的に動かさないなら以下を追加。
 ※nullなので、crm_monの表示は変わらない。

$ cat stonith_stop.txt
	configure
	property stonith-enabled="false"
	commit

$ sudo crm < stonith_stop.txt
$ sudo crm < check.txt | tail -1
	stonith-enabled="false"

■heartbeatの設定の確認

$ sudo grep -v "^#\|^\$" /etc/ha.d/ha.cf 
logfile	/var/log/ha-log
logfacility	local0
keepalive 2
udpport	694
bcast	eth0		# Linux
auto_failback on
node xen-debian2
node xen-debian1
crm yes

■bcastからucastに変更

$ sudo grep -v "^#\|^\$" /etc/ha.d/ha.cf
logfile	/var/log/ha-log
logfacility	local0
keepalive 2
udpport	694
ucast eth0 192.168.152.91
ucast eth0 192.168.152.92
auto_failback on
node xen-debian
node kvm-debian
crm yes

sudo /etc/init.d/heartbeat restart

■DRBDリソースの追加
 デバイスをMaster/Slaveに紐付けて設定
 タイムアウトの警告が出るが登録自体は可能。

$ cat drbd.txt 
	configure
	# フェイルオーバーの設定
	property default-resource-stickiness=200

	# DRBDデバイスの設定 res_drdb0
	primitive res_drbd0 ocf:linbit:drbd \
	params drbd_resource="drbd0" drbdconf="/etc/drbd.conf" \
	op monitor interval="20s"

	# DRDBマスターとスレーブの設定 ms_drbd0
	ms ms_drbd0 res_drbd0 \
	meta master-max="1" master-node-max="1" \
	clone-max="2" clone-node-max="1" notify="true"

	# ファイルシステムのマウントの設定 res_fs
	primitive res_fs ocf:heartbeat:Filesystem \
	params device="/dev/drbd0" fstype="ext3" directory="/media/drbd0" \
	op monitor interval="20s"

	# Webサーバの設定 res_apache2
	primitive res_apache2 ocf:heartbeat:apache \
	params configfile="/etc/apache2/apache2.conf" port="80" \
	op monitor interval="20s"

	# 仮想IPの設定 res_vip
	primitive res_vip ocf:heartbeat:IPaddr2 \
	params ip="192.168.152.90" cidr_netmask="24" nic="eth0" \
	op monitor interval="10s"

	# グループの設定 rgroup_web
	group rgroup_web res_vip res_fs res_apache2

	# 起動順序の設定
	colocation c_rgroup_web_on_drbd0 inf: rgroup_web ms_drbd0:Master
	order o_drbd_befor_rgroup_web inf: ms_drbd0:promote rgroup_web:start
	# 適用
	commit

$ sudo crm < drbd.txt 
WARNING: 37: res_drbd0: default timeout 20s for start is smaller than the advised 240
WARNING: 37: res_drbd0: default timeout 20s for stop is smaller than the advised 100
WARNING: 37: res_drbd0: action monitor not advertised in meta-data, it may not be supported by the RA
WARNING: 37: res_fs: default timeout 20s for start is smaller than the advised 60
WARNING: 37: res_fs: default timeout 20s for stop is smaller than the advised 60
WARNING: 37: res_fs: default timeout 20s for monitor is smaller than the advised 40
WARNING: 37: res_apache2: default timeout 20s for start is smaller than the advised 40s
WARNING: 37: res_apache2: default timeout 20s for stop is smaller than the advised 60s

$ sudo crm status | tail -10
Online: [ kvm-debian2 xen-debian1 ]

 st-null	(stonith:null):	Started xen-debian1
 Master/Slave Set: ms_drbd0 [res_drbd0]
     Masters: [ kvm-debian2 ]
     Slaves: [ xen-debian1 ]
 Resource Group: rgroup_web
     res_vip	(ocf::heartbeat:IPaddr2):	Started kvm-debian2
     res_fs	(ocf::heartbeat:Filesystem):	Started kvm-debian2
     res_apache2	(ocf::heartbeat:apache):	Started kvm-debian2

■コンテンツのコピー
 上記の設定でプライマリ側にマウントされているはずなので、
 コピーしてシンボリックリンクを作成。
 DRBDが落ちた場合でもトップページは見える。

$ sudo drbdadm role drbd0 
Primary/Secondary
$ mount | grep drbd
/dev/drbd0 on /media/drbd0 type ext3 (rw,relatime,errors=continue,user_xattr,acl,barrier=1,data=ordered)

$ sudo cp -pr /var/www/ /media/drbd0/www
$ sudo ln -s /media/drbd0/ /var/www/drdb

■以下のようなテストページでも作って、閲覧出来ればOK。

$ echo "<html><head><title>Hello drbd0</title></head><body>Hello drbd0</body></html>" | \
  sudo tee /media/drbd0/drdb.html >/dev/null

$ w3m -dump -no-proxy http://localhost/drbd/drbd.html
Hello drbd0

■セカンダリ側ではシンボリックリンクを張るだけ。

$ sudo ln -s /media/drbd0/ /var/www/drdb

■マイグレーションしてみる。

$ sudo crm_mon -A -1 | tail -5
Node Attributes:
* Node xen-debian2:
    + master-res_drbd0:1              	: 10000     
* Node xen-debian1:
    + master-res_drbd0:0              	: 10000     

$ sudo crm_mon -A -1 | grep "Masters\|Slaves"
     Masters: [ xen-debian1 ]
     Slaves: [ xen-debian2 ]


$ cat migrate.txt 
	resource
	migrate rgroup_web

■以下のようにストップする。

Online: [ kvm-debian xen-debian ]

     Masters: [ xen-debian1 ]
     Slaves: [ xen-debian2 ]
 Resource Group: rgroup_web
     res_vip	(ocf::heartbeat:IPaddr2):	Started xen-debian1
     res_fs	(ocf::heartbeat:Filesystem):	Stopped 
     res_apache2	(ocf::heartbeat:apache):	Stopped 

■切り替わった

 Master/Slave Set: ms_drbd0 [res_drbd0]
     Masters: [ xen-debian2 ]
     Slaves: [ xen-debian1 ]
 Resource Group: rgroup_web
     res_vip    (ocf::heartbeat:IPaddr2):       Started xen-debian2
     res_fs     (ocf::heartbeat:Filesystem):    Started xen-debian2
     res_apache2        (ocf::heartbeat:apache):        Started xen-debian2

■migrateコマンドはメンテナンス用のコマンドであるため、
 そのまま放置するとサービスが切り替わらなくなってしまうらしい。
 ということでアンマイグレート。

$ cat unmigrate.txt 
	resource
	unmigrate rgroup_web

$ sudo < unmigrate.txt

■やり直すには、resourceに移動、stop <res>で停止してから削除する。

■既存の設定のバックアップ
 XMLとテキストのバックアップを取っておく。
 verifyを追加したので、crmで当てた時と同じWARNINGが出ることが分かる。

$ cat check.txt 
	configure
	verify
	show

$ sudo cibadmin -Q > backup_`date '+%Y%m%d'`.xml

$ sudo crm < check.txt > backup_`date '+%Y%m%d'`.txt 2>> err.log
$ cat err.log 
WARNING: 2: res_drbd0: default timeout 20s for start is smaller than the advised 240
WARNING: 2: res_drbd0: default timeout 20s for stop is smaller than the advised 100
WARNING: 2: res_drbd0: action monitor not advertised in meta-data, it may not be supported by the RA
WARNING: 2: res_fs: default timeout 20s for start is smaller than the advised 60
WARNING: 2: res_fs: default timeout 20s for stop is smaller than the advised 60
WARNING: 2: res_fs: default timeout 20s for monitor is smaller than the advised 40
WARNING: 2: res_apache2: default timeout 20s for start is smaller than the advised 40s
WARNING: 2: res_apache2: default timeout 20s for stop is smaller than the advised 60s

■仮想IPにアクセスすると、どっちがマスターか気にしなくても、正常にコンテンツを返す。
 ※セカンダリは当然コンテンツが無いので失敗する。

$ w3m -dump -no-proxy http://192.168.152.90/drdb/drdb.html
Hello drbd0
$ w3m -dump -no-proxy http://192.168.152.91/drdb/drdb.html
w3m: Can't load http://192.168.152.91/drdb/drdb.html.
$ w3m -dump -no-proxy http://192.168.152.92/drdb/drdb.html
Hello drbd0