lvs+keepalived 实现负载均衡

虚拟机配置参考:http://blog.csdn.net/huqian5201314/article/details/77952026

201和202 上分别安装 keepalived 和 ipvsadm

如果不能用yum 安装请去官网下载:http://www.linuxvirtualserver.org

yum -y install ipvsadm

安装之后201配置 /etc/keepalived/keepalived.conf


global_defs {
   router_id NGINX_UPSTREAM
}

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

virtual_server 192.168.0.110 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 192.168.0.203 80 {
        weight 2
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

      real_server 192.168.0.204 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

202配置 /etc/keepalived/keepalived.conf


global_defs {
   router_id NGINX_UPSTREAM
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.110
    }
}

virtual_server 192.168.0.110 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP

    real_server 192.168.0.203 80 {
        weight 2
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }

      real_server 192.168.0.204 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

110 为虚拟IP 203和204分别是web服务

接下来配置203和204的健康脚本是一样的

vi realserver.sh

#!/bin/bash
#description Config LVS to realserver lo and apply noarp
#Written by ce

VIP=192.168.0.110
. /etc/init.d/functions

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

        exit 0

启动脚本(注意给执行权限 chmod u+x realserver.sh):

./realserver.sh start

然后 ifconfig 查看可以看到,多出来一个lo:0 的绑定地址

lo:0 Link encap:Local Loopback
inet addr:192.168.0.110 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1

4台机器都配置好以后 ,浏览器访问 hq.com (本机hosts的设置 指向到VIP 110)

可以看到203或者204的页面 可以正常访问了。

同样4台机器 201,202,203,204 随便关闭其中一台网站依然可以访问,达到了负载均衡的效果。

LVS 和 nginx 都可以显示负载均衡的转发,不同的是nginx是在第7层转发,lvs是第4层(不抗流量)。
这个例子中lvs的两台服务器上都没装nginx,直接通过网络转发的。

综上比较,个人觉得web网站,keepalived + nginx 转发 已经足够了。lvs可以用来负载其他应用,如mysql 或者其他。

备注:persistence_timeout 0 这个超时参数根据实际情况而定,这里只是方便测试。

你可能感兴趣的:(lvs,keepalived,nginx负载均衡配置)