#LVS+keepalived 文档

VIP:192.168.95.135

机器1:192.168.95.130 既是direct server 也是realserver (安装软件:ipvsadm keepalived nginx)

机器2:1192.168.95.131  为realserver (安装软件:nginx)


机器1:

1. 安装ipvsadm:

cd /opt/soft

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

tar -zxvf  ipvsadm-1.24.tar.gz

cd ipvsadm-1.24

make &&make install


以下对安装做一些验证:

1.先执行ipvsadm命令

2.lsmod |grep ip_vs

ip_vs_wlc                963  1

ip_vs                  91193  3 ip_vs_wlc

ipv6                  264702  15 ip_vs,ip6t_REJECT,nf_conntrack_ipv6


######################FAQ###################################

编译时候报 make[1]: *** [libipvs.o] Error 1 错误

解决方法:

#安装前要建立一个连接文件,否则会出错

ln -s /usr/src/kernels/2.6.9-42.EL-i686/ /usr/src/linux 一定要与当前的运行的内核相一致

#但是有时你找不到这个内核的路径,网上很多都是以上的解决方法连接,可有的系统安装完后并没有kernerls的目录

#解决方法:yum install kernel-devel、

############################################################


2. 安装keepalived

echo "1" > /proc/sys/net/ipv4/ip_forward

#install keepalived

yum -y install openssl-devel

cd /opt/soft

wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

tar zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure --prefix=/usr/local/keepalived

make

make install

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

mkdir /etc/keepalived

cd /etc/keepalived/


3. 配置keepalived

#guration File for keepalived

#global define

global_defs {

   router_id LVS_DEVEL

}


vrrp_sync_group VGM {

 group {

    VI_1

       }

}


vrrp_instance VI_1 {

state MASTER

interface eth1

virtual_router_id 51

priority 180

advert_int 5

authentication {

auth_type PASS

auth_pass 1111

}


virtual_ipaddress {

 192.168.95.135

  }

}


# setting port 80 forward

virtual_server 192.168.95.135 80 {

delay_loop 6

lb_algo wlc

lb_kind DR

#persistence_timeout 20

protocol TCP


real_server 192.168.95.130 80 {

 weight 100

 TCP_CHECK {

 connect_timeout 3

 nb_get_retry 3

 delay_before_retry 3

 connect_port 80

 }

  }


 real_server 192.168.95.131 80 {

  weight 100

  TCP_CHECK {

  connect_timeout 3

  nb_get_retry 3

  delay_before_retry 3

  connect_port 80

  }

 }

}


4. 编写lvs客户端配置脚本

[root@centos01 nginx]# cat /data/shell/lvs_real

#!/bin/bash

#description : start realserver

VIP=192.168.95.135

/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


机器2:

只需配置lvs客户端脚本

[root@centos01 nginx]# cat /data/shell/lvs_real

#!/bin/bash

#description : start realserver

VIP=192.168.95.135

/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


结果测试:

在机器1上: service keepalived start

两台机器上执行上述的lvs_real start

机器1上可看到lvs正常运行:

[root@centos01 nginx]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

 -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  swf.98poker.com:http wlc

 -> 192.168.95.131:http          Route   100    0          0        

 -> 192.168.95.130:http          Local   100    0          0  

执行ip addr 可看到vip已在eth1上


关闭机器2的nginx,可看到此时机器2已从realserver中剔除:

[root@centos01 nginx]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

 -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  swf.98poker.com:http wlc

 -> 192.168.95.130:http          Local   100    0          0  

开启机器2的nginx服务,机器2又自动加入到realserver中。


此外,机器2也可像机器一样,既是direct server 又是realserver,作为机器1的backup,具体配置跟机器1差不多(global_defs的router_id,修改state为BACKUP,priority减小)


总结:只需在负载均衡机器上安装ipvsadm和keepalived,并执行keepalived启动。在real server机器上仅需开启本地回环地址(脚本)即可,不用安装任何软件。如果既是负载均衡机器有事real server就都安装。