***********************************************************************************
LVS的安装源:ipvsadm
LVS依赖包:popt、popt-devel、popt-static、libnl-devel、libnl
popt-static:popt-static
***********************************************************************************
1.REAL SERVER配置脚本
使用该脚本时,在下面路径新建脚本,根据情况修改变量RS_VIP为实际的VIP。
/usr/local/sbin/realserver.sh
使用命令realserver.sh start|stop来进行脚本的启停。
#!/bin/bash # description: Config realserver lo and apply noarp # Written by :Liaojizhou RS_VIP=172.24.0.29 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $RS_VIP netmask 255.255.255.255 broadcast $RS_VIP /sbin/route add -host $RS_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 $RS_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
执行下面命令,开机自动启动
echo "/usr/local/sbin/realserver.sh start" >> /etc/rc.local
2.DR模式的load balancer配置脚本
注意:如果使用了keepalived,请跳过这一段,直接看第三步。
使用该脚本时,在下面路径新建脚本,根据情况修改变量RS_VIP为实际的VIP。
/usr/local/sbin/lvs-dr.sh
DR_VIP:提供给客户端访问的VIP
RIP:后端真实服务器(RS)的IP,如果有多台,需对应添加,并且在后面的转发规则中增加对该RS的转发规则
如果要转发多个端口,需参考脚本中80和443端口转发的格式,增加相应规则
#!/bin/bash # description: start LVS of DirectorServer # Written by :Liaojizhou # website director vip. DR_VIP=172.24.254.23 RIP1=172.24.254.18 RIP2=172.24.254.19 sh /etc/rc.d/init.d/functions logger $0 called with $1 case "$1" in start) # set vip /sbin/ipvsadm --set 30 5 60 /sbin/ifconfig bond0:0 $DR_VIP broadcast $DR_VIP netmask 255.255.255.255 broadcast $DR_VIP up /sbin/route add -host $DR_VIP dev bond0:0 /sbin/ipvsadm -A -t $DR_VIP:80 -s wrr -p 3 /sbin/ipvsadm -a -t $DR_VIP:80 -r $RIP1:80 -g -w 1 /sbin/ipvsadm -a -t $DR_VIP:80 -r $RIP2:80 -g -w 1 /sbin/ipvsadm -A -t $DR_VIP:443 -s wrr -p 3 /sbin/ipvsadm -a -t $DR_VIP:443 -r $RIP1:443 -g -w 1 /sbin/ipvsadm -a -t $DR_VIP:443 -r $RIP2:443 -g -w 1 touch /var/lock/subsys/ipvsadm >/dev/null 2>&1 echo "ipvsadm started" ;; stop) /sbin/ipvsadm -C /sbin/ipvsadm -Z ifconfig bond0:0 down route del $DR_VIP rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1 echo "ipvsadm stoped" ;; status) if [ ! -e /var/lock/subsys/ipvsadm ];then echo "ipvsadm stoped" exit 1 else echo "ipvsadm OK" fi ;; *) echo "Usage: $0 {start|stop|status}" exit 1 esac exit 0
3.LVS+KEEPALIVED配置脚本
使用了keepalived之后,就不用使用上面的lvs-dr.sh脚本了,而是通过keepalived来启动ipvsadm程序
keepalived的安装包见附件:keepalived安装包
cd keepalived
./configure --sysconf=/etc --with-kernel-dir=/usr/src/linux ; make&& make install ; ln -s /usr/local/sbin/keepalived /sbin/keepalived
该服务的控制可以使用service keepalived {status,start,stop}来操作
chkconfig keepalived on
配置文件在此:/etc/keepalived/keepalived.conf
注意:复制下面脚本的时候请把中文注释全部删去,否则会出现乱码,导致vi编辑时自动跳出,后续维护极为不便!!!
! Configuration File for keepalived global_defs { smtp_server 127.0.0.1 # smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER #备份服务器上将MASTER改为BACKUP interface bond0 virtual_router_id 51 priority 100 # 备份服务上将100改为99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.235 #VIP } } virtual_server 192.168.1.235 80 { delay_loop 6 #(每隔6秒查询realserver状态) lb_algo wlc #(lvs 算法) lb_kind DR #(Direct Route) persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver) protocol TCP #(用TCP协议检查realserver状态) real_server 192.168.1.231 80 { weight 3 #(权重) TCP_CHECK { connect_timeout 10 #(10秒无响应超时) nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.1.232 80 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
更多参考:http://www.keepalived.org/pdf/sery-lvs-cluster.pdf