LVS+Keepalived介绍
LVS
LVS是LinuxVirtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);十种调度算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。
Keepalvied
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
IP配置信息:
LVS-DR-Master master.director 192.168.88.147
LVS-DR-BACKUP slave.director 192.168.88.148
LVS-DR-VIP lo:0 192.168.88.100
WEB1-Realserver webo.davy 192.168.88.134
WEB2-Realserver web0.davy 192.168.88.138
GateWay 192.168.88.253
主从director安装LVS和Keepalvied软件包
1. 下载相关软件包
#mkdir/usr/local/src/lvs
#cd/usr/local/src/lvs
#wgethttp://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
#wgethttp://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
#tarzxvf ipvsadm-1.24.tar.gz
#cdipvsadm-1.24
#make&& make install
#find /-name ipvsadm # 查看ipvsadm的位置
#tarzxvf keepalived-1.1.15.tar.gz
#cdkeepalived-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/
#servicekeepalived start|stop #做成系统启动服务方便管理.
3.在主负载均衡服务器上配置keepalived.conf
主调度器
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from
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.88.100
}
}
virtual_server 192.168.88.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.88.134 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.88.138 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
从服务器
#vi/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email{
}
notification_email_from
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.170
}
}
virtual_server 192.168.2.17080 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.2.171 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.2.17280 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
#/etc/init.d/keepalived start 启动keepalived 服务,keepalived就能利用keepalived.conf配置文件,实现负载均衡和高可用.
配置Realserver脚本.
#!/bin/bash
# Written by NetSeek
# description: Config realserver lo and applynoarp
WEB_VIP=192.168.88.100
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $WEB_VIP netmask255.255.255.255 broadcast $WEB_VIP
/sbin/route add -host $WEB_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 $WEB_VIP >/dev/null2>&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"
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep$WEB_VIP`
isrothere=`netstat -rn | grep"lo:0" | grep $web_VIP`
if [ ! "$islothere" -o !"isrothere" ];then
# Either the route or the lo:0device
# not found.
echo "LVS-DR real serverStopped."
else
echo "LVS-DR Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0{start|status|stop}"
exit 1
;;
esac
exit 0
赋给权限 chmod 755realserver.sh
附上realserver机上的/etc/sysctl.conf:
# Kernel sysctl configuration file for Red HatLinux
#
# For binary values, 0 is disabled, 1 isenabled. See sysctl(8) and
# sysctl.conf(5) for more details.
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debuggingfunctionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append thePID to the core filename.
# Useful for debugging multi-threadedapplications.
kernel.core_uses_pid = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
或者采用secondary ipaddress方式配置
# vi/etc/sysctl.conf
添加以下内容如上所示:
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
#sysctl –p
#ip addr add 61.164.122.8/32 dev lo
#ip add list 查看是否绑定
启动realserver启本,在DR上可以查看LVS当前状态:
查看lvs服务是否正常
#watch ipvsadm –ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort Forward Weight ActiveConn InActConn
TCP 61.164.122.8:80 wrr persistent 60
-> 61.164.122.10:80 Route 3 0 0
-> 61.164.122.9:80 Route 3 0 0
#tail –f/var/log/message 监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。