服务器维护,服务器代维,安全设置,漏洞扫描,入侵检测服务

运维之家

 找回密码
 注册
搜索
查看: 7105|回复: 0

RedhatAS4.4 集群(LVS+heartbeat)双虚拟IP

[复制链接]
dirtysea 发表于 2010-12-21 09:48:04 | 显示全部楼层 |阅读模式
针对我们自己的项目环境,采用集群方案有很好的可行性,不牵涉共享存储这一块,现在是realserver通过程序访问自己的存储设备。下面就详细介绍通过直接路由实现虚拟服务器(VS/DR)的配置方案。

各层的作用:
Load Balancer(负载均衡器):
Load Balancer是整个集群系统的前端,负责把客户请求转发到Real Server上。
Backup是备份Load Balancer,当Load Balancer不可用时接替它,成为实际的Load Balancer。
Load Balancer通过Ldirectord监测各Real Server的健康状况。在Real Server不可用时把它从群中剔除,恢复时重新加入。
Shared Storage(共享存储):
Shared Storage为所有Real Server提供共享存储空间和一致的数据内容。这一部分在这里不涉及,因为我们的数据访问不存在冲突。

各服务器IP分配:
公网环境采用双线双IP,单ip就更好配置了。
Virtual IP:      222.169.91.69  (电信)
                  111.222.33.44  (网通)
Load Balancer:   222.169.91.72
Backup:          222.169.91.70
Real Server 1:   222.169.91.67  (电信)
                  111.222.33.55  (网通)
Real Server 2:   222.169.91.68  (电信)
                  111.222.33.66  (网通)
IPVS是LVS集群系统的核心软件,它的主要作用是:
安装在Load Balancer上,把发往Virtual IP的请求转发到Real Server上。
IPVS安装主要包括三方面:
在Load Banlancer上安装IPVS内核补丁
在Load Banlancer上安装IPVS管理软件
在Real Server上安装ARP hidden内核补丁
注意事项:
1. Kernel,IPVS,IPVSadm,ARP hidden之间的版本必须对应。
2. Kernel 2.4.28和2.6.10及以上版本已内置IPVS,Redhat AS4 UP4 Linux 发行版也在其内核里编译了IPVS,
3. ARP hidden可以用arp_ignore/arp_announce或者arptables_jf代替
HeartBeat是Linux-HA的高可用性集群软件,它的主要作用是:
安装在Load Balancer和Backup上,运行于active/standby模式。
当Load Balancer失效时,Backup自动激活,成为实际的Load Balancer。
切换到active模式时,按顺序启动Virtual IP、IPVS和Ldirectord。
切换到standby模式时,按顺序关闭Ldirectord、IPVS和Virtual IP。

所需软件列表:
ipvsadm-1.24.tar.gz
libnet-1.1.2.1.tar.gz
e2fsprogs    可以用rpm 安装光盘
heartbeat-2.0.2.tar.gz  
配置基于DR模式Lvs集群:
ln -s /usr/src/kernels/2.6.9-42.ELsmp-i686/ /usr/src/linux
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make && make install

1、配置VIP脚本:
[root@nsbak ~]# more /etc/init.d/lvsdr
#!/bin/sh
#chkconfig: 2345 20 80
# description: start LVS   of  Directorserver
VIP1=222.169.91.69
VIP2=111.222.33.44
RIP1=222.169.91.67
RIP2=111.222.33.55
RIP3=222.169.91.68
RIP4=111.222.33.66
. /etc/rc.d/init.d/functions
case "$1" in
    start)
        echo " start LVS  of DirectorServer"
       # set the Virtual  IP Address
       /sbin/ifconfig eth0:0 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up
       /sbin/ifconfig eth0:2 $VIP2 broadcast $VIP2 netmask 255.255.255.255 up
       /sbin/route add -host $VIP1 dev eth0:0
       /sbin/route add -host $VIP2 dev eth0:2
       #Clear IPVS table
       /sbin/ipvsadm -C
      #set LVS
      /sbin/ipvsadm -A -t $VIP1:80 -s lc
      /sbin/ipvsadm -A -t $VIP2:80 -s lc
      /sbin/ipvsadm -a -t $VIP1:80 -r $RIP1:80 -g
      /sbin/ipvsadm -a -t $VIP1:80 -r $RIP3:80 -g
      /sbin/ipvsadm -a -t $VIP2:80 -r $RIP2:80 -g
      /sbin/ipvsadm -a -t $VIP2:80 -r $RIP4:80 -g
      #Run LVS
      /sbin/ipvsadm
      #end
        ;;
    stop)
        echo "close LVS Directorserver"
        /sbin/ipvsadm -C
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
esac

(-s lc 是使用了最少连接算法,可以自行选择相应的算法,更改lc就可以了,ipvsadm -h查看帮助。-g 是使用lvs工作DR直接路由模式),如果有多realserver
直接添加就可以了,之后启动此脚本就可以了.

2、配置realserver脚本:
[sysadmin@localhost ~]$ more /etc/init.d/lvs
#!/bin/bash
#
# lvsdrrs init script to hide loopback interfaces on LVS-DR
# Real servers. Modify this script to suit
# your needs. You at least need to set the correct VIP address(es).
#
# Script to start LVS DR real server.
#
# chkconfig: 2345 20 80
# description: LVS DR real server
#
# You must set the VIP address to use here:
VIP1=222.169.91.69
VIP2=111.222.33.44
case "$1" in
start)
       # Start LVS-DR real server on this machine.
        /sbin/ifconfig lo down
        /sbin/ifconfig lo up
        
        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
        
        /sbin/ifconfig lo:0 $VIP1 netmask 255.255.255.255 up
        /sbin/ifconfig lo:1 $VIP2 netmask 255.255.255.255 up
        /sbin/route add -host $VIP1 dev lo:0
        /sbin/route add -host $VIP2 dev lo:1
;;
stop)
        # Stop LVS-DR real server loopback device(s).
        /sbin/ifconfig lo:0 down
        /sbin/ifconfig lo:1 down
;;
status)
        # Status of LVS-DR real server.
        islothere=`/sbin/ifconfig lo:0 | grep $VIP`
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
        if [ ! "$islothere" -o ! "isrothere" ];then
            # Either the route or the lo:0 device
            # not found.
            echo "LVS-DR real server Stopped."
        else
            echo "LVS-DR Running."
        fi
;;
*)
        # Invalid entry.
        echo "$0: Usage: $0 {start|status|stop}"
        exit 1
;;
esac
此脚本使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://222.169.91.69
  应该可以分别看到:This is realserver1 和 This is realserver1.
3、配置基于高可用Lvs+heartbeat
1.1安装
tar -zxvf libnet.tar.gz
cd libnet
./configure
make
make install
tar zxvf heartbeat-2.0.2.tar.gz
cd heartbeat-2.0.2
./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/
groupadd -g 694 haclient
useradd -u 694 -g haclient hacluster
这里需要配置文件有三个:ha.cf、haresources、authkeys。

Ha.cf配置文件:
logfacility     local0 日志等级
keepalive 2   设定心跳(监测)时间时间为2秒
deadtime 20   节点死亡时间阀值,就是从节点在过了20后还没有收到心跳就认为主节点死亡
warntime 10   发出警告时间
initdead 120   网络启动的时间
udpport 694   使用udp端口694 进行心跳监测
mcast eth0 225.0.0.1 694 1 0 监听地址
auto_failback on  当主节点恢复后,是否自动切回
node    ns.abc.cn
node    nsbak.abc.cn
ping 222.169.91.65
respawn root /usr/lib/heartbeat/ipfail
apiauth ipfail gid=root uid=root
资源文件(/etc/ha.d/haresources):
ns.abc.cn 222.169.91.69 lvsdr ldirectord
认证文件(/etc/ha.d/authkeys):
auth 1
1 crc
选取一种认证方式,这个文件的权限必须是600
配置/etc/hosts文件:
vi /etc/hosts
添加两台机器的名字,做好名字到IP的对应。
ns.abc.cn:             222.169.91.72
nsbak.abc.cn:          222.169.91.70
使用同样的方法配置节点222.169.91.70  
备份节点222.169.91.70 上的heartbeat配置与节点1要完全相同,lvs配置也要相同。
上面的安装配置仅是对于heartbeat的,还并没有与LVS整合。
完装完毕进行测试,关闭主节点机器,另一台自动接管,主节点恢复后自动接管回服务。如果以上测试没有问题,那么开始和lvs整合。
配置Ldirectord
Ldirectord的作用是监测Real Server,当Real Server失效时,把它从Load Balancer列表中删除,恢复时重新添加,在安装heartbeat时已经安装了Ldirectord。
配置(/etc/ha.d/ldirectord.cf):
# Global Directives
checktimeout=30
checkinterval=10
fallback=127.0.0.1:80
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=no
# A sample virual with a fallback that will override the gobal setting
virtual=222.169.91.69:80
        real=222.169.91.67:80  gate
        real=222.169.91.68:80  gate
        fallback=127.0.0.1:80   gate
        service=http
        request="test.html"
        receive="Test Page"
        virtualhost=www.abc.cn
        scheduler=lc
        protocol=tcp
# A sample virual with a fallback that will override the gobal setting
virtual=111.222.33.44:80
        real=111.222.33.55:80   gate
        real=111.222.33.66:80   gate
        fallback=127.0.0.1:80   gate
        service=http
        request="test.html"
        receive="Test Page"
        virtualhost=www.abc.cn
        scheduler=lc
        protocol=tcp
在每个Real Server的中添加监控页:
echo "Test Page" >> /var/www/html/index.html
现在可以在主节点222.169.91.72上启动heartbeat
/etc/init.d/heartbeat start
在备份节点222.169.91.70也启动heartbeat  /etc/init.d/heartbeat start
测试:关闭主节点,备份节点将自动接管directorserver服务,当realserver挂机或者apache死掉,directorserver将不在将请求发给该realserver。

几个常用命令 : ipvsadm -ln, ipvsadm -lcn

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/25264/showart_496784.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|手机版|Archiver|运维之家

GMT+8, 2024-4-19 02:42 , Processed in 0.207977 second(s), 14 queries .

Powered by Dirtysea

© 2008-2020 Dirtysea.com.

快速回复 返回顶部 返回列表