主:hh.huangmingming.cn 192.168.1.31
從:yo.huangmingming.cn 192.168.1.250
主和從hosts檔案配置:
192.168.1.31 hh hh.huangmingming.cn
192.168.1.250 yo yo.huangmingming.cn
一、安裝epel擴充源
[root@yo ~]# wget http://mirrors.sohu.com/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
[root@hh ~]# yum list |grep heartbeat
heartbeat.x86_64 3.0.4-2.el6 epel
heartbeat-devel.i686 3.0.4-2.el6 epel
heartbeat-devel.x86_64 3.0.4-2.el6 epel
heartbeat-libs.i686 3.0.4-2.el6 epel
heartbeat-libs.x86_64 3.0.4-2.el6 epel
二、安裝heartbeat、libnet(主從都需要安裝)
[root@hh ~]# yum -y install heartbeat
[root@hh ~]# yum -y install libnet
[root@hh ~]# yum -y install nginx (安裝nginx做測試)
三、配置heartbeat(在主上配置)
1、拷貝配置檔案authkeys、ha.cf、haresources到目錄/etc/ha.d/下
[root@hh ~]# cd /usr/share/doc/heartbeat-3.0.4/
[root@hh heartbeat-3.0.4]# ls
apphbd.cf authkeys AUTHORS ChangeLog COPYING COPYING.LGPL ha.cf haresources README
[root@hh heartbeat-3.0.4]# cp authkeys ha.cf haresources /etc/ha.d/
2、配置authkeys檔案
[root@hh ~]# cd /etc/ha.d/
[root@hh ha.d]# vim authkeys
auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!
[root@hh ha.d]# chmod 600 authkeys (authkeys檔案的權限要注意改成600)
3、配置haresources檔案
[root@hh ha.d]# vim haresources
hh.huangmingming.cn 192.168.1.13/24/eth0:0 nginx (指定叢集IP)
4、配置ha.cf檔案
[root@hh ha.d]# vim ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 60
udpport 694
ucast eth0 192.168.1.250
auto_failback on
node hh.huangmingming.cn
node yo.huangmingming.cn
ping 192.168.1.1
respawn hacluster /usr/lib64/heartbeat/ipfail
5、把這三個檔案發送到yo(從機器上)
[root@hh ha.d]# scp authkeys haresources ha.cf yo:/etc/ha.d/
6、啟動heartbeat服務,先主後從
[root@hh ha.d]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO: Resource is stopped
Done.
四、啟動産生的錯誤
1、<--respawn hacluster /usr/lib64/heartbeat/ipfail (注意目前系統是64位還是32位的否則會産生以下錯誤)--/>
heartbeat: udpport setting must precede media statementsheartbeat[4227]: 2016/01/01_05:04:26 ERROR: Client child command [/usr/lib/heartbeat/ipfail] is not executable
heartbeat[4227]: 2016/01/01_05:04:26 ERROR: Heartbeat not started: configuration error.
heartbeat[4227]: 2016/01/01_05:04:26 ERROR: Configuration error, heartbeat not started.
2、<--以下兩個錯誤都是因為主機名配置不對造成的,在heartbeat配置中指定主機名的地方要與目前主機名對應,否則啟不來--/>
heartbeat: udpport setting must precede media statementsheartbeat: baudrate setting must precede media statementsheartbeat[28195]: 2015/10/29_21:31:14 info: Pacemaker support: false
heartbeat[28195]: 2015/10/29_21:31:14 ERROR: Current node [hh.huangmingming.com] not in configuration!
heartbeat[28195]: 2015/10/29_21:31:14 info: By default, cluster nodes are named by `uname -n` and must be declared with a 'node' directive in the ha.cf file.
heartbeat[28195]: 2015/10/29_21:31:14 info: See also: http://linux-ha.org/wiki/Ha.cf#node_directive
heartbeat[28195]: 2015/10/29_21:31:14 WARN: Logging daemon is disabled --enabling logging daemon is recommended
heartbeat[28195]: 2015/10/29_21:31:14 ERROR: Configuration error, heartbeat not started.
Heartbeat failure [rc=6]. Failed.
heartbeat: udpport setting must precede media statementsheartbeat: baudrate setting must precede media statementsheartbeat[30724]: 2015/10/29_21:56:29 info: Pacemaker support: false
heartbeat[30724]: 2015/10/29_21:56:29 WARN: Logging daemon is disabled --enabling logging daemon is recommended
heartbeat[30724]: 2015/10/29_21:56:29 info: **************************
heartbeat[30724]: 2015/10/29_21:56:29 info: Configuration validated. Starting heartbeat 3.0.4
heartbeat[30724]: 2015/10/29_21:56:29 ERROR: Bad nodename in /etc/ha.d//haresources [hh]
heartbeat[30724]: 2015/10/29_21:56:29 ERROR: Configuration error, heartbeat not started.
五、故障模拟測試,如主down掉,看會不會切換到從繼續提供服務
1、正常情況下
[root@hh ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:97:EE:BF
inet addr:192.168.1.31 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe97:eebf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:37359 errors:0 dropped:0 overruns:0 frame:0
TX packets:22139 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:36402808 (34.7 MiB) TX bytes:2377053 (2.2 MiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:97:EE:BF
inet addr:192.168.1.13 Bcast:192.168.1.255 Mask:255.255.255.0
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:97:EE:BF
inet addr:192.168.1.144 Bcast:192.168.1.255 Mask:255.255.255.0
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:28 errors:0 dropped:0 overruns:0 frame:0
TX packets:28 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2727 (2.6 KiB) TX bytes:2727 (2.6 KiB)
[root@hh ha.d]# ps aux |grep nginx (主)
root 31367 0.0 0.1 96488 1728 ? Ss 21:58 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 31368 0.0 0.2 96876 2500 ? S 21:58 0:00 nginx: worker process
root 31643 0.0 0.0 103252 824 pts/0 S+ 22:16 0:00 grep nginx
[root@yo ha.d]# netstat -tnlp |grep nginx (從)
[root@yo ha.d]# ps aux |grep nginx
root 5217 0.0 0.0 103256 828 pts/0 S+ 06:11 0:00 grep nginx
2、建立測試頁面,并在正常情況下測試通路,此時由主提供服務
[root@hh ha.d]# echo "AAAAAAAAAAAAAAAAhh192.168.1.31" >/usr/share/nginx/html/index.html
[root@yo ha.d]# echo "AAAAAAAAAAAAAAAAyo192.168.1.250" >/usr/share/nginx/html/index.html
3、主服務節點down掉之後(測試)
[root@hh ~]# iptables -A INPUT -p icmp -j DROP
[root@hh ~]# tail /var/log/ha-log (檢視日志資訊)
ResourceManager(default)[32095]:2015/10/29_22:24:20 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.13/24/eth0:0 start
IPaddr(IPaddr_192.168.1.13)[32256]:2015/10/29_22:24:20 INFO: Adding inet address 192.168.1.13/24 with broadcast address 192.168.1.255 to device eth0 (with label eth0:0)
IPaddr(IPaddr_192.168.1.13)[32256]:2015/10/29_22:24:20 INFO: Bringing device eth0 up
IPaddr(IPaddr_192.168.1.13)[32256]:2015/10/29_22:24:20 INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 -p /var/run/resource-agents/send_arp-192.168.1.13 eth0 192.168.1.13 auto not_used not_used
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.13)[32230]:2015/10/29_22:24:20 INFO: Success
ResourceManager(default)[32095]:2015/10/29_22:24:20 info: Running /etc/init.d/nginx start
Oct 29 22:24:20 hh.huangmingming.cn ipfail: [30881]: info: NS: We are still alive!
Oct 29 22:24:20 hh.huangmingming.cn ipfail: [30881]: info: Link Status update: Link yo.huangmingming.cn/eth0 now has status dead
Oct 29 22:24:22 hh.huangmingming.cn ipfail: [30881]: info: Asking other side for ping node count.
Oct 29 22:24:22 hh.huangmingming.cn ipfail: [30881]: info: Checking remote count of ping nodes.
4、在從(yo)上檢視有沒有自動啟動nginx,并在用戶端通路,此時由從提供服務
root 5534 0.0 0.1 96496 1972 ? Ss 06:20 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 5535 0.0 0.2 96884 2960 ? S 06:20 0:00 nginx: worker process
root 5546 0.0 0.0 103256 828 pts/0 S+ 06:24 0:00 grep nginx
[root@yo ha.d]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:8B:40:4A
inet addr:192.168.1.250 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe8b:404a/64 Scope:Link
RX packets:36106 errors:0 dropped:0 overruns:0 frame:0
TX packets:21435 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:39751462 (37.9 MiB) TX bytes:2077943 (1.9 MiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:8B:40:4A
RX packets:31 errors:0 dropped:0 overruns:0 frame:0
TX packets:31 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:3005 (2.9 KiB) TX bytes:3005 (2.9 KiB)
删除防火牆再測試
[root@hh ~]# iptables -nvL