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

LVS+keepalived

时间:2014-11-06 20:16:00      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:lvs   keepalived   

为什么要使 用负载均衡技术?

1、系统高可用性

2、  系统可扩展性

3、  负载均衡能力

LVS+keepalived能很好的实现以上的要求,LVS提 供负载均衡,keepalived提供健康检查,故障转移,提高系统的可用性!采用这样的架构以后 很容易对现有系统进行扩展,只要在后端添加或者减少realserver,只要更改lvs的 配置文件,并能实现无缝配置变更!

 

二、LVS+Keepalived介绍

1、  LVS

LVS是一个开源的软件,可以实现LINUX平台下的简单负载均衡。LVSLinux Virtual Server的缩写,意思是Linux虚拟服务器。目前有三种IP负 载均衡技术(VS/NATVS/TUNVS/DR);八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。

2、  keepalived

Keepalived 是运行在lvs 之上,它的主要功能是实现真实机的故障隔离及负载均衡器间的失败 切换,提高系统的可用性

 

三、LVS+keepalived负载均衡架构图

  bubuko.com,布布扣

 

四、LVS+keepalived的安装和配置

1.         配置环境

              System OSCentOS release 5.4

              Softwareipvsadm-1.24.tar.gz, keepalived-1.1.19.tar.gz

2.         信息列表

名称

IP

 LVS-Master

192.168.1.112

 LVS-BACKUP

192.168.1.113

 LVS-VIP

192.168.1.115

 Realserver1

192.168.1.105

 Realserver2

192.168.1.103

Realserver3

192.168.1.104

3.         安装lvs

分别在backup lvsmaster lvs上安装

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

ln -s /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linux

tar zxvf ipvsadm-1.24.tar.gz

cd ipvsadm-1.24

make && make install

4.         安装keepalived

分别在backup lvsmaster lvs上安装

wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
tar zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./configure --prefix=/usr/local/keepalived
make 
make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived

5.         配置keepadlived

LVS-Master的配置文件如下

[root@linux5 ~]# cat /etc/keepalived/keepalived.conf

global_defs {

notification_email {

               jimo291@gmail.com    #email 通知

        }

        notification_email_from jimo291@gmail.com

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        router_id LVS1          # 设置lvsid,在一个网络内应该是唯一的

}

vrrp_sync_group test {           #设置vrrp

group {

        loadbalance

}

}

 

vrrp_instance loadbalance {

        state MASTER       #设置lvs的状态,报错MASTERBACKUP两种,必须大写

        interface eth0     #设置对外服务的接口

        lvs_sync_daemon_inteface eth0   #设置lvs监听的接口

        virtual_router_id 51                     #设置虚拟路由表示

        priority 180            #设置优先级,数值越大,优先级越高

        advert_int 5           #设置同步时间间隔

authentication {                    #设置验证类型和密码

        auth_type PASS

        auth_pass 1111

}

virtual_ipaddress {                #设置lvs vip

        192.168.1.115

}

}

 

virtual_server 192.168.1.115 80 {

        delay_loop 6          #健康检查时间间隔

        lb_algo rr               #负载均衡调度算法

        lb_kind DR            #负载均衡转发规则

        #persistence_timeout 20  #设置会话保持时间,对bbs等很有用

        protocol TCP                #协议

        real_server 192.168.1.105 80 {

        weight 3                #设置权重

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

        real_server 192.168.1.103 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

        real_server 192.168.1.104 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

}

 

LVS-backup的配置文件如下

global_defs {

notification_email {

               jimo291@gmail.com

        }

        notification_email_from jimo291@gmail.com

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        router_id LVS2

}

vrrp_sync_group test {

group {

        loadbalance

}

}

 

vrrp_instance loadbalance {

        state BACKUP

        interface eth0

        lvs_sync_daemon_inteface eth0

        virtual_router_id 51

        priority 150

        advert_int 5

authentication {

        auth_type PASS

        auth_pass 1111

}

virtual_ipaddress {

        192.168.1.115

}

}

 

virtual_server 192.168.1.115 80 {

        delay_loop 6

        lb_algo rr

        lb_kind DR

        #persistence_timeout 20

        protocol TCP

        real_server 192.168.1.105 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

        real_server 192.168.1.103 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

        real_server 192.168.1.104 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

}

      

6.         Realserver的配置

三台客户端的脚本都一样!

cat /etc/rc.d/init.d/realserver.sh

#!/bin/bash

# description: Config realserver lo and apply noarp

 

SNS_VIP=192.168.1.115

 

/etc/rc.d/init.d/functions

 

case "$1" in

start)

       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_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 $SNS_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

 

7.         测试

1)        首先测试各个readserver,确定各个readserver都能正常访问,测试realserver.sh脚本,看启动后 能不能绑定lvs vip,停止后能不能去除绑定的vip

2)        测试lvs能否进行负载均衡转发,利用ipvsadm命令查看,如果想只当当前的 请求转发到哪个服务器去了,可以用ipvsadm选项来查看,其完整形式为:ipvsadm –lcn | grep 192.168.1.115

3)        停掉主lvslvs backup是否接管!

4)        更详细信息请查看日志,tail -f /var/log/messages


本文出自 “XFICC” 博客,请务必保留此出处http://xficc.blog.51cto.com/1189288/1573768

LVS+keepalived

标签:lvs   keepalived   

原文地址:http://xficc.blog.51cto.com/1189288/1573768

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