码迷,mamicode.com
首页 > 其他好文 > 详细

LVS+Keepalived实现高可用

时间:2018-04-30 17:53:41      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:LVS;Keepalive

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实现高可用

标签:LVS;Keepalive

原文地址:http://blog.51cto.com/gdutcxh/2109249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!