LVS Keepalived構建高可用負載均衡(測試篇)

NO IMAGE

一、 啟動LVS高可用叢集服務

首先,啟動每個real server節點的服務:
[[email protected] ~]# /etc/init.d/lvsrs start
start LVS of REALServer
然後,分別在主備Director Server啟動Keepalived服務:
[root[email protected] ~]#/etc/init.d/Keepalived start
[[email protected] ~]#/ ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP bogon:http rr
-> real-server1:http Route 1 1 0
-> real-server2:http Route 1 1 0
此時檢視Keepalived服務的系統日誌資訊如下:
[[email protected] ~]# tail -f /var/log/messages
Feb 28 10:01:56 localhost Keepalived: Starting Keepalived v1.1.19 (02/27,2011)
Feb 28 10:01:56 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.12.25 added
Feb 28 10:01:56 localhost Keepalived_healthcheckers: Opening file ‘/etc/keepalived/keepalived.conf’.
Feb 28 10:01:56 localhost Keepalived_healthcheckers: Configuration is using : 12063 Bytes
Feb 28 10:01:56 localhost Keepalived: Starting Healthcheck child process, pid=4623
Feb 28 10:01:56 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.12.25 added
Feb 28 10:01:56 localhost Keepalived: Starting VRRP child process, pid=4624
Feb 28 10:01:56 localhost Keepalived_healthcheckers: Activating healtchecker for service [192.168.12.246:80]
Feb 28 10:01:56 localhost Keepalived_vrrp: Opening file ‘/etc/keepalived/keepalived.conf’.
Feb 28 10:01:56 localhost Keepalived_healthcheckers: Activating healtchecker for service [192.168.12.237:80]
Feb 28 10:01:57 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb 28 10:01:58 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Feb 28 10:01:58 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Feb 28 10:01:58 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.12.135 added
Feb 28 10:01:58 localhost avahi-daemon[2778]: Registering new address record for 192.168.12.135 on eth0.

二、 高可用性功能測試

高可用性是通過LVS的兩個Director Server完成的,為了模擬故障,我們先將主Director Server上面的Keepalived服務停止,然後觀察備用Director Server上Keepalived的執行日誌,資訊如下:
Feb 28 10:08:52 lvs-backup Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb 28 10:08:54 lvs-backup Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Feb 28 10:08:54 lvs-backup Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Feb 28 10:08:54 lvs-backup Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.12.135
Feb 28 10:08:54 lvs-backup Keepalived_vrrp: Netlink reflector reports IP 192.168.12.135 added
Feb 28 10:08:54 lvs-backup Keepalived_healthcheckers: Netlink reflector reports IP 192.168.12.135 added
Feb 28 10:08:54 lvs-backup avahi-daemon[3349]: Registering new address record for 192.168.12.135 on eth0.
Feb 28 10:08:59 lvs-backup Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.12.135
從日誌中可以看出,主機出現故障後,備機立刻檢測到,此時備機變為MASTER角色,並且接管了主機的虛擬IP資源,最後將虛擬IP繫結在eth0裝置上。
接著,重新啟動主Director Server上的Keepalived服務,繼續觀察備用Director Server的日誌狀態:
備用Director Server的日誌狀態:
Feb 28 10:12:11 lvs-backup Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Feb 28 10:12:11 lvs-backup Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Feb 28 10:12:11 lvs-backup Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Feb 28 10:12:11 lvs-backup Keepalived_vrrp: Netlink reflector reports IP 192.168.12.135 removed
Feb 28 10:12:11 lvs-backup Keepalived_healthcheckers: Netlink reflector reports IP 192.168.12.135 removed
Feb 28 10:12:11 lvs-backup avahi-daemon[3349]: Withdrawing address record for 192.168.12.135 on eth0.
從日誌可知,備機在檢測到主機重新恢復正常後,重新返回BACKUP角色,並且釋放了虛擬IP資源。

三、 負載均衡測試

這裡假定兩個real server節點配置www服務的網頁檔案根目錄均為/webdata/www目錄,然後分別執行如下操作:
在real server1 執行:
echo “This is real server1” /webdata/www/index.html
在real server2 執行:
echo “This is real server2” /webdata/www/index.html
接著開啟瀏覽器,訪問http://192.168.12.135這個地址,然後不斷重新整理此頁面,如果能分別看到“This is real server1”和“This is real server2”就表明LVS已經在進行負載均衡了。

四、 故障切換測試

故障切換是測試當某個節點出現故障後,Keepalived監控模組是否能及時發現,然後遮蔽故障節點,同時將服務轉移到正常節點來執行。
這裡我們將real server 1節點服務停掉,假定這個節點出現故障,然後檢視主、備機日誌資訊,相關日誌如下:
Feb 28 10:14:12 localhost Keepalived_healthcheckers: TCP connection to [192.168.12.246:80] failed !!!
Feb 28 10:14:12 localhost Keepalived_healthcheckers: Removing service [192.168.12.246:80] from VS [192.168.12.135:80]
Feb 28 10:14:12 localhost Keepalived_healthcheckers: Remote SMTP server [192.168.12.1:25] connected.
Feb 28 10:14:12 localhost Keepalived_healthcheckers: SMTP alert successfully sent.
通過日誌可以看出,Keepalived監控模組檢測到192.168.12.246這臺主機出現故障後,將此節點從叢集系統中剔除掉了。
此時訪問http://192.168.12.135這個地址,應該只能看到“This is real server2”了,這是因為節點1出現故障,而Keepalived監控模組將節點1從叢集系統中剔除了。
下面重新啟動real server 1節點的服務,可以看到Keepalived日誌資訊如下:
Feb 28 10:15:48 localhost Keepalived_healthcheckers: TCP connection to [192.168.12.246:80] success.
Feb 28 10:15:48 localhost Keepalived_healthcheckers: Adding service [192.168.12.246:80] to VS [192.168.12.135:80]
Feb 28 10:15:48 localhost Keepalived_healthcheckers: Remote SMTP server [192.168.12.1:25] connected.
Feb 28 10:15:48 localhost Keepalived_healthcheckers: SMTP alert successfully sent.
從日誌可知,Keepalived監控模組檢測到192.168.12.246這臺主機恢復正常後,又將此節點加入了叢集系統中。
此時再次訪問http://192.168.12.135這個地址,然後不斷重新整理此頁面,應該又能分別看到“This is real server1”和“This is real server2”頁面了,這說明在real server 1節點恢復正常後,Keepalived監控模組將此節點加入了叢集系統中。

本文出自 “技術成就夢想” 部落格

您可能感興趣的文章:

Keepalived HAProxy實現MySQL高可用負載均衡的配置LVS Keepalived構建高可用負載均衡配置方法(配置篇)haproxy keepalived實現高可用負載均衡(例項配置)haproxy keepalived實現高可用負載均衡(理論篇)keepalived實現nginx高可用