Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157132
  • 博文数量: 61
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 345
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-29 10:46
文章分类

全部博文(61)

文章存档

2012年(61)

我的朋友

分类:

2012-05-23 11:23:47

LVS-HA负载均衡应用方案(ipvsadm+heartbeat)
 
2010-09-11 TsengYia#126.com http://tsengyia.blog.chinaunix.net/
 
################################################################
系统环境:RHEL5 [ 2.6.18-8.el5 ]
软件环境:
    ◇ RHEL5系统光盘自带的rpm包(多数默认已有,带“[*]”标记的可能需要额外安装):
        ipvsadm-1.24-8.1.i386    [*]
       
        perl-5.8.8-10
        mod-perl-2.0.2-6.1
        perl-libwww-perl-5.805-1.1.1    [*]
        perl-Compress-Zlib-1.42-1.fc6
        perl-HTML-Parser-3.55-1.fc6
        perl-HTML-Tagset-3.10-2.1.1
        perl-URI-1.35-3
        perl-Net-SSLeay-1.30-4.fc6
        perl-Net-DNS-0.59-1.fc6    [*]
        perl-Net-IP-1.25-2.fc6    [*]
 
    ◇ 下载的软件包(保存到/dl_pkgs/目录):
      Linux-HA组件:
        Reusable-Cluster-Components-glue-1.0.6.tar.bz2
        Cluster-Resource-Agents-agents-1.0.3.tar.bz2
        Heartbeat-3-0-STABLE-3.0.3.tar.bz2
      支持组件:
        MailTools-2.06.tar.gz
        libnet-1.1.4-3.el5.i386.rpm
      —— 参考下载地址:
       
       
       
       
       
##########################################################################
一、网络拓扑结构
    采用基于DR方式的LVS群集方案,Host1~Host5作为分流负载的五台Web应用服务器,其中Host1、Host2兼作主、从负载均衡器。Web应用文档使用共享存储(NAS设备)的方式,数据交换采用千兆以太网。
    群集VirtualIP:173.17.17.1 。
    节点RealserverIP:173.17.17.11~15 。
 
 
二、配置主节点Host1 (Master Load-Balancer)
    1、修改IP地址、主机名映射及proc参数
      1) 修改IP地址
        eth0的IP地址为173.17.17.11/24,连接至Internet。
        eth1的IP地址为192.168.168.1/30,作为心跳检测线路,交叉连接至Host2 (192.168.168.2/30)。
        eth2的IP地址为192.168.4.11/24,连接内部网络的数据库、网络存储设备。
 
      2) 修改主机名及hosts文件
shell> vi /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=host1.example.com
shell> vi /etc/hosts
173.17.17.1   www
173.17.17.11  host1.example.com host1
173.17.17.12  host2.example.com host2
173.17.17.13  host3.example.com host3
173.17.17.14  host4.example.com host4
173.17.17.15  host5.example.com host5
shell> hostname host1.example.com
 
      3) 修改proc参数
shell> vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.eth1.send_redirects = 0
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
shell> sysctl -p
 
    2、安装ipvsadm及libnet、MailTools工具
shell> mount /dev/cdrom /media/cdrom        #//挂载RHEL5安装光盘
shell> cd /media/cdrom/Server/
shell> rpm -ivh ../Cluster/ipvsadm-1.24-8.1.i386.rpm
shell> rpm -ivh perl-libwww-perl-5.805-1.1.1.noarch.rpm perl-Net-DNS-0.59-1.fc6.i386.rpm perl-Net-IP-1.25-2.fc6.noarch.rpm
shell> cd /dl_pkgs/
shell> rpm -ivh libnet-1.1.4-3.el5.i386.rpm
shell> tar zxvf MailTools-2.06.tar.gz
shell> cd MailTools-2.06
shell> perl Makefile.PL
shell> make
shell> make install
 
    3、安装Linux-HA组件
      1) 添加群集用户、组
shell> groupadd haclient
shell> useradd -M -s /sbin/nologin -g haclient hacluster
      2) 安装Cluser-Glue组件
shell> cd /dl_pkgs/
shell> tar jxvf Reusable-Cluster-Components-glue-1.0.6.tar.bz2
shell> cd Reusable-Cluster-Components-glue-1.0.6
shell> ./autogen.sh
shell> ./configure --prefix=/ --with-daemon-user=hacluster --with-daemon-group=haclient
      —— 这里需注意,接下来make的时候可能会报如下错误(可能在新版本中会修正):
main.c:64:warning:function declaration isn't a prototype
main.c:78:warning:function declaration isn't a prototype
      解决办法:修改lib/stonith/main.c文件,注释掉其中的第64、76-81、390-391行。参考了WindLeaf的帖子:
shell> make && make install
      3) 安装Resource-Agents组件
shell> tar jxvf Cluster-Resource-Agents-agents-1.0.3.tar.bz2
shell> cd Cluster-Resource-Agents-agents-1.0.3
shell> ./autogen.sh
shell> ./configure --prefix=/ --with-daemon-user=hacluster --with-daemon-group=haclient
shell> make && make install
      4) 安装Heartbeat组件
shell> ln -s /include/* /usr/include/        #//添加符号链接,便于自动识别前面2个步骤安装的头文件
shell> tar jxvf Heartbeat-3-0-STABLE-3.0.3.tar.bz2
shell> cd Heartbeat-3-0-STABLE-3.0.3
shell> ./bootstrap
shell> ./configure --prefix=/
shell> make && make install
shell> cp doc/authkeys doc/ha.cf doc/haresources /etc/ha.d/        #//复制HA配置样例文件
shell> cp ../Cluster-Resource-Agents-agents-1.0.3/ldirectord/ldirectord.cf /etc/ha.d/
 
    4、配置heartbeat
      1) 修改authkeys文件
shell> vi /etc/ha.d/authkeys
auth 2
2 sha1 RandString!
shell> chmod 600 /etc/ha.d/authkeys
 
      2) 配置ha.cf
shell> vi /etc/ha.d/ha.cf
logfile /var/log/ha-log        #//ha日志文件位置
logfacility local0
keepalive 5        #//心跳频率间隔(秒)
deadtime 30        #//确认对方已当机的超时间隔
warntime 10
initdead 120        #//主机初始化(重启)时的当机超时间隔
udpport 694
ucast eth1 192.168.168.2        #//向Host2发送心跳状态
auto_failback on
watchdog /dev/watchdog        #//启用该行可以使当heartbeat服务当掉以后重新启动系统,调试阶段可以先注释掉该行。
node host1.example.com host2.example.com        #//热备双方的完整主机名,与“uname -n”命令的结果要保持一致
ping 173.17.17.254        #//通过ping其他主机检视自身状态
respawn hacluster /usr.lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
use_logd no
 
      3) 配置haresources
shell> vi /etc/ha.d/haresources
host1.example.com IPaddr::173.17.17.1/24 ldirectord::ldirectord.cf        #//使用主节点的主机名、群集VirtIP地址,加载ldirectord脚本
 
      4) 配置ldirectord.cf
shell> vi /etc/ha.d/ldirectord.cf
checktimeout=10        #//检测群集节点是否存活的超时时间(秒)
checkinterval=5        #//检测群集节点的时间间隔(秒)
autoreload=yes
logfile="/var/log/ldirectord.log"
quiescent=yes        #//启用休眠,当某个节点不可用时,将weight置为0,不再分配新的连接,待节点恢复后再还原weight
virtual=173.17.17.1:80        #//每个服务可以对应一个virtual段配置,如21、25等
    real=173.17.17.11:80 gate
    real=173.17.17.12:80 gate
    real=173.17.17.13:80 gate
    real=173.17.17.14:80 gate
    real=173.17.17.15:80 gate
    service=http
    scheduler=rr        #//指定使用哪种负载均衡分配算法,rr表示轮询、lc表示最少连接……。详细可参考“man ipvsadm”手册页中的
“--schedule”部分
    persistent=600        #//设置连接保持超时(秒)
    protocol=tcp
    checktype=negotiate
    checkport=80
    request=".testpage"        #//通过访问测试网页来检测群集节点的Web服务是否可用
    receive="Test Page."
    virtualhost=
      5) 测试ldirectord服务
        配置完毕可以执行service ldirectord start,看是否能正常运行,根据提示(或参考日志文件/var/log/ldirectord.log)进行故障排
除。
        —— 这里需注意,启动ldirectord服务时可能会报如下错误:
\1 better written as $1 at //sbin/ldirectord line 1252.
\1 better written as $1 at //sbin/ldirectord line 1252.
        解决办法:修改/sbin/ldirectord文件的第1252行,将“\1”修改为“$1”。
 
    5、启动heartbeat服务
shell> chkconfig --level 35 heartbeat on
shell> service heartbeat start
 
        #//跟踪日志文件/var/log/ha-log 排除错误
 
 
三、配置备节点Host2 (Slave Load-Balancer)
    1、修改IP地址、主机名映射及proc参数
        eth0的IP地址为173.17.17.12/24,连接至Internet。
        eth1的IP地址为192.168.168.2/30,作为心跳检测线路,交叉连接至Host1 (192.168.168.1/30)。
        eth2的IP地址为192.168.4.12/24,连接内部网络的数据库、网络存储设备。
        参考主节点Host1中的安装过程。
 
    2、安装ipvsadm及libnet、MailTools工具
        参考主节点Host1中的安装过程。
 
    3、安装Linux-HA组件
        参考主节点Host1中的安装过程。
        为了节省再次编译的时间,可以直接使用Host1主机中已编译的源码目录(过程略)代码,在此基础上再make install。
 
    4、配置heartbeat
        参考主节点Host1中的安装过程。
        配置文件和H1基本一致,可以直接使用Host1主机中的配置文件。如果使用ucast单播监测心跳状态,需互相设置对端地址(例如H2中,ha.cf文件中设置 ucast eth1 192.168.168.2)。
 
 
四、配置Web节点服务器Host1~Host5 (Real Web-Server)
    1、安装httpd服务、部署Web应用(例如Apache或Tomcat、Weblogic等),详细过程略
      ……
      1) 建立负载节点测试网页,便于负载均衡器验证服务是否可用
shell> echo "Test Page." > /var/www/html/.testpage
shell> chattr +i /var/www/html/.testpage        #//锁定文件避免无意中被删除
      2) 适当调整httpd.conf配置文件
SetEnvIf Request_URI \.testpage image-request        #//避免将访问网页.testpage的信息写入日志,减轻日志文件压力
SetEnvIf Request_URI \.gif image-request
SetEnvIf Request_URI \.jpg image-request
SetEnvIf Request_URI \.png image-request
CustomLog logs/access_log common env=!image-request
 

    —— 以下步骤在负载分配器(Host1、Host2)中可不用设置
    2、修改proc参数
shell> vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
shell> sysctl -p
 
    3、添加虚拟地址及路由记录
shell> vi /opt/add_vip.sh
#!/bin/bash
ifconfig lo:0 173.17.17.1 netmask 255.255.255.255 up
route add -host 173.17.17.1 dev lo:0
shell> chmod +x /opt/add_vip.sh
shell> /opt/add_vip.sh
shell> echo "/opt/add_vip.sh"  >> /etc/rc.local
 
 
五、验证负载均衡及故障切换
    1、在Host1服务器上执行“ipvsadm -L -n”命令:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  173.17.17.1:http rr
  -> 173.17.17.11:80             Route    1      0          0
  -> 173.17.17.12:80             Route    1      0          0
  -> 173.17.17.13:80             Route    1      0          0
  -> 173.17.17.14:80             Route    1      0          0
  -> 173.17.17.15:80             Route    1      0          0
 
    2、在Host1服务器上执行“ifconfig eth0:0”查看是否有IP地址为173.17.17.1的虚拟接口。
 
    3、使用外部客户端浏览器多次访问
        查看各台真实web服务器的访问日志。
        查看Host1主机中的/var/log/ldirectord.log日志。
 
    4、关闭Host1主机或断开网络连接,等待一段时间(一般在30秒内),观察Host2主机的替代过程;对比Host、Host2主机的ha-log日志,访问看Web站点是否仍然可用。
 
    5、关闭部分节点的Web服务,执行“ipvsadm -L -n”命令查看,如果Weight为0表示对应节点的服务已不可用。
 
    6、在Host1中执行“ipvsadm -L -n -c”命令可以查看当前的负载分配连接情况。
    ……
阅读(1029) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~