heartbeat是一种心跳工具,在双机配置中通过相互监听对方的心跳,当有master心跳消失时,slave就会接管heartbeat的资源,继续对外提供服务。heartbeat的master slave是手工指定的,在haresources中手工配置了节点信息,让某一个节点为主节点,其他节点默认就是slave节点,master节点和slave节点之间相互发送心跳信息,当slave收不到master的心跳时,当超过超时时间,就认为master故障,slave就会接管haresource中的资源信息。
1、 增加网卡,编辑配置文件,启用网卡
启用eth1 ,配置eth1地址为10.0.0.2,配置脚本是将eth0的配置文件拷贝一份,然后修改为eth1的配置。
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1
查看eth1的MAC地址,在cat /etc/udev/rules.d/70-persistent-net.rules,将获取到的MAC地址写入到ifcfg-eth1中
启用eth1,ifup eth1
2、 测试心跳接口之间的连通性
3、 下载安装heartbeat软件
首先安装依赖软件,安装几个组件包,组件包在DVD1中。
yum -y groupinstall "Development tools"
yum -y groupinstall "High Availability"
yum -y groupinstall "High Availability Management"
yum -y install autoconf automake libtool gcc gcc-c++ libxml2-devel bzip2-devel glib2-devel libtool libtool-ltdl PyXML
下面的软件包,在DVD2中,需要将DVD1和DVD2打进一个ISO文件中进行安装
yum -y install cluster-glue
yum -y install pacemaker
安装了这两个组件之后,下面的组件就可以不用安装了
yum -y install heartbeat*
http://rpmfind.net/linux/RPM/epel/6/x86_64/Packages/h/heartbeat-3.0.4-2.el6.x86_64.html heartbeat组件下载地址
http://rpmfind.net/linux/rpm2html/search.php?query=yum 开源软件包下载
下载三个组件,上传至服务器中。
heartbeat-3.0.4-2.el6.x86_64.rpm heartbeat-devel-3.0.4-2.el6.x86_64.rpm heartbeat-libs-3.0.4-2.el6.x86_64.rpm
然后通过rpm -ivh 进行安装
[root@Zabbix heartbeat_install_rpm]# rpm -ivh heartbeat-3.0.4-2.el6.x86_64.rpm heartbeat-devel-3.0.4-2.el6.x86_64.rpm heartbeat-libs-3.0.4-2.el6.x86_64.rpm
warning: heartbeat-3.0.4-2.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:heartbeat-libs ########################################### [ 33%]
2:heartbeat ########################################### [ 67%]
3:heartbeat-devel ########################################### [100%]
通过命令rpm -ql heartbeat,列出所有关于heartbeat的软件目录,找到三个文件,
[root@Zabbix ~]# cat /etc/ha.d/README.config
You need three configuration files to make heartbeat happy,
and they all go in this directory.
They are:
ha.cf Main configuration file
haresources Resource configuration file
authkeys Authentication information
将配置文件,从安装目录中拷贝到应用目录中。
[root@Server129 heartbeat_install]# cp /usr/share/doc/heartbeat-3.0.4/ha.cf /etc/ha.d/
[root@Server129 heartbeat_install]# cp /usr/share/doc/heartbeat-3.0.4/haresources /etc/ha.d/
[root@Server129 heartbeat_install]# cp /usr/share/doc/heartbeat-3.0.4/authkeys /etc/ha.d/
[root@Server129 heartbeat_install]#
[root@Server129 heartbeat_install]# cd /etc/ha.d/
[root@Server129 ha.d]# ls
authkeys ha.cf harc haresources rc.d README.config resource.d shellfuncs
其中,ha.cf是主要的配置文件,在ha.cf中主要定义了ha的日志文件、keepalive的时间、deadtime时间、warntime告警时间、初始化等待时间(重启)、心跳监控端口UDP、串口波特率、心跳端口(以太网线)、对端peer的地址和出接口。具体配置重点如下:
debugfile /var/log/ha-debug
logfile /var/log/ha-log
keepalive 2
deadtime 30
warntime 10
udpport 694
ucast eth0 192.168.1.2 #节点之间相互单播,出接口和对端peer心跳端口地址
auto_failback on #自动回切,当master主机恢复,自动接管haresource,通常应该是手动切换,所以应该是off
watchdog /dev/watchdog
node ken3 #需要将在ha cluster组中的所有的结点的主机名
ping 10.10.10.254 #监测第三方地址,如果地址不通,则会发生切换,不能是ha 组中的节点
日志文件要创建目录,/var/log目录是自动创建的,如果修改了需要手动创建日志目录。
配置后文件如下:
[root@Server129 ha.d]# egrep -v "^#|^$" ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
ucast eth1 10.0.0.2
auto_failback off
watchdog /dev/watchdog
node Server129
node Zabbix
[root@Server129 ha.d]#
Node中的域名,主机必须要能解析到,可以配置在/etc/hosts文件中。
配置认证文件authenkeys,选择1最简单的校验方式CRC
[root@CentOS_server ha.d]# egrep -v "^#" authkeys
auth 1
1 crc
配置ha的资源haresources,由于我们配置的是nginx服务,所以要将nginx服务的启动命令,防止在ha.d目录下的resource.d目录下,然后在haresources中配置。
CentOS_server IPaddr::192.168.245.200/24/eth0 nginx
在两台服务器上都配置这条命令,意思是默认的master主机是CentOS_server,使用的虚拟地址是192.168.245.200/24/eth0,启动的脚本是nginx。
[root@CentOS_server ha.d]# ls resource.d/
apache db2 Filesystem ICP IPaddr IPsrcaddr LinuxSCSI MailTo OCF Raid1 ServeRAID WinPopup
AudibleAlarm Delay hto-mapfuncs ids IPaddr2 IPv6addr LVM nginx portblock SendArp WAS Xinetd
[root@CentOS_server ha.d]#
在resource.d目录中,存放了所有需要heartbeat服务切换的应用启动脚本。
4、 配置nginx服务脚本,nginx服务器现在监听的IP地址应该是虚拟服务器的地址。
server {
listen 192.168.245.200:80;
server_name www.testnginx.org testnginx.org;
5、 在两台服务器上配置完成后,就可以进行heartbeat的测试了。启动两台设备的heartbeat服务。/etc/init.d/heartbeat start来启动,然后观察日志文件中的记录信息。日志文件在/var/log/ha-log中,通过每台主机上的ha.cf来进行配置。
6、 服务起来之后,就可以通过VIP来访问应用了。当主机的heartbeat守护进程故障之后,就会触发主备切换,slave主机接管VIP资源。
7、 配置好heartbeat之后,被heartbeat接管的资源,就不能开机自启动了,需要从开机启动中移除,然后将heartbeat添加开机启动。
8、 Heartbeat是基于RPC远程过程调用来实现的,启动了heartbeat之后,系统会默认启动RPC,
nginx启动脚本如下:
#!/bin/sh # chkconfig: 2345 80 90 # description: Start and Stop nginx #PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon" NAME=nginx DAEMON=/application/nginx/sbin/$NAME CONFIGFILE=/application/nginx/conf/$NAME.conf PIDFILE=/application/nginx/logs/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME set -e [ -x "$DAEMON" ] || exit 0 do_start() { $DAEMON -c $CONFIGFILE || echo -n "nginx already running" } do_stop() { kill -INT `cat $PIDFILE` || echo -n "nginx not running" } do_reload() { kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" do_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" do_stop echo "." ;; reload|graceful) echo -n "Reloading $DESC configuration..." do_reload echo "." ;; restart) echo -n "Restarting $DESC: $NAME" do_stop do_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2 exit 3 ;; esac exit 0
经过测试,只要断开了网络心跳线,就会出现脑裂的问题,所以建议是将心跳和业务放在同一个网卡上,或者是使用单独的物理外置心跳线。