一:环境说明:
LVS-NAT-Master: 192.168.5.132
LVS-NAT-Backup: 192.168.5.129
VIP: 192.168.5.130
VIP2: 192.168.1.101
RealServer1: 192.168.1.102
RealServer1: 192.168.1.103
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-NAT,配置LVS脚本实现负载均衡
vim /etc/init.d/lvsnat
#!/bin/bash
#!/bin/bash
#LVS script NAT
VIP=192.168.5.130
RIP1=192.168.1.102
RIP2=192.168.1.103
VIP2=192.168.1.101
case "$1" in
start)
echo "1">/proc/sys/net/ipv4/ip_forward
/sbin/iptables -F
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 $VIP netmask 255.255.255.0 up
/sbin/ifconfig eth1 $VIP2 netmask 255.255.255.0 up
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -m
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -m
/sbin/ipvsadm
echo "start LVS of DirectorServer NAT"
;;
stop)
echo "0">/proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 down
echo "stop LVS of DirectorServer Nat"
;;
*)
echo "Usage:$0 {start|stop}"
exit 1
esac
chmod a+x /etc/init.d/lvsdr
service lvsdr start|stop
查看命令:
ipvsadm -ln
ipvsadm -lcn 查看请求转发情况
备注:此脚本在LVS-NAT-Master和Backup上均需执行
四:配置Realserver脚本
vim /etc/init.d/realserver
#!/bin/bash
VIP=192.168.1.101
route add default gw $VIP eth0
chmod a+x /etc/init.d/realserver
/etc/init.d/realserver
可以通route -n 查看路由情况
备注:此脚本在两台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
# 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 {
192.168.5.130
}
}
virtual_server 192.168.5.130 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
protocol TCP
real_server 192.168.1.102 80 {
weight 1
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.103 80 {
weight 1
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;