1、什麼是高可用集羣:html
所謂高可用集羣就是在其出現故障時,能夠把業務自動轉移到其餘主機上並讓服務正常運行的集羣架構。
前端
2、heartbeat的概念node
Linux-HA的全稱是High-Availability Linux,它是一個開源項目,這個開源項目的目標是:經過社區開發者的共同努力,提供一個加強LInux可靠性(reliability)、可用性(availability)和可服務性(serviceability)(RAS)的羣集解決方案。其中heartbeat就是Linux-HA項目中的一個組件,也是目前開源HA項目中比較成功的一個例子,它提供了全部HA軟件所須要的基本功能,好比心跳檢測和資源接管、監測集羣中的系統服務、在集羣中的節點間轉移共享IP地址的全部者等,自1999年開始到如今,Heartbeat在行業內獲得了普遍的使用,也發行了不少的版本,能夠從linux-HA的官方網站www.linux-ha.org下載到Heartbeat的最新版本。python
3、高可用集羣的構架層次mysql
一、後端主機層:這一層主要是正在運行在物理主機上的服務。linux
二、Message Layer:信息傳遞層,主要傳遞心跳信息。web
三、Cluster Resources Manager(CRM):集羣資源管理器層,這一層是心跳信息傳遞層管理器。用於管理信息的傳遞和收集。算法
四、Local Resources Manager(LRM):本地資源管理器,用於對於收集到的心跳信息進行資源決策調整,是否轉移服務等等。sql
五、Resource Agent(RA):資源代理層,這一層主要是具體啓動或中止具體資源的腳本。遵循{start|stop|restart|status}服務腳本使用格式。shell
4、HA集羣的工做模式
一、A/P:two node:主被模型,要藉助於ping node工做
二、N-M:N個節點M個服務,N>M,活動節點爲N,備用節點爲N-M
三、N-N:N個節點N個服務,N節點都有服務,若是一個壞了,會有一個節點運行多個服務。
四、A/A:雙主模型,兩個節點都是活動的,兩個節點運行兩個不一樣的服務。也能夠是同一個服務,好比ipvs,前端基於DNS輪訓。
5、配置Heartbeat集羣
一、基於Heartbeat V1版harousce配置httpd高可用
#安裝前準備: 一、ssh多節點互信 [root@node1 ~]# ssh-keygen -t rsa [root@node1 ~]# ssh-copy-id -i root@node2 二、節點時間一致檢查 [root@node1 ~]# date ;ssh node2 'date' 2016年 08月 09日 星期二 10:57:30 CST 2016年 08月 09日 星期二 10:57:30 CST 三、多節點名稱解析 [root@node1 ~]# uname -n;ssh node2 'uname -n' node1.bjwf.com node2.bjwf.com 四、安裝heartbeat [root@node1 ~]# yum install perl-TimeDate PyXML libnet net-snmp-libs libtool-ltdl -y [root@node1 ~]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm Preparing... ########################################### [100%] 1:heartbeat-pils ########################################### [ 33%] 2:heartbeat-stonith ########################################### [ 67%] 3:heartbeat ########################################### [100%] #另外一個節點同樣的方法 五、準備Heartbeat的配置文件 [root@node1 ~]# cd /usr/share/doc/heartbeat-2.1.4/ [root@node1 heartbeat-2.1.4]# cp authkeys ha.cf haresources /etc/ha.d/ [root@node1 heartbeat-2.1.4]# cd /etc/ha.d/ [root@node1 ha.d]# vim authkeys #配置節點信息通訊密碼 auth 2 2 sha1 woleigequ@xyoaq`!;q [root@node1 ha.d]# chmod 600 authkeys [root@node1 ha.d]# vim ha.cf logfile /var/log/ha-log #定義日誌格式 keepalive 2 #指定心跳使用間隔時間爲2秒(即每兩秒鐘發送一次廣播) deadtime 30 #指定備用節點在30s內沒有收到主節點的心跳信息號,則當即接管主節點的服務資源 warntime 10 #指定心跳延遲的時間爲10s,當10s鍾內備份節點不能接收到主節點的心跳信號時,就會 往日誌中寫入一個警告日誌,但此時不會切換服務。 udpport 694 #監聽端口 mcast eth0 225.7.1.1 694 1 0 #節點多播方式傳遞心跳信息 auto_failback on #用來定義主節點恢復後,是否將服務自動切回 node node1.bjwf.com #Heartbeat集羣節點 node node2.bjwf.com ping 192.168.120.254 #ping節點仲裁 compression bz2 compression_threshold 2 [root@node1 ha.d]# vi haresources node1 192.168.120.10/24/eth0 httpd #以node1爲dc,定義集羣VIP及資源httpd服務 [root@node1 ha.d]# scp authkeys ha.cf haresources node2:/etc/ha.d authkeys 100% 659 0.6KB/s 00:00 ha.cf 100% 10KB 10.4KB/s 00:00 haresources 100% 5940 5.8KB/s 00:00 六、安裝httpd服務 [root@node1 ha.d]# yum -y install httpd [root@node1 ha.d]# echo "node1.bjwf.com" > /var/www/html/index.html [root@node1 ha.d]# chkconfig httpd off [root@node1 ha.d]# service httpd stop 中止 httpd: [肯定] [root@node2 ha.d]# yum -y install httpd [root@node2 ha.d]# echo "node2.bjwf.com" > /var/www/html/index.html [root@node2 ha.d]# chkconfig httpd off [root@node2 ha.d]# service httpd stop 中止 httpd: [肯定] 七、啓動Heartbeat服務,並測試 [root@node1 ha.d]# service heartbeat start;ssh node2 'service heartbeat start' logd is already running Starting High-Availability services: 2016/08/09_11:55:15 INFO: Resource is stopped Done. logd is already running Starting High-Availability services: 2016/08/09_11:55:15 INFO: Resource is stopped Done. [root@node1 ha.d]# netstat -tunlp;ssh node2 'netstat -tunlp' Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1331/sshd tcp 0 0 :::80 :::* LISTEN 2292/httpd tcp 0 0 :::22 :::* LISTEN 1331/sshd udp 0 0 0.0.0.0:36099 0.0.0.0:* 1900/heartbeat: wri udp 0 0 225.7.1.1:694 0.0.0.0:* 1900/heartbeat: wri Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1331/sshd tcp 0 0 :::80 :::* LISTEN 1642/httpd tcp 0 0 :::22 :::* LISTEN 1331/sshd udp 0 0 0.0.0.0:45834 0.0.0.0:* 1886/heartbeat: wri udp 0 0 225.7.1.1:694 0.0.0.0:* 1886/heartbeat: wri [root@node3 ~]# curl http://192.168.120.10 #在node3上測試 node1.bjwf.com [root@node1 ha.d]# ifconfig #查看VIP是否在node1上 eth0 Link encap:Ethernet HWaddr 00:0C:29:20:EC:07 inet addr:192.168.120.100 Bcast:192.168.120.255 Mask:255.255.255.0 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:20:EC:07 inet addr:192.168.120.10 Bcast:192.168.120.255 Mask:255.255.255.0 [root@node1 ha.d]# service heartbeat stop #停掉node1,查看是否會自動轉移 Stopping High-Availability services: Done. [root@node3 ~]# curl http://192.168.120.10 node2.bjwf.com #服務已經轉移 [root@node2 ~]# ifconfig #VIP已經轉移 eth0 Link encap:Ethernet HWaddr 00:0C:29:25:07:08 inet addr:192.168.120.101 Bcast:192.168.120.255 Mask:255.255.255.0 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:25:07:08 inet addr:192.168.120.10 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 八、在Node3上安裝nfs服務,實現節點間自動切換 [root@node3 ~]# yum -y install nfs-utils #安裝服務 [root@node3 ~]# vim /etc/exports #修改配置文件 /data/html 192.168.120.0/24(rw,no_root_squash,async) [root@node3 ~]# echo "NFS server" > /data/html/index.html [root@node3 ~]# service rpcbind start #nfs依賴這個服務 [root@node3 ~]# service nfs start #啓動nfs [root@node1 ~]# showmount -e 192.168.120.102;ssh node2 'showmount -e 192.168.120.102' Export list for 192.168.120.102: /data/html 192.168.120.0/24 #node1上測試 Export list for 192.168.120.102: /data/html 192.168.120.0/24 #node2上測試 [root@node1 ~]# mount -t nfs 192.168.120.102:/data/html/ /var/www/html [root@node1 ~]# curl http://192.168.120.100 NFS server [root@node2 ~]# mount -t nfs 192.168.120.102:/data/html/ /var/www/html [root@node2 ~]# curl http://192.168.120.101 NFS server #修改node1和node2的haresources配置文件,是Heartbeat能自動掛載目錄 [root@node1 ~]# vim /etc/ha.d/haresources node1.bjwf.com 192.168.120.10/24/eth0 Filesystem::192.168.120.102:/data/html::/var/www/html::nfs httpd [root@node1 ~]# scp /etc/ha.d/haresources node2:/etc/ha.d/ #改完後傳給node2一份 haresources 100% 6008 5.9KB/s 00:00 中止heartbeat,httpd服務並卸載nfs共享目錄以後在重啓Heartbeat服務 [root@node1 ~]# umount /var/www/html;ssh node2 'umount /var/www/html’ [root@node1 ~]# service httpd stop;ssh node2 'service httpd stop' [root@node1 ~]# service heartbeat start;ssh node2 'service heartbeat start' Starting High-Availability services: 2016/08/09_14:42:59 INFO: Resource is stopped Done. Starting High-Availability services: 2016/08/09_14:42:59 INFO: Resource is stopped Done. #重啓Heartbeat服務 [root@node1 ~]# ifconfig #節點一上查看資源狀況 eth0 Link encap:Ethernet HWaddr 00:0C:29:20:EC:07 inet addr:192.168.120.100 Bcast:192.168.120.255 Mask:255.255.255.0 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:20:EC:07 inet addr:192.168.120.10 Bcast:192.168.120.255 Mask:255.255.255.0 [root@node1 ~]# mount |grep html #文件系統已經掛載 192.168.120.102:/data/html on /var/www/html type nfs (rw,vers=4,addr=192.168.120.102,clientaddr=192.168.120.100) [root@node1 ~]# curl http://192.168.120.10 NFS server #服務也正常 [root@node1 ~]# service heartbeat stop #關掉節點1,查看節點2是否已經切換 Stopping High-Availability services: Done. [root@node2 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:25:07:08 inet addr:192.168.120.101 Bcast:192.168.120.255 Mask:255.255.255.0 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:25:07:08 inet addr:192.168.120.10 Bcast:192.168.120.255 Mask:255.255.255.0 [root@node2 ~]# mount |grep html 192.168.120.102:/data/html on /var/www/html type nfs (rw,vers=4,addr=192.168.120.102,clientaddr=192.168.120.101) [root@node2 ~]# netstat -tnlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1331/sshd tcp 0 0 0.0.0.0:48665 0.0.0.0:* LISTEN - tcp 0 0 :::80 :::* LISTEN 3370/httpd tcp 0 0 :::22 :::* LISTEN 1331/sshd tcp 0 0 :::43097 :::* LISTEN - [root@node2 ~]# curl http://192.168.120.10 NFS server #資源已經正常切換至節點2
二、基於Heartbeat V2版配置httpd高可用(crm、heartbeat-gui工具)
一、關閉Heartbeat服務,並修改配置 [root@node1 ~]# service heartbeat stop [root@node2 ~]# service heartbeat stop [root@node1 ha.d]# vim ha.cf crm on [root@node1 ha.d]# scp ha.cf node2:/etc/ha.d ha.cf 100% 10KB 10.4KB/s 00:00 二、安裝Heartbeat-gui工具 [root@node1 ~]# yum -y install pygtk2-libglade [root@node1 heartbeat2]# rpm -ivh heartbeat-gui-2.1.4-12.el6.x86_64.rpm [root@node2 ~]# yum -y install pygtk2-libglade [root@node2 heartbeat2]# rpm -ivh heartbeat-gui-2.1.4-12.el6.x86_64.rpm 三、啓動Heartbeat服務 [root@node1 heartbeat2]# passwd hacluster [root@node1 heartbeat2]# service heartbeat start [root@node1 heartbeat2]# yum -y install xorg-x11-xauth [root@node2 heartbeat2]# passwd hacluster [root@node2 heartbeat2]# service heartbeat start [root@node2 heartbeat2]# yum -y install xorg-x11-xauth [root@node1 ~]# hb_gui & #啓動圖形報錯,這塊須要安裝xmanager Traceback (most recent call last): File "/usr/bin/hb_gui", line 41, in <module> import gtk, gtk.glade, gobject File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module> _init() File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init _gtk.init_check() RuntimeError: could not open display #Xshell裏面設置-文件-屬性-SSH-隧道-勾上轉發X11鏈接到xmanager
(1)鏈接上gui工具
(2)查看狀態信息
(3)定義一個組資源
#定義一個VIP
#定義一個服務
#啓動組資源
#查看運行節點 [root@node3 ~]# curl http://192.168.120.10 node1.bjwf.com
#切換資源
[root@node3 ~]# curl http://192.168.120.10 node2.bjwf.com
(4)從新定義組資源(NFS)
#注意:這塊須要把剛纔定義的組資源裏面的webserver刪了,先添加filesystem資源
[root@node3 ~]# curl http://192.168.120.10 NFS server [root@node2 ~]# ifconfig #查看資源 eth0 Link encap:Ethernet HWaddr 00:0C:29:25:07:08 inet addr:192.168.120.101 Bcast:192.168.120.255 Mask:255.255.255.0 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:25:07:08 inet addr:192.168.120.10 Bcast:192.168.120.138 Mask:255.255.255.255 [root@node2 ~]# mount |grep --color "html" 192.168.120.102:/data/html on /var/www/html type nfs (rw,vers=4,addr=192.168.120.102,clientaddr=192.168.120.101) [root@node2 ~]# netstat -tnlp|grep 80 tcp 0 0 :::80 :::* LISTEN 4652/httpd
#切換一下機器
[root@node3 ~]# curl http://192.168.120.10 NFS server [root@node1 ~]# ifconfig #能夠看到資源已經切換 eth0 Link encap:Ethernet HWaddr 00:0C:29:20:EC:07 inet addr:192.168.120.100 Bcast:192.168.120.255 Mask:255.255.255.0 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:20:EC:07 inet addr:192.168.120.10 Bcast:192.168.120.138 Mask:255.255.255.255 [root@node1 ~]# mount |grep --color "html" 192.168.120.102:/data/html on /var/www/html type nfs (rw,vers=4,addr=192.168.120.102,clientaddr=192.168.120.100) [root@node1 ~]# netstat -tnlp|grep 80 tcp 0 0 :::80 :::* LISTEN 5494/httpd
三、基於Heartbeat V1+Ldirectord實現LVS集羣高可用
一、實驗環境 node1:node1.bjwf.com 192.168.120.100 node2:node2.bjwf.com 192.168.120.101 Real Server1:node3.bjwf.com 192.168.120.102 Real Server2:node4.bjwf.com 192.168.120.103 二、注意事項 (1)時間同步 (2)節點通訊(ssh認證) (3)解析節點須在/etc/hosts文件中,可用uname -n查看主機名 (4)ping node (偶數節點須要) 三、安裝所需安裝包 #在node1和node2上安裝heartbeat軟件包 [root@node1 ~]# yum install perl-TimeDate net-snmp-libs libnet PyXML -y [root@node1 heartbeat2]# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm Preparing... ########################################### [100%] 1:heartbeat-pils ########################################### [ 33%] 2:heartbeat-stonith ########################################### [ 67%] 3:heartbeat ########################################### [100%] [root@node1 heartbeat2]# yum -y install heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm #配置Heartbeat [root@node1 heartbeat2]# cd /usr/share/doc/heartbeat-2.1.4/ [root@node1 heartbeat-2.1.4]# cp ha.cf authkeys haresources /etc/ha.d [root@node1 heartbeat-2.1.4]# chmod 600 authkeys [root@node1 heartbeat-2.1.4]# openssl rand -hex 8 #生產密鑰 264708ac8cbb46d1 [root@node1 heartbeat-2.1.4]# vim /etc/ha.d/authkeys auth 2 2 sha1 264708ac8cbb46d1 #配置主配置文件 [root@node1 ~]# vim /etc/ha.d/ha.cf logfile /var/log/ha-log #日誌存放位置 keepalive 2 #指定心跳使用間隔時間 deadtime 30 #指定備用節點接管主節點服務資源超時時間 warntime 10 #指定心跳延遲的時間 initdead 120 #解決網絡啓動延時 udpport 694 #設置廣播通訊端口 mcast eth0 225.0.35.1 694 1 0 #定義廣播地址 auto_failback on #定義當主節點恢復後,是否將服務自動切回 node node1.bjwf.com #主節點 node node2.bjwf.com #備用節點 ping 192.168.120.254 #仲裁設備 compression bz2 #壓縮方式 compression_threshold 2 #超過多少k啓用壓縮 #配置資源管理器 [root@node1 ha.d]# vim haresources node1.bjwf.com 172.16.10.10/32/eth0/172.16.10.10 ldirectord::/etc/ha.d/ldirectord.cf #將配置文件傳送給備用節點 [root@node1 ha.d]# scp ha.cf authkeys haresources node2:/etc/ha.d ha.cf 100% 10KB 10.3KB/s 00:00 authkeys 100% 656 0.6KB/s 00:00 haresources 100% 5990 5.9KB/s 00:00 #配置ldirectord [root@node1 ha.d]# cp /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf /etc/ha.d [root@node1 ha.d]# vim /etc/ha.d/ldirectord.cf checktimeout=3 #探測超時時間 checkinterval=1 #探測間隔時間 autoreload=yes #修改配置文件,無需重啓服務就能自動重載 logfile="/var/log/ldirectord.log" #日誌文件 quiescent=yes #real server 宕機後從lvs列表中刪除,恢復後自動添加進列表 # Sample for an http virtual service virtual=172.16.10.10:80 #VIP real=192.168.120.102:80 gate #real server real=192.168.120.103:80 gate #real server fallback=127.0.0.1:80 gate #若是RS節點都宕機,則啓用本地環回口地址 service=http #基於http協議探測 request=".test.html" #探測文件 receive="Test Page" #探測內容,判斷RS是否存活 scheduler=rr #調度算法 #persistent=600 #netmask=255.255.255.255 [root@node1 ha.d]# scp -p /etc/ha.d/ldirectord.cf node2:/etc/ha.d ldirectord.cf 100% 7632 7.5KB/s 00:00 [root@node1 ha.d]# chkconfig ldirectord off;ssh node2 'chkconfig ldirectord off' #準備fallback文件 [root@node1 ha.d]# echo "Sorry" > /var/www/html/index.html [root@node1 ha.d]# service httpd start [root@node1 ha.d]# curl http://localhost Sorry [root@node2 heartbeat2]# echo "Sorry" > /var/www/html/index.html [root@node2 heartbeat2]# service httpd start [root@node2 heartbeat2]# curl http://localhost Sorry 四、RS設置 #配置內核參數 [root@node3 ~]# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore [root@node3 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [root@node3 ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce [root@node3 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce [root@node3 ~]# ifconfig lo:0 172.16.10.10 netmask 255.255.255.0 broadcast 172.16.10.10 up [root@node3 ~]# route add -host 172.16.10.10 dev lo:0 [root@node4 ~]# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore [root@node4 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [root@node4 ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce [root@node4 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce [root@node4 ~]# ifconfig lo:0 172.16.10.10 netmask 255.255.255.255 broadcast 172.16.10.10 [root@node4 ~]# route add -host 172.16.10.10 dev lo:0 #準備健康檢測文件和站點文件 [root@node3 ~]# echo "Test Page" > /var/www/html/.test.html [root@node3 ~]# echo "node3.bjwf.com" > /var/www/html/index.html [root@node3 ~]# service httpd start [root@node4 ~]# echo "Test Page" > /var/www/html/.test.html [root@node4 ~]# echo "node4.bjwf.com" > /var/www/html/index.html [root@node4 ~]# service httpd start #測試頁面 [root@node1 ~]# curl http://192.168.120.102 node3.bjwf.com [root@node1 ~]# curl http://192.168.120.103 node4.bjwf.com #啓動Heartbeat [root@node1 ~]# service heartbeat start;ssh node2 'service heartbeat start' logd is already running Starting High-Availability services: 2016/08/10_15:18:50 INFO: Resource is stopped Done. logd is already running Starting High-Availability services: 2016/08/10_15:18:50 INFO: Resource is stopped Done. #查看資源是否生效 [root@node1 ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.16.10.10:80 rr -> 127.0.0.1:80 Local 1 0 0 -> 192.168.120.102:80 Route 0 0 0 -> 192.168.120.103:80 Route 0 0 0 [root@node1 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:20:EC:07 inet addr:192.168.120.100 Bcast:192.168.120.255 Mask:255.255.255.0 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:20:EC:07 inet addr:172.16.10.10 Bcast:172.16.10.10 Mask:255.255.255.255 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
###今天先寫到這,還有幾張圖,暫時截取不了。因此只能等晚上更新了。還有高可用mysql,實際上是一個道理,因此這就不寫了!以上僅爲我的學習整理,若有錯漏,大神勿噴~~~