本文参考了linuxtone论坛上NetSeek的文章,再结合实际应用,整理出来的,已在实际使用。
LVS+Keepalived 介绍
LVS
LVS是Linux Virtual 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-master:192.168.0.152
lvs-backup:192.168.0.153
vip:192.168.0.200
web1:192.168.0.111
web2:192.168.0.140
netmask:255.255.255.0
gateway:192.168.0.253
三. 安装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/**************/ /usr/src/linux
#tar zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#make && make install
#find / -name ipvsadm # 查看ipvsadm的位置
注意:在lvs-master和lvs-backup上安装ipvsadm,由于lvs已被继承在linux内核中,所以只需安装ipvsadm就可以了。不建议用源码make;make install 安装,会有莫名其妙的错误报出。直接:
#yum install ipvsadm
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure && make && make install
注:在执行./configure会提示没有安装openssl和popt,直接安装就可以:
#yum -y install openssl* popt*
#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 #做成系统启动服务方便管理.
在realserver上配置脚本:
#vi /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
VIP=192.168.0.200
. /etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ ifconfig lo:0 $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
- sysctl -p >/dev/null 2>&1
- echo "RealServer Start OK"
;;
stop)
/sbin/ ifconfig lo:0 down
- 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上执行./realserver.sh start | stop 来启动和停止
- 虚拟ip地址的广播地址是它本身,子网掩码是255.255.255.255。 为什么要这样呢?因为有若干机器要使用同一个ip地址,用本身做广播地址和把子网掩码设成4个255就不会造成ip地址冲突了,否则lvs将不能正常转发访问请求。
- 利用Keepalvied实现负载均衡和和高可用性
1.配置在主负载均衡服务器上配置keepalived.conf
#vi /etc/keepalived/keepalived.conf - ! Configuration File for keepalived
global_defs {
- notification_email {
- 530414084@qq.com
- }
- # notification_email_from sns-lvs@gmail.com
- smtp_server 127.0.0.1
- # smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- # VIP1
- vrrp_instance VI_1 {
- state MASTER #备份服务器上将MASTER改为BACKUP
- interface eth0
- virtual_router_id 51
- priority 100 # 备份服务上将100改为99
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.0.200
- #(如果有多个VIP,继续换行填写.)
- }
- }
virtual_server 192.168.0.200 80 {
- delay_loop 6 #(每隔6秒查询realserver状态)
- lb_algo wrr #(lvs 算法)
- lb_kind DR #(Direct Route)
- persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
- protocol TCP #(用TCP协议检查realserver状态)
-
real_server 192.168.0.140 80 {
- weight 3 #(权重)
- TCP_CHECK {
- connect_timeout 10 #(10秒无响应超时)
- nb_get_retry 3
- delay_before_retry 3
- connect_port 80
- }
- }
- real_server 192.168.0.111 80 {
- weight 3
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 80
- }
- }
- }
- BACKUP服务器同上配置,先安装lvs再按装keepalived,然后配置/etc/keepalived/keepalived.conf,只需上面标示的部分改一下即可.#/etc/init.d/keepalived start 启动keepalived 服务,keepalived就能利用keepalived.conf 配置文件,实现负载均衡和高可用.
- 查看lvs服务是否正常
#watch ipvsadm –ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort Forward Weight ActiveConn InActConn
TCP 192.168.0.200:80 wrr persistent 60
-> 192.168.0.140 :80 Route 3 0 0
-> 192.168.0.111:80 Route 3 0 0
- #tail –f /var/log/message 监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。
5.停Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管服务。
- 这样,当用户访问192.168.0.200的80端口的时候,就会被随机分配到realserver其中的一台上面,当realserver其中一台宕机的话,LVS会自动把宕机的机器提出服务器群集,当这台宕机的服务器恢复正常的时候,LVS会自动把它加入服务器群集中。
如果LVS-MASTER宕机的话,,LVS- BACKUP就会自动接管LVS-MASTER上运行的服务,当LVS-MASTER恢复正常的时候,LVS- BACKUP就会把运行的服务交给LVS-MASTER来运行。