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 | Public IP |
172.16.249.8 | priv1 | Private IP | |
Real Server 1 | 172.16.249.9 | rs1 | Public IP |
172.16.250.250 | none | Virtual IP | |
Real Server 2 | 172.16.249.10 | rs2 | Public IP |
172.16.250.250 | none | Virtual IP |
整個高可用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集羣系統已經運行起來了。