一、部署環境:
服務器版本:CentOS6.5
雙主熱備模式:
VIP:192.168.3.30(MASTER上)
VIP:192.168.3.32(BACKUP上)
主機網絡參數:
接口 | MASTER | BACKUP | 說明 |
eth1 | 192.168.3.23 | 192.168.3.24 | 內網管理IP |
eth2 | 192.168.5.23 | 192.168.5.24 | 心跳線 |
eth3 | 192.168.2.23 | 192.168.2.24 | 外網(臨時下載文件用) |
網絡拓撲:
二、需求分析:
通過Heartbeat的高可用功能保證雙主VIP同時在線。MySQL01默認綁定VIP:192.168.3.30,MySQL02默認綁定192.168.3.32。任一主機宕機後自動切換。
三、相關配置:
添加心跳線路由:
1)MySQL01端:
route add -host 192.168.5.24 dev eth2 echo "route add -host 192.168.5.24 dev eth2" >>/etc/rc.local # 加入到rc.local開機自啓,也可修改默認靜態路由現實
2)MySQL02端:
route add -host 192.168.5.23 dev eth2 echo "route add -host 192.168.5.23 dev eth2" >>/etc/rc.local
安裝heartbeat:
[root@MySQL01 ~]# mkdir tools [root@MySQL01 ~]# cd tools [root@MySQL01 tools]# wget [root@MySQL01 tools]# rpm -ivh epel-release-6-8.noarch.rpm [root@MySQL01 tools]# yum install heartbeat -y
配置heartbeat:
[root@MySQL01 tools]# cd /usr/share/doc/heartbeat-3.0.4/ [root@MySQL01 heartbeat-3.0.4]# ls [root@MySQL01 heartbeat-3.0.4]# cp ha.cf haresources authkeys /etc/ha.d/ [root@MySQL01 heartbeat-3.0.4]# ls -ls /etc/ha.d/ [root@MySQL01 heartbeat-3.0.4]# cd /etc/ha.d/
修改HOSTS文件或者配置DNS參數:
注:由於外網網段192.168.2.0中有DNS服務器,所以在這裏就沒配HOSTS文件:
如果沒有DNS服務器的話需要手工添加HOST記錄,主機名的查看方法:
[root@MySQL01 resource.d]# uname -n MySQL01.stephenzhou.net [root@MySQL01 ha.d]# cat /etc/hosts 127.0.0.1 localhost.localdomain localhost.localdomain localhost4 localhost4.localdomain4 localhost MySQL01 ::1 localhost.localdomain localhost.localdomain localhost6 localhost6.localdomain6 localhost MySQL01 192.168.2.23 MySQL01.stpehenzhou.net 192.168.2.24 MySQL02.stpehenzhou.net 192.168.3.23 MySQL01.stpehenzhou.net 192.168.3.24 MySQL02.stpehenzhou.net
查看epel是否已經安裝好:
[root@MySQL01 resource.d]# rpm -qa|grep epel epel-release-6-8.noarch
配置heartbeat的三個主要配置文件(ha.cf/haresources/authkeys),完了過後複製到MySQL01及MySQL02的/etc/ha.d/目錄下:
1. ha.cf
#created by stephen#2014-07-07 debugfile /var/log/ha-debug logfile /var/log/ha-log logfacility local0 keepalive 2 deadtime 30 warntime 10 initdead 60 serial /dev/ttyS0 # Linux mcast eth2 225.0.0.1 694 1 0 # 使用組/多播形式,通過eth2走心跳線傳送,端口爲:udp--694 auto_failback on node MySQL01.stephenzhou.net # 此處必須使用與‘uname -n’一致的名字 node MySQL02.stephenzhou.net crm no
2. haresources
# stephen services # 192.168.2.23 www.stephenzhou.net # MySQL01.stephenzhou.net IPaddr::192.168.2.30/24/eth3 #MySQL01.stephenzhou.net IPaddr::192.168.2.30/24/eth3 httpd # 192.168.2.24 bbs.stephenzhou.net # MySQL02.stephenzhou.net IPaddr::192.168.2.32/24/eth3 # MySQL master mysqlm.stephenzhou.net #MySQL01.stephenzhou.net IPaddr::192.168.3.30/24/eth1 mysqld MySQL01.stephenzhou.net IPaddr::192.168.3.30/24/eth1 # 此處也必須使用與‘uname -n’一致的名字 # MySQL slave mysqls.stephenzhou.net MySQL02.stephenzhou.net IPaddr::192.168.3.32/24/eth1
3. authkeys
auth 1 1 sha1 47e9336850f1db6fa58bc470bc9b7810eb397f04
配置防火牆規則已允許心跳流量(/etc/sysconfig/iptables):
[root@MySQL01 ha.d]# cat /etc/sysconfig/iptables # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -p udp --dport 694 -j ACCEPT # 允許端口UDP 694的流量進入 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
測試heartbeat是否配置成功:
1)MySQL01端:
[root@MySQL01 ha.d]# /etc/init.d/heartbeat start Starting High-Availability services: INFO: Resource is stopped INFO: Resource is stopped Done. [root@MySQL01 ha.d]# ip add|grep 192.168.3 # 發現VIP已經綁定 inet 192.168.3.23/24 brd 192.168.3.255 scope global eth1 inet 192.168.3.30/24 brd 192.168.3.255 scope global secondary eth1 [root@MySQL01 ha.d]# iptables -vL # 看到udp端口已經有心跳流量了 Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 48 12296 ACCEPT udp -- any any anywhere anywhere udp dpt:ha-cluster 36 2376 ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- any any anywhere anywhere 0 0 ACCEPT all -- lo any anywhere anywhere 0 0 ACCEPT tcp -- any any anywhere anywhere state NEW tcp dpt:ssh 1 229 REJECT all -- any any anywhere anywhere reject-with icmp-host-prohibited [root@MySQL01 ha.d]# tcpdump -i eth2 udp -s 1500 # 對心跳線所在網卡udp端口抓包的話可以看到心跳流量 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth2, link-type EN10MB (Ethernet), capture size 1500 bytes 16:55:16.399325 IP 192.168.5.23.35912 > 225.0.0.1.ha-cluster: UDP, length 226 16:55:17.216819 IP 192.168.5.24.53014 > 225.0.0.1.ha-cluster: UDP, length 226 16:55:17.371805 IP 192.168.5.23.35912 > 225.0.0.1.ha-cluster: UDP, length 239 16:55:18.399199 IP 192.168.5.23.35912 > 225.0.0.1.ha-cluster: UDP, length 226 16:55:19.222381 IP 192.168.5.24.53014 > 225.0.0.1.ha-cluster: UDP, length 226 ... ^C 16 packets captured 16 packets received by filter 0 packets dropped by kernel
2)MySQL02端:
[root@MySQL02 ha.d]# /etc/init.d/heartbeat start Starting High-Availability services: INFO: Resource is stopped INFO: Resource is stopped Done. [root@MySQL02 ha.d]# ip add|grep 192.168.3 inet 192.168.3.24/24 brd 192.168.3.255 scope global eth1 inet 192.168.3.32/24 brd 192.168.3.255 scope global secondary eth1
四、參考博文:
CentOS 6.5 heartbeat高可用集羣的詳解實現以及工作流程
heartbeat mysql nfs 高可用mysql集羣