LVS+Keepalived实现高可用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS+Keepalived实现高可用相关的知识,希望对你有一定的参考价值。

LVS可以实现负载均衡,但是不能够进行健康检查,比如一个rs出现故障,LVS 仍然会把请求转发给故障的rs服务器,这样就会导致请求的无效性。keepalive 软件可以进行健康检查,而且能同时实现 LVS 的高可用性,解决 LVS 单点故障的问题,其实 keepalive 就是为 LVS 而生的。

LVS-DR+Keepalived

一、实验环境:

Keepalived1 + lvs1(Director1):192.168.0.48
Keepalived2 + lvs2(Director2):192.168.0.58
Real server1:192.168.0.18
Real server2:192.168.0.28
VIP: 192.168.0.38

二、安装系统软件

LB+LVS+Keepalived的两个节点
        yum install ipvsadm keepalived -y
RS+nginx的两个节点
        yum install epel-release -y
        yum install nginx -y
        chkconfig nginx on
        service nginx start

三、设置配置脚本

两个RS节点
# vim /usr/local/sbin/lvs_dr_rs.sh
#! /bin/bash
vip=192.168.0.38
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip 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
2节点rs 上分别执行脚本:
bash /usr/local/sbin/lvs_dr_rs.sh

两个keepalived节点:
Master节点:
vim /etc/keepalived/keepalived.conf
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.38
    }
}
virtual_server 192.168.0.38 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP
real_server 192.168.0.18 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
real_server 192.168.0.28 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

backup节点:
vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.38
    }
}
virtual_server 192.168.0.38 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 0
    protocol TCP
real_server 192.168.0.18 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
real_server 192.168.0.28 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}
keepalived的2个节点执行如下命令,开启转发功能:
# echo 1 > /proc/sys/net/ipv4/ip_forward

先主后从分别启动keepalive
service keepalived start

三、验证结果
1、访问VIP,轮询到两个后端web服务器。
2、LVS主节点挂了,不影响服务的正常运行。

以上是关于LVS+Keepalived实现高可用的主要内容,如果未能解决你的问题,请参考以下文章

RedHat 7配置keepalived+LVS实现高可用的Web负载均衡

基于Keepalived实现LVS双主高可用集群

LVS+Keepalived实现高可用负载均衡

Keepalived实现LVS-DR集群高可用

LVS/DR + keepalived负载均衡高可用实现

LVS基础及LVS+Keepalived实现双主高可用负载均衡