博客已经搬家,请访问如下地址:http://www.czhphp.com
一:环境说明:
LVS-DR-Master: 10.3.0.82
LVS-DR-Backup: 10.3.0.70
VIP: 10.3.0.60
RealServer1: 10.3.0.69
RealServer1: 10.3.0.83
Centos:5.7
二:安装LVS和Keepalvied软件包
1. 下载相关软件包
mkdir /usr/local/src/lvs
cd /usr/local/src/lvs
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
2. 安装LVS和Keepalived
lsmod |grep ip_vs
uname -r
2.6.18-53.el5PAE
ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/ /usr/src/linux
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make && make install
find / -name ipvsadm # 查看ipvsadm的位置
或者使用yum install ipvsadm
tar zxvf keepalived-1.1.15.tar.gz
cd keepalived-1.1.15
./configure && make && make install
find / -name keepalived # 查看keepalived位置
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
service keepalived start|stop #做成系统启动服务方便管理.
备注:在安装keepalived的过程中,第一次的版本太高我下载1.2的版本,
以上是我的kernel版本,在安装的过程中总报错,网上也没有找到相关的说明,所以就下载了个低版本的结果就ok了。
三:LVS-DR,配置LVS脚本实现负载均衡
vim /etc/init.d/lvsdr
#!/bin/bash
RIP1=10.3.0.69
RIP2=10.3.0.83
VIP=10.3.0.60
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of DirectorServer"
# set the Virtual IP Address and sysctl parameter
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:0
echo "1" >/proc/sys/net/ipv4/ip_forward
#Clear IPVS table
/sbin/ipvsadm -C
#set LVS
#Web Apache
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
#Run LVS
/sbin/ipvsadm
;;
stop)
echo "close LVS Directorserver"
echo "0" >/proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 down
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
chmod a+x /etc/init.d/lvsdr
service lvsdr start|stop
查看命令:
ipvsadm -ln
ipvsadm -lcn 查看请求转发情况
备注:此脚本在LVS-DR-Master和Backup上均需执行
四:配置Realserver脚本
vim /etc/init.d/realserver
#!/bin/bash
VIP=10.3.0.60
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 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
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
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
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
chmod a+x /etc/init.d/realserver
service realserver start|stop
可以通过ifconfig或者ip add 命令查看是否绑定
备注:此脚本在两台realserver上均须执行。
五:利用Keepalvied实现负载均衡和和高可用性
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
caozhenhuamail@gmail.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 100 #备份服务器将100改为98
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.3.0.60
}
}
virtual_server 10.3.0.60 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 10.3.0.69 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.3.0.83 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
service keepalived start|stop
备注:BACKUP服务器同上配置,只需将红色标示的部分改一下即可.
六:测试
1 realserver其中一台服务停止:
我用的是两台nginx服务器
ps -aux |grep nginx #查找nginx主线程pid
kill -QUIT 4195
观察DR情况:
10.3.0.83服务器已经被清除了,证明ok;
2. 把刚才停掉得83的nginx启动
继续观察DR情况:
83已经成功被加入到realserver组
3:停掉master的keepalived看是否backup能接管:
访问10.3.0.60 观察backup的DR情况:
发现backup的DR接收到了客户端发出得请求,而且成功转发,也可以通过日志信息查看
tail -f /var/log/message
4:我们把master的keepalived重新启动,观察客户端请求是否会重新被master接收并且转发:
visit: http://10.3.0.60 观察
master:
请求成功接收并且转发
backup 日志:
tail -f /var/log/message
备份服务器日志说明它已经进入备份状态,而且也没有接收到客户端发来的请求;
七: 以上是自己操作的流程,有什么问题请大家给予指正,对了,还有点要说明的是如果你的机器的防火墙都开得话,试验是不可能成功的,除非你对防火墙的过滤规则设置的正确,因为这次主要是测试负载均衡,所以我直接把所有的机器的iptables都停掉了,service iptables stop;