lvs 集群配置


实验环境
DR_MASTER  192.168.0.130
DR_BACKUP  192.168.0.131
DR_VIP      192.168.0.138
WEBONE     192.168.0.132
WEBTWO    192.168.0.133
GETEWAY    192.168.0.1
1、    下载相关软件:
http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
2、    安装lvs和keepalived
modproble –l | grep ip_vs 看内核是否支持lvs
ln –s /usr/src/kernels/$(uname -r)/ /usr/src/linux
tar –zxvf ipvsadm-1.24.tar.gz
make
make install
tar –zxvf keepalived-1.1.15.tar.gz
./configure  –sysconf=/etc/(可选) 
--with-kernel-dir=/usr/src/kernels/`uname -r` (配置lvs需要编译的选项)
make
make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d
cp /usr/local/etc/sysconfgi/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


3、配置lvs 实现负载均衡
lvsconf.sh
#/bin/bash
#LVS-DR 配置脚本
GATEWAY=192.168.0.1
DR_VIP=192.168.0.138
WEBONE=192.168.0.132
WEBTWO=192.168.0.133
case "$1" in
start)
        echo "Start lvs of director server"
        /sbin/iptables -F
        /sbin/iptables -Z
        /sbin/ifconfig eth0:0 $DR_VIP boradcast $DR_VIP netmask 255.255.255.255 up
        /sbin/route add -host $DR_VIP dev eht0:0
        echo "1" >/proc/sys/net/ipv4/ip_forward
        /sbin/ipvsadm -A -t $DR_VIP:80  -s rr
        /sbin/ipvsadm -a -t $DR_VIP:80 -r $WEBONE -g -w 1
        /sbin/ipvsadm -a -t $DR_VIP:80 -r $WEBTWO -g -w 1
        touch /var/lock/subsys/ipvsadm>/dev/null 2>&1
#run lvs
        /sbin/ipvsadm
        ;;
stop)
        echo "Stop lvs director server"
        echo "0" >/proc/sys/net/ipv4/ip_forward
        /sbin/ifconfig eth0:0 down
        rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
        ;;
status)
        if [ ! -e /var/lock/subsys/ipvsadm ]; then
        echo "ipvsadm is stop"
        exit 1
        else
        /sbin/ipvsadm -ln
        echo "ipvsadm is ok"
        fi
        ;;
*)
        echo "Usage :$0{start|stop|status}"
        exit 1
esac
exit 0


4、配置真实服务器脚本
realconf.sh
#!/bin/bash
#description:config realserver lo and applu noarp
DR_VIP=192.168.0.138
#./etc/rc.d/init.d/functions
case "$1" in
start)
        ifconfig lo:0 $DR_VIP netmask 255.255.255.255 broadcast $DR_VIP up
        /sbin/route add -host $DR_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
        touch /var/nginxtesting
        sysctl -p >/dev/null 2>&1
        echo "Realserver start ok"
        ;;
stop)
        ifconfig lo:0 down
        route del $DR_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
        rm -rf /var/nginxtesting
        echo "Realserver stoped"
        ;;
status)
        if [ -e /var/nginxtesting ]; then
        echo "Realserver is starting"
        else
        echo "Realserver is stopped"
        fi
        ;;
*)
        echo "Usage:$0{start|stop|status}"
        esac
exit 0

     

你可能感兴趣的:(LVS)