系統環境:html
centos64位系統linux
lvs-master:10.20.189.240
lvs-backup:10.20.189.241
vip:10.20.189.239
web1:10.20.189.242
web2:10.20.189.243web
首先在lvs-master:10.20.189.240算法
[root@master ~]# cd /usr/local/src
[root@master ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gzcentos
[root@master ~]#
[root@master ~]# tar -zxvf ipvsadm-1.24.tar.gzbash
[root@master ~]# cd ipvsadm-1.24服務器
[root@master ~]#
(32位系統用:ln
不加這個軟鏈接的話,在執行make的時候會出現相似以下的錯誤編譯信息:oop
libipvs.h:14:23: error: net/ip_vs.h: No such file or directory
查找下此文件
/usr/src/kernels/2.6.18-238.el5-x86_64/include/net/ip_vs.h
[root@master ~]# make && make install
[root@master ~]# cd ..
[root@master ~]# tar -zxvf keepalived-1.2.7.tar.gz
[root@master ~]# cd keepalived-1.2.7
[root@master ~]# ./configure --sysconf=/etc
configure: error:
解決:
[root@master ~]# yum -y install openssl-devel
版本1.2.7的編譯信息以下:
------------------------
Keepalived version
Compiler
Compiler flags
Extra Lib
Use IPVS Framework
IPVS sync daemon support : Yes
IPVS use libnl
Use VRRP Framework
Use VRRP VMAC
SNMP support
Use Debug flags
版本1.1.17的編譯信息以下:
Keepalived configuration
------------------------
Keepalived version
Compiler
Compiler flags
Extra Lib
Use IPVS Framework
IPVS sync daemon support : Yes
Use VRRP Framework
Use LinkWatch
Use Debug flags
[root@master ~]# make
[root@master ~]# make install
[root@master ~]#
[root@master ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_instance VI_1 {
}
virtual_server 10.20.189.239 80 {
}
[root@master ~]#
[root@master ~]# chkconfig --level 2345 keepalived on
lvs-backup對照lvs-master安裝相應軟件,注意keepalive.conf有細微區別
配置realserver
[root@web_1 ~]# yum install httpd -y
[root@web_1 ~]# /etc/init.d/httpd start
[root@web_1 ~]# vi /root/lvs_real.sh
#!/bin/bash
SNS_VIP=10.20.189.239
/etc/rc.d/init.d/functions
case "$1" in
start)
stop)
*)
esac
exit 0
[root@web_1 ~]# chmod +x /root/lvs_real.sh
[root@web_1 ~]# /root/lvs_real.sh start
[root@web_1 ~]# ifconfig
eth0
lo
lo:0
[root@web_1 ~]# echo "/root/lvs_real.sh start" >> /etc/rc.local
#LVS_master、LVS_backup上開啓keepalived,LVS_master先綁定VIP
LVS_master:
[root@localhost keepalived]# ip add
1: lo: mtu 16436 qdisc noqueue
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
LVS_backup:
[root@wpstest-9d9b39c keepalived]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
#解析域名,測試訪問,LVS轉發
#測試關閉LVS_master,短暫的掉包後,LVS_backup立刻接替工做
/etc/init.d/keepalived stop
LVS_backup接替LVS_master綁定VIP
[root@wpstest-9d9b39c keepalived]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
LVS_master重啓完成後,就會自動接回控制權,繼續負責轉發
#測試關閉其中一臺realserver
經過上面測試能夠知道,當realserver故障或者沒法提供服務時,負載均衡器經過健康檢查自動把失效的機器從轉發隊列刪除掉,實現故障隔離,保證用戶的訪問不受影響
#重啓被關閉的realserver
當realserver故障恢復後,負載均衡器經過健康檢查自動把恢復後的機器添加到轉發隊列中