labunix's blog

labunixのラボUnix

Wheezyにheartbeat+pacemakerを導入する

■Wheezyにheartbeat+pacemakerを導入する
 ※ノードを監視するのみで、他には何もやらない。

$ sudo apt-get install -y heartbeat pacemaker
$ lv -s /usr/share/doc/heartbeat/ha.cf.gz | \
  sudo tee /etc/ha.d/ha.cf >/dev/null

■デフォルトではログファシリティとフォールバックがONになっているのみ。
 後でコメントを読もう。

$ grep -v "^#\|^\$" /etc/ha.d/ha.cf  
logfacility	local0
auto_failback on

■authkeysを設定する

$ apropos authkeys
authkeys (5)         - Authentication file for the Heartbeat cluster messagin...

■書式は以下。

$ man authkeys | grep -A 4 format
       The file must follow the following format:

           auth num
           num method secret
           num method secret

■番号は1から15まで指定出来るが、ファイル中でユニークでなければならない。

$ man authkeys | grep -A 1 "num is"
       num is a numerical identifier, between 1 and 15 inclusive. It must be
       unique within the file.

■現在のアクティブなメソッドとパスワードは1つのみ。

$ man authkeys | grep "auth num select"
       auth num selects the currently active authentication method and secret.

■md5/sha1/crcのいずれかを選択出来る。
 Windowsの壊れたZIP等でよくお目にかかるあのCRC巡回エラーのCRC。

 3つの特徴がある。
    パリティや単純な加算によるチェックサムに比べ検出精度が高い。
    高速である。
    1ビットずつの走査により計算ができ、実現するためのハードウェア回路が簡単。

 注意点
  CRC の数学的特性上、CRC値が変化しないように元のデータを改ざんすることが容易

$ man authkeys | grep -A 12 SIGNATURE
SUPPORTED SIGNATURE METHODS
       The following signature methods are supported in authkeys (listed here
       in alphabetical order):

       md5
           MD5 hash method. This method requires a shared secret.

       sha1
           SHA-1 hash method. This method requires a shared secret.

       crc
           Cyclic Redundancy Check hash method. This method does not require a
           shared secret and is insecure; it’s use is strongly discouraged.

■というわけで、numに「2」を、methodを「sha1」でパスワードを「heartbeat」とする。

$ echo -e "auth 1\n1 md5 helloworld\n2 sha1 heartbeat\n3 crc" | \
  sudo tee /etc/ha.d/authkeys
auth 1
1 md5 helloworld
2 sha1 heartbeat
3 crc

■authkeysは600の権限にする。
 忘れると、エラーで起動しない。

$ sudo chmod 600 /etc/ha.d/authkeys

heartbeat[7193]: 2013/07/20_22:55:27 ERROR: Bad permissions on keyfile [/etc/ha.d//authkeys], 600 recommended.
heartbeat[7193]: 2013/07/20_22:55:27 ERROR: Authentication configuration error.
heartbeat[7193]: 2013/07/20_22:55:27 ERROR: Configuration error, heartbeat not started.

■まずはログファイルを指定する。

$ grep -B 2 "#logfile" /etc/ha.d/ha.cf 
# 	File to write other messages to
#
logfile	/var/log/ha-log

$ sudo sed -i s/"#logfile"/"logfile"/ /etc/ha.d/ha.cf 

■監視間隔()の設定

$ grep -B 2 "#keepalive" /etc/ha.d/ha.cf 
#	keepalive: how long between heartbeats?
#
#keepalive 2

$ sudo sed -i s/"#keepalive"/"keepalive"/ /etc/ha.d/ha.cf

■マニュアルにあるとおり、IANAに新生は出ているが、「/etc/services」に記述は無い。

$ man ha.cf | grep -A 14 "udpport\$" 
       udpport
           The udpport directive specifies which port Heartbeat will use for
           its UDP intra-cluster communication. There are two common reasons
           for overriding this value: there are multiple bcast clusters on the
           same subnet, or this port is already in use in accordance with some
           locally-established policy.

           The default value for this parameter is the the port ha-cluster in
           /etc/services (if present), or 694 if port ha-cluster is not in
           /etc/services. 694 is the IANA registered port number for Heartbeat
           (a.k.a. ha-cluster).

           A sample udpport directive is shown below.

               udpport 694

$ URL="http://www.iana.org/assignments/service-names-port-numbers"; \
  FILE="service-names-port-numbers.txt"; \
  w3m -dump "${URL}/${FILE}" | grep " 694 " | sed s/"  *"/","/g
ha-cluster,694,tcp,ha-cluster,[Alan_Robertson],[Alan_Robertson]
ha-cluster,694,udp,ha-cluster,[Alan_Robertson],[Alan_Robertson]

$ grep 694 /etc/services | wc -l
0

$ grep "#udpport" /etc/ha.d/ha.cf 
#udpport	694
$ sudo sed -i s/"#udpport"/"udpport"/ /etc/ha.d/ha.cf 

■どのNICからUDPでブロードキャストするかを指定。

$ man ha.cf | grep -A 8 "The bcast " 
           The bcast directive is used to configure which interfaces Heartbeat
           sends UDP broadcast traffic on. More than one interface can be
           specified on the line. The udpport directive is used to configure
           which port is used for these broadcast communications if the
           udpport directive is specified before the bcast directive,
           otherwise the default port will be used. A couple of sample bcast
           lines are shown below.

               bcast eth0 eth1  # on Linux systems

$ grep "#bcast" /etc/ha.d/ha.cf 
#bcast	eth0		# Linux
#bcast	eth1 eth2	# Linux
#bcast	le0		# Solaris
#bcast	le1 le2		# Solaris

$ sudo sed s/"#\(bcast[ \t] *eth0[ \t]*#\)"/"\1"/ /etc/ha.d/ha.cf | grep ^bcast
bcast	eth0		# Linux

$ sudo sed -i s/"#\(bcast[ \t] *eth0[ \t]*#\)"/"\1"/ /etc/ha.d/ha.cf

■監視対象のノードを設定する。
 以下にあるように「uname -n」で求められるホスト名を記述する。

$ man ha.cf | grep -A 7 "^ *Node name"
           Node names in the directive must match the "uname -n" of that
           machine.

           You can declare multiple node names in one directive. You can also
           use the directive multiple times. Normally every node in the
           cluster must be listed in the ha.cf file, including the current
           node, unless the autojoin directive is enabled.

$ grep "#node" /etc/ha.d/ha.cf 
#node	ken3
#node	kathy

$ sudo sed -i s/"#node[ \t]ken3"/"node `uname -n`"/ /etc/ha.d/ha.cf

■同様に一行追加する。

$ grep ^node /etc/ha.d/ha.cf 
node xen-debian1
node xen-debian2

■pacemakerでモニタ出来るようにする。
 crmはpacemakerのエイリアス。

$ man ha.cf | grep -A 21 "crm\$"
       crm
           historical, for Cluster Resource Manager, now an alias to pacemaker

       pacemaker
           Enables the Pacemaker cluster manager. For historical reasons, the
           default for this option is off; however, it should always be set to
           respawn.

           When set to respawn, the directive automatically implies:

               apiauth stonithd        uid=root
               apiauth stonithd-ng     uid=root
               apiauth attrd           uid=hacluster
               apiauth crmd            uid=hacluster
               apiauth cib             uid=hacluster

               respawn hacluster       ccm
               respawn hacluster       cib
               respawn hacluster       attrd
               respawn root            stonithd
               respawn root            lrmd
               respawn hacluster       crmd

■respawnでプロセスが終了したら再起動する設定はひとまず置いておいて、
 「crm yes」を設定する。

$ echo -e "# for pacemaer\ncrm yes" | sudo tee -a  /etc/ha.d/ha.cf
# for pacemaer
crm yes

■一通り設定したら以下のようになっているはず。

$ 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-debian1
node xen-debian2
crm yes

■

$ sudo /etc/init.d/heartbeat restart
Stopping High-Availability services: Done.

Starting High-Availability services: Done.

■pacemakerの設定
 以下を参考に作成。

$ lv -s /usr/share/doc/pacemaker/crm_fencing.txt.gz | grep -A 10 "^A dummy"
A dummy stonith resource configuration, which may be used in some
testing scenarios is very simple:

	configure
	primitive st-null stonith:null \
		params hostlist="node1 node2"
	clone fencing st-null
	commit

.NB
**************************

$ echo '        configure
        primitive st-null stonith:null \
                params hostlist="xen-debian1 xen-debian2"
        clone fencing st-null
        commit
' | tee sample.txt >/dev/null

■設定と確認

$ sudo crm < sample.txt
$ 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"
clone fencing st-null
property $id="cib-bootstrap-options" \
	dc-version="1.1.7-ee0730e13d124c3d58f00016c3376a1de5323cff" \
	cluster-infrastructure="Heartbeat"

$ sudo crm_mon -1
============
Last updated: Sat Jul 20 23:15:07 2013
Last change: Sat Jul 20 23:02:52 2013 via cibadmin on xen-debian2
Stack: Heartbeat
Current DC: xen-debian2 (fbe03c12-747e-45d7-8a25-469bc9c28f2d) - 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-null]
     Started: [ xen-debian2 xen-debian1 ]


■xen-debian1の再起動を行うとOFFLINEになり、復帰すると上記の状態に戻りました。

============
Last updated: Sat Jul 20 23:21:28 2013
Last change: Sat Jul 20 23:02:52 2013 via cibadmin on xen-debian2
Stack: Heartbeat
Current DC: xen-debian2 (fbe03c12-747e-45d7-8a25-469bc9c28f2d) - partition with q
uorum
Version: 1.1.7-ee0730e13d124c3d58f00016c3376a1de5323cff
2 Nodes configured, unknown expected votes
2 Resources configured.
============

Online: [ xen-debian2 ]
OFFLINE: [ xen-debian1 ]

 Clone Set: fencing [st-null]
     Started: [ xen-debian2 ]
     Stopped: [ st-null:1 ]

Node Attributes:
* Node xen-debian2: