Chinaunix首页 | 论坛 | 博客
  • 博客访问: 381111
  • 博文数量: 58
  • 博客积分: 2941
  • 博客等级: 少校
  • 技术积分: 970
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 11:37
文章分类

全部博文(58)

文章存档

2015年(1)

2014年(1)

2012年(1)

2011年(19)

2009年(1)

2008年(1)

2007年(11)

2006年(10)

2005年(13)

分类: LINUX

2006-02-24 14:30:56

作者:
最后更新:2005年5月18日

目录


  • 集群技术主要分为三大类:

    • 高可用性(High Available Cluster),例:
    • 负载均衡(Load balancing Cluster),例:、
    • 高性能计算(High Performance Computing),例:

    我们这里使用RedHat AS 3.x,LVS,Linux-HA,Ldirectord,构造一个高可用的负载均衡集群系统。如图:

    各层的作用:

    • Load Balancer(负载均衡器):
      Load Balancer是整个集群系统的前端,负责把客户请求转发到Real Server上。
      Backup是备份Load Balancer,当Load Balancer不可用时接替它,成为实际的Load Balancer。
      Load Balancer通过Ldirectord监测各Real Server的健康状况。在Real Server不可用时把它从群中剔除,恢复时重新加入。
    • Server Array(服务器群):
      Server Array是一组运行实际应用服务的机器,比如WEB, Mail, FTP, DNS, Media等等。
      在实际应用中,Load Balancer和Backup也可以兼任Real Server的角色。
    • Shared Storage(共享存储):
      Shared Storage为所有Real Server提供共享存储空间和一致的数据内容。

    各服务器IP分配:

    Virtual IP: 192.168.136.10
    Load Balancer: 192.168.136.11
    Backup: 192.168.136.12
    Real Server 1: 192.168.136.101
    Real Server 2: 192.168.136.102
    Real Server 3: 192.168.136.103


  • IPVS是LVS集群系统的核心软件,它的主要作用是:

    • 安装在Load Balancer上,把发往Virtual IP的请求转发到Real Server上。

    IPVS的负载均衡机制有三种,这里使用IP Tunneling机制:

    • Virtual Server via NAT
    • Virtual Server via IP Tunneling
    • Virtual Server via Direct Routing

    IPVS的负载调度算法有十种:

    • 轮叫(Round Robin)
    • 加权轮叫(Weighted Round Robin)
    • 最少链接(Least Connections)
    • 加权最少链接(Weighted Least Connections)
    • 基于局部性的最少链接(Locality-Based Least Connections)
    • 带复制的基于局部性最少链接(Locality-Based Least Connections with Replication)
    • 目标地址散列(Destination Hashing )
    • 源地址散列(Source Hashing)
    • 最短期望延迟(Shortest Expected Delay)
    • 无须队列等待(Never Queue)

    IPVS安装主要包括三方面:

    • 在Load Banlancer上安装IPVS内核补丁
    • 在Load Banlancer上安装IPVS管理软件
    • 在Real Server上安装ARP hidden内核补丁

    关于如何编译内核请参考其他文档,这里使用从UltraMonkey下载的已编译好的内核。

    在Load Banlancer、Backup和Real Server上使用同一内核,IPVS和ARP hidden都已编译在这个内核里:

    wget
    wget
    #wget

    rpm -Fhv mkinitrd-3.5.13-1.um.1.i386.rpm
    rpm -Fhv kernel-2.4.21-27.0.2.EL.um.1.i686.rpm

    在Load Banlancer和Backup上安装IPVS管理软件:

    wget
    tar zxf ipvs-1.0.10.tar.gz
    cd ipvs-1.0.10/ipvs/ipvsadm
    make install

    chkconfig --del ipvsadm

    配置IPVS(/etc/sysconfig/ipvsadm),添加Real Server:

    -A -t 192.168.136.10:80 -s rr
    -a -t 192.168.136.10:80 -r 192.168.136.11:80 -i
    -a -t 192.168.136.10:80 -r 192.168.136.12:80 -i
    -a -t 192.168.136.10:80 -r 192.168.136.101:80 -i
    -a -t 192.168.136.10:80 -r 192.168.136.102:80 -i
    -a -t 192.168.136.10:80 -r 192.168.136.103:80 -i

    相关链接:

    Kernel:
    IPVS和IPVSadm:
    ARP hidden:

    注意事项:

    1. Kernel,IPVS,IPVSadm,ARP hidden之间的版本必须对应。
    2. 自己编译内核时,从下载标准内核源文件,不要使用发行版的内核源文件。
    3. Kernel 2.4.28和2.6.10及以上版本已内置IPVS,有些Linux发行版也在其内核里编译了IPVS。
    4. ARP hidden可以用arp_ignore/arp_announce或者arptables代替


  • HeartBeat是Linux-HA的高可用性集群软件,它的主要作用是:

    • 安装在Load Balancer和Backup上,运行于active/standby模式。
      当Load Balancer失效时,Backup自动激活,成为实际的Load Balancer。
    • 切换到active模式时,按顺序启动Virtual IP、IPVS和Ldirectord。
      切换到standby模式时,按顺序关闭Ldirectord、IPVS和Virtual IP。

    HeartBeat串口线连接测试方法:

    在Load Balancer上:cat < /dev/ttyS0
    在Backup上:echo hello > /dev/ttyS0

    修改主机名(/etc/hosts):

    127.0.0.1         localhost.localdomain localhost
    192.168.136.11    loadbalancer
    192.168.136.12    backup

    安装:

    groupadd -g 694 haclient
    useradd -u 694 -g haclient hacluster

    rpm -ivh /mnt/cdrom/RedHat/RPMS/glib2-devel-*

    wget
    tar zxf libnet.tar.gz
    cd libnet
    ./configure
    make
    make install

    wget
    tar zxf heartbeat-1.99.4.tar.gz
    cd heartbeat-1.99.4
    ./ConfigureMe configure --disable-swig --disable-snmp-subagent
    make
    make install
    cp doc/ha.cf doc/haresources doc/authkeys /etc/ha.d/
    cp ldirectord/ldirectord.cf /etc/ha.d/

    chkconfig --add heartbeat
    chkconfig --del ldirectord

    主配置文件(/etc/ha.d/ha.cf):

    #debugfile /var/log/ha-debug
    logfile /var/log/ha-log
    logfacility local0
    keepalive 2
    deadtime 30
    warntime 10
    initdead 120
    udpport 694
    baud 19200
    serial /dev/ttyS0
    mcast eth0 225.0.0.1 694 1 0

    # 当主节点恢复后,是否自动切回
    auto_failback on

    # stonith用来保证共享存储环境中的数据完整性
    #stonith baytech /etc/ha.d/conf/stonith.baytech

    # watchdog能让系统在出现故障1分钟后重启该机器。这个功能可以帮助服务器在确实停止心跳后能够重新恢复心跳。
    # 如果使用该特性,则在内核中装入"softdog"内核模块,用来生成实际的设备文件,输入"insmod softdog"加载模块。
    # 输入"grep misc /proc/devices"(应为10),输入"cat /proc/misc | grep watchdog"(应为130)。
    # 生成设备文件:"mknod /dev/watchdog c 10 130" 。
    #watchdog /dev/watchdog

    node loadbalancer
    node backup

    # 默认heartbeat并不检测除本身之外的其他任何服务,也不检测网络状况。
    # 所以当网络中断时,并不会进行Load Balancer和Backup之间的切换。
    # 可以通过ipfail插件,设置'ping nodes'来解决这一问题。详细说明参考hearbeat文档。
    #ping 192.168.136.1 172.16.0.1
    ping_group group1 192.168.136.1 192.168.136.2
    respawn root /usr/lib/heartbeat/ipfail
    apiauth ipfail gid=root uid=root

    # 其他一些插件可以在/usr/lib/heartbeat下找到
    #apiauth ipfail uid=hacluster
    #apiauth ccm uid=hacluster
    #apiauth cms uid=hacluster
    #apiauth ping gid=haclient uid=alanr,root
    #apiauth default gid=haclient

    资源文件(/etc/ha.d/haresources):

    loadbalancer lvs IPaddr::192.168.136.10/24/eth0 ipvsadm ldirectord

    认证文件(/etc/ha.d/authkeys),选取一种认证方式,这个文件的权限必须是600:

    auth 1
    1 crc
    #2 sha1 sha1_any_password
    #3 md5 md5_any_password

    相关链接:

    Linux-HA:


  • 安装HeartBeat过程中,已经自动安装了Ldirectord,它的作用是:

    • 监测Real Server,当Real Server失效时,把它从Load Balancer列表中删除,恢复时重新添加。

    配置(/etc/ha.d/ldirectord.cf):

    # Global Directives
    checktimeout=3
    checkinterval=1
    fallback=127.0.0.1:80
    autoreload=yes
    logfile="/var/log/ldirectord.log"
    quiescent=yes

    # A sample virual with a fallback that will override the gobal setting
    virtual=192.168.136.10:80
            real=192.168.136.11:80  ipip
            real=192.168.136.12:80  ipip
            real=192.168.136.101:80 ipip
            real=192.168.136.102:80 ipip
            real=192.168.136.103:80 ipip
            fallback=127.0.0.1:80   gate
            service=http
            request="test.html"
            receive="Test Page"
            virtualhost=
            scheduler=rr
            #persistent=600
            #netmask=255.255.255.255
            protocol=tcp

    在每个Real Server的中添加监控页:

    echo "Test Page" >> /var/www/html/test.html


  • 在启动集群系统之前,我们认为包括Load Balancer和Backup在内的所有服务器都是Real Server。

    在服务器上添加以下脚本/etc/init.d/tunl,用来配置tunl端口,应用arp补丁:

    #!/bin/sh
    # chkconfig: 2345 70 10
    # description: Config tunl port and apply arp patch

    VIP=192.168.136.10

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

    case "$1" in
        start)
            echo "Tunl port starting"
            ifconfig tunl0 $VIP netmask 255.255.255.255 broadcast $VIP up
            echo 1 > /proc/sys/net/ipv4/ip_forward
            echo 1 > /proc/sys/net/ipv4/conf/all/hidden
            echo 1 > /proc/sys/net/ipv4/conf/tunl0/hidden
            ;;
        stop)
            echo "Tunl port closing"
            ifconfig tunl0 down
            echo 1 > /proc/sys/net/ipv4/ip_forward
            echo 0 > /proc/sys/net/ipv4/conf/all/hidden
            ;;
        *)
            echo "Usage: $0 {start|stop}"
            exit 1
    esac

    如果有多个Virutal IP,可以使用tunl0:0,tunl0:1...。

    chmod 755 /etc/init.d/tunl
    chkconfig --add tunl

    在Load Balancer和Backup上,这个脚本的启动级必须先于heartbeat,关闭级必须后于heartbeat。


  • 在HeartBeat资源文件(/etc/ha.d/haresources)中定义了实现集群所需的各个软件的启动脚本。

    这些脚本必须放在/etc/init.d或者/etc/ha.d/resource.d目录里,启动顺序不能变:

    loadbalancer lvs IPaddr::192.168.136.10/24/eth0 ipvsadm ldirectord

    IPaddr的作用是启动Virutal IP,它是HeartBeart自带的一个脚本。

    ipvsadm的作用是在启动的时候把所有Real Server加入群中。

    ldirectord的作用是启动ldirectord监控程序。

    lvs的作用是为启动Load Balancer做准备,关闭tunl端口,取消arp补丁:

    #!/bin/sh
    # chkconfig: 2345 90 10
    # description: Preparing for Load Balancer and Real Server switching

    VIP=192.168.136.10

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

    case "$1" in
        start)
            echo "Preparing for Load Balancer"
            ifconfig tunl0 down
            echo 1 > /proc/sys/net/ipv4/ip_forward
            echo 0 > /proc/sys/net/ipv4/conf/all/hidden
            ;;
        stop)
            echo "Preparing for Real Server"
            ifconfig tunl0 $VIP netmask 255.255.255.255 broadcast $VIP up
            echo 1 > /proc/sys/net/ipv4/ip_forward
            echo 1 > /proc/sys/net/ipv4/conf/all/hidden
            echo 1 > /proc/sys/net/ipv4/conf/tunl0/hidden
            ;;
        *)
            echo "Usage: lvs {start|stop}"
            exit 1
    esac

    chmod 755 /etc/ha.d/resource.d/lvs

    启动集群系统:

    /etc/init.d/heartbeat start




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