<--目录-->

1)安装LVS和Keepalived

2)lvs脚文内容

3)keepalived配置文件内容



【安装LVS和Keepalived】

#安装lvs工具

yum -y install ipvsadm


#安装keepalived

yum -y install gcc kernel-devel openssl-devel popt-devel

tar zxf keepalived-1.2.13.tar.gz

cd keepalived-1.2.13

./configure

make && make install

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/

chkconfig --add keepalived

chkconfig keepalived on


#更换keepalived文件

#lvs 更换keepalived配置文件即可,换配置文件后还需更改虚拟IP


#更换web服务器文件(法1)

#在两台需要负载均衡的web服务器更换sysctl.conf,添加ifcfg-lo:0,添加ifcfg-lo:0后还需更改虚拟IP,或直接添加lvs这个脚本亦可


#更换web服务器文件(法2)

#1)在两台需要负载均衡的web服务器/opt目录下直接添加lvs这个脚本即可

#2)设置开机启动

tail -1 /etc/rc.local

/opt/lvs start


#5)测试轮询访问

#for((i=1;i<11;i++));do curl http://虚拟IP;echo $i;sleep 2;echo; done



#注意:

#1、如lvs出现裂脑情况,则可能是防火墙、selinux没关,网络关闭了

#2、发现设置了DR模式,rr轮询的调度算法,但访问刷新还是访问同一台web,那是因为设置了会话保持时间,把#他注释掉即可



【lvs脚文内容】

#!/bin/bash

# Written by NetSeek

# description: Config realserver lo and apply noarp

WEB_VIP=192.168.2.100       #此处改为你设定的vip地址


. /etc/rc.d/init.d/functions


case "$1" in

start)

ifconfig lo:0 $WEB_VIP netmask 255.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/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"

;;

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:0 device

# not found.

echo "LVS-DR real server Stopped."

else

echo "LVS-DR Running."

fi

;;

*)

# Invalid entry.

echo "$0: Usage: $0 {start|status|stop}"

exit 1

;;

esac

exit 0



【keepalived配置文件内容】

! Configuration File for keepalived


global_defs {

   notification_email {

     [email protected]

     [email protected]

     [email protected]

   }

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}


vrrp_instance VI_1 {

    state MASTER    #主为MASTER,备为BACKUP

    interface eth0

    virtual_router_id 51    

    priority 100   #主的优先级比较高,主为100,备为90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.2.100

    }

}


virtual_server 192.168.2.100 80 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    #persistence_timeout 10

    protocol TCP


    real_server 192.168.2.3 80 {

        weight 1

TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

    

    real_server 192.168.2.4 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }   

}


virtual_server 192.168.2.100 443 {

    delay_loop 6

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.255.0

    #persistence_timeout 10

    protocol TCP


    real_server 192.168.2.3 443 {

        weight 1

TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

    

    real_server 192.168.2.4 443 {

        weight 1

        TCP_CHECK {

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }   

}