一、Heartbeat概述:
Heartbeat的工作原理:heartbeat最核心的包括两个部分,心跳监测部分和资源接管部分,心跳监测可以通过网络链路和串口进行,而且支持冗余链路,它们之间相互发送报文来告诉对方自己当前的状态,如果在指定的时间内未收到对方发送的报文,那么就认为对方失效,这时需启动资源接管模块来接管运行在对方主机上的资源或者服务。
Heartbeat-3.X版本以后被分为了4个模块,这些安装包都可以从官网:
http://www.linux-ha.org/wiki/Downloads下载得到:
目前的这些版本是:
ClusterLabs-resource-agents-v3.9.2-0-ge261943.tar.gz ??#集群实验资源代理
Heartbeat-3-0-7e3a82377fa8.tar.bz2 ?# ?心跳主程序包
pacemaker-1.1.9-1512.el6.src.rpm ???# ?起搏器
Reusable-Cluster-Components-glue--glue-1.0.9.tar.bz2 ?#可重复使用的群集组件
端口号:694[root@xuegod62 ~]# cat /etc/services | grep 694ha-cluster 694/tcp # Heartbeat HA-clusterha-cluster 694/udp # Heartbeat HA-cluster
扩展: 谁管理着TCP/UDP公共服务的端口定义
IANA 就是指(Internet Assigned Numbers Authority) ,Internet号分配的机构。负责对IP地址分配规划以及对TCP/UDP公共服务的端口定义。
IANA的所有任务可以大致分为三个类型:
一、域名。IANA管理DNS域名根和.int,.arpa域名以及IDN(国际化域名)资源。
二、数字资源。IANA协调全球IP和AS(自治系统)号并将它们提供给各区域Internet注册机构。
注: AS自治系统号,是BGP路由协议中的号。
三、协议分配。IANA与各标准化组织一同管理协议编号系统。
官网:http://www.iana.org/
二、实战:使用Heartbeat实现web服务器高可用
1.Heartbeat 拓扑图
xuegod63 主web
xuegod64 从web
xuegod62 ?NFS
2.准备工作:xuegod63,xuegod64
1. 修改主机名,永久生效 # vim /etc/sysconfig/networkHOSTNAME=xuegod63.cn2. 解析 ?# vim /etc/hosts192.168.1.63 ??xuegod63.cn192.168.1.64 ??xuegod64.cn3. 关防火墙,关掉selinux# iptables -F# getenforce
3.配置xuegod62为NFS服务器,提供存储资源
[root@xuegod62 ~]# yum -y install nfs-utils [root@xuegod62 ~]# mkdir /wwwdir[root@xuegod62 ~]# echo ‘Heartbeat web test‘> /wwwdir/index.html[root@xuegod62 ~]# cat !$cat /wwwdir/index.htmlHeartbeat web test[root@xuegod62 ~]# vim /etc/exports[root@xuegod62 ~]# cat !$cat /etc/exports/wwwdir192.168.1.0/24(rw)[root@xuegod62 ~]# chmod 777 -R /wwwdir/[root@xuegod62 ~]# ll -d /wwwdir/drwxrwxrwx 2 root root 24 Jul ?3 15:19 /wwwdir/[root@xuegod62 ~]# systemctl start nfs[root@xuegod62 ~]# systemctl enable nfsCreated symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.[root@xuegod62 ~]# showmount -eExport list for xuegod70.cn:/wwwdir 192.168.1.0/24
4.xuegod63,64测试nfs 存储挂载并安装httpd web服务器(基本操作是一致)
[root@xuegod63 ~]# yum install nfs-utils httpd -y[root@xuegod63 ~]# systemctl start nfs[root@xuegod63 ~]# showmount -e 192.168.1.62Export list for 192.168.1.62:/wwwdir 192.168.1.0/24[root@xuegod63 ~]# mount -t nfs 192.168.1.62mount: 在 /etc/fstab 中找不到 192.168.1.62[root@xuegod63 ~]# mount -t nfs 192.168.1.62:/wwwdir /var/www/html/[root@xuegod63 ~]# df -h文件系统 ??????????????容量 ?已用 ?可用 已用% 挂载点/dev/sda3 ??????????????19G ?1.9G ??17G ??11% /devtmpfs ??????????????479M ????0 ?479M ???0% /devtmpfs ?????????????????489M ????0 ?489M ???0% /dev/shmtmpfs ?????????????????489M ?6.7M ?482M ???2% /runtmpfs ?????????????????489M ????0 ?489M ???0% /sys/fs/cgroup/dev/sr0 ??????????????4.3G ?4.3G ????0 ?10% /mnt/dev/sda1 ?????????????497M ?113M ?385M ??23% /boottmpfs ??????????????????98M ????0 ??98M ???0% /run/user/0192.168.1.62:/wwwdir ??19G ?1.5G ??18G ???8% /var/www/html[root@xuegod63 ~]# vim /etc/httpd/conf/httpd.conf [root@xuegod63 ~]# systemctl restart httpd[root@xuegod63 ~]# systemctl enable httpdCreated symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.[root@xuegod63 ~]# elinks --dump 192.168.1.62 ??#测试挂载情况 ??Heartbeat web test#卸载资源:后期这些资源通过heartbeat直接加载[root@xuegod63 ~]# umount /var/www/html/[root@xuegod63 ~]# systemctl stop httpd[root@xuegod63 ~]# systemctl disable httpd
5.xuegod63安装heartbeat
heartbeat在Ceontos7下编译步骤
下载:http://www.linux-ha.org/wiki/Downloads,从官方下载最新的HeartBeat版本:
Heartbeat 3.0.6
Cluster Glue 1.0.12
Resource Agents 3.9.6
1.配置基础环境(配置好网络yum源)安装依赖包#yum install -y bzip2 bzip2-devel gcc gcc-c++ autoconf automake libtool e2fsprogs-devel glib2-devel libxml2 libxml2-devel libtool-ltdl-devel asciidoc libuuid-devel docbook如果LTDL包安装不成功,下载rpm包安装:#rpm -ivh libtool-ltdl-devel-2.4.2-22.el7_3.x86_64.rpm2.编译Cluster Glue# tar jxf Reusable-Cluster-Components-glue.tar.bz2# cd Reusable-Cluster-Components-glue--0a7add1d9996/# groupadd haclient ?创建用户组# useradd -g haclient hacluster ??创建用户# ./autogen.sh # ./configure --prefix=/usr/local/heartbeat/# make && make install3.编译Resource Agents# tar -zxf resource-agents-3.9.6.tar.gz# cd resource-agents-3.9.6# ./autogen.sh# ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS=‘/lib64/libuuid.so.1‘# make# make install4.编译Heartbeat# tar -jxvf Heartbeat-3.0.6.tar.bz2# cd Heartbeat-3-0-958e11be8686/# ./bootstrap# export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"# ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS=‘/lib64/libuuid.so.1‘# make # make install5.生成配置文件:# cd /usr/local/heartbeat/etc/ha.d/# cp /root/Heartbeat-3-0-958e11be8686/doc/{ha.cf,haresources,authkeys} .# chkconfig --add heartbeat# chkconfig heartbeat on# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys# mkdir -pv /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/# cp /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/## ln -sv /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/
6、Heartbeat配置Heartbeat的配置主要涉及到ha.cf、haresources、authkeys这三个文件。 ???ha.cf: ?主配置文件, ???haresource: ?用来配置要让Heartbeat托管的服务, ???authkey: ?是用来指定Heartbeat的认证方式。 ???(1) 配置ha.cf----主配置文件# vim /usr/local/heartbeat/etc/ha.d/ha.cf (注:前面数字是行号)24 debugfile /var/log/ha-debug ??????????##用于记录heartbeat的调试信息29 logfile/var/log/ha-log ???????????????##用于记录heartbeat的日志信息34 logfacilitylocal0 ???????????????????##设置heartbeat的日志,这里用的是系统日志48 keepalive 2 ?????????????????????????????##设定心跳(监测)时间时间为2秒56 deadtime 30 ????????##指定若备用节点在30秒内未收到主节点心跳信号,则接管主服务器资源61 warntime 10 ??????????##指定心跳延迟的时间为10秒,10秒内备节点不能接收主节点心跳信号, ?????????????????????????????????即往日志写入警告日志,但不会切换服务71 initdead 60 ????????##系统启动或重启后预留的忽略时间段,取值至少为deadtime的两倍76 udpport ?694 ????????????????????????##广播/单播通讯使用的Udp端口91 #bcast ens32 ??# Linux ????????????##使用网卡eno32发送心跳检测113 #mcast eth0 225.0.0.1 694 1 0 ???##采用网卡eth0的Udp多播来组织心跳,一般在备用节点Bcast、ucast和mcast分别代表广播、单播和多播,是组织心跳的的方式,任选其一121 ucast ens32 192.168.1.64 ??##采用网卡eth32的udp单播来组织心跳,后面跟的IP地址为双机对方IP地址157 auto_failback on ????????????????##定义当主节点恢复后,是否将服务自动切回211 node xuegod63.cn ????????????????????##主节点名称212 node xuegod64.cn ????????????????????##备用节点名称220 ping 192.168.1.1 ???????????????##通过ping网关检测心跳是否正常,仅用来测试网络253 respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail ??##指定和heartbeat一起启动、关闭的进程259 apiauth ipfail gid=haclient uid=hacluster ????##设置启动IPfail的用户和组 ???(2) 配置haresources-----资源文件Haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址及启动服务集群资源,文件每一行可包含一个或多个资源脚本名,资源间使用空格隔开,
参数间使用两个冒号隔开,主节点和备份节点中资源文件haresources要完全一样一般格式为:node-name network ?<resource-group>node-name表示主节点的主机名,必须和ha.cf文件中指定的节点名一致。network用于设定集群的IP地址、子网掩码和网络设备标识等。resource-group用于指定需Heartbeat托管的服务(即这些服务可由Heartbeat来启动和关闭)。注意:这里指定的IP地址就是集群对外服务的IP地址 ??如要托管这些服务,必须将服务写成可通过start/stop来启动或关闭的脚本,放到/etc/init.d/ ??或/etc/ha.d/resource.d/目录下,Heartbeat会根据脚本名称自动去/etc/init.d或者 ??/etc/ha.d/resource.d目录下找到相应脚本进行启动或关闭操作。例:# vim /usr/local/heartbeat/etc/ha.d/haresources (注:前面数字为行号)44 xuegod63.cn IPaddr::192.168.2.111/24/ens32 Filesystem::192.168.2.62:/wwwdir::/var/www/html::nfs ?httpdxuegod71.cn IPaddr::192.168.10.111/24/ens33 Filesystem::192.168.10.70:/wwwdir::/var/www/html::nfs ?httpd注:xuegod63.cn是主服务器的主机名, xuegod64上不需要修改。这样资源默认会加一这个主机上。当xuegod63坏了,xuegod64会再接管。IPaddr::192.168.1.111/24/eth32 ?#指定VIP及绑定到哪个网卡上Filesystem::192.168.1.62:/wwwdir::/var/www/html::nfs ??#指定要挂载的存储httpd ?#指定要启动的服务。这个服务必须是在/etc/init.d下或者/usr/local/heartbeat/etc/ha.d/resource.d目录下 ???(3) 配置authkeys-----心跳密im钥验证文件#vim /usr/local/heartbeat/etc/ha.d/authkeys auth 33 md5 Hello!注:auth后填序号,可任意填写,但第二行开头必须为序号名,然后为验证方式,支持三种( crc md5 sha1 )方式验证,最后面是自定义密钥。我应该选哪种验证?如果Heartbeat运行于安全网络之上,如本例中的交叉线,可以使用crc,从资源的角度来看,这是代价最低的方法。如果网络并不安全,但也希望降低CPU使用,则使用md5。
最后,如果想得到最好的认证,而不考虑CPU使用情况,则使用sha1,它在三者之中最难破解。 ???(4) 编写httpd启动脚本# vim /usr/local/heartbeat/etc/ha.d/resource.d/httpd #!/bin/bash/bin/systemctl $1 httpd ????# chmod 755 !$ ???⑸ 复制配置文件至备机# scp -rp /usr/local/heartbeat/etc/ha.d/* root@192.168.2.64:/usr/local/heartbeat/etc/ha.d/
三、测试
1.手动加载VIP 192.168.1.111到ens32上
[root@xuegod63 ~]# cd /usr/local/heartbeat/etc/ha.d/resource.d[root@xuegod63 resource.d]# ./IPaddr 192.168.1.111/24/ens32 startIPaddr[7116]: INFO: ?SuccessINFO: ?Success查看 VIP:[root@xuegod63 ~]# ifconfig ???#可以看到 ens32:1 ?的IP为:192.168.1.111
2.手动加载NFS存储资源到/var/www/html (加载需要fuser支持,yum install psmisc -y)
[root@xuegod63 resource.d]# ./Filesystem 192.168.1.62:/wwwdir /var/www/html/ nfs start Filesystem[23567]: INFO: ?SuccessINFO: ?Success [root@xuegod63 ~]# ls /var/www/html/index.html /var/www/html/index.html[root@xuegod63 ~]# df ?-h………192.168.2.62:/wwwdir ???18G ?1.1G ??17G ???6% /var/www/html手动启动httpd服务[root@xuegod63 ~]# systemctl start httpd测试打开主服务页面http://192.168.1.63
3.xuegod64上配置heartbeat
修改文件权限:[root@xuegod64 ~]# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys ?#此文件必须是600权限 否则heartbeat启动不成功修改单播地址:[root@xuegod64 ~]# vim /usr/local/heartbeat/etc/ha.d/ha.cf改:ucast ens32 192.168.1.64为:ucast ens32 192.168.1.63挂载需要fuser支持,yum install psmisc -y
4.两机器启动heartbeat服务
[root@xuegod63 ~]# /etc/init.d/heartbeat restart[root@xuegod64 ~]# /etc/init.d/heartbeat restart查看端口号:[root@xuegod63 ~]# netstat -antup | grep 694udp ???????0 ?????0 0.0.0.0:694 ???????0.0.0.0:* ??????????6895/heartbeat: wr查看端口号:[root@xuegod64 ~]# netstat -antup | grep 694udp ???????0 ?????0 0.0.0.0:694 ???????0.0.0.0:* ??????????6895/heartbeat: wri
5.查看集群
在xuegod63查看集群资源:[root@xuegod63 ~]# ifconfig ?#查看VIP加载ens32:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> ?mtu 1500 ???????inet 192.168.1.111 ?netmask 255.255.255.0 ?broadcast 192.168.1.255 ???????ether 00:0c:29:f0:fc:2c ?txqueuelen 1000 ?(Ethernet) [root@xuegod63 ~]# df -h[root@xuegod63 ~]# df ?-h #查看挂载………192.168.2.62:/wwwdir ???18G ?1.1G ??17G ???6% /var/www/html[root@xuegod63 ~]# netstat -anput | grep httpd ??#查看httpdxuegod64上查看,没有任务资源:[root@xuegod64 ~]# ifconfig[root@xuegod64 ~]# df -h[root@xuegod64 ~]# netstat -anput | grep httpd通过VIP测试:两台主机都开启时,所有请求到转到了xuegod63上。访问:http://192.168.1.111/ 正常
6.故障设定
当掉xuegod63,关闭网卡,等30秒钟,所有请求都转到xuegod64上了[root@xuegod63 ha.d]# ifdown ens32[root@xuegod64 ~]# ifconfig ...eth0:0 ???Link encap:Ethernet ?HWaddr 00:0C:29:48:80:95 ???????????inet addr:192.168.1.111 ?Bcast:192.168.1.255 ?Mask:255.255.255.0[root@xuegod64 ~]# df -h...192.168.1.62:/wwwdir ?9.7G ?3.4G ?5.8G ?37% /var/www/html[root@xuegod64 ~]#systemctl status httpdhttpd (pid ?6375) is running...xuegod63上把ens32网卡再次开启:[root@xuegod63 ~]# ifup ens32等30秒后,查看:[root@xuegod63 ~]# df -h...192.168.1.62:/wwwdir ?9.7G ?3.4G ?5.8G ?37% /var/www/html ???#已经加载了httpd资源[root@xuegod63 ~]# systemctl status httpdhttpd (pid ?27097) is running...[root@xuegod63 ~]# ifconfig 。。。eth0:0 ???Link encap:Ethernet ?HWaddr 00:0C:29:12:EC:1E ???????????inet addr:192.168.1.111 ?Bcast:192.168.1.255 ?Mask:255.255.255.0资源已经回切过来。在xuegod64上查看释放资料:[root@xuegod64 ~]# ifconfig ??#查看不到ens32:0 ?192.168.1.111这个IP地址[root@xuegod64 ~]# df -hFilesystem ???????????Size ?Used Avail Use% Mounted on/dev/sda2 ????????????9.7G ?3.7G ?5.5G ?41% /tmpfs ????????????????569M ????0 ?569M ??0% /dev/shm/dev/sda1 ????????????194M ??28M ?157M ?15% /boot/dev/sr0 ?????????????3.4G ?3.4G ????0 100% /mnt[root@xuegod64 ~]# service httpd statushttpd is stopped
扩展:
heartbeat自带的断网切换的工具-ipfail
ipfail断网切换的原理
?? 关于ipfail这个断网切换的原理很简单,首先heartbeat要判断自己的网络是否正常其实就是通过ping某个ip,如果可以ping的通,说明网络是通的,如果ping不通了,说明是网络断了,或者是主服务器的网卡坏了,然后执行切换的动作。
ping一个group的ipfail配置:
ping_group group1 172.16.103.254 172.16.103.212
Heartbeat实现web服务器高可用
原文地址:https://www.cnblogs.com/happy1983/p/9258645.html