Cent OS 5.4 Heartbeat3.0.3 + LVS

记录一下自己搭建Heartbeat+LVS的过程,花费了不少时间,并没有想象中的那么简单。

  Heartbeat提供了高可用集群最基本的功能,例如,节点间的内部通信方式、集群合作管理机制、监控工具和失效切换功能等。但是Heartbeat仅仅是个HA软件,它仅能完成心跳监控和资源接管,不会监视它控制的资源或应用程序。要监控资源和应用程序是否运行正常,必须使用第三方的插件,例如ipfail、Mon和Ldirector等。Heartbeat自身包含了几个插件,分别是ipfail、Stonith和 Ldirectord。

  首先到http://www.linux-ha.org/wiki/Downloads中下载所需软件,分别是Heartbeat、Cluster Glue、Resource Agents。编译的顺序是:先Cluster Glue, 再Resource Agents,然后Heartbeat。另外需要确认安装了autoconf, automake,pkgconfig,libxslt-devel等包

===============================================================

环境

Linux-master  10.10.50.217

Linux-slave  10.10.50.151

Linux-web-01  10.10.50.197

Linux-web-02  10.10.50.215

VIP:10.10.50.216

===============================================================

Heartbeat


编译安装的时候可能遇到一个问题是

?
1
2
3
4
5
6
7
8
9
10
11
12
13
./.libs/libplumb.so: undefined reference to `uuid_parse'
./.libs/libplumb.so: undefined reference to `uuid_generate'
./.libs/libplumb.so: undefined reference to `uuid_copy'
./.libs/libplumb.so: undefined reference to `uuid_is_null'
./.libs/libplumb.so: undefined reference to `uuid_unparse'
./.libs/libplumb.so: undefined reference to `uuid_clear'
./.libs/libplumb.so: undefined reference to `uuid_compare'
collect2: ld returned 1 exit status
gmake[2]: *** [ipctest] Error 1
gmake[2]: Leaving directory `/root/Reusable-Cluster-Components-glue-1.0.6/lib/clplumbing'
gmake[1]: *** [all-recursive] Error 1
gmake[1]: Leaving directory `/root/Reusable-Cluster-Components-glue-1.0.6/lib'
make: *** [all-recursive] Error 1

 

  解决方法:在configure时在后面加上LIBS='/lib/libuuid.so.1'。这个问题在对三个包进行make的时候都会出现类似的错误,所以make三个包时都要用到,我在编译中没有遇到这个问题。

 

安装步骤

安装过程中会在INTERNET中校验一些xml文件,会耗费大量时间,要耐心等待。如果报错可能是网络引起,多试几次就行。

一、安装 Reusable-Cluster-Components-glue-1.0.6.tar.bz2

#groupadd hacluster

#groupadd haclient

#useradd hacluster -g hacluster

#tar -jxf Reusable-Cluster-Components-glue-1.0.6.tar.bz2

#cd Reusable-Cluster-Components-glue-1.0.6

#./autogen.sh

#./configure

#make

#make install

这里编译的时候会遇到另外一个问题

 

?
1
2
3
4
5
6
7
8
9
10
cc1: warnings being treated as errors 
main.c:64: warning: function declaration isn't a prototype
main.c:78: warning: function declaration isn't a prototype
gmake[2]: *** [main.o] Error 1
gmake[2]: Leaving directory
`/root/Reusable-Cluster-Components-glue-1.0.6/lib/stonith'
gmake[1]: *** [all-recursive] Error 1
gmake[1]: Leaving directory `/root/Reusable-Cluster-Components-glue-1.0.6/lib'
make: *** [all-recursive] Error 1
error: Bad exit status from /var/tmp/rpm-tmp.55884 ( %build )

 

  解决方法:编辑lib/stonith/main.c,将version相关的注释掉

1、找到其64行,用/* ....*/注释掉。

2、找到其76到81行 用/* ....*/注释掉。

3、找到其390到 391 行,用/* ....*/注释掉。

 

二、安装 Cluster-Resource-Agents-agents-1.0.3

#tar -jxf Cluster-Resource-Agents-agents-1.0.3.tar.bz2

#cd Cluster-Resource-Agents-agents-1.0.3

#./autogen.sh

#./configure

#make

#make install

 

三、安装 Heartbeat-3-0-STABLE-3.0.3.tar.bz2

# tar -jxf Heartbeat-3-0-STABLE-3.0.3.tar.bz2

#cd Heartbeat-3-0-STABLE-3.0.3

#./bootstrap

#./ConfigureMe configure

#make

#make install

 

编辑配置文件

# cp /usr/share/doc/ha.cf /etc/ha.d/

#cp /usr/share/doc/haresources /etc/ha.d/

#cp /usr/share/doc/authkeys /etc/ha.d/

heartbeat的配置文件有两个位置/etc/ha.d与/usr/etc/ha.d,将/usr/etc/ha.d中文件全部copy到/etc/ha.d中,删除/usr/etc/ha.d,然后使用命令ln -s /etc/ha.d /usr/etc/ha.d创建软连接

 

1、配置ha.cf

debugfile /var/log/ha-debug:该文件保存heartbeat的调试信息
logfile /var/log/ha-log:heartbeat的日志文件
keepalive 2:心跳的时间间隔,默认时间单位为秒
deadtime 30:超出该时间间隔未收到对方节点的心跳,则认为对方已经死亡。
warntime 10:超出该时间间隔未收到对方节点的心跳,则发出警告并记录到日志中。
initdead 120:在某些系统上,系统启动或重启之后需要经过一段时间网络才能正常工作,该选项用于解决这种情况产生的时间间隔。取值至少为deadtime的两倍。

udpport 694:设置广播通信使用的端口,694为默认使用的端口号。
baud 19200:设置串行通信的波特率。
serial /dev/ttyS0:选择串行通信设备,用于双机使用串口线连接的情况。如果双机使用以太网连接,则应该关闭该选项。
bcast eth0:设置广播通信所使用的网络接口卡。
auto_failback on:heartbeat的两台主机分别为主节点和从节点。主节点在正常情况下占用资源并运行所有的服务,遇到故障时把资源交给从节点并由从节点运行服务。在该选项设为on的情况下,一旦主节点恢复运行,则自动获取资源并取代从节点,否则不取代从节点。

ping ping-node1 ping-node2:指定ping node,ping node并不构成双机节点,它们仅仅用来测试网络连接。
respawn hacluster /usr/lib/heartbeat/ipfail:指定与heartbeat一同启动和关闭的进程,该进程被自动监视,遇到故障则重新启动。最常用的进程是ipfail,该进程用于检测和处理网络故障,需要配合ping语句指定的ping node来检测网络连接。

 

2、 配置haresources文件

haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址以及启动的服务等。其配置语句格式如下:

node-name  network-config  <resource-group>
其中node-name指定双机系统的主节点,取值必须匹配ha.cf文件中node选项设置的主机名中的一个,node选项设置的另一个主机名成为从节点。network-config用于设置VIP(虚拟IP,也就是真正对外提供服务的IP)。resource-group用于设置heartbeat启动的服务,该服务最终由双机系统通过集群IP对外提供。

这次试验中的配置如下:

Linux-Master 10.10.50.216 httpd

 

3、配置authkeys文件

authkeys文件用于heartbeat的鉴权设置,共有三种可用的鉴权方式:crc、md5和sha1。三种方式安全性依次提高,但同时占用的系统资源也依次扩大。crc安全性最低,适用于物理上比较安全的网络,sha1提供最为有效的鉴权方式,占用的系统资源也最多。

其配置语句格式如下:
auth <number> 
<number> <authmethod> [<authkey>]
举例说明:
auth 1 
1 sha1 key-for-sha1
其中键值key-for-sha1可以任意指定,number设置必须保证上下一致。

auth 2 
2 crc
crc方式不需要指定键值。
最后,设置authkeys文件权限为600(即-rw-------),命令为:chmod 600 authkeys


启动服务

#/etc/init.d/heartbeat start

 


另外有两个比较有用的脚本

/usr/share/heartbeat/hb_standby  #强制将资源漂移到备份节点

/usr/share/heartbeat/hb_takeover  #强制将资源漂移到主节点

 

===============================================================

LVS

 

安装步骤

#tar zxvf ipvsadm-1.24.tar.gz

#cd ipvsadm-1.24

#ln -s /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linux

#make && make install

#cp /usr/local/src/Cluster-Resource-Agents-agents-1.0.3/ldirectord/ldirectord.cf /etc/ha.d/

 

配置ldirectord.cf

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Global Directives
checktimeout=10
checkinterval=3
#fallback=127.0.0.1:80
autoreload=yes
#logfile="/var/log/ldirectord.log"
#logfile="local0"
#emailalert="[email protected]"
#emailalertfreq=3600
#emailalertstatus=all
quiescent= no
 
# Sample for an http virtual service
virtual=10.10.50.216:80
         real=10.10.50.197:80 gate
         real=10.10.50.215:80 gate
         fallback=127.0.0.1:80 gate
         service=http
         scheduler=wlc
         #persistent=600
         #netmask=255.255.255.255
         protocol=tcp
         checktype=negotiate
         checkport=80
         request= "check.html"
         receive= "OKAY"
         #virtualhost=www.x.y.z

 

LVS脚本,在Linux-master、Linux-slave上运行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/sh
#director.sh
 
VIP=10.10.50.216
RIP1=10.10.50.197
RIP2=10.10.50.215
 
. /etc/rc.d/init.d/functions
 
case "$1" in
     start)
        echo " start LVS  of DirectorServer"
        /sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add -host $VIP dev eth0:0
        /sbin/ipvsadm -C
        /sbin/ipvsadm -A -t $VIP :80 -s wlc
        /sbin/ipvsadm -a -t $VIP :80 -r $RIP1 :80 -g
        /sbin/ipvsadm -a -t $VIP :80 -r $RIP2 :80 -g
        /sbin/ipvsadm
         ;;
     stop)
         echo "close LVS Directorserver"
         ifconfig eth0:0 down
         /sbin/ipvsadm -C
         ;;
     *)
         echo "Usage: $0 {start|stop}"
         exit 1
esac

 

客户端脚本,在Linux-web-01、Linux-web-02上运行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/sh
#realserver.sh
 
VIP=10.10.50.216
 
. /etc/rc.d/init.d/functions
 
case "$1" in
     start)
         echo "Tunl port starting"
         /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up
         /sbin/route add -host $VIP dev lo:0
         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 lo:0 down
         ;;
     *)
         echo "Usage: $0 {start|stop}"
         exit 1
esac

 

我这里客户端上的WEB程序选用的是Nginx,在客户端输入

#echo "OKAY" > /usr/local/nginx/html/check.html


ipvsadm 的用法和格式如下:

ipvsadm -A|E -t|u|f virutal-service-address:port [-s scheduler] [-p
[timeout]] [-M netmask]
ipvsadm -D -t|u|f virtual-service-address
ipvsadm -C
ipvsadm -R
ipvsadm -S [-n]
ipvsadm -a|e -t|u|f service-address:port -r real-server-address:port
[-g|i|m] [-w weight]
ipvsadm -d -t|u|f service-address -r server-address
ipvsadm -L|l [options]
ipvsadm -Z [-t|u|f service-address]
ipvsadm --set tcp tcpfin udp
ipvsadm --start-daemon state [--mcast-interface interface]
ipvsadm --stop-daemon
ipvsadm -h
命令选项解释:
有两种命令选项格式,长的和短的,具有相同的意思。在实际使用时,两种都可
以。
-A --add-service 在内核的虚拟服务器表中添加一条新的虚拟服务器记录。也就是增加一台新的虚拟服务器。
-E --edit-service 编辑内核虚拟服务器表中的一条虚拟服务器记录。
-D --delete-service 删除内核虚拟服务器表中的一条虚拟服务器记录。
-C --clear 清除内核虚拟服务器表中的所有记录。
-R --restore 恢复虚拟服务器规则
-S --save 保存虚拟服务器规则,输出为-R 选项可读的格式
-a --add-server 在内核虚拟服务器表的一条记录里添加一条新的真实服务器记录。也就是在一个虚拟服务器中增加一台新的真实服务器
-e --edit-server 编辑一条虚拟服务器记录中的某条真实服务器记录
-d --delete-server 删除一条虚拟服务器记录中的某条真实服务器记录
-L|-l --list 显示内核虚拟服务器表
-Z --zero 虚拟服务表计数器清零(清空当前的连接数量等)
--set tcp tcpfin udp 设置连接超时值
--start-daemon 启动同步守护进程。他后面可以是master 或backup,用来说明LVS Router 是master 或是backup。在这个功能上也可以采用keepalived 的
VRRP 功能。
--stop-daemon 停止同步守护进程
-h --help 显示帮助信息
其他的选项:
-t --tcp-service service-address 说明虚拟服务器提供的是tcp 的服务[vip:port] or [real-server-ip:port]
-u --udp-service service-address 说明虚拟服务器提供的是udp 的服务[vip:port] or [real-server-ip:port]
-f --fwmark-service fwmark 说明是经过iptables 标记过的服务类型。
-s --scheduler scheduler 使用的调度算法,有这样几个选项
rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
默认的调度算法是: wlc.
-p --persistent [timeout] 持久稳固的服务。这个选项的意思是来自同一个客户的多次请求,将被同一台真实的服务器处理。timeout 的默认值为300 秒。
-M --netmask netmask persistent granularity mask
-r --real-server server-address 真实的服务器[Real-Server:port]
-g --gatewaying 指定LVS 的工作模式为直接路由模式(也是LVS 默认的模式)
-i --ipip 指定LVS 的工作模式为隧道模式
-m --masquerading 指定LVS 的工作模式为NAT 模式
-w --weight weight 真实服务器的权值
--mcast-interface interface 指定组播的同步接口
-c --connection 显示LVS 目前的连接 如:ipvsadm -L -c
--timeout 显示tcp tcpfin udp 的timeout 值 如:ipvsadm -L --timeout
--daemon 显示同步守护进程状态
--stats 显示统计信息
--rate 显示速率信息
--sort 对虚拟服务器和真实服务器排序输出
--numeric -n 输出IP 地址和端口的数字形式

猜你喜欢

转载自huaonline.iteye.com/blog/1341463