zoukankan      html  css  js  c++  java
  • CentOS4.5下LVS方案

    环境描述:
    本文在配置LVS时使用三台linux,一台做Directorserver (192.168.0.25) ,两台做realserver(192.168.0.127 192.168.0.12,在配置lvs+heartbeat时又添加了一台(192.168.0.126)做为备份主节点,虚拟VIP: 192.168.0.100

    软件列表:
    ipvsadm-1.24.tar.gz
    libnet.tar 下载地址:http://www.packetfactory.net/libnet/ 稳定版本是:1.1.2.1
    e2fsprogs 可以用rpm 安装光盘
    heartbeat-2.0.2.tar.gz
    2.6内核已经集成IPVS内核补订了,所以不再需要重新编译内核.

    配置此集群分以下几种情况:
    (1)、配置基于DR模式Lvs集群
    (2)、配置基于隧道模式Lvs集群
    (3)、配置基于高可用Lvs+heartbeat
    (4)、此种配置方式可以加强LVS的主节点的高安全性前提下(主节点简称DR,备份主节点DRbak),考虑充分利用资源可以将DRbak做为realserver

    一、配置基于DR模式Lvs集群

    1、下载ipvsadm管理程序
    http://www.linuxvirtualserver.org/software/
    注意对应自己的内核版本
    ipvsadm-1.24.tar.gz
    tar zxvf ipvsadm-1.24.tar.gz
    cd ipvsadm-1.24
    make
    make install

    注意在make时可能会出现很多错误的信息,请按照如下操作就可以心编译正常
    ln -s /usr/src/kernels/2.6.9-22.EL-i686/ /usr/src/linux
    cd ipvsadm-1.24
    make
    make install

    2、配置VIP脚本
    [root@ns ~]#more /etc/init.d/lvsDR
    #!/bin/sh
    #create in 20060812 by ghb
    # description: start LVS of Directorserver
    VIP=192.168.0.100
    RIP1=192.168.0.127
    RIP2=192.168.0.128
    #RIPn=192.168.0.128~254
    GW=192.168.0.1
    . /etc/rc.d/init.d/functions
    case $1 in
        start)
        echo "start LVS of DirectorServer"
        # set the Virtual IP Address
        /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev eth0:0
        #Clear IPVS table
        /sbin/ipvsadm -C
        #set LVS
        /sbin/ipvsadm -A -t $VIP:80 -s rr                   #(如果需要session保持添加-p 默认保持300秒)
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
        #/sbin/ipvsadm -a -t $VIP:80 -r $RIP3:80 -g
        #Run LVS
        /sbin/ipvsadm
        #end
        ;;
        stop)
        echo "close LVS Directorserver"
        /sbin/ipvsadm -C
        ;;
        *)
        echo "Usage: $0" {start|stop}
        exit 1
    esac
    (-s rr 是使用了轮叫算法,可以自行选择相应的算法,更改rr就可以了,ipvsadm -h查看帮助。-g 是使用lvs工作DR直接路由模式,也可自行修改)
    如果有多个realserver直接添加就可以了,之后启动此脚本就可以了。

    3、配置realserver脚本
    #!/bin/bash
    #description : start realserver
    #create in 20060812 by ghb
    VIP=192.168.0.100
    /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
    /sbin/route add -host $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
    #end
    此脚本使realserver不响应arp请求,将此脚本分别在realserver上执行就可以了。
    测试:分别启动realserver上的httpd服务
    在realserver1 执行 echo "This is realserver1" > /var/www/html/index.html
    在realserver2 执行 echo "This is realserver2" > /var/www/html/index.html
    打开IE浏览器输入http://192.168.0.100 应该可以分别看到:This is realserver1 和 This is realserver1。

    二、配置基于隧道模式Lvs集群
    1、安装ipvsadmin方法和上面一样,在此略过
    2、配置LVS directorserver 脚本
    [root@ns ~]# more /etc/init.d/tunlvs
    #!/bin/sh
    # description: start LVS of Directorserver
    VIP=192.168.0.100
    RIP1=192.168.0.127
    RIP2=192.168.0.128
    #RIPn=192.168.0.n
    GW=192.168.0.1
    . /etc/rc.d/init.d/functions
    case $1 in
        start)
        echo "start LVS of DirectorServer"
        # set the Virtual IP Address
        /sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev tunl0
        #Clear IPVS table
        /sbin/ipvsadm -C
        #set LVS
        /sbin/ipvsadm -A -t $VIP:80 -s rr
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -i
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -i
        #/sbin/ipvsadm -a -t $VIP:80 -r $RIP3:80 -i
        #Run LVS
        /sbin/ipvsadm
        #end
        ;;
        stop)
        echo "close LVS Directorserver"
        ifconfig tunl0 down
        /sbin/ipvsadm -C
        ;;
        *)
        echo "Usage: $0" {start|stop}
        exit 1
    esac

    3、配置realserver
    [root@localhost ~]# more /etc/init.d/tunl
    #!/bin/sh
    # ghb in 20060812
    # description: Config realserver tunl port and apply arp patch
    VIP=192.168.0.100
    . /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
        /sbin/route add -host $VIP dev tunl0
        echo "1" > /proc/sys/net/ipv4/conf/tunl0/arp_ignore
        echo "2" > /proc/sys/net/ipv4/conf/tunl0/arp_announce
        echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
        sysctl -p
        ;;
        stop)
        echo "Tunl port closing"
        ifconfig tunl0 down
        echo "1" > /proc/sys/net/ipv4/ip_forward
        echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
        ;;
        *)
        echo "Usage: $0" {start|stop}
        exit 1
    esac
    如果有多个Virutal IP,可以使用tunl0:0,tunl0:1...。
    此脚本分别在realserver上执行,目的使realserver忽略arp响应,并设定vip.
    测式同样按照上面的方法测试。

    三、配置基于高可用Lvs+heartbeat
    1、确定LVS使用DR或/tun模式,请对照上面的配置。
    本例使用tun模式
    Director server 端脚本文件
    [root@ns ~]# more /etc/init.d/tunlvs
    #!/bin/sh
    # description: start LVS of Directorserver
    VIP=192.168.0.100
    RIP1=192.168.0.127
    RIP2=192.168.0.128
    #RIPn=192.168.0.n
    GW=192.168.0.1
    . /etc/rc.d/init.d/functions
    case $1 in
        start)
        echo "start LVS of DirectorServer"
        # set the Virtual IP Address
        /sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev tunl0
        #Clear IPVS table
        /sbin/ipvsadm -C
        #set LVS
        /sbin/ipvsadm -A -t $VIP:80 -s rr
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -i
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -i
        #/sbin/ipvsadm -a -t $VIP:80 -r $RIP3:80 -i
        #Run LVS
        /sbin/ipvsadm
        #end
        ;;
        stop)
        echo "close LVS Directorserver"
        ifconfig tunl0 down
        /sbin/ipvsadm -C
        ;;
        *)
        echo "Usage: $0" {start|stop}
        exit 1
    esac
    realserver端同样使用上面的配置文件就可以。

    2、安装heartbeat
    libnet.tar 下载地址:http://www.packetfactory.net/libnet/ 稳定版本是:1.1.2.1
    e2fsprogs 可以用rpm 安装光盘
    heartbeat-2.0.2.tar.gz 下载地址: http://www.linux-ha.org/download/
    2.1安装
    tar -zxvf libnet.tar.gz
    cd libnet
    ./configure
    make
    make install
    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/
    配置:
    主配置文件(/etc/ha.d/ha.cf)
    #debugfile /var/log/ha-debug
    logfile /var/log/ha-log #指名heartbeat的日志存放位置
    #crm yes #支持ClusterResourceManager(集群资源管理)功能
    #bcast eth1 #指明心跳方式使用以太广播方式,并且是在eth1接口上进行广播。
    logfacility local0
    keepalive 2#指明心跳时间为2秒(即每两秒钟在eth1上发送一次广播)。
    deadtime 30#指定在30秒内没有心跳信号,则立即切换服务。
    warntime 10 #指明心跳延迟的时间为十秒。当10秒钟内备份机不能联系上主机(当前活动的服务器,即无心跳信号),就会往日志中写入一个警告日志,但此时不会切换服务。
    initdead 120 #With some configurations, the network takes some time to start working after a reboot. This is a separate deadtime to handle that case. It should be at least twice the normal deadtime.
    udpport 694#Use port number 694 for bcast or ucast communication. This is the default, and the official IANA registered port number.
    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 ns.ghb.com #Mandatory. Hostname of machine in cluster as described by uname -n.
    node nsbak.ghb.com
    # 默认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):
    ns.aaa.com IPaddr::192.168.0.127/24/eth0 httpd
    #设置ns.aaa.com为主节点,集群服务器的ip地址为192.168.0.127 netmask 为255.255.255.240集群的服务有httpd
    认证文件(/etc/ha.d/authkeys),选取一种认证方式,这个文件的权限必须是600
    auth 1
    1 crc
    #2 sha1 sha1_any_password
    #3 md5 md5_any_password
    使用同样的方法配置节点2
    备份节点192.168.0.126 上的heartbeat和apache的配置与节点1要完全相同,lvs配置也要相同。
    2.2
    完装完毕进行测试,关闭主节点机器,另一台自动接管,主节点恢复后自动接管回服务。如果以上测试没有问题,那么开始和lvs整合。
    配置Ldirectord
    Ldirectord的作用是监测Real Server,当Real Server失效时,把它从Load Balancer列表中删除,恢复时重新添加,在安装heartbeat时已经安装了Ldirectord。
    配置(/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.0.100:80
    real=192.168.0.127:80 gate
    real=192.168.0.128:80 gate
    fallback=127.0.0.1:80 gate
    service=http
    request="test.html"
    receive="Test Page"
    virtualhost="www.xxxxxx.net"
    scheduler=rr
    #persistent=600
    #netmask=255.255.255.255
    protocol=tcp
    在每个Real Server的中添加监控页:
    echo "Test Page" >> /var/www/html/test.html
    修改heartbeat的资源文件/etc/ha.d/haresources
    ns.ghb.com 192.168.0.100 tunlvs ldirectord httpd
    现在可以在主节点192.168.0.25上启动heartbeat
    /etc/init.d/heartbeat start
    在备份节点也启动heartbeat /etc/init.d/heartbeat start
    测试:关闭主节点,备份节点将自动接管directorserver服务。(主节点正常时用ifconfig 是可以看tunl接可口的,而备份节点用ifconfig 命令是看不到的,只有在接管主节点服务后才是可以见的)
    至此第三部分配置完毕。
    四、考虑充份使用资源,将备份节点也做为realserver.
    在主备director server 上都要做如下配置
    1.director server 需要在脚本文件中将添加realserver,我们预先设置所有机器为realserver。
    #!/bin/sh
    # description: start LVS of Directorserver
    VIP=192.168.0.100
    RIP1=192.168.0.127
    RIP2=192.168.0.128
    RIP3=192.168.0.25
    RIP4=192.168.0.126
    GW=192.168.0.1
    . /etc/rc.d/init.d/functions
    case $1 in
        start)
        echo "start LVS of DirectorServer"
        # set the Virtual IP Address
        /sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev tunl0
        #Clear IPVS table
        /sbin/ipvsadm -C
        #set LVS
        /sbin/ipvsadm -A -t $VIP:80 -s rr
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -i
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -i
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP3:80 -i
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP3:80 -i
        #Run LVS
        /sbin/ipvsadm
        #end
        ;;
        stop)
        echo "close LVS Directorserver"
        ifconfig tunl0 down
        /sbin/ipvsadm -C
        ;;
        *)
        echo "Usage: $0" {start|stop}
        exit 1
    esac
    2.修改/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.0.100:80
    real=192.168.0.126:80 gate
    real=192.168.0.127:80 gate
    real=192.168.0.128:80 gate
    real=192.168.0.25:80 gate
    fallback=127.0.0.1:80 gate
    service="http"
    request="test.html"
    receive="Test Page"
    virtualhost="www.xxxxx.net"
    scheduler=rr
    #persistent=600
    #netmask=255.255.255.255
    protocol=tcp
    3、将realserver的启动脚本加入到主节点,和备份节点中,并且这个脚本的启动级必须先于heartbeat,关闭级必须后于heartbeat
    chkconfig tunl on 添加到系统启动
    4、创建closetunl启动脚本,为启动director server 做准备
    more /etc/init.d/closetunl
    #!/bin/sh
    # create in 200608 ghb
    # description: close tunl0 and arp_ignore
    VIP=192.168.136.100
    . /etc/rc.d/init.d/functions
    case $1 in
        start)
        echo "start director server and close tunl"
        ifconfig tunl0 down
        echo "1" > /proc/sys/net/ipv4/ip_forward
        echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
        ;;
        stop)
        echo "start Real Server"
        ifconfig tunl0 $VIP netmask 255.255.255.255 broadcast $VIP up
        /sbin/route add -host $VIP dev tunl0
        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
        ;;
        *)
        echo "Usage: lvs" {start|stop}
        exit 1
    esac
    chmod 755 /etc/init.d/closetunl
    5、修改/etc/ha.d/haresources
    ns.wdxc.com closetunl 192.168.0.100 tunlvs ldirectord httpd
    6、测试
    http://192.168.0.100 应该可以轮到四台机器上
  • 相关阅读:
    视图转换例子实践
    Xml序列化和反序列化对象使用MemoryStream实践
    iOS中控制器的实践和学习(2)认识XCode4模版(A1,A3,B2简易图)
    iOS中控制器的实践和学习(1)抛出UI问题
    拖动雪花视图实例学习
    <海量数据库解决方案>2011051901
    <海量数据库解决方案>2011051301
    提示(警告)视图的简单应用
    Delphi Prism Visual Studio Pascal For .NET
    Migrating Delphi Applications to .NET
  • 原文地址:https://www.cnblogs.com/tonykan/p/3514069.html
Copyright © 2011-2022 走看看