搭建heartbeat高可用

1:準備階段
兩臺服務器一主一從
我這裏採用了yum的方式進行安裝,如想使用rpm安裝,請自行下載rpm包
安裝epel拓展源
國外的速度是在是太慢,我這裏選擇了阿里的拓展源,PS  感謝馬雲。

[root@tudou ~]# rm -rf /etc/yum.repos.d/* 
[root@tudou ~]# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-6.repo

關閉防火牆

[root@tudou ~]# iptables -F

2:安裝heartbeat 以及依賴

[root@tudou ~]# yum install -y heartbeat
[root@tudou ~]# yum install -y libnet

3:拷貝樣例文件並配置

heartbeat 給我們提供了一些樣例配置,在/usr/share/doc/heartbeat

[root@tudou ~]# cd /usr/share/doc/heartbeat-3.0.4/
[root@tudou heartbeat-3.0.4]# ls
apphbd.cf  AUTHORS    COPYING       ha.cf        README
authkeys   ChangeLog  COPYING.LGPL  haresources
[[email protected]]# cp authkeys ha.cf haresources /etc/ha.d/

編輯認證文件(authkeys)

auth 3
#1 crc
#2 sha1 HI!
3 md5 Hello!

  authkeys文件用於設定Heartbeat的認證方式,共有3種可用的認證方 式,即crc、md5和sha1。3種認證方式的安全性依次提高,但是佔用的系統資源也依次增加。如果Heartbeat集羣運行在安全的網絡上,可以使 用crc方式;如果HA每個節點的硬件配置很高,建議使用sha1,這種認證方式安全級別最高;如果是處於網絡安全和系統資源之間,可以使用md5認證方 式。這裏我們使用md5認證方式,設置如下:
PS:確保該文件權限爲600

-rw-------. 1 root root  643 Aug  7 14:10 authkeys


配置心跳的監控(haresources)

node1 192.168.1.122/24/eth0:0 nginx

指定 node1調用nginx服務,系統附加一個虛擬IP 192.168.1.122給eth0:0

這裏如果node1宕機後,node2可以自動啓動nginx服務,並新分配IP 192.168.1.122給node2的eth0:0

配置主配置文件(ha.conf)

logfile /var/log/ha_log/ha-log.log   ## ha的日誌文件記錄位置。如沒有該目錄,則需
要手動添加
ucast eth0 192.168.1.107    ##探測對方ip
keepalive 2    ##設定心跳(監測)時間時間爲2秒
warntime 10    
deadtime 30    
initdead 120
hopfudge 1
udpport 694    ##使用udp端口694 進行心跳監測
auto_failback on ##當主啓動時備切換
node node1  ##節點
node node2  ##節點2
ping 192.168.1.1   ##通過ping 網關來監測心跳是否正常。 
respawn hacluster /usr/lib64/heartbeat/ipfail ##自動拉起進程
apiauth ipfail gid=root uid=root
debugfile /Datas/logs/ha_log/ha-debug.log

拷貝配置文件到備機

[[email protected]]# scp ha.cf haresources authkeys [email protected]:/etc/ha.d/
[email protected]'s password: 
ha.cf                                         100%  682     0.7KB/s   00:00    
haresources                                   100% 5888     5.8KB/s   00:00    
authkeys                                      100%  643     0.6KB/s   00:00    
[[email protected]]#

啓動hearbeat /etc/init.d/heartbeat start
查看日誌 tail -f /var/log/ha_log/ha-log.log

heartbeat 第一次啓動非常慢,不要着急,耐心等耐,當vip啓動完成,基本就沒問題了

Aug 08 16:56:46 node1 heartbeat: [1557]: info: **************************
Aug 08 16:56:46 node1 heartbeat: [1557]: info: Configuration validated. Starting heartbeat 3.0.4
Aug 08 16:56:46 node1 heartbeat: [1558]: info: heartbeat: version 3.0.4
Aug 08 16:56:46 node1 heartbeat: [1558]: WARN: change_logfile_ownership: failed to chown dbgfile: No such file or directory
Aug 08 16:56:46 node1 heartbeat: [1558]: info: Heartbeat generation: 1470604473
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: write socket priority set to IPTOS_LOWDELAY on eth0
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: bound send socket to device: eth0
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: set SO_REUSEPORT(w)
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: bound receive socket to device: eth0
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: set SO_REUSEPORT(w)
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ucast: started on port 694 interface eth0 to 192.168.1.107
Aug 08 16:56:46 node1 heartbeat: [1558]: info: glib: ping heartbeat started.
Aug 08 16:56:46 node1 heartbeat: [1558]: info: G_main_add_TriggerHandler: Added signal manual handler
Aug 08 16:56:46 node1 heartbeat: [1558]: info: G_main_add_TriggerHandler: Added signal manual handler
Aug 08 16:56:46 node1 heartbeat: [1558]: info: G_main_add_SignalHandler: Added signal handler for signal 17
Aug 08 16:56:46 node1 heartbeat: [1558]: info: Local status now set to: 'up'
Aug 08 16:56:47 node1 heartbeat: [1558]: info: Link 192.168.1.1:192.168.1.1 up.
Aug 08 16:56:47 node1 heartbeat: [1558]: info: Status update for node 192.168.1.1: status ping
Aug 08 16:58:46 node1 heartbeat: [1558]: WARN: node node2: is dead
Aug 08 16:58:46 node1 heartbeat: [1558]: info: Comm_now_up(): updating status to active
Aug 08 16:58:46 node1 heartbeat: [1558]: info: Local status now set to: 'active'
Aug 08 16:58:46 node1 heartbeat: [1558]: info: Starting child client "/usr/lib64/heartbeat/ipfail " (0,0)
Aug 08 16:58:46 node1 heartbeat: [1558]: WARN: No STONITH device configured.
Aug 08 16:58:46 node1 heartbeat: [1558]: WARN: Shared disks are not protected.
Aug 08 16:58:46 node1 heartbeat: [1558]: info: Resources being acquired from node2.
Aug 08 16:58:46 node1 heartbeat: [1570]: info: Starting "/usr/lib64/heartbeat/ipfail " as uid 0  gid 0 (pid 1570)
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.122)[1613]:  2016/08/08_16:58:46 INFO:  Resource is stopped
Aug 08 16:58:46 node1 heartbeat: [1572]: info: Local Resource acquisition completed.
Aug 08 16:58:57 node1 heartbeat: [1558]: info: Local Resource acquisition completed. (none)
Aug 08 16:58:57 node1 heartbeat: [1558]: info: local resource transition completed.
Aug 08 17:05:27 node1 heartbeat: [1558]: WARN: Shutdown delayed until current resource activity finishes.
Aug 08 17:11:19 HaMaster heartbeat: [1241]: info: Pacemaker support: false
Aug 08 17:11:19 HaMaster heartbeat: [1241]: ERROR: Current node [hamaster] not in configuration!
Aug 08 17:11:19 HaMaster heartbeat: [1241]: info: By default, cluster nodes are named by `uname -n` and must be declared with a 'node' directive in the ha.cf file.
Aug 08 17:11:19 HaMaster heartbeat: [1241]: info: See also: http://linux-ha.org/wiki/Ha.cf#node_directive
Aug 08 17:11:19 HaMaster heartbeat: [1241]: WARN: Logging daemon is disabled --enabling logging daemon is recommended
Aug 08 17:11:19 HaMaster heartbeat: [1241]: ERROR: Configuration error, heartbeat not started.
Aug 08 21:16:49 HaMaster heartbeat: [1220]: info: Pacemaker support: false
Aug 08 21:16:49 HaMaster heartbeat: [1220]: ERROR: Current node [hamaster] not in configuration!
Aug 08 21:16:49 HaMaster heartbeat: [1220]: info: By default, cluster nodes are named by `uname -n` and must be declared with a 'node' directive in the ha.cf file.
Aug 08 21:16:49 HaMaster heartbeat: [1220]: info: See also: http://linux-ha.org/wiki/Ha.cf#node_directive
Aug 08 21:16:49 HaMaster heartbeat: [1220]: WARN: Logging daemon is disabled --enabling logging daemon is recommended
Aug 08 21:16:49 HaMaster heartbeat: [1220]: ERROR: Configuration error, heartbeat not started.
Aug 08 21:18:34 node1 heartbeat: [1510]: info: Pacemaker support: false
Aug 08 21:18:34 node1 heartbeat: [1510]: WARN: Logging daemon is disabled --enabling logging daemon is recommended
Aug 08 21:18:34 node1 heartbeat: [1510]: info: **************************
Aug 08 21:18:34 node1 heartbeat: [1510]: info: Configuration validated. Starting heartbeat 3.0.4
Aug 08 21:18:34 node1 heartbeat: [1511]: info: heartbeat: version 3.0.4
Aug 08 21:18:34 node1 heartbeat: [1511]: info: Heartbeat generation: 1470604474
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: write socket priority set to IPTOS_LOWDELAY on eth0
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: bound send socket to device: eth0
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: set SO_REUSEPORT(w)
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: bound receive socket to device: eth0
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: set SO_REUSEPORT(w)
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ucast: started on port 694 interface eth0 to 192.168.1.110
Aug 08 21:18:34 node1 heartbeat: [1511]: info: glib: ping heartbeat started.
Aug 08 21:18:34 node1 heartbeat: [1511]: info: G_main_add_TriggerHandler: Added signal manual handler
Aug 08 21:18:34 node1 heartbeat: [1511]: info: G_main_add_TriggerHandler: Added signal manual handler
Aug 08 21:18:34 node1 heartbeat: [1511]: info: G_main_add_SignalHandler: Added signal handler for signal 17
Aug 08 21:18:34 node1 heartbeat: [1511]: info: Local status now set to: 'up'
Aug 08 21:18:35 node1 heartbeat: [1511]: info: Link 192.168.1.1:192.168.1.1 up.
Aug 08 21:18:35 node1 heartbeat: [1511]: info: Status update for node 192.168.1.1: status ping
Aug 08 21:20:35 node1 heartbeat: [1511]: WARN: node node2: is dead
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Comm_now_up(): updating status to active
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Local status now set to: 'active'
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Starting child client "/usr/lib64/heartbeat/ipfail " (0,0)
Aug 08 21:20:35 node1 heartbeat: [1511]: WARN: No STONITH device configured.
Aug 08 21:20:35 node1 heartbeat: [1511]: WARN: Shared disks are not protected.
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Resources being acquired from node2.
Aug 08 21:20:35 node1 heartbeat: [1521]: info: Starting "/usr/lib64/heartbeat/ipfail " as uid 0  gid 0 (pid 1521)
harc(default)[1522]:    2016/08/08_21:20:35 info: Running /etc/ha.d//rc.d/status status
mach_down(default)[1557]:       2016/08/08_21:20:35 info: /usr/share/heartbeat/mach_down: nice_failback: foreign resources acquired
mach_down(default)[1557]:       2016/08/08_21:20:35 info: mach_down takeover complete for node node2.
Aug 08 21:20:35 node1 heartbeat: [1511]: info: mach_down takeover complete.
Aug 08 21:20:35 node1 heartbeat: [1511]: info: Initial resource acquisition complete (mach_down)
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.122)[1595]:  2016/08/08_21:20:35 INFO:  Resource is stopped
Aug 08 21:20:35 node1 heartbeat: [1523]: info: Local Resource acquisition completed.
harc(default)[1696]:    2016/08/08_21:20:35 info: Running /etc/ha.d//rc.d/ip-request-resp ip-request-resp
ip-request-resp(default)[1696]: 2016/08/08_21:20:35 received ip-request-resp 192.168.1.122/24/eth0:0 OK yes
ResourceManager(default)[1717]: 2016/08/08_21:20:35 info: Acquiring resource group: node1 192.168.1.122/24/eth0:0 nginx
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.122)[1744]:  2016/08/08_21:20:35 INFO:  Resource is stopped
ResourceManager(default)[1717]: 2016/08/08_21:20:35 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.122/24/eth0:0 start
IPaddr(IPaddr_192.168.1.122)[1875]:     2016/08/08_21:20:35 INFO: Adding inet address 192.168.1.122/24 with broadcast address 192.168.1.255 to device eth0 (with label eth0:0)
IPaddr(IPaddr_192.168.1.122)[1875]:     2016/08/08_21:20:35 INFO: Bringing device eth0 up
IPaddr(IPaddr_192.168.1.122)[1875]:     2016/08/08_21:20:35 INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 -p /var/run/resource-agents/send_arp-192.168.1.122 eth0 192.168.1.122 auto not_used not_used
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.1.122)[1849]:  2016/08/08_21:20:35 INFO:  Success
ResourceManager(default)[1717]: 2016/08/08_21:20:36 info: Running /etc/init.d/nginx  start
Aug 08 21:20:45 node1 heartbeat: [1511]: info: Local Resource acquisition completed. (none)
Aug 08 21:20:45 node1 heartbeat: [1511]: info: local resource transition completed.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章