LVS是一款自由软件,任何人都可以免费获取并使用它,而且Linux也是一个开源操作系统,这二者的组合大大节约了企业的应用成本.同时LVS具有髙稳定性和高可靠性,在高并发和髙吞吐量下,具有高负荷处理能力,当某个服务节点出现故障时,并不影响整个系统服务的正常运行。这些优点使LVS已经广泛应用在企业、教育行业以及很多知名网站。可能已经有许多同学已经发现了,LVS在具有上述优点的同时,还存在一个致命的缺点,如果所有的用户请求都经过Director Server将任务分发到各个服务器节点,那么,当只有一台Director Server时,将会出现单点故障点,如果这个Director Server 出现故障,整个LVS系统将陷入瘫痪状态。
虽然Director Server仅完成用户请求的分发处理,负栽并不是很大,但是对于一个健壮的集群系统来说,单点故障是绝对不允许的。要避免这种单点故障,最实用、最简单的办法 就是对Director Server进行髙可用集群,常见的方案就是为Director Server做一个双机热备: 正常状态下主Director Server工作,备用Director Server监控主Director Server的状态,当 主Director Server出现异常或者故障时,备用Director Server马上接过主Director Server的工作,负责对用户请求进行分发处理。这样就避免了一台Director Server的单点故障问题, 保证了负栽均衡端持续地提供服务。
高可用LVS负载均衡集群体系结构
单一的Director Server可能会造成真个LVS集群系统的单点故障,为了解决这个 题,就需要保证Director Server的髙可用性,最常用的方法就是在负栽均衡层构建Director Server 双机热备系统。
高可用的LVS负载均衡群集体系结构如下图所示:

如上图所示:可以看出,整个体系结构仍然分为三层,在HA负栽均衡层由主、备两台 Director Server构成双机热备系统,双机之间通过心跳线连接。在正常状态下主Director Server使用虚拟IP接收用户请求,并根据设定好的策略和算法将请求分发给各个服务节点, 备用Director Server负责监控主Director Server的运行状态。当主Director Server发生异常 或出现故障时,备用Director Server负责接管主Director Server的虚拟IP和服务并继续接收 用户请求和分发处理。通过这种相互监控策略,任意一方主机出故障时.另一方都能够将IP 和服务接管,这就保证了负栽均衡层业务请求的不间断运行。
高可用性软件 Heartbeat 与 Keepalived
开源HA软件Heartbeat的介绍
heartbeat是Linux-HA项目中的一个组件,也是目前开源HA项目中最成功的一个例子, 它提供了所有HA软件需要的基本功能,比如心跳检测和资源接管,监测集群中的系统服务,在群集的节点间转移共享IP地址的所有者等。自1999年开始到现在,heartbeat在行业内得 到了广泛应用,也发行了很多的版本。可以从Linux-HA的官方网站www.linux-ha.org下载 到heartbeat的最新版本。
HeartBeat
运行于备用主机上的Heartbeat可以通过以太网连接检测主服务器的运行状态,一旦其无法检测到主服务器的“心跳”则自动接管主服务器的资源。通常情况下,主、备服务器间的心跳连接是一个独立的物理连接,这个连接可以是串行线缆、一个由“交叉线”实现的以太网连接。Heartbeat甚至可同时通过多个物理连接检测主服务器的工作状态,而其只要能通过其中一个连接收到主服务器处于活动状态的信息,就会认为主服务器处于正常状态。从实践经验的角度来说,建议为Heartbeat配置多条独立的物理连接,以避免Heartbeat通信线路本身存在单点故障。
1、串行电缆:被认为是比以太网连接安全性稍好些的连接方式,因为hacker无法通过串行连接运行诸如telnet、ssh或rsh类的程序,从而可以降低其通过已劫持的服务器再次侵入备份服务器的几率。但串行线缆受限于可用长度,因此主、备服务器的距离必须非常短。
2、以太网连接:使用此方式可以消除串行线缆的在长度方面限制,并且可以通过此连接在主备服务器间同步文件系统,从而减少了从正常通信连接带宽的占用。
基于冗余的角度考虑,应该在主、备服务器使用两个物理连接传输heartbeat的控制信息;这样可以避免在一个网络或线缆故障时导致两个节点同时认为自已是唯一处于活动状态的服务器从而出现争用资源的情况,这种争用资源的场景即是所谓的“脑裂”(split-brain)或“partitioned cluster”。在两个节点共享同一个物理设备资源的情况下,脑裂会产生相当可怕的后果。
为了避免出现脑裂,可采用下面的预防措施:
1、如前所述,在主、备节点间建立一个冗余的、可靠的物理连接来同时传送控制信息;
2、一旦发生脑裂时,借助额外设备强制性地关闭其中一个节点;
第二种方式即是俗称的“将其它节点‘爆头’(shoot the other node in the head)”,简称为STONITH。基于能够通过软件指令关闭某节点特殊的硬件设备,Heartbeat即可实现可配置的Stonith。但当主、备服务器是基于WAN进行通信时,则很难避免“脑裂”情景的出现。因此,当构建异地“容灾”的应用时,应尽量避免主、备节点共享物理资源。
Heartbeat的控制信息:
“心跳”信息: (也称为状态信息)仅150 bytes大小的广播、组播或多播数据包。可为以每个节点配置其向其它节点通报“心跳”信息的频率,以及其它节点上的heartbeat进程为了确认主节点出节点出现了运行等错误之前的等待时间。
集群变动事务(transition)信息:ip-request和ip-request-rest是相对较常见的两种集群变动信息,它们在节点间需要进行资源迁移时为不同节点上heartbeat进程间会话传递信息。比如,当修复了主节点并且使其重新“上线”后,主节点会使用ip-request要求备用节点释放其此前从因主节点故障而从主节点那里接管的资源。此时,备用节点则关闭服务并使用ip-request-resp通知主节点其已经不再占用此前接管的资源。主接点收到ip-request-resp后就会重新启动服务。
重传请求:在某集群节点发现其从其它节点接收到的heartbeat控制信息“失序”(heartbeat进程使用序列号来确保数据包在传输过程中没有被丢弃或出现错误)时,会要求对方重新传送此控制信息。 Heartbeat一般每一秒发送一次重传请求,以避免洪泛。
上面三种控制信息均基于UDP协议进行传送,可以在/etc/ha.d/ha.cf中指定其使用的UDP端口或者多播地址(使用以太网连接的情况下)。
此外,除了使用“序列号/确认”机制来确保控制信息的可靠传输外,Heartbeat还会使用MD5或SHA1为每个数据包进行签名以确保传输中的控制信息的安全性。
资源脚本:
资源脚本(resource scripts)即Heartbeat控制下的脚本。这些脚本可以添加或移除IP别名(IP alias)或从属IP地址(secondary IP address),或者包含了可以启动/停止服务能力之外数据包的处理功能等。通常,Heartbeat会到/etc/init.d/或/etc/ha.d/resource.d/目录中读取脚本文件。Heartbeat需要一直明确了解“资源”归哪个节点拥有或由哪个节点提供。在编写一个脚本来启动或停止某个资源时,一定在要脚本中明确判断出相关服务是否由当前系统所提供。
Heartbeat的配置文件:
/etc/ha.d/ha.cf
定义位于不同节点上的heartbeat进程间如何进行通信;
/etc/ha.d/haresources
定义对某个资源来说哪个服务器是主节点,以及哪个节点应该拥有客户端访问资源时的目标IP地址。
/etc/ha.d/authkeys
定义Heartbeat包在通信过程中如何进行加密。
当ha.cf或authkeys文件发生改变时,需要重新加载它们就可以使用之生效;而如果haresource文件发生了改变,则只能重启heartbeat服务方可使之生效。
尽管Heartbeat并不要求主从节点间进行时钟同步,但它们彼此间的时间差距不能超过1分钟,否则一些配置为高可用的服务可能会出异常。
Heartbeat当前也不监控其所控制的资源的状态,比如它们是否正在运行,是否运行良好以及是否可供客户端访问等。要想监控这些资源,冉要使用额外的Mon软件包来实现。
haresources配置文件介绍:
主从节点上的/etc/ra.d/raresource文件必须完全相同。文件每行通常包含以下组成部分:
1、服务器名字:指正常情况下资源运行的那个节点(即主节点),后跟一个空格或tab;这里指定的名字必须跟某个节点上的命令"uname -n"的返回值相同;
2、IP别名(即额外的IP地址,可选):在启动资源之前添加至系统的附加IP地址,后跟空格或tab;IP地址后面通常会跟一个子网掩码和广播地址,彼此间用“/”隔开;
3、资源脚本:即用来启动或停止资源的脚本,位于/etc/init.d/或/etc/ha.d/resourcd.d目录中;如果需要传递参数给资源脚本,脚本和参数之间需要用两个冒号分隔,多个参数时彼此间也需要用两个冒号分隔;如果有多个资源脚本,彼此间也需要使用空格隔开;
格式如下:
primary-server [IPaddress[/mask/interface/broadcast]] resource1[::arg1::arg2] resource2[::arg1::arg2]
例如:
primary-server 221.67.132.195 sendmail httpd
这里下栽的软件包是heartbeat-2.1.3.tar.gz,可通过源码进行安装。
同时还需要安装一个libnet工具包。libnet是一个髙层次的API工具,可以从 http://sourceforge.net/projects/libnet-dev/files/latest/download 下载到,这里下载的是libnet-1.2-rc3.tar.gz。 heartbeat的安装非常简单,基本操作步骤如下:
(1)安装libnet 前提条件:需要安装这两个包组
# yum -y groupinstall "Development Tools"
# yum -y groupinstall " Server Platform Development"
# tar -zxvf libnet-1.2-rc3.tar.gz
# cd libnet-1.2-rc3
# ./configure
# make && make install
(2) 安装heartbeat;
yum install perl-TimeDate net-snmp-libs libnet PyXML perl-MailTools
# 安装heartbeat 所依赖的程序包
rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm
# 安装heartbeat 2 以及程序包
Preparing... ########################################### [100%]
1:heartbeat-pils ########################################### [ 33%]
2:heartbeat-stonith ########################################### [ 67%]
3:heartbeat ########################################### [100%]
cp /usr/share/doc/heartbeat-2.1.4/{authkeys,ha.cf,haresources} /etc/ha.d/
# 复制配置文件、认证文件、以及集群资源配置文件
openssl rand -hex 8
80dd467590678c72
# 使用openssl 生成加密秘钥
cat authkeys
# 更改 authkeys 文件
# Authentication file. Must be mode 600
#
#
# Must have exactly one auth directive at the front.
# auth send authentication using this method-id
#
# Then, list the method and key that go with that method-id
#
# Available methods: crc sha1, md5. Crc doesn't need/want a key.
#
# You normally only have one authentication method-id listed in this file
#
# Put more than one to make a smooth transition when changing auth
# methods and/or keys.
#
#
# sha1 is believed to be the "best", md5 next best.
#
# crc adds no security, except from packet corruption.
# Use only on physically secure networks.
#
auth 2
#1 crc
2 sha1 80dd467590678c72
#3 md5 Hello!
chmod 600 authkeys
# 更改authkeys 文件的默认权限为600
heartbeat的安装包中包含了一个ldirectord插件,这个插件以后会用到。在heartbeat安 装完毕后,此插件默认已经安装。但是为了保证丨directord可用,还需要一个perl-MailTools 的rpm包,这个rpm从系统盘中找到后安装即可。
开源HA软件Keepalived的介绍
Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态。它根据layer3,4&5交换机制检测每个服务节点的状态,如果某个服务节点出现异常,或工作出现故障,Keepalived将检测到,并将出现故障的服务节点从集群系统中剔除,而当故障节点 恢复正常后,Keepalived又可以自动将此服务节点重新加入到服务器集群中。这些工作全部自动完成,不需要人工干涉,需要人工完成的只是修复出现故障的服务节点。
Keepalived 后来又加入了 VRRP 的功能。VRRP 是 Virtual Router Redundancy Protocol虚
拟路由器冗余协议)的缩写,它的作用是解决静态路由出现的单点故障问题,它能够保证 网络不间断地、稳定地运行。综上所述,Keepalived—方面具有服务器运行检测功能,另 一方面也具有HA cluster功能。因此通过Keepalived可以搭建一个高可用的LVS负载均衡 集群系统。
安装 Keepalived
Keepalived的官方网址是http://www.keepalived.org,可以在这里下载到各种版本的 Keepalived,这里下载的是 keepalived-1.2.12.tar.gz。安装步嫌如下:
安装keepalived 官方站点:http://www.keepalived.org/download.htm
# tar -zxvf keepalived-1.2.12.tar.gz
# cd keepalived-1.2.12
# ./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
# make && make install
# ln -s /usr/local/sbin/keepalived /sbin/
# keepalived --help
Usage: keepalived [OPTION...]
-f, --use-file=FILE Use the specified configuration file
-P, --vrrp Only run with VRRP subsystem
-C, --check Only run with Health-checker subsystem
-l, --log-console Log messages to local console
-D, --log-detail Detailed log messages
-S, --log-facility=[0-7] Set syslog facility to LOG_LOCAL[0-7]
-V, --dont-release-vrrp Don't remove VRRP VIPs and VROUTEs on daemon stop
-I, --dont-release-ipvs Don't remove IPVS topology on daemon stop
-R, --dont-respawn Don't respawn child processes
-n, --dont-fork Don't fork the daemon process
-d, --dump-conf Dump the configuration data
-p, --pid=FILE Use specified pidfile for parent process
-r, --vrrp_pid=FILE Use specified pidfile for VRRP child process
-c, --checkers_pid=FILE Use specified pidfile for checkers child process
-v, --version Display the version number
-h, --help Display this help message
在编译选项中,“--sysconf”指定了 Keepalived配置文件的安装路径,即路径为/etc/ Keepalived/Keepalived.conf. “--with-kenel-dir”是个很重要的参数,但这个参数并不是要把Keepalived编译进内核,而是指定使用内核源码中的头文件,即include目录。只有使用 LVS时,才需要用到此参数,其他时候是不需要的。
安装完成,执行如下操作:
[root@node4 ~]# keepalived --help
Usage: keepalived [OPTION...]
-f, --use-file=FILE Use the specified configuration file
-P, --vrrp Only run with VRRP subsystem
-C, --check Only run with Health-checker subsystem
-l, --log-console Log messages to local console
-D, --log-detail Detailed log messages
-S, --log-facility=[0-7] Set syslog facility to LOG_LOCAL[0-7]
-V, --dont-release-vrrp Don't remove VRRP VIPs and VROUTEs on daemon stop
-I, --dont-release-ipvs Don't remove IPVS topology on daemon stop
-R, --dont-respawn Don't respawn child processes
-n, --dont-fork Don't fork the daemon process
-d, --dump-conf Dump the configuration data
-p, --pid=FILE Use specified pidfile for parent process
-r, --vrrp_pid=FILE Use specified pidfile for VRRP child process
-c, --checkers_pid=FILE Use specified pidfile for checkers child process
-v, --version Display the version number
-h, --help Display this help message
这里列出了keepalive的各种用法,同时也表明keepalived 已安装成功了
安装LVS软件
LVS是通过IPVS模块来实现的。IPVS是LVS集群系统的核心软件,主要用于完成用 户的请求到达负载调度
系统安装完成之后,可以通过如下命令检查kernel是否支持LVS的IPVS模块
[root@node4 ~]# modprobe -l | grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko
在director server 上安装IPVS管理软件;
这里我们使用yum 安装
[root@node4 ~]# yum -y install ipvsadm
搭建高可用LVS集群
LVS集群有DR、TUN、NAT三种配置模式,可以对WWW服务、FTP服务、MAIL 服务等进行负栽均衡。下面通过3个实例详细讲述如何搭建WWW服务的髙可用LVS集群 系统,以及基于DR模式的LVS集群配置.在进行实例介绍之前进行约定:操作系统采用 CentOS 6.5,地址规划如下表所示:
节点类型 | IP地址规划 | 主机名 | 类型 |
Master Director server | 172.16.249.17 | node4 | Public IP |
172.16.249.7 | priv1 | Private IP | |
172.16.250.250 | none | Virtual IP | |
slave Director server | 172.16.249.18 | node5 | |
172.16.249.8 | |||
Real Server 1 | 172.16.249.9 | rs1 | |
Real Server 2 | 172.16.249.10 | rs2 | |
整个高可用LVS集群系统的拓扑图如下图所示:
通过heartbeat搭建LVS高可用性集群
1.配置lvs集群
配置LVS的方法有很多,可以通过LVS提供的ipvsadm命令进行配罝,也可以通过第 三方插件或工具来进行配置,例如通过Ldirectord来配置LVS,或者通过Redhat提供的界面工具piranha来配置等,这里选择通过Ldirectord来配置LVS.
(1)通过 Ldirectord 在主、备 Director Server 上配置LVS
Ldirectord是heartbeat的一个插件,Ldirectord主要用于监控集群系统中每个服务节点的运行状态,当某个节点的服务出现异常 或主机出现故障时,将此节点从集群系统中剔除,并且在节点恢复正常后,重新将此节点加 入集群系统。除了监控服务节点外,Ldirectord的另一个功能是配置LVS,只需设置好Ldircclord 的配置文件,启动服务即可,Ldirectord会自动调用ipvsadm命令创建LVS路由表信息。 Ldirectord配置文件的默认路径为/etc/ha.d/ldirectord.cf,这里详细介绍一下这个文件中每个选项的含义.
如果使用rpm包安装的话配置文件会在其它的目录;我们就是使用RPM包安装的;
[root@node4 ha.d]# rpm -ql heartbeat-ldirectord
/etc/ha.d/resource.d/ldirectord
/etc/init.d/ldirectord
/etc/logrotate.d/ldirectord
/usr/sbin/ldirectord
/usr/share/doc/heartbeat-ldirectord-2.1.4
/usr/share/doc/heartbeat-ldirectord-2.1.4/COPYING
/usr/share/doc/heartbeat-ldirectord-2.1.4/README
/usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf
/usr/share/man/man8/ldirectord.8.gz
编辑配置文件;并将它复制到;ha.d目录下;
vim /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf
cp -p /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf /etc/ha.d
checktimeout=20 #判定Real Server出错的时间间隔
checkinterval=10 #指定Ldirectord在两次检查之间的间隔时间
fallback=127.0.0.1:80 #当所有的Real Server节点不能工作时,Web 服务重定向的地址
autoreload=yes #是否自动重载配置文件,选yes时,配置文件发生变化,自动载入配置信息
logfile="/var/log/ldirectord.log" # 设定Ldirectord日志输出文忤路径
#logfile="local0"
#emailalert="[email protected]"
#emailalertfreq=3600
#emailalertstatus=all
quiescent=yes # 选择no时,如果一个节点在checktimeout设里的时间用期内没有响应, ldirectord将会从LVS的路由表中直接移除Real Server,此时,将中断现有的客户端连接,并使LVS丢掉所有的连接踉踪记录和持续连接模板: 如果选择yes,务某个Real Server失效时,Ldirectord将失效节点的权值设置为0,新的连接将不能到达,但是并不会从LVS路由表中清除此节点,同时,连接跟踪记录和桎序连接模板仍然保留在Director上
注意,以上几行为Ldirectord.cf文件的全局设置,它们可以应用到多个虚拟主机。下面 是每个虚拟主机的配置
# Sample for an http virtual service
virtual=172.16.250.250:80 #指定虚拟的IP地址和端口号,注意,在virtual这行后面的行 .必须缩进4个空格或以一个tab字符进行标记
real=172.16.249.9:80 gate #指定Real Server股务器地址和端口,同时设定LVS工作模式,用gate表示DR模式,ipip表示TUNL模式,masq表示NAT模式
real=172.16.249.10:80 gate
fallback=127.0.0.1:80 gate
service=http #指定服务的类塑,这里是对HTTP股务进行负栽均衡
request="index.html"# Ldirectord将根据指定的Real Server地址,结合该选项給 出的请求頁面,发送访问请求,检查Real Server上的服务是否正常运行,必须确保这里給出的页面地址是可访问的,不然Ldirectord会误认为此节点已经失效,发生错误监控现象
receive="Test Page" #指定请求和应答字串,也就是index.html的内容
virtualhost=some.domain.com.au
scheduler=rr #指定调度算法,这里是rr (轮均)算法
#persistent=600
#netmask=255.255.255.255
protocol=tcp #指定协议的类型,LVS支特TCP和UDP协议
checktype=negotiate # 指定 Ldirectord 的检測类型,checktype可以是 connect、external, negotiate, off、on, ping 和checktimeout 这几个,默认为negotiate,通过页面交互来判断服务节点是否正常
checkport=80 # 指定监控端口
request="index.html"
receive="Test Page"
virtualhost=www.henhenlu.com # 虚拟服务器的名称,可以任意指定
配置完毕后就可以执行如下命令启动或关闭Ldirectord服务:
/etc/rc.d/init.d/ldirectord {start|stop}
(2) Real server 的配置
在LVS的DR和TUN模式下,用户的访问请求到达Real Server后,是直接返回给用户的,不再经过前端的Director Server,因此,需要在每个Real server节点上增加虚拟的VIP地址,这样数据才能直接返回给用户。增加VIP地址的操作可以通过创建脚本的方式来实 现。创建文件
vim /root/realserver.sh
脚本内容如下:
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
. /etc/rc.d/init.d/functions
VIP=172.16.250.250
host=`/bin/hostname`
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
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig lo:0 down
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
;;
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 real server Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
给脚本加权限,并执行
chmod 755 /etc/rc.d/init.d/functions
chmod +x realserver.sh
./realserver.sh start
2.在主、备 Director Server 上配置 heartbeat
在搭建Director Server的双机热备系统之前,首先需要在两台主机上安装heartbeat软 件。heartbeat的安装已经在前面介绍过,这里不再讲述,直接进入heartbeat的配置。
(1)配置 heartbeat 的主配置文件(/etc/ha.d/ha.cf)
下面对ha.cf文件的每个选项进行详细介绍。
ha.cf配置文件部分参数详解:
autojoin none
#集群中的节点不会自动加入
logfile /var/log/ha-log
#指名heartbaet的日志存放位置
keepalive 2
#指定心跳使用间隔时间为2秒(即每两秒钟在eth1上发送一次广播)
deadtime 30
#指定备用节点在30秒内没有收到主节点的心跳信号后,则立即接管主节点的服务资源
warntime 10
#指定心跳延迟的时间为十秒。当10秒钟内备份节点不能接收到主节点的心跳信号时,就会往日志中写入一个警告日志,但此时不会切换服务
initdead 120
#在某些系统上,系统启动或重启之后需要经过一段时间网络才能正常工作,该选项用于解决这种情况产生的时间间隔。取值至少为deadtime的两倍。
udpport 694
#设置广播通信使用的端口,694为默认使用的端口号。
baud 19200
#设置串行通信的波特率
bcast eth0
# Linux 指明心跳使用以太网广播方式,并且是在eth0接口上进行广播。
#mcast eth0 225.0.0.1 694 1 0
#采用网卡eth0的Udp多播来组织心跳,一般在备用节点不止一台时使用。Bcast、ucast和mcast分别代表广播、单播和多播,是组织心跳的三种方式,任选其一即可。
#ucast eth0 192.168.1.2
#采用网卡eth0的udp单播来组织心跳,后面跟的IP地址应为双机对方的IP地址
auto_failback on
#用来定义当主节点恢复后,是否将服务自动切回,heartbeat的两台主机分别为主节点和备份节点。主节点在正常情况下占用资源并运行所有的服务,遇到故障时把资源交给备份节点并由备份节点运行服务。在该选项设为on的情况下,一旦主节点恢复运行,则自动获取资源并取代备份节点,如果该选项设置为off,那么当主节点恢复后,将变为备份节点,而原来的备份节点成为主节点
#stonith baytech /etc/ha.d/conf/stonith.baytech
# stonith的主要作用是使出现问题的节点从集群环境中脱离,进而释放集群资源,避免两个节点争用一个资源的情形发生。保证共享数据的安全性和完整性。
#watchdog /dev/watchdog
#该选项是可选配置,是通过Heartbeat来监控系统的运行状态。使用该特性,需要在内核中载入"softdog"内核模块,用来生成实际的设备文件,如果系统中没有这个内核模块,就需要指定此模块,重新编译内核。编译完成输入"insmod softdog"加载该模块。然后输入"grep misc /proc/devices"(应为10),输入"cat /proc/misc |grep watchdog"(应为130)。最后,生成设备文件:"mknod /dev/watchdog c 10 130" 。即可使用此功能
node node1.magedu.com
#主节点主机名,可以通过命令“uanme –n”查看。
node node2.magedu.com
#备用节点主机名
ping 192.168.12.237
#选择ping的节点,ping 节点选择的越好,HA集群就越强壮,可以选择固定的路由器作为ping节点,但是最好不要选择集群中的成员作为ping节点,ping节点仅仅用来测试网络连接
ping_group group1 192.168.12.120 192.168.12.237
#类似于ping ping一组ip地址
apiauth pingd gid=haclient uid=hacluster
respawn hacluster /usr/local/ha/lib/heartbeat/pingd -m 100 -d 5s
#该选项是可选配置,列出与heartbeat一起启动和关闭的进程,该进程一般是和heartbeat集成的插件,这些进程遇到故障可以自动重新启动。最常用的进程是pingd,此进程用于检测和监控网卡状态,需要配合ping语句指定的ping node来检测网络的连通性。其中hacluster表示启动pingd进程的身份。
当前环境配置:
#
# There are lots of options in this file. All you have to have is a set
# of nodes listed {"node ...} one of {serial, bcast, mcast, or ucast},
# and a value for "auto_failback".
#
# ATTENTION: As the configuration file is read line by line,
# THE ORDER OF DIRECTIVE MATTERS!
#
# In particular, make sure that the udpport, serial baud rate
# etc. are set before the heartbeat media are defined!
# debug and log file directives go into effect when they
# are encountered.
#
# All will be fine if you keep them ordered as in this example.
#
#
# Note on logging:
# If all of debugfile, logfile and logfacility are not defined,
# logging is the same as use_logd yes. In other case, they are
# respectively effective. if detering the logging to syslog,
# logfacility must be "none".
#
# File to write debug messages to
#debugfile /var/log/ha-debug
#
#
# File to write other messages to
#
logfile /var/log/ha-log
#
#
# Facility to use for syslog()/logger
#
#logfacility local0
#
#
# A note on specifying "how long" times below...
#
# The default time unit is seconds
# 10 means ten seconds
#
# You can also specify them in milliseconds
# 1500ms means 1.5 seconds
#
#
# keepalive: how long between heartbeats?
#
keepalive 1000ms
#
# deadtime: how long-to-declare-host-dead?
#
# If you set this too low you will get the problematic
# split-brain (or cluster partition) problem.
# See the FAQ for how to use warntime to tune deadtime.
#
deadtime 8
#
# warntime: how long before issuing "late heartbeat" warning?
# See the FAQ for how to use warntime to tune deadtime.
#
warntime 4
#
#
# Very first dead time (initdead)
#
# On some machines/OSes, etc. the network takes a while to come up
# and start working right after you've been rebooted. As a result
# we have a separate dead time for when things first come up.
# It should be at least twice the normal dead time.
#
#initdead 120
#
#
# What UDP port to use for bcast/ucast communication?
#
udpport 694
#
# Baud rate for serial ports...
#
#baud 19200
#
# serial serialportname ...
#serial /dev/ttyS0 # Linux
#serial /dev/cuaa0 # FreeBSD
#serial /dev/cuad0 # FreeBSD 6.x
#serial /dev/cua/a # Solaris
#
#
# What interfaces to broadcast heartbeats over?
#
bcast eth0 # Linux
#bcast eth1 eth2 # Linux
#bcast le0 # Solaris
#bcast le1 le2 # Solaris
#
# Set up a multicast heartbeat medium
# mcast [dev] [mcast group] [port] [ttl] [loop]
#
# [dev] device to send/rcv heartbeats on
# [mcast group] multicast group to join (class D multicast address
# 224.0.0.0 - 239.255.255.255)
# [port] udp port to sendto/rcvfrom (set this value to the
# same value as "udpport" above)
# [ttl] the ttl value for outbound heartbeats. this effects
# how far the multicast packet will propagate. (0-255)
# Must be greater than zero.
# [loop] toggles loopback for outbound multicast heartbeats.
# if enabled, an outbound packet will be looped back and
# received by the interface it was sent on. (0 or 1)
# Set this value to zero.
#
#
#mcast eth0 225.0.0.1 694 1 0
#
# Set up a unicast / udp heartbeat medium
# ucast [dev] [peer-ip-addr]
#
# [dev] device to send/rcv heartbeats on
# [peer-ip-addr] IP address of peer to send packets to
#
#ucast eth0 192.168.1.2
#
#
# About boolean values...
#
# Any of the following case-insensitive values will work for true:
# true, on, yes, y, 1
# Any of the following case-insensitive values will work for false:
# false, off, no, n, 0
#
#
#
# auto_failback: determines whether a resource will
# automatically fail back to its "primary" node, or remain
# on whatever node is serving it until that node fails, or
# an administrator intervenes.
#
# The possible values for auto_failback are:
# on - enable automatic failbacks
# off - disable automatic failbacks
# legacy - enable automatic failbacks in systems
# where all nodes do not yet support
# the auto_failback option.
#
# auto_failback "on" and "off" are backwards compatible with the old
# "nice_failback on" setting.
#
# See the FAQ for information on how to convert
# from "legacy" to "on" without a flash cut.
# (i.e., using a "rolling upgrade" process)
#
# The default value for auto_failback is "legacy", which
# will issue a warning at startup. So, make sure you put
# an auto_failback directive in your ha.cf file.
# (note: auto_failback can be any boolean or "legacy")
#
auto_failback on
#
#
# Basic STONITH support
# Using this directive assumes that there is one stonith
# device in the cluster. Parameters to this device are
# read from a configuration file. The format of this line is:
#
# stonith <stonith_type> <configfile>
#
# NOTE: it is up to you to maintain this file on each node in the
# cluster!
#
#stonith baytech /etc/ha.d/conf/stonith.baytech
#
# STONITH support
# You can configure multiple stonith devices using this directive.
# The format of the line is:
# stonith_host <hostfrom> <stonith_type> <params...>
# <hostfrom> is the machine the stonith device is attached
# to or * to mean it is accessible from any host.
# <stonith_type> is the type of stonith device (a list of
# supported drives is in /usr/lib/stonith.)
# <params...> are driver specific parameters. To see the
# format for a particular device, run:
# stonith -l -t <stonith_type>
#
#
# Note that if you put your stonith device access information in
# here, and you make this file publically readable, you're asking
# for a denial of service attack ;-)
#
# To get a list of supported stonith devices, run
# stonith -L
# For detailed information on which stonith devices are supported
# and their detailed configuration options, run this command:
# stonith -h
#
#stonith_host * baytech 10.0.0.3 mylogin mysecretpassword
#stonith_host ken3 rps10 /dev/ttyS1 kathy 0
#stonith_host kathy rps10 /dev/ttyS1 ken3 0
#
# Watchdog is the watchdog timer. If our own heart doesn't beat for
# a minute, then our machine will reboot.
# NOTE: If you are using the software watchdog, you very likely
# wish to load the module with the parameter "nowayout=0" or
# compile it without CONFIG_WATCHDOG_NOWAYOUT set. Otherwise even
# an orderly shutdown of heartbeat will trigger a reboot, which is
# very likely NOT what you want.
#
#watchdog /dev/watchdog
#
# Tell what machines are in the cluster
# node nodename ... -- must match uname -n
#node ken3
#node kathy
node node1.firefox.com
node node2.firefox.com
#
# Less common options...
#
# Treats 10.10.10.254 as a psuedo-cluster-member
# Used together with ipfail below...
# note: don't use a cluster node as ping node
#
ping 172.16.0.1
#
# Treats 10.10.10.254 and 10.10.10.253 as a psuedo-cluster-member
# called group1. If either 10.10.10.254 or 10.10.10.253 are up
# then group1 is up
# Used together with ipfail below...
#
#ping_group group1 10.10.10.254 10.10.10.253
#
# HBA ping derective for Fiber Channel
# Treats fc-card-name as psudo-cluster-member
# used with ipfail below ...
#
# You can obtain HBAAPI from http://hbaapi.sourceforge.net. You need
# to get the library specific to your HBA directly from the vender
# To install HBAAPI stuff, all You need to do is to compile the common
# part you obtained from the sourceforge. This will produce libHBAAPI.so
# which you need to copy to /usr/lib. You need also copy hbaapi.h to
# /usr/include.
#
# The fc-card-name is the name obtained from the hbaapitest program
# that is part of the hbaapi package. Running hbaapitest will produce
# a verbose output. One of the first line is similar to:
# Apapter number 0 is named: qlogic-qla2200-0
# Here fc-card-name is qlogic-qla2200-0.
#
#hbaping fc-card-name
#
#
# Processes started and stopped with heartbeat. Restarted unless
# they exit with rc=100
#
#respawn userid /path/name/to/run
#respawn hacluster /usr/lib/heartbeat/ipfail
#
# Access control for client api
# default is no access
#
#apiauth client-name gid=gidlist uid=uidlist
#apiauth ipfail gid=haclient uid=hacluster
###########################
#
# Unusual options.
#
###########################
#
# hopfudge maximum hop count minus number of nodes in config
#hopfudge 1
#
# deadping - dead time for ping nodes
#deadping 30
#
# hbgenmethod - Heartbeat generation number creation method
# Normally these are stored on disk and incremented as needed.
#hbgenmethod time
#
# realtime - enable/disable realtime execution (high priority, etc.)
# defaults to on
#realtime off
#
# debug - set debug level
# defaults to zero
#debug 1
#
# API Authentication - replaces the fifo-permissions-based system of the past
#
#
# You can put a uid list and/or a gid list.
# If you put both, then a process is authorized if it qualifies under either
# the uid list, or under the gid list.
#
# The groupname "default" has special meaning. If it is specified, then
# this will be used for authorizing groupless clients, and any client groups
# not otherwise specified.
#
# There is a subtle exception to this. "default" will never be used in the
# following cases (actual default auth directives noted in brackets)
# ipfail (uid=HA_CCMUSER)
# ccm (uid=HA_CCMUSER)
# ping (gid=HA_APIGROUP)
# cl_status (gid=HA_APIGROUP)
#
# This is done to avoid creating a gaping security hole and matches the most
# likely desired configuration.
#
#apiauth ipfail uid=hacluster
#apiauth ccm uid=hacluster
#apiauth cms uid=hacluster
#apiauth ping gid=haclient uid=alanr,root
#apiauth default gid=haclient
# message format in the wire, it can be classic or netstring,
# default: classic
#msgfmt classic/netstring
# Do we use logging daemon?
# If logging daemon is used, logfile/debugfile/logfacility in this file
# are not meaningful any longer. You should check the config file for logging
# daemon (the default is /etc/logd.cf)
# more infomartion can be fould in http://www.linux-ha.org/ha_2ecf_2fUseLogdDirective
# Setting use_logd to "yes" is recommended
#
# use_logd yes/no
#
# the interval we reconnect to logging daemon if the previous connection failed
# default: 60 seconds
#conn_logd_time 60
#
#
# Configure compression module
# It could be zlib or bz2, depending on whether u have the corresponding
# library in the system.
compression bz2
#
# Confiugre compression threshold
# This value determines the threshold to compress a message,
# e.g. if the threshold is 1, then any message with size greater than 1 KB
# will be compressed, the default is 2 (KB)
compression_threshold 2
crm respawn
(2)配置heartbeat的认证文件(/etc/ha.d/authkeys)
authkeys文件用于设定heartbeat的认证方式,该文件有3种可用的认证方式:crc、sha1和md5,这里使用sha1的认证方式。设置如下:使用openssl生成秘钥;然后再将其写入配置文件中;
openssl rand -hex 8
80dd467590678c72
cat /etc/ha.d/authkeys
auth 2
#1 crc
2 sha1 80dd467590678c72
#3 md5 Hello!
需要要说明的一点是,无论“auth”后面指定的是什么数字,在下一行必须作为关键字再 次出现,例如指定了 “auth6”,下面一定要有一行“6认证类型'
最后确保这个文件的权限是600 (即-rw——)。
(3) 配置heartbeat的资源文件;这里我们使用hb_gui 配置;
# rpm -ivh heartbeat-gui-2.1.4-12.el6.x86_64.rpm
[root@node1 ~]# hb_gui &
由于heartbeat托管了主、备Director Server上的Ldirectord服务,因此只需在主、备两 台机器上启动heartbeat服务即可,这样Ldirectord服务就在主机上启动起来了。
(2)启动Real Server节点服务
分别在两个Real Server节点上执行如下脚本:
[root@rsl~]#./reserver.sh
至此,通过heartbeat构建的髙可用LVS集群系统已经配置完成并运行起来了.
5.2通过Keepalived搭建LVS高可用性集群系统
1.配置 Keepalived
Keepalived的配置非常简单,仅需要一个配置文件即可完成对HA cluster和LVS服务节点监控。Keepalived的安装已经在前面介绍过,在通过Keepalived搭建高可用的LVS集群实 例中,主、备Director Server都需要安装Keepalived软件,安装成功后,默认的配置文件路 径为/etc/Keepalived/Keepalived.conf. —个完整的keepalived配置文件由3个部分组成,分 别是全局定义部分、vrrp实例定义部分以及虚拟服务器定义部分.下面详细介绍这个配置文 件中毎个选项的详细含义和用法。
! Configuration File for keepalived
global_defs {
notification_email {
[email protected] # 设置报警邮件地址,可以设置多个
[email protected] # 每一行一个。注意如果开启邮件报警,需要开启本机的sendmail服务
[email protected]
}
notification_email_from [email protected] # 设置邮件的发送地址
smtp_server 192.168.200.1 # 设置stmp server的地址
smtp_connect_timeout 30 # 设置stmp server 的超时时间
router_id LVS_DEVEL # 表示运行keepalived服务器的一个标识。发邮件时显示在邮件主题中的信息
}
# vrrp实例定义部分
vrrp_instance VI_1 {
state MASTER #指定Keepalived的角色,MASTER表示此主机是主服务器, # BACKUP表示此主机是备用服务器
interface eth0 #指定HA监測网络的接口
virtual_router_id 51 #虚拟路由标识,这个标识是一个数字,—个vrrp实例使用唯一的标识, #即同一个vrrp_instance下,MASTER和BACKUP必播是一致的
priority 100 #定义优先级,數字越大,优先级趣高.在一个vrrp_instance下, #MASTER的优先级必大于BACKUP的优先级
advert_int 1 #设定MASTER与BACKUP负載均衡器之间同步检查的时间间隔,单位是秒
authentication { #设定验证类型和密码
auth_type PASS #设置验证类型,主要有PASS和AH两种
auth_pass 1111 设置验证密码,在一个vrrp_instance下, #MASTER与BACKUP必用使用相同的密码才能正常通信
}
# 虚拟服务器定义部分
virtual_server 192.168.200.100 443 {
#设里虚拟服务器,需要相定虛拟IP地址和服务端口,IP与端口之间用空格隔开
delay_loop 6
#设里运行情况检查时间,单位是秒
lb_algo rr
#设里负载调度算法,这里设置为rr,即轮询算法
lb_kind NAT
#设置LVS实现负栽均衡的机制,有NAT、TUN和DR三个模式可选
persistence_timeout 50
#会话保持时间,单位是秒.这个选项对动态网页是非常有用的,为集群系统中的session共享提供了一个很好的解决方案.有了这个会诂保持功能,用户的请求会祓一直分发到某个服务节点,直到超过这个会谘的保持时间.需要注意的是, 这个会话保持时间是最大无响应超时时间,也就是说,用户在操作动态頁网面时,如果在50秒内没有执行任何操作,那么接下来的操作会被分发到 另外的节点,但是如果用户一直在操作动态頁面,则不受50秒的时间限制 .
protocol TCP
# 指定转发协议类型,有TCP和UDP两种
real_server 192.168.201.100 443 {
# 置服务节点1,需要指定real server的其实IP地址和端口,IP与端口之间用空格隔开
weight 1
配置服务节点的权值,权值大小用數字表示,數字越大,权值越高,设置权值的大小可以为不同性能的服务器分配不同的负载,可以为性能高的服务器设置较高的权值.而为性能较低的服务器设置相对较低的权值,这样才能合埋地利用和分配系统資源 #realserve的状态检测设罝部分,单位是秒
HTTP_GET | SSL_GET{
url{ #检查url,可以指定多个
path /
digest <string> #检查后的摘要信息
status_code 200 #检查的返回状态码
}
connect_port <port>
bindto <IPADD>
connect_timeout 5
#表示5秒无响应超时
nb_get_retry 3
#表示重读次数
delay_before_retry 2
#表示重式间隔
}
当前环境的配置:
[root@node1 /]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 172.16.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.250.250
}
}
virtual_server 172.16.250.250:80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1:80
real_server 172.16.249.10 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.249.9 80 {
weight 1
TCP_CHECK {
connect_timeout3
nb_get_retry 3
delay_before_retry 3
}
}
}
在配置Keepalived.conf时,需要特别注意配置文件的语法格式,因为Keepalived在启 动时并不检测配置文件的正确性,即使没有配置文件,Keepalived也照样能够启动,所以一 定要保证配置文件正确。
在默认情况下,Keepalived在启动时会査找/etc/Keepalived/Keepalived.conf配置文件, 如果配置文件放在了其他路径下,可以通过“Keepalived -f”参数指定配置文件的路径即可。
Keepalived.conf配置完毕后,将此文件复制到备用Director Server对应的路径下,然后 进行以下两个简单的修改即可:
□将 “state MASTER” 更改为 “state BACKUP”。
□将“priority 100”更改为一个较小的值,这里改为“priority 80”。
2.配置Real server节点
与heartbeat+LVS类似,Keepalived+LVS也需要为Real server节点配置相关的脚本,以 达到与Director Server相互通信的目的。脚本的内容已经在前面介绍过,这里不再讲述。
3.启动Keepalived+LVS集群系统
在主、备Director Server上分别启动Keepalived服务,可以执行如下操作:
/etc/rc.d/init.d/keepalived start
./reserver start
至此,keepalived+LVS 高可用的LVS集群系统已经运行起来了。