1 LVS DR体系结构
图1 LVS DR体系结构
在LVS DR中,Director(调度器)接收到用户的请求,然后根据负载均衡算法选取一台realserver,将包转发过去,最后由realserver直接回复给用户。
2 准备工作
系统:CentOS 6.3
内核:2.6.32-279.el6.i686
需lvs dr模式需下载如下软件包:
Ø popt-1.14.tar.gz
Ø libnl-devel-1.1-14.el6.i686.rpm
Ø ipvsadm-1.26.tar.gz
Ø keepalived-1.2.7.tar.gz
3 安装LVS、keepalived
1. # yum install kernel-devel #没有内核源码的需安装kernel-devel
2. #tar zxvf popt-1.14.tar.gz
3. # cd popt-1.14
4. #./configure
5. # make && make install
6. # rpm -ivh libnl-devel-1.1-14.el6.i686.rpm
7. #lsmod |grep ip_vs
8. #uname -r
9. 2.6.32-279.el6.i686
10. #ln -s /usr/src/kernels/2.6.32-279.el6.i686/ /usr/src/linux
11.
12. #tar zxvf ipvsadm-1.26.tar.gz
13. #cd ipvsadm-1.26
14. #make && make install
15. #find / -name ipvsadm #查看ipvsadm的位置
16. /sbin/ipvsadm 17. /etc/rc.d/init.d/ipvsadm #表明成功安装 |
18. #tar zxvf keepalived-1.2.7.tar.gz
19. #cd keepalived-1.2.7
20. #./configure &&make && make install
21. #find /-name keepalived #查看keepalived位置
22. #cp/usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
23. #cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/
24. #mkdir/etc/keepalived
25. #cp/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
26. #cp/usr/local/sbin/keepalived /usr/sbin/
27. #servicekeepalived start|stop #做成系统启动服务方便管理.
4 配置高可用lVS分布式集群
4.1 配置Keepalvied
一、在主负载均衡服务器上配置keepalived.conf
#/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
yydcj@163.com
}
notification_email_from yydcj@163.com
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #备份服务器上将MASTER改为BACKUP
interface eth0 #承载VIP地址的物理接口
virtual_router_id 51 #同一个 vrrp_stance,MASTER 和 BACKUP 的 virtual_router_id 是一致的,同时在整个 vrrp 内是唯一的。
priority 100 # 备份服务上将100改为99
advert_int 1 #通告间隔秒数
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.7.4.201
#(如果有多个VIP,继续换行填写.)
}
}
virtual_server 172.7.4.201 8080 {
delay_loop 6 #(每隔6秒查询realserver状态)
lb_algo rr #(lvs 算法)
lb_kind DR #(Direct Route)
nat_mask 255.255.255.0
persistence_timeout 0 #(同一IP的连接n秒内被分配到同一台realserver,0为随机分配)
protocol TCP #(用TCP协议检查realserver状态)
real_server 172.7.4.250 8080 {
weight 1 #(权重,使用不同的权重值的目的在于为不同性能的机器分配不同的负载,
#性能较好的机器,负载分担大些;反之,性能差的机器,则分担较少的
#负载,这样就可以合理的利用不同性能的机器资源。)
TCP_CHECK {
connect_port 8080
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3 #重试次数
delay_before_retry 3 #重试间隔
}
}
}
二、 BACKUP 服务器同上配置,先安装 lvs 再按装 keepalived, 仍后配置 /etc/keepalived/keepalived.conf ,只需将红色标示的部分改一下即可 .
#/etc/init.d/keepalivedstart 启动keepalived服务,keepalived就能利用keepalived.conf配置文件,实现负载均衡和高可用。整个LVS负均衡HA方案,由keepalived.conf一个文件即可搞定!
4.2 配置Realserver脚本
# /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by : yydcj@163.com
SNS_VIP=172.7.4.201
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev lo:0
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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>&1
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 "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
启动realserver #/usr/local/sbin/realserver.sh start
停止realserver #/usr/local/sbin/realserver.shstop
4.3 查看高可用lvs服务是否正常
#watchipvsadm –ln
停掉Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管服务。