欢迎来到李先生的博客

深山的鹿,不知归处;万般皆苦,只可自渡。
扩大
缩小

光速搭lvs + keepalived + nginx

环境:
 
VIP         192.168.2.224
LVS        192.168.2.217     centos7
nginx1    192.168.2.231     centos6
nginx2    192.168.2.194     centos6
 
LVS原理:
 
一、LVS节点需做的
 
1、安装ipvsadm,keepalived
yum -y install keepalived ipvsadm
yum install net-tools
 
2、修改配置keepalived的配置文件
global_defs {
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.2.224
    }
}

virtual_server 192.168.2.231 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    protocol TCP

    real_server 192.168.2.194 80 {
        weight 1
        SSL_GET {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    
    real_server 192.168.2.231 80 {
        weight 1
        SSL_GET {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
 
3、启动keepalived
 
systemctl start keepalived
 
4、执行下面这个脚本
#!/bin/bash
VIP=192.168.2.224
RIP1=192.168.2.231
RIP2=192.168.2.194
case "$1" in
start)
        ipvsadm --save > /etc/sysconfig/ipvsadm
        systemctl start ipvsadm
        ifconfig eno16777736:0 $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
        route add -host $VIP dev eno16777736:0
        ipvsadm -A -t $VIP:80 -s rr
        ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
        ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
        echo "ipvsadm starting --------------------[ok]"
        ;;
stop)
        ipvsadm -C
        systemctl stop ipvsadm
        ifconfig eno16777736:0 down
        route del $VIP
        echo "ipvsamd stoped----------------------[ok]"
         ;;
status)
        if [ ! -e /var/lock/subsys/ipvsadm ];then
        echo "ipvsadm stoped---------------"
        exit 1
                else
                echo "ipvsamd Runing ---------[ok]"
        fi
        ;;
*)

echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
 
二、web节点需做的
 
1、先在两台nginx上把nginx装好,通过本机IP访问能区别两台机器,方便后面的测试,我这的nginx直接用的yum安装
 
 
 
 
2、分别在两台机器上执行如下脚本
 
#!/bin/bash
VIP=192.168.2.224

case "$1" in
    start)
        ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP 
        /sbin/route add -host $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 $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 Stopd"
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
esac

exit 0
 
三、测试
 
这里的测试要在非lvs,nginx机器上测试,也就是与这些无关的机器上测试
 
 
 
 注:参考网上各路大哥的,但是还是有很多问题,我这个绝对的可以。
 

posted on 2018-07-04 17:59  Captain_Li  阅读(298)  评论(1编辑  收藏  举报

导航