LVS是一款自由軟體,任何人都可以免費擷取并使用它,而且Linux也是一個開源作業系統,這二者的組合大大節約了企業的應用成本.同時LVS具有髙穩定性和高可靠性,在高并發和髙吞吐量下,具有高負荷處理能力,當某個服務節點出現故障時,并不影響整個系統服務的正常運作。這些優點使LVS已經廣泛應用在企業、教育行業以及很多知名網站。可能已經有許多同學已經發現了,LVS在具有上述優點的同時,還存在一個緻命的缺點,如果所有的使用者請求都經過Director Server将任務分發到各個伺服器節點,那麼,當隻有一台Director Server時,将會出現單點故障點,如果這個Director Server 出現故障,整個LVS系統将陷入癱瘓狀态。
雖然Director Server僅完成使用者請求的分發處理,負栽并不是很大,但是對于一個健壯的叢集系統來說,單點故障是絕對不允許的。要避免這種單點故障,最實用、最簡單的辦法 就是對Director Server進行髙可用叢集,常見的方案就是為Director Server做一個雙機熱備: 正常狀态下主Director Server工作,備用Director Server監控主Director Server的狀态,當 主Director Server出現異常或者故障時,備用Director Server馬上接過主Director Server的工作,負責對使用者請求進行分發處理。這樣就避免了一台Director Server的單點故障問題, 保證了負栽均衡端持續地提供服務。
高可用LVS負載均衡叢集體系結構
單一的Director Server可能會造成真個LVS叢集系統的單點故障,為了解決這個 題,就需要保證Director Server的髙可用性,最常用的方法就是在負栽均衡層建構Director Server 雙機熱備系統。
高可用的LVS負載均衡群集體系結構如下圖所示:

如上圖所示:可以看出,整個體系結構仍然分為三層,在HA負栽均衡層由主、備兩台 Director Server構成雙機熱備系統,雙機之間通過心跳線連接配接。在正常狀态下主Director Server使用虛拟IP接收使用者請求,并根據設定好的政策和算法将請求分發給各個服務節點, 備用Director Server負責監控主Director Server的運作狀态。當主Director Server發生異常 或出現故障時,備用Director Server負責接管主Director Server的虛拟IP和服務并繼續接收 使用者請求和分發處理。通過這種互相監控政策,任意一方主機出故障時.另一方都能夠将IP 和服務接管,這就保證了負栽均衡層業務請求的不間斷運作。
高可用性軟體 Heartbeat 與 Keepalived
開源HA軟體Heartbeat的介紹
heartbeat是Linux-HA項目中的一個元件,也是目前開源HA項目中最成功的一個例子, 它提供了所有HA軟體需要的基本功能,比如心跳檢測和資源接管,監測叢集中的系統服務,在群集的節點間轉移共享IP位址的所有者等。自1999年開始到現在,heartbeat在行業内得 到了廣泛應用,也發行了很多的版本。可以從Linux-HA的官方網站www.linux-ha.org下載下傳 到heartbeat的最新版本。
HeartBeat
運作于備用主機上的Heartbeat可以通過以太網連接配接檢測主伺服器的運作狀态,一旦其無法檢測到主伺服器的“心跳”則自動接管主伺服器的資源。通常情況下,主、備伺服器間的心跳連接配接是一個獨立的實體連接配接,這個連接配接可以是串行線纜、一個由“交叉線”實作的以太網連接配接。Heartbeat甚至可同時通過多個實體連接配接檢測主伺服器的工作狀态,而其隻要能通過其中一個連接配接收到主伺服器處于活動狀态的資訊,就會認為主伺服器處于正常狀态。從實踐經驗的角度來說,建議為Heartbeat配置多條獨立的實體連接配接,以避免Heartbeat通信線路本身存在單點故障。
1、串行電纜:被認為是比以太網連接配接安全性稍好些的連接配接方式,因為hacker無法通過串行連接配接運作諸如telnet、ssh或rsh類的程式,進而可以降低其通過已劫持的伺服器再次侵入備份伺服器的幾率。但串行線纜受限于可用長度,是以主、備伺服器的距離必須非常短。
2、以太網連接配接:使用此方式可以消除串行線纜的在長度方面限制,并且可以通過此連接配接在主備伺服器間同步檔案系統,進而減少了從正常通信連接配接帶寬的占用。
基于備援的角度考慮,應該在主、備伺服器使用兩個實體連接配接傳輸heartbeat的控制資訊;這樣可以避免在一個網絡或線纜故障時導緻兩個節點同時認為自已是唯一處于活動狀态的伺服器進而出現争用資源的情況,這種争用資源的場景即是所謂的“腦裂”(split-brain)或“partitioned cluster”。在兩個節點共享同一個實體裝置資源的情況下,腦裂會産生相當可怕的後果。
為了避免出現腦裂,可采用下面的預防措施:
1、如前所述,在主、備節點間建立一個備援的、可靠的實體連接配接來同時傳送控制資訊;
2、一旦發生腦裂時,借助額外裝置強制性地關閉其中一個節點;
第二種方式即是俗稱的“将其它節點‘爆頭’(shoot the other node in the head)”,簡稱為STONITH。基于能夠通過軟體指令關閉某節點特殊的硬體裝置,Heartbeat即可實作可配置的Stonith。但當主、備伺服器是基于WAN進行通信時,則很難避免“腦裂”情景的出現。是以,當建構異地“容災”的應用時,應盡量避免主、備節點共享實體資源。
Heartbeat的控制資訊:
“心跳”資訊: (也稱為狀态資訊)僅150 bytes大小的廣播、多點傳播或多點傳播資料包。可為以每個節點配置其向其它節點通報“心跳”資訊的頻率,以及其它節點上的heartbeat程序為了确認主節點出節點出現了運作等錯誤之前的等待時間。
叢集變動事務(transition)資訊:ip-request和ip-request-rest是相對較常見的兩種叢集變動資訊,它們在節點間需要進行資源遷移時為不同節點上heartbeat程序間會話傳遞資訊。比如,當修複了主節點并且使其重新“上線”後,主節點會使用ip-request要求備用節點釋放其此前從因主節點故障而從主節點那裡接管的資源。此時,備用節點則關閉服務并使用ip-request-resp通知主節點其已經不再占用此前接管的資源。主接點收到ip-request-resp後就會重新啟動服務。
重傳請求:在某叢集節點發現其從其它節點接收到的heartbeat控制資訊“失序”(heartbeat程序使用序列号來確定資料包在傳輸過程中沒有被丢棄或出現錯誤)時,會要求對方重新傳送此控制資訊。 Heartbeat一般每一秒發送一次重傳請求,以避免洪泛。
上面三種控制資訊均基于UDP協定進行傳送,可以在/etc/ha.d/ha.cf中指定其使用的UDP端口或者多點傳播位址(使用以太網連接配接的情況下)。
此外,除了使用“序列号/确認”機制來確定控制資訊的可靠傳輸外,Heartbeat還會使用MD5或SHA1為每個資料包進行簽名以確定傳輸中的控制資訊的安全性。
資源腳本:
資源腳本(resource scripts)即Heartbeat控制下的腳本。這些腳本可以添加或移除IP别名(IP alias)或從屬IP位址(secondary IP address),或者包含了可以啟動/停止服務能力之外資料包的處理功能等。通常,Heartbeat會到/etc/init.d/或/etc/ha.d/resource.d/目錄中讀取腳本檔案。Heartbeat需要一直明确了解“資源”歸哪個節點擁有或由哪個節點提供。在編寫一個腳本來啟動或停止某個資源時,一定在要腳本中明确判斷出相關服務是否由目前系統所提供。
Heartbeat的配置檔案:
/etc/ha.d/ha.cf
定義位于不同節點上的heartbeat程序間如何進行通信;
/etc/ha.d/haresources
定義對某個資源來說哪個伺服器是主節點,以及哪個節點應該擁有用戶端通路資源時的目标IP位址。
/etc/ha.d/authkeys
定義Heartbeat包在通信過程中如何進行加密。
當ha.cf或authkeys檔案發生改變時,需要重新加載它們就可以使用之生效;而如果haresource檔案發生了改變,則隻能重新開機heartbeat服務方可使之生效。
盡管Heartbeat并不要求主從節點間進行時鐘同步,但它們彼此間的時間差距不能超過1分鐘,否則一些配置為高可用的服務可能會出異常。
Heartbeat目前也不監控其所控制的資源的狀态,比如它們是否正在運作,是否運作良好以及是否可供用戶端通路等。要想監控這些資源,冉要使用額外的Mon軟體包來實作。
haresources配置檔案介紹:
主從節點上的/etc/ra.d/raresource檔案必須完全相同。檔案每行通常包含以下組成部分:
1、伺服器名字:指正常情況下資源運作的那個節點(即主節點),後跟一個空格或tab;這裡指定的名字必須跟某個節點上的指令"uname -n"的傳回值相同;
2、IP别名(即額外的IP位址,可選):在啟動資源之前添加至系統的附加IP位址,後跟空格或tab;IP位址後面通常會跟一個子網路遮罩和廣播位址,彼此間用“/”隔開;
3、資源腳本:即用來啟動或停止資源的腳本,位于/etc/init.d/或/etc/ha.d/resourcd.d目錄中;如果需要傳遞參數給資源腳本,腳本和參數之間需要用兩個冒号分隔,多個參數時彼此間也需要用兩個冒号分隔;如果有多個資源腳本,彼此間也需要使用空格隔開;
格式如下:
primary-server [IPaddress[/mask/interface/broadcast]] resource1[::arg1::arg2] resource2[::arg1::arg2]
例如:
primary-server 221.67.132.195 sendmail httpd
這裡下栽的軟體包是heartbeat-2.1.3.tar.gz,可通過源碼進行安裝。
同時還需要安裝一個libnet工具包。libnet是一個髙層次的API工具,可以從 http://sourceforge.net/projects/libnet-dev/files/latest/download 下載下傳到,這裡下載下傳的是libnet-1.2-rc3.tar.gz。 heartbeat的安裝非常簡單,基本操作步驟如下:
(1)安裝libnet 前提條件:需要安裝這兩個包組
# yum -y groupinstall "Development Tools"
# yum -y groupinstall " Server Platform Development"
# tar -zxvf libnet-1.2-rc3.tar.gz
# cd libnet-1.2-rc3
# ./configure
# make && make install
(2) 安裝heartbeat;
yum install perl-TimeDate net-snmp-libs libnet PyXML perl-MailTools
# 安裝heartbeat 所依賴的程式包
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 heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm
# 安裝heartbeat 2 以及程式包
Preparing... ########################################### [100%]
1:heartbeat-pils ########################################### [ 33%]
2:heartbeat-stonith ########################################### [ 67%]
3:heartbeat ########################################### [100%]
cp /usr/share/doc/heartbeat-2.1.4/{authkeys,ha.cf,haresources} /etc/ha.d/
# 複制配置檔案、認證檔案、以及叢集資源配置檔案
openssl rand -hex 8
80dd467590678c72
# 使用openssl 生成加密秘鑰
cat authkeys
# 更改 authkeys 檔案
# Authentication file. Must be mode 600
#
#
# Must have exactly one auth directive at the front.
# auth send authentication using this method-id
#
# Then, list the method and key that go with that method-id
#
# Available methods: crc sha1, md5. Crc doesn't need/want a key.
#
# You normally only have one authentication method-id listed in this file
#
# Put more than one to make a smooth transition when changing auth
# methods and/or keys.
#
#
# sha1 is believed to be the "best", md5 next best.
#
# crc adds no security, except from packet corruption.
# Use only on physically secure networks.
#
auth 2
#1 crc
2 sha1 80dd467590678c72
#3 md5 Hello!
chmod 600 authkeys
# 更改authkeys 檔案的預設權限為600
heartbeat的安裝包中包含了一個ldirectord插件,這個插件以後會用到。在heartbeat安 裝完畢後,此插件預設已經安裝。但是為了保證丨directord可用,還需要一個perl-MailTools 的rpm包,這個rpm從系統盤中找到後安裝即可。
開源HA軟體Keepalived的介紹
Keepalived起初是為LVS設計的,專門用來監控叢集系統中各個服務節點的狀态。它根據layer3,4&5交換機制檢測每個服務節點的狀态,如果某個服務節點出現異常,或工作出現故障,Keepalived将檢測到,并将出現故障的服務節點從叢集系統中剔除,而當故障節點 恢複正常後,Keepalived又可以自動将此服務節點重新加入到伺服器叢集中。這些工作全部自動完成,不需要人工幹涉,需要人工完成的隻是修複出現故障的服務節點。
Keepalived 後來又加入了 VRRP 的功能。VRRP 是 Virtual Router Redundancy Protocol虛
拟路由器備援協定)的縮寫,它的作用是解決靜态路由出現的單點故障問題,它能夠保證 網絡不間斷地、穩定地運作。綜上所述,Keepalived—方面具有伺服器運作檢測功能,另 一方面也具有HA cluster功能。是以通過Keepalived可以搭建一個高可用的LVS負載均衡 叢集系統。
安裝 Keepalived
Keepalived的官方網址是http://www.keepalived.org,可以在這裡下載下傳到各種版本的 Keepalived,這裡下載下傳的是 keepalived-1.2.12.tar.gz。安裝步嫌如下:
安裝keepalived 官方站點:http://www.keepalived.org/download.htm
# tar -zxvf keepalived-1.2.12.tar.gz
# cd keepalived-1.2.12
# ./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
# make && make install
# ln -s /usr/local/sbin/keepalived /sbin/
# keepalived --help
Usage: keepalived [OPTION...]
-f, --use-file=FILE Use the specified configuration file
-P, --vrrp Only run with VRRP subsystem
-C, --check Only run with Health-checker subsystem
-l, --log-console Log messages to local console
-D, --log-detail Detailed log messages
-S, --log-facility=[0-7] Set syslog facility to LOG_LOCAL[0-7]
-V, --dont-release-vrrp Don't remove VRRP VIPs and VROUTEs on daemon stop
-I, --dont-release-ipvs Don't remove IPVS topology on daemon stop
-R, --dont-respawn Don't respawn child processes
-n, --dont-fork Don't fork the daemon process
-d, --dump-conf Dump the configuration data
-p, --pid=FILE Use specified pidfile for parent process
-r, --vrrp_pid=FILE Use specified pidfile for VRRP child process
-c, --checkers_pid=FILE Use specified pidfile for checkers child process
-v, --version Display the version number
-h, --help Display this help message
在編譯選項中,“--sysconf”指定了 Keepalived配置檔案的安裝路徑,即路徑為/etc/ Keepalived/Keepalived.conf. “--with-kenel-dir”是個很重要的參數,但這個參數并不是要把Keepalived編譯進核心,而是指定使用核心源碼中的頭檔案,即include目錄。隻有使用 LVS時,才需要用到此參數,其他時候是不需要的。
安裝完成,執行如下操作:
[root@node4 ~]# keepalived --help
Usage: keepalived [OPTION...]
-f, --use-file=FILE Use the specified configuration file
-P, --vrrp Only run with VRRP subsystem
-C, --check Only run with Health-checker subsystem
-l, --log-console Log messages to local console
-D, --log-detail Detailed log messages
-S, --log-facility=[0-7] Set syslog facility to LOG_LOCAL[0-7]
-V, --dont-release-vrrp Don't remove VRRP VIPs and VROUTEs on daemon stop
-I, --dont-release-ipvs Don't remove IPVS topology on daemon stop
-R, --dont-respawn Don't respawn child processes
-n, --dont-fork Don't fork the daemon process
-d, --dump-conf Dump the configuration data
-p, --pid=FILE Use specified pidfile for parent process
-r, --vrrp_pid=FILE Use specified pidfile for VRRP child process
-c, --checkers_pid=FILE Use specified pidfile for checkers child process
-v, --version Display the version number
-h, --help Display this help message
這裡列出了keepalive的各種用法,同時也表明keepalived 已安裝成功了
安裝LVS軟體
LVS是通過IPVS子產品來實作的。IPVS是LVS叢集系統的核心軟體,主要用于完成用 戶的請求到達負載排程
系統安裝完成之後,可以通過如下指令檢查kernel是否支援LVS的IPVS子產品
[root@node4 ~]# modprobe -l | grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko
在director server 上安裝IPVS管理軟體;
這裡我們使用yum 安裝
[root@node4 ~]# yum -y install ipvsadm
搭建高可用LVS叢集
LVS叢集有DR、TUN、NAT三種配置模式,可以對WWW服務、FTP服務、MAIL 服務等進行負栽均衡。下面通過3個執行個體詳細講述如何搭建WWW服務的髙可用LVS叢集 系統,以及基于DR模式的LVS叢集配置.在進行執行個體介紹之前進行約定:作業系統采用 CentOS 6.5,位址規劃如下表所示:
節點類型 | IP位址規劃 | 主機名 | 類型 |
Master Director server | 172.16.249.17 | node4 | Public IP |
172.16.249.7 | priv1 | Private IP | |
172.16.250.250 | none | Virtual IP | |
slave Director server | 172.16.249.18 | node5 | |
172.16.249.8 | |||
Real Server 1 | 172.16.249.9 | rs1 | |
Real Server 2 | 172.16.249.10 | rs2 | |
整個高可用LVS叢集系統的拓撲圖如下圖所示:
通過heartbeat搭建LVS高可用性叢集
1.配置lvs叢集
配置LVS的方法有很多,可以通過LVS提供的ipvsadm指令進行配罝,也可以通過第 三方插件或工具來進行配置,例如通過Ldirectord來配置LVS,或者通過Redhat提供的界面工具piranha來配置等,這裡選擇通過Ldirectord來配置LVS.
(1)通過 Ldirectord 在主、備 Director Server 上配置LVS
Ldirectord是heartbeat的一個插件,Ldirectord主要用于監控叢集系統中每個服務節點的運作狀态,當某個節點的服務出現異常 或主機出現故障時,将此節點從叢集系統中剔除,并且在節點恢複正常後,重新将此節點加 入叢集系統。除了監控服務節點外,Ldirectord的另一個功能是配置LVS,隻需設定好Ldircclord 的配置檔案,啟動服務即可,Ldirectord會自動調用ipvsadm指令建立LVS路由表資訊。 Ldirectord配置檔案的預設路徑為/etc/ha.d/ldirectord.cf,這裡詳細介紹一下這個檔案中每個選項的含義.
如果使用rpm包安裝的話配置檔案會在其它的目錄;我們就是使用RPM包安裝的;
[root@node4 ha.d]# rpm -ql heartbeat-ldirectord
/etc/ha.d/resource.d/ldirectord
/etc/init.d/ldirectord
/etc/logrotate.d/ldirectord
/usr/sbin/ldirectord
/usr/share/doc/heartbeat-ldirectord-2.1.4
/usr/share/doc/heartbeat-ldirectord-2.1.4/COPYING
/usr/share/doc/heartbeat-ldirectord-2.1.4/README
/usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf
/usr/share/man/man8/ldirectord.8.gz
編輯配置檔案;并将它複制到;ha.d目錄下;
vim /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf
cp -p /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf /etc/ha.d
checktimeout=20 #判定Real Server出錯的時間間隔
checkinterval=10 #指定Ldirectord在兩次檢查之間的間隔時間
fallback=127.0.0.1:80 #當所有的Real Server節點不能工作時,Web 服務重定向的位址
autoreload=yes #是否自動重載配置檔案,選yes時,配置檔案發生變化,自動載入配置資訊
logfile="/var/log/ldirectord.log" # 設定Ldirectord日志輸出文忤路徑
#logfile="local0"
#emailalert="[email protected]"
#emailalertfreq=3600
#emailalertstatus=all
quiescent=yes # 選擇no時,如果一個節點在checktimeout設裡的時間用期内沒有響應, ldirectord将會從LVS的路由表中直接移除Real Server,此時,将中斷現有的用戶端連接配接,并使LVS丢掉所有的連接配接踉蹤記錄和持續連接配接模闆: 如果選擇yes,務某個Real Server失效時,Ldirectord将失效節點的權值設定為0,新的連接配接将不能到達,但是并不會從LVS路由表中清除此節點,同時,連接配接跟蹤記錄和桎序連接配接模闆仍然保留在Director上
注意,以上幾行為Ldirectord.cf檔案的全局設定,它們可以應用到多個虛拟主機。下面 是每個虛拟主機的配置
# Sample for an http virtual service
virtual=172.16.250.250:80 #指定虛拟的IP位址和端口号,注意,在virtual這行後面的行 .必須縮進4個空格或以一個tab字元進行标記
real=172.16.249.9:80 gate #指定Real Server股務器位址和端口,同時設定LVS工作模式,用gate表示DR模式,ipip表示TUNL模式,masq表示NAT模式
real=172.16.249.10:80 gate
fallback=127.0.0.1:80 gate
service=http #指定服務的類塑,這裡是對HTTP股務進行負栽均衡
request="index.html"# Ldirectord将根據指定的Real Server位址,結合該選項給 出的請求頁面,發送通路請求,檢查Real Server上的服務是否正常運作,必須確定這裡給出的頁面位址是可通路的,不然Ldirectord會誤認為此節點已經失效,發生錯誤監控現象
receive="Test Page" #指定請求和應答字串,也就是index.html的内容
virtualhost=some.domain.com.au
scheduler=rr #指定排程算法,這裡是rr (輪均)算法
#persistent=600
#netmask=255.255.255.255
protocol=tcp #指定協定的類型,LVS支特TCP和UDP協定
checktype=negotiate # 指定 Ldirectord 的檢測類型,checktype可以是 connect、external, negotiate, off、on, ping 和checktimeout 這幾個,預設為negotiate,通過頁面互動來判斷服務節點是否正常
checkport=80 # 指定監控端口
request="index.html"
receive="Test Page"
virtualhost=www.henhenlu.com # 虛拟伺服器的名稱,可以任意指定
配置完畢後就可以執行如下指令啟動或關閉Ldirectord服務:
/etc/rc.d/init.d/ldirectord {start|stop}
(2) Real server 的配置
在LVS的DR和TUN模式下,使用者的通路請求到達Real Server後,是直接傳回給使用者的,不再經過前端的Director Server,是以,需要在每個Real server節點上增加虛拟的VIP位址,這樣資料才能直接傳回給使用者。增加VIP位址的操作可以通過建立腳本的方式來實 現。建立檔案
vim /root/realserver.sh
腳本内容如下:
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
. /etc/rc.d/init.d/functions
VIP=172.16.250.250
host=`/bin/hostname`
case "$1" in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig lo down
/sbin/ifconfig lo up
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
if [ ! "$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
給腳本權重限,并執行
chmod 755 /etc/rc.d/init.d/functions
chmod +x realserver.sh
./realserver.sh start
2.在主、備 Director Server 上配置 heartbeat
在搭建Director Server的雙機熱備系統之前,首先需要在兩台主機上安裝heartbeat軟 件。heartbeat的安裝已經在前面介紹過,這裡不再講述,直接進入heartbeat的配置。
(1)配置 heartbeat 的主配置檔案(/etc/ha.d/ha.cf)
下面對ha.cf檔案的每個選項進行詳細介紹。
ha.cf配置檔案部分參數詳解:
autojoin none
#叢集中的節點不會自動加入
logfile /var/log/ha-log
#指名heartbaet的日志存放位置
keepalive 2
#指定心跳使用間隔時間為2秒(即每兩秒鐘在eth1上發送一次廣播)
deadtime 30
#指定備用節點在30秒内沒有收到主節點的心跳信号後,則立即接管主節點的服務資源
warntime 10
#指定心跳延遲的時間為十秒。當10秒鐘内備份節點不能接收到主節點的心跳信号時,就會往日志中寫入一個警告日志,但此時不會切換服務
initdead 120
#在某些系統上,系統啟動或重新開機之後需要經過一段時間網絡才能正常工作,該選項用于解決這種情況産生的時間間隔。取值至少為deadtime的兩倍。
udpport 694
#設定廣播通信使用的端口,694為預設使用的端口号。
baud 19200
#設定串行通信的波特率
bcast eth0
# Linux 指明心跳使用以太網廣播方式,并且是在eth0接口上進行廣播。
#mcast eth0 225.0.0.1 694 1 0
#采用網卡eth0的Udp多點傳播來組織心跳,一般在備用節點不止一台時使用。Bcast、ucast和mcast分别代表廣播、單點傳播和多點傳播,是組織心跳的三種方式,任選其一即可。
#ucast eth0 192.168.1.2
#采用網卡eth0的udp單點傳播來組織心跳,後面跟的IP位址應為雙機對方的IP位址
auto_failback on
#用來定義當主節點恢複後,是否将服務自動切回,heartbeat的兩台主機分别為主節點和備份節點。主節點在正常情況下占用資源并運作所有的服務,遇到故障時把資源交給備份節點并由備份節點運作服務。在該選項設為on的情況下,一旦主節點恢複運作,則自動擷取資源并取代備份節點,如果該選項設定為off,那麼當主節點恢複後,将變為備份節點,而原來的備份節點成為主節點
#stonith baytech /etc/ha.d/conf/stonith.baytech
# stonith的主要作用是使出現問題的節點從叢集環境中脫離,進而釋放叢集資源,避免兩個節點争用一個資源的情形發生。保證共享資料的安全性和完整性。
#watchdog /dev/watchdog
#該選項是可選配置,是通過Heartbeat來監控系統的運作狀态。使用該特性,需要在核心中載入"softdog"核心子產品,用來生成實際的裝置檔案,如果系統中沒有這個核心子產品,就需要指定此子產品,重新編譯核心。編譯完成輸入"insmod softdog"加載該子產品。然後輸入"grep misc /proc/devices"(應為10),輸入"cat /proc/misc |grep watchdog"(應為130)。最後,生成裝置檔案:"mknod /dev/watchdog c 10 130" 。即可使用此功能
node node1.magedu.com
#主節點主機名,可以通過指令“uanme –n”檢視。
node node2.magedu.com
#備用節點主機名
ping 192.168.12.237
#選擇ping的節點,ping 節點選擇的越好,HA叢集就越強壯,可以選擇固定的路由器作為ping節點,但是最好不要選擇叢集中的成員作為ping節點,ping節點僅僅用來測試網絡連接配接
ping_group group1 192.168.12.120 192.168.12.237
#類似于ping ping一組ip位址
apiauth pingd gid=haclient uid=hacluster
respawn hacluster /usr/local/ha/lib/heartbeat/pingd -m 100 -d 5s
#該選項是可選配置,列出與heartbeat一起啟動和關閉的程序,該程序一般是和heartbeat內建的插件,這些程序遇到故障可以自動重新啟動。最常用的程序是pingd,此程序用于檢測和監控網卡狀态,需要配合ping語句指定的ping node來檢測網絡的連通性。其中hacluster表示啟動pingd程序的身份。
目前環境配置:
#
# There are lots of options in this file. All you have to have is a set
# of nodes listed {"node ...} one of {serial, bcast, mcast, or ucast},
# and a value for "auto_failback".
#
# ATTENTION: As the configuration file is read line by line,
# THE ORDER OF DIRECTIVE MATTERS!
#
# In particular, make sure that the udpport, serial baud rate
# etc. are set before the heartbeat media are defined!
# debug and log file directives go into effect when they
# are encountered.
#
# All will be fine if you keep them ordered as in this example.
#
#
# Note on logging:
# If all of debugfile, logfile and logfacility are not defined,
# logging is the same as use_logd yes. In other case, they are
# respectively effective. if detering the logging to syslog,
# logfacility must be "none".
#
# File to write debug messages to
#debugfile /var/log/ha-debug
#
#
# File to write other messages to
#
logfile /var/log/ha-log
#
#
# Facility to use for syslog()/logger
#
#logfacility local0
#
#
# A note on specifying "how long" times below...
#
# The default time unit is seconds
# 10 means ten seconds
#
# You can also specify them in milliseconds
# 1500ms means 1.5 seconds
#
#
# keepalive: how long between heartbeats?
#
keepalive 1000ms
#
# deadtime: how long-to-declare-host-dead?
#
# If you set this too low you will get the problematic
# split-brain (or cluster partition) problem.
# See the FAQ for how to use warntime to tune deadtime.
#
deadtime 8
#
# warntime: how long before issuing "late heartbeat" warning?
# See the FAQ for how to use warntime to tune deadtime.
#
warntime 4
#
#
# Very first dead time (initdead)
#
# On some machines/OSes, etc. the network takes a while to come up
# and start working right after you've been rebooted. As a result
# we have a separate dead time for when things first come up.
# It should be at least twice the normal dead time.
#
#initdead 120
#
#
# What UDP port to use for bcast/ucast communication?
#
udpport 694
#
# Baud rate for serial ports...
#
#baud 19200
#
# serial serialportname ...
#serial /dev/ttyS0 # Linux
#serial /dev/cuaa0 # FreeBSD
#serial /dev/cuad0 # FreeBSD 6.x
#serial /dev/cua/a # Solaris
#
#
# What interfaces to broadcast heartbeats over?
#
bcast eth0 # Linux
#bcast eth1 eth2 # Linux
#bcast le0 # Solaris
#bcast le1 le2 # Solaris
#
# Set up a multicast heartbeat medium
# mcast [dev] [mcast group] [port] [ttl] [loop]
#
# [dev] device to send/rcv heartbeats on
# [mcast group] multicast group to join (class D multicast address
# 224.0.0.0 - 239.255.255.255)
# [port] udp port to sendto/rcvfrom (set this value to the
# same value as "udpport" above)
# [ttl] the ttl value for outbound heartbeats. this effects
# how far the multicast packet will propagate. (0-255)
# Must be greater than zero.
# [loop] toggles loopback for outbound multicast heartbeats.
# if enabled, an outbound packet will be looped back and
# received by the interface it was sent on. (0 or 1)
# Set this value to zero.
#
#
#mcast eth0 225.0.0.1 694 1 0
#
# Set up a unicast / udp heartbeat medium
# ucast [dev] [peer-ip-addr]
#
# [dev] device to send/rcv heartbeats on
# [peer-ip-addr] IP address of peer to send packets to
#
#ucast eth0 192.168.1.2
#
#
# About boolean values...
#
# Any of the following case-insensitive values will work for true:
# true, on, yes, y, 1
# Any of the following case-insensitive values will work for false:
# false, off, no, n, 0
#
#
#
# auto_failback: determines whether a resource will
# automatically fail back to its "primary" node, or remain
# on whatever node is serving it until that node fails, or
# an administrator intervenes.
#
# The possible values for auto_failback are:
# on - enable automatic failbacks
# off - disable automatic failbacks
# legacy - enable automatic failbacks in systems
# where all nodes do not yet support
# the auto_failback option.
#
# auto_failback "on" and "off" are backwards compatible with the old
# "nice_failback on" setting.
#
# See the FAQ for information on how to convert
# from "legacy" to "on" without a flash cut.
# (i.e., using a "rolling upgrade" process)
#
# The default value for auto_failback is "legacy", which
# will issue a warning at startup. So, make sure you put
# an auto_failback directive in your ha.cf file.
# (note: auto_failback can be any boolean or "legacy")
#
auto_failback on
#
#
# Basic STONITH support
# Using this directive assumes that there is one stonith
# device in the cluster. Parameters to this device are
# read from a configuration file. The format of this line is:
#
# stonith <stonith_type> <configfile>
#
# NOTE: it is up to you to maintain this file on each node in the
# cluster!
#
#stonith baytech /etc/ha.d/conf/stonith.baytech
#
# STONITH support
# You can configure multiple stonith devices using this directive.
# The format of the line is:
# stonith_host <hostfrom> <stonith_type> <params...>
# <hostfrom> is the machine the stonith device is attached
# to or * to mean it is accessible from any host.
# <stonith_type> is the type of stonith device (a list of
# supported drives is in /usr/lib/stonith.)
# <params...> are driver specific parameters. To see the
# format for a particular device, run:
# stonith -l -t <stonith_type>
#
#
# Note that if you put your stonith device access information in
# here, and you make this file publically readable, you're asking
# for a denial of service attack ;-)
#
# To get a list of supported stonith devices, run
# stonith -L
# For detailed information on which stonith devices are supported
# and their detailed configuration options, run this command:
# stonith -h
#
#stonith_host * baytech 10.0.0.3 mylogin mysecretpassword
#stonith_host ken3 rps10 /dev/ttyS1 kathy 0
#stonith_host kathy rps10 /dev/ttyS1 ken3 0
#
# Watchdog is the watchdog timer. If our own heart doesn't beat for
# a minute, then our machine will reboot.
# NOTE: If you are using the software watchdog, you very likely
# wish to load the module with the parameter "nowayout=0" or
# compile it without CONFIG_WATCHDOG_NOWAYOUT set. Otherwise even
# an orderly shutdown of heartbeat will trigger a reboot, which is
# very likely NOT what you want.
#
#watchdog /dev/watchdog
#
# Tell what machines are in the cluster
# node nodename ... -- must match uname -n
#node ken3
#node kathy
node node1.firefox.com
node node2.firefox.com
#
# Less common options...
#
# Treats 10.10.10.254 as a psuedo-cluster-member
# Used together with ipfail below...
# note: don't use a cluster node as ping node
#
ping 172.16.0.1
#
# Treats 10.10.10.254 and 10.10.10.253 as a psuedo-cluster-member
# called group1. If either 10.10.10.254 or 10.10.10.253 are up
# then group1 is up
# Used together with ipfail below...
#
#ping_group group1 10.10.10.254 10.10.10.253
#
# HBA ping derective for Fiber Channel
# Treats fc-card-name as psudo-cluster-member
# used with ipfail below ...
#
# You can obtain HBAAPI from http://hbaapi.sourceforge.net. You need
# to get the library specific to your HBA directly from the vender
# To install HBAAPI stuff, all You need to do is to compile the common
# part you obtained from the sourceforge. This will produce libHBAAPI.so
# which you need to copy to /usr/lib. You need also copy hbaapi.h to
# /usr/include.
#
# The fc-card-name is the name obtained from the hbaapitest program
# that is part of the hbaapi package. Running hbaapitest will produce
# a verbose output. One of the first line is similar to:
# Apapter number 0 is named: qlogic-qla2200-0
# Here fc-card-name is qlogic-qla2200-0.
#
#hbaping fc-card-name
#
#
# Processes started and stopped with heartbeat. Restarted unless
# they exit with rc=100
#
#respawn userid /path/name/to/run
#respawn hacluster /usr/lib/heartbeat/ipfail
#
# Access control for client api
# default is no access
#
#apiauth client-name gid=gidlist uid=uidlist
#apiauth ipfail gid=haclient uid=hacluster
###########################
#
# Unusual options.
#
###########################
#
# hopfudge maximum hop count minus number of nodes in config
#hopfudge 1
#
# deadping - dead time for ping nodes
#deadping 30
#
# hbgenmethod - Heartbeat generation number creation method
# Normally these are stored on disk and incremented as needed.
#hbgenmethod time
#
# realtime - enable/disable realtime execution (high priority, etc.)
# defaults to on
#realtime off
#
# debug - set debug level
# defaults to zero
#debug 1
#
# API Authentication - replaces the fifo-permissions-based system of the past
#
#
# You can put a uid list and/or a gid list.
# If you put both, then a process is authorized if it qualifies under either
# the uid list, or under the gid list.
#
# The groupname "default" has special meaning. If it is specified, then
# this will be used for authorizing groupless clients, and any client groups
# not otherwise specified.
#
# There is a subtle exception to this. "default" will never be used in the
# following cases (actual default auth directives noted in brackets)
# ipfail (uid=HA_CCMUSER)
# ccm (uid=HA_CCMUSER)
# ping (gid=HA_APIGROUP)
# cl_status (gid=HA_APIGROUP)
#
# This is done to avoid creating a gaping security hole and matches the most
# likely desired configuration.
#
#apiauth ipfail uid=hacluster
#apiauth ccm uid=hacluster
#apiauth cms uid=hacluster
#apiauth ping gid=haclient uid=alanr,root
#apiauth default gid=haclient
# message format in the wire, it can be classic or netstring,
# default: classic
#msgfmt classic/netstring
# Do we use logging daemon?
# If logging daemon is used, logfile/debugfile/logfacility in this file
# are not meaningful any longer. You should check the config file for logging
# daemon (the default is /etc/logd.cf)
# more infomartion can be fould in http://www.linux-ha.org/ha_2ecf_2fUseLogdDirective
# Setting use_logd to "yes" is recommended
#
# use_logd yes/no
#
# the interval we reconnect to logging daemon if the previous connection failed
# default: 60 seconds
#conn_logd_time 60
#
#
# Configure compression module
# It could be zlib or bz2, depending on whether u have the corresponding
# library in the system.
compression bz2
#
# Confiugre compression threshold
# This value determines the threshold to compress a message,
# e.g. if the threshold is 1, then any message with size greater than 1 KB
# will be compressed, the default is 2 (KB)
compression_threshold 2
crm respawn
(2)配置heartbeat的認證檔案(/etc/ha.d/authkeys)
authkeys檔案用于設定heartbeat的認證方式,該檔案有3種可用的認證方式:crc、sha1和md5,這裡使用sha1的認證方式。設定如下:使用openssl生成秘鑰;然後再将其寫入配置檔案中;
openssl rand -hex 8
80dd467590678c72
cat /etc/ha.d/authkeys
auth 2
#1 crc
2 sha1 80dd467590678c72
#3 md5 Hello!
需要要說明的一點是,無論“auth”後面指定的是什麼數字,在下一行必須作為關鍵字再 次出現,例如指定了 “auth6”,下面一定要有一行“6認證類型'
最後確定這個檔案的權限是600 (即-rw——)。
(3) 配置heartbeat的資源檔案;這裡我們使用hb_gui 配置;
# rpm -ivh heartbeat-gui-2.1.4-12.el6.x86_64.rpm
[root@node1 ~]# hb_gui &
由于heartbeat托管了主、備Director Server上的Ldirectord服務,是以隻需在主、備兩 台機器上啟動heartbeat服務即可,這樣Ldirectord服務就在主機上啟動起來了。
(2)啟動Real Server節點服務
分别在兩個Real Server節點上執行如下腳本:
[root@rsl~]#./reserver.sh
至此,通過heartbeat建構的髙可用LVS叢集系統已經配置完成并運作起來了.
5.2通過Keepalived搭建LVS高可用性叢集系統
1.配置 Keepalived
Keepalived的配置非常簡單,僅需要一個配置檔案即可完成對HA cluster和LVS服務節點監控。Keepalived的安裝已經在前面介紹過,在通過Keepalived搭建高可用的LVS叢集實 例中,主、備Director Server都需要安裝Keepalived軟體,安裝成功後,預設的配置檔案路 徑為/etc/Keepalived/Keepalived.conf. —個完整的keepalived配置檔案由3個部分組成,分 别是全局定義部分、vrrp執行個體定義部分以及虛拟伺服器定義部分.下面詳細介紹這個配置文 件中毎個選項的詳細含義和用法。
! Configuration File for keepalived
global_defs {
notification_email {
[email protected] # 設定報警郵件位址,可以設定多個
[email protected] # 每一行一個。注意如果開啟郵件報警,需要開啟本機的sendmail服務
[email protected]
}
notification_email_from [email protected] # 設定郵件的發送位址
smtp_server 192.168.200.1 # 設定stmp server的位址
smtp_connect_timeout 30 # 設定stmp server 的逾時時間
router_id LVS_DEVEL # 表示運作keepalived伺服器的一個辨別。發郵件時顯示在郵件主題中的資訊
}
# vrrp執行個體定義部分
vrrp_instance VI_1 {
state MASTER #指定Keepalived的角色,MASTER表示此主機是主伺服器, # BACKUP表示此主機是備用伺服器
interface eth0 #指定HA監測網絡的接口
virtual_router_id 51 #虛拟路由辨別,這個辨別是一個數字,—個vrrp執行個體使用唯一的辨別, #即同一個vrrp_instance下,MASTER和BACKUP必播是一緻的
priority 100 #定義優先級,數字越大,優先級趣高.在一個vrrp_instance下, #MASTER的優先級必大于BACKUP的優先級
advert_int 1 #設定MASTER與BACKUP負載均衡器之間同步檢查的時間間隔,機關是秒
authentication { #設定驗證類型和密碼
auth_type PASS #設定驗證類型,主要有PASS和AH兩種
auth_pass 1111 設定驗證密碼,在一個vrrp_instance下, #MASTER與BACKUP必用使用相同的密碼才能正常通信
}
# 虛拟伺服器定義部分
virtual_server 192.168.200.100 443 {
#設裡虛拟伺服器,需要相定虛拟IP位址和服務端口,IP與端口之間用空格隔開
delay_loop 6
#設裡運作情況檢查時間,機關是秒
lb_algo rr
#設裡負載排程算法,這裡設定為rr,即輪詢算法
lb_kind NAT
#設定LVS實作負栽均衡的機制,有NAT、TUN和DR三個模式可選
persistence_timeout 50
#會話保持時間,機關是秒.這個選項對動态網頁是非常有用的,為叢集系統中的session共享提供了一個很好的解決方案.有了這個會诂保持功能,使用者的請求會祓一直分發到某個服務節點,直到超過這個會谘的保持時間.需要注意的是, 這個會話保持時間是最大無響應逾時時間,也就是說,使用者在操作動态頁網面時,如果在50秒内沒有執行任何操作,那麼接下來的操作會被分發到 另外的節點,但是如果使用者一直在操作動态頁面,則不受50秒的時間限制 .
protocol TCP
# 指定轉發協定類型,有TCP和UDP兩種
real_server 192.168.201.100 443 {
# 置服務節點1,需要指定real server的其實IP位址和端口,IP與端口之間用空格隔開
weight 1
配置服務節點的權值,權值大小用數字表示,數字越大,權值越高,設定權值的大小可以為不同性能的伺服器配置設定不同的負載,可以為性能高的伺服器設定較高的權值.而為性能較低的伺服器設定相對較低的權值,這樣才能合埋地利用和配置設定系統資源 #realserve的狀态檢測設罝部分,機關是秒
HTTP_GET | SSL_GET{
url{ #檢查url,可以指定多個
path /
digest <string> #檢查後的摘要資訊
status_code 200 #檢查的傳回狀态碼
}
connect_port <port>
bindto <IPADD>
connect_timeout 5
#表示5秒無響應逾時
nb_get_retry 3
#表示重讀次數
delay_before_retry 2
#表示重式間隔
}
目前環境的配置:
[root@node1 /]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 172.16.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.250.250
}
}
virtual_server 172.16.250.250:80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1:80
real_server 172.16.249.10 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.249.9 80 {
weight 1
TCP_CHECK {
connect_timeout3
nb_get_retry 3
delay_before_retry 3
}
}
}
在配置Keepalived.conf時,需要特别注意配置檔案的文法格式,因為Keepalived在啟 動時并不檢測配置檔案的正确性,即使沒有配置檔案,Keepalived也照樣能夠啟動,是以一 定要保證配置檔案正确。
在預設情況下,Keepalived在啟動時會査找/etc/Keepalived/Keepalived.conf配置檔案, 如果配置檔案放在了其他路徑下,可以通過“Keepalived -f”參數指定配置檔案的路徑即可。
Keepalived.conf配置完畢後,将此檔案複制到備用Director Server對應的路徑下,然後 進行以下兩個簡單的修改即可:
□将 “state MASTER” 更改為 “state BACKUP”。
□将“priority 100”更改為一個較小的值,這裡改為“priority 80”。
2.配置Real server節點
與heartbeat+LVS類似,Keepalived+LVS也需要為Real server節點配置相關的腳本,以 達到與Director Server互相通信的目的。腳本的内容已經在前面介紹過,這裡不再講述。
3.啟動Keepalived+LVS叢集系統
在主、備Director Server上分别啟動Keepalived服務,可以執行如下操作:
/etc/rc.d/init.d/keepalived start
./reserver start
至此,keepalived+LVS 高可用的LVS叢集系統已經運作起來了。