Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1658588
  • 博文数量: 135
  • 博客积分: 2820
  • 博客等级: 少校
  • 技术积分: 2544
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-16 13:33
文章分类

全部博文(135)

文章存档

2015年(1)

2014年(8)

2013年(16)

2012年(43)

2011年(56)

2010年(11)

分类: LINUX

2012-02-27 13:45:08

lvs+keepalived 负载均+衡高可用

lvs-MASTER 192.168.10.6
lvs-BACKUP  192.168.10.7
VIP         192.168.10.8
WEB1         192.168.10.9
WEB2         192.168.10.10
GATEWAY   192.168.10.1
NETMASK    255.255.255.0

准备工作:
系统环境:CentOS 5.4(定制安装)
组件:
Base
Development Libraries
Development Tools
Editors
Text-based Internet

时间同步:
[root@web10 html]# mount /dev/cdrom /mnt/ && cd /mnt/CentOS/ && rpm -ivh ntp-4.2.2p1-9.el5.centos.2.i386.rpm && ntpdate time.nist.gov

===========================

lvs安装配置:
[root@web6 CentOS]# modprobe ip_vs   //内核加载ip_vs模块
[root@web6 CentOS]# lsmod | grep ip_vs   //查看是否加载成功
ip_vs                  77441  0

下载相关软件包:
[root@web6 ~]# mkdir /usr/local/src/lvs
[root@web6 ~]# cd /usr/local/src/lvs
[root@web6 lvs]# wget -c

[root@web6 lvs]# ln -s /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linux     //做个内核软链,必须的。
安装ipvsadm软件:
[root@web6 lvs]# tar xzvf ipvsadm-1.24.tar.gz
[root@web6 lvs]# cd ipvsadm-1.24
[root@web6 ipvsadm-1.24]# make && make install
安装keepalived软件:
[root@web6 lvs]# tar xzvf keepalived-1.1.15.tar.gz
[root@web6 lvs]# cd keepalived-1.1.15
[root@web6 keepalived-1.1.15]# ./configure --prefix=/usr/local/keepalived --with-man=/usr/share/man --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686

最后的配置情况:
Keepalived configuration
------------------------
Keepalived version       : 1.1.15
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
Use VRRP Framework       : Yes
Use LinkWatch            : No
Use Debug flags          : No

[root@web6 keepalived-1.1.15]# make && make install
配置文件的拷贝:
[root@web6 keepalived-1.1.15]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@web6 keepalived-1.1.15]# cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/
[root@web6 keepalived-1.1.15]# mkdir /etc/keepalived
[root@web6 keepalived-1.1.15]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@web6 keepalived-1.1.15]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

keepalived 主配置文件的配置:

[root@web6 ~]# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
         xxx@xxx.com
   }
   notification_email_from xxx@xxx.com
   smtp_server 127.0.0.1
  # smtp_connect_timeout 30
   router_id LVS_DEVEL
}

# VIP1
vrrp_instance VI_1 {
    state MASTER             #备份服务器上将MASTER改为BACKUP   
    interface eth0
    virtual_router_id 51
    priority 100    # 备份服务上将100改为99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.8   
        #(如果有多个VIP,继续换行填写.)
    }
}

virtual_server 192.168.10.8 80 {
    delay_loop 6                  #(每隔10秒查询realserver状态)
    lb_algo wrr                  #(lvs 算法)
    lb_kind DR                  #(Direct Route)
    persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)
    protocol TCP                #(用TCP协议检查realserver状态)

    real_server 192.168.10.9 80 {
        weight 3               #(权重)
        TCP_CHECK {
        connect_timeout 10       #(10秒无响应超时)
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.10.10 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}
=============================
或者用脚本(这个脚本在实际当中是不启动的,lvs只用keepalived.conf 这个配置文件)
[root@web6 ~]#vi /usr/local/sbin/lvs-dr.sh
        #!/bin/bash
        # description: start LVS of DirectorServer
        
        GW=192.168.10.1
        # website director vip.
        SNS_VIP=192.168.10.8
        SNS_RIP1=192.168.10.9
        SNS_RIP2=192.168.10.10

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

        logger $0 called with $1

        case "$1" in

        start)
        # set squid vip
        /sbin/ipvsadm --set 30 5 60
        /sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
        /sbin/route add -host $SNS_VIP dev eth0:0
        /sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3
        /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1
        /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1
        touch /var/lock/subsys/ipvsadm >/dev/null 2>&1

       ;;
stop)
        /sbin/ipvsadm -C
        /sbin/ipvsadm -Z
        ifconfig eth0:0 down
        route del $SNS_VIP
        rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
        echo "ipvsadm stoped"
       ;;

status)

        if [ ! -e /var/lock/subsys/ipvsadm ];then
                echo "ipvsadm stoped"
                exit 1
        else
                echo "ipvsadm OK"
        fi
      ;;

*)
        echo "Usage: $0 {start|stop|status}"
        exit 1
esac

exit 0
          
=======================================
[root@web6 ~]# cd /usr/local/sbin
[root@web6 ~]# vi realserver.sh

        #vi /usr/local/sbin/realserver.sh
        #!/bin/bash
        # description: Config realserver lo and apply noarp
        
        SNS_VIP=192.168.10.8
         
        . /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
     
=========
启动方式:
lvs: service keepalived start (两个lvs)
realserver :  ./realserver.sh start  (两个web)
=========
查看lvs状态
watch ipvsadm -ln
watch -n 0.1 -L -n --stats

查看lvs vip状态
[root@web6 ~]# ip ad li eth0
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:65:ec:25 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.6/24 brd 192.168.10.255 scope global eth0
    inet 192.168.10.8/32 scope global eth0
    inet6 fe80::20c:29ff:fe65:ec25/64 scope link
       valid_lft forever preferred_lft forever



测试:elinks -dump







参考 netseek
如有问题请留言

阅读(3517) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

夏冰软件2012-03-08 15:38:45

写的不错,学习一下