lvs+keeplive相关

realserver shell代码

[root@localhost ~]# cat lvs.sh
ption: Config realserver lo and apply noarp
WEB_VIP=192.168.0.90
                                                                     
. /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

注意相关route和lo:0

ipvsadm方式shell代码

[root@localhost ~]# cat lvs_dr.sh
#!/bin/bash
# description: start LVS of DirectorServer
GW=192.168.0.1
                                                    
# website director vip.
WEB_VIP=192.168.0.90
WEB_RIP1=192.168.0.7
WEB_RIP2=192.168.0.8
. /etc/rc.d/init.d/functions
                                                    
logger $0 called with $1
                                                    
case "$1" in
                                                    
start)
        # Clear all iptables rules.
        /sbin/iptables -F
        # Reset iptables counters.
        /sbin/iptables -Z
        # Clear all ipvsadm rules/services.
        /sbin/ipvsadm -C
                                                    
 #set lvs vip for dr
        /sbin/ipvsadm --set 30 5 60
        /sbin/ifconfig eth0:0 $WEB_VIP broadcast $WEB_VIP netmask 255.255.255.255 up
        /sbin/route add -host $WEB_VIP dev eth0:0
        /sbin/ipvsadm -A -t $WEB_VIP:8080 -s wrr -p 3
        /sbin/ipvsadm -a -t $WEB_VIP:8080 -r $WEB_RIP1:8080 -g -w 1
        /sbin/ipvsadm -a -t $WEB_VIP:8080 -r $WEB_RIP2:8080 -g -w 1
        touch /var/lock/subsys/ipvsadm >/dev/null 2>&1
                                                           
        # set Arp
        /sbin/arping -I eth0 -c 5 -s $WEB_VIP $GW >/dev/null 2>&1
       ;;
stop)
        /sbin/ipvsadm -C
        /sbin/ipvsadm -Z
        ifconfig eth0:0 down
        route del $WEB_VIP  >/dev/null 2>&1
        rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
        /sbin/arping -I eth0 -c 5 -s $WEB_VIP $GW
        echo "ipvsadm stoped"
       ;;
                                                    
status)
                                                    
        if [ ! -e /var/lock/subsys/ipvsadm ];then
                echo "ipvsadm is stoped"
                exit 1
        else
                ipvsadm -ln
                echo "..........ipvsadm is OK."
        fi
      ;;
                                                    
*)
        echo "Usage: $0 {start|stop|status}"
        exit 1
esac
                                                    
exit 0


不用keeplive的话,可以直接用上面代码调用

用Keeplive就不用上面代码,keeplive配置文件

[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! 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
   lvs_id string
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.90
    }
}
virtual_server 192.168.0.90 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP
    real_server 192.168.0.7 80 {
        weight 3
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.0.8 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

多个VIP的话 继续添加,

安装ipvsadm出现错误解决办法

ln -s /usr/src/kernels/2.6.18-128.el5-x86_64/ /usr/src/linux
cp /usr/src/kernels/2.6.18-128.el5-x86_64/include/net/ip_vs.h /usr/include/net

安装之前检查

[root@localhost ~]# modprobe -l | grep ip_vs
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_dh.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_ftp.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_lblc.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_lblcr.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_lc.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_nq.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_rr.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_sed.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_sh.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_wlc.ko
/lib/modules/2.6.18-128.el5/kernel/net/ipv4/ipvs/ip_vs_wrr.ko
[root@localhost ~]# lsmod | grep ip_vs
ip_vs_rr               35009  1
ip_vs_wrr              35905  0
ip_vs                 121217  5 ip_vs_rr,ip_vs_wrr
[root@localhost ~]# 如果没有的话
[root@localhost ~]# modprobe ip_vs 加载一下
内核版本在2.4以上内核中有相关ip_vs,2.4一下需要编译内核


你可能感兴趣的:(LVS,keeplive)