一、簡介
VS/NAT原理圖:
二、系統環境
實驗拓撲:
系統平台:CentOS 6.3
Kernel:2.6.32-279.el6.i686
LVS版本:ipvsadm-1.26
keepalived版本:keepalived-1.2.4
三、安裝
0、安裝LVS前系統需要安裝popt-static,kernel-devel,make,gcc,openssl-devel,lftp,libnl*,popt*
1、在兩台Director Server上分别配置LVS+Keepalived
LVS install -------------
[root@CentOS-LVS_MASTER ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
[root@CentOS-LVS_MASTER ~]# ln -s /usr/src/kernels/2.6.32-279.el6.i686//usr/src/linux/
[root@CentOS-LVS_MASTER ~]# tar zxvf ipvsadm-1.26.tar.gz
[root@CentOS-LVS_MASTER ~]# cd ipvsadm-1.26
[root@CentOS-LVS_MASTER ipvsadm-1.26]# make && make install
Keepalived install -------------
[root@CentOS-LVS_MASTER ~]# wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
[root@CentOS-LVS_MASTER ~]# tar zxvf keepalived-1.2.4.tar.gz
[root@CentOS-LVS_MASTER ~]# cd keepalived-1.2.4
[root@CentOS-LVS_MASTER keepalived-1.2.4]# ./configure && make && make install
######### 将keepalived做成啟動服務,友善管理##########
[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@CentOS-LVS_MASTER ~]# mkdir /etc/keepalived/
[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@CentOS-LVS_MASTER ~]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@CentOS-LVS_MASTER ~]# service keepalived start | stop
2、開啟路由轉發
[root@CentOS-LVS_MASTER ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@CentOS-LVS_MASTER ~]# sysctl -p
3、配置Keepalived
[root@CentOS-LVS_MASTER ~]# less /etc/keepalived/keepalived.conf
BACKUP伺服器同上配置,先安裝lvs再安裝keepalived,然後配置/etc/keepalived/keepalived.conf,隻需将批注部分改一下即可。
4、分别在2台Real Server上面設定網關
把網關都設定成:192.168.10.10
5、在2台RealServer中分别配置HTTP
[root@WEB1 ~]# yum -y install httpd
[root@WEB1 ~]# cd /var/www/html/
[root@WEB1 html]# cat index.html
<h1>WEB1/192.168.10.4</h1>
[root@WEB1 html]# /etc/init.d/httpd start
另一台機器配置一樣,過程略。
6、分别在CentOS-LVS_MASTER、CentOS-LVS_BACKUP上執行service keepalived start啟動keepalived就可實作負載均衡及高可用叢集了;
[root@CentOS-LVS_MASTER keepalived]# service keepalived start
四、測試
####高可用性測試####
模拟故障,将CentOS-LVS_MASTER上的keepalived服務停掉,然後觀察CentOS-LVS_BACKUP上的日志,資訊如下
從日志中可知,主機出現故障後,備機立刻檢測到,此時備機變為MASTER角色,并且接管了主機的虛拟IP資源,最後将虛拟IP綁定在etho裝置上。
将CentOS-LVS_MASTER 上的keepalived服務開啟後,CentOS-LVS_BACKUP的日志狀态。
從日志可知,備機在檢測到主機重新恢複正常後,釋放了虛拟IP資源重新成為BACKUP角色
####故障切換測試####
故障切換是測試當某個節點出現故障後,Keepalived監制子產品是否能及時發現然後屏蔽故障節點,同時将伺服器轉移到正常節點來執行。
将WEB2節點服務停掉,假設這個節點出現故障,然後主、備機日志資訊如下
從以上可以看出,Keepalived監控子產品檢測到192.168.10.5這台主機出現故障後,将WEB2從叢集系統中剔除掉了。 此時通路http://10.0.0.227隻能看到WEB1了)
重新啟動WEB2節點的服務,日志資訊如下:
Keepalived監控子產品檢測到192.168.10.5這台主機恢複正常後,又将此節點加入叢集系統中,再次通路就可以通路到WEB2頁面了)
本文轉自 dengaosky 51CTO部落格,原文連結:http://blog.51cto.com/dengaosky/1874394,如需轉載請自行聯系原作者