网站建设与管理的专业群,可以自己做网站的软件下载,python做后台网站的多吗,wordpress模板学校用Heartbeat实现web服务器高可用heartbeat概述: Heartbeat 项目是 Linux-HA 工程的一个组成部分#xff0c;它实现了一个高可用集群系统。心跳服务和集群通信是高可用集群的两个关键组件#xff0c;在 Heartbeat 项目里#xff0c;由 heartbeat 模块实现了这两个功能。端口号… 用Heartbeat实现web服务器高可用 heartbeat概述: Heartbeat 项目是 Linux-HA 工程的一个组成部分它实现了一个高可用集群系统。心跳服务和集群通信是高可用集群的两个关键组件在 Heartbeat 项目里由 heartbeat 模块实现了这两个功能。端口号6941heartbeat的工作原理heartbeat最核心的包括两个。部分心跳监测部分和资源接管部分心跳监测可以通过网络链路和串口进行而且支持冗余链路它们之间相互发送报文来告诉对方自己当前的状态如果在指定的时间内未收到对方发送的报文那就认为对方失效这时需启动资源接管模块来接管运行在对方主机上的资源或者服务 2高可用集群高可用集群是指一组通过硬件和软件连接起来的独立计算机它们在用户面前表现为一个单一系统在这样的一组计算机系统内部的一个或者多个节点停止工作服务会从故障节点切换到正常工作的节点上运行不会引起服务中断。从这个定义可以看出集群必须检测节点和服务何时失效何时恢复为可用。这个任务通常由一组被称为“心跳”的代码完成。在Linux-HA里这个功能由一个叫做heartbeat的程序完成3Heartbeat-3.X版本以后被分为了4个模块1ClusterLabs-resource-agents-v3.9.2-0-ge261943.tar.gz #集群实验资源代理 2Heartbeat-3-0-7e3a82377fa8.tar.bz2 # 心跳主程序包 3pacemaker-1.1.9-1512.el6.src.rpm # 起搏器 4Reusable-Cluster-Components-glue--glue-1.0.9.tar.bz2 #可重复使用的群集组件 一实验拓扑 二实验目标1用heartbeat实现web服务器高可用 三实验环境Note1WEB主 xuegod63.cn 192.168.1.63Note2WEB备 xuegod64.cn 192.168.1.64NFS xuegod62.cn 192准备工作保持两台节点主机一致 1. 修改主机名永久生效 # vim /etc/sysconfig/network HOSTNAMExuegod63.cn 2. 解析 # vim /etc/hosts 192.168.1.63 xuegod63.cn 192.168.1.64 xuegod64.cn 3时间一致[rootxuegod63 ~]# date2016年 10月 30日 星期日 15:18:47 CST4. 关防火墙,关掉selinux [rootxuegod63 ~]# service iptables stop 四实验代码--------------------------------------------------------------------------------------------------------------1、配置xuegod62为NFS服务器提供存储资源 1安装NFS服务[rootxuegod62 ~]# rpm -ivh /mnt/Packages/nfs-utils-1.2.3-39.el6.x86_64.rpm 2编写测试网页并共享[rootxuegod62 ~]# mkdir /wwwdir [rootxuegod62 ~]# echo heartbeat http ha /wwwdir/index.html [rootxuegod62 ~]#vim /etc/exports #编写共享文件及网段/wwwdir 192.168.1.0/24(rw) :3添加共享目录权限 [rootxuegod62 ~]# chmod 777 -R /wwwdir/ 4开启nfs服务 [rootxuegod62 ~]# service nfs restart [rootxuegod62 ~]# chkconfig nfs on 2、xuegod63测试nfs 存储挂载并安装httpd web服务器 1安装阿帕奇服务并测试NFS文件有没有共享成功[rootxuegod63 ~]# yum install httpd -y [rootxuegod63 ~]# showmount -e 192.168.1.62 #查看NFS共享文件Export list for 192.168.1.62: /wwwdir 192.168.1.0/24 2挂载文件夹到本地网站根目录[rootxuegod63 ~]# mount -t nfs 192.168.1.62:/wwwdir /var/www/html/ [rootxuegod63 ~]#df -h[rootxuegod63 ~]# service httpd restart 3测试 [rootxuegod63 ~]# yum install elinks -y [rootxuegod63 ~]# elinks --dump 192.168.1.63 heartdeat http ha httpd//:192.168.1.634卸载资源后期这些资源通过heartbeat直接加载 [rootxuegod63 ~]# umount /var/www/html/ [rootxuegod63 ~]# service httpd stop [rootxuegod63 ~]# chkconfig httpd off #后期依靠hartbeat启动3、xuegod64测试nfs 存储挂载并安装httpd web服务器 1安装阿帕奇服务并测试NFS文件有没有共享成功[rootxuegod64 ~]# yum install httpd -y [rootxuegod64 ~]# showmount -e 192.168.1.62 Export list for 192.168.1.62: /wwwdir 192.168.1.0/24 2挂载文件夹到本地网站根目录[rootxuegod64 ~]# mount -t nfs 192.168.1.62:/wwwdir /var/www/html/ [rootxuegod64 ~]# service httpd restart 3测试 [rootxuegod64 ~]# yum install elinks -y [rootxuegod64 ~]# elinks --dump 192.168.1.64 heartdeat http ha 4卸载资源后期这些资源通过heartbeat直接加载 [rootxuegod64 ~]# umount /var/www/html/ [rootxuegod64 ~]# service httpd stop [rootxuegod64 ~]# chkconfig httpd off 4、xuegod63安装heartbeat 1配置好yum源 [rootxuegod63 ~]# cat /etc/yum.repos.d/rhel-source.repo [rhel-source] nameRed Hat Enterprise Linux $releasever - $basearch - Source baseurlfile:///mnt/ enabled1 gpgcheck0 gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release #增加以下红色内容[rhel-ha] nameRed ha baseurlfile:///mnt/HighAvailability enabled1 gpgcheck0 gpgkeyfile:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release [rootxuegod63 ~]# yum clean all [rootxuegod63 ~]#yum list#说明如果不配置这个baseurlfile:///mnt/HighAvailabilit 则无法使用yum安装cluster-glue和resource-agents 。 centos 6.2或5的源里 cluster-glue、resource-agents、pacemaker 都有就是没有heartbeat。 幸好heartbeat的source里提供了.spec 可以用rpmbuild来生成rpm包。 2安装heartbeat [rootxuegod63 ~]# tar -jxvf /root/Heartbeat-3-0-958e11be8686.tar.bz2 [rootxuegod63 ~]# cd Heartbeat-3-0-958e11be8686[rootxuegod63 Heartbeat-3-0-958e11be8686]# rpmbuild -ba heartbeat-fedora.spec error: File /root/rpmbuild/SOURCES/heartbeat.tar.bz2: #错误提示。有那个文件或目录但是会创建好目录[rootxuegod63 ~]# tar -jxvf /root/Heartbeat-3-0-958e11be8686.tar.bz2 -C /root/rpmbuild/SOURCES/ [rootxuegod63 ~]# cd /root/rpmbuild/SOURCES/ [rootxuegod63 SOURCES]# mv Heartbeat-3-0-958e11be8686 heartbeat #修改名称[rootxuegod63 SOURCES]# tar -jcvf heartbeat.tar.bz2 heartbeat # 打包生成的heartbeat.tar.bz2软件包如果直接使用源码包生成rpm包时会报错。1开始生成RPM包 参数rpmbuild参数 -bb 只编译二进制rpm包 -bs 只编译源码srpm包 -ba 同时编译二进制和源码srpm包 [rootxuegod64 ~]# yum install ncurses-devel openssl-devel gettext bison flex mailx cluster-glue-libs-devel docbook-dtds docbook-style-xsl libtool-ltdl-devel -y[rootxuegod63 SOURCES]# cd heartbeat [rootxuegod63 heartbeat]# rpmbuild -ba heartbeat-fedora.spec 3安装软件包[rootxuegod63 ~]# cd /root/rpmbuild/RPMS/x86_64/ [rootxuegod63 x86_64]# yum install -y cluster-glue resource-agents [rootxuegod63 x86_64]# rpm -ivh heartbeat-libs-3.0.6-1.el6.x86_64.rpm [rootxuegod63 x86_64]# rpm -ivh heartbeat-3.0.6-1.el6.x86_64.rpm 5、在xuegod64上安装heartbeat 1把软件包和yum配置文件拷贝到xuegod64上 [rootxuegod63 ~]#cd /root/rpmbuild/RPMS/x86_64/ [rootxuegod63 x86_64]# scp -r ./* 192.168.1.64:/root/ [rootxuegod63 x86_64]#scp /etc/yum.repos.d/rhel-source.repo 192.168.1.64:/etc/yum.repos.d/[rootxuegod64 ~]#cd /root/rpmbuild/RPMS/x86_64/ [rootxuegod64 x86_64]#yum install ncurses-devel openssl-devel gettext bison flex mailx cluster-glue-libs-devel docbook-dtds docbook-style-xsl -y [rootxuegod64 x86_64]# rpm -ivh heartbeat-libs-3.0.5-1.el6.x86_64.rpm [rootxuegod64 x86_64]# yum install -y cluster-glue resource-agents [rootxuegod64 x86_64]# rpm -ivh heartbeat-3.0.6-1.el6.x86_64.rpm 2查看生成的用户和组 [rootxuegod64 ~]# grep haclient /etc/group haclient:x:489: [rootxuegod64 ~]# id hacluster uid495(hacluster) gid489(haclient) groups489(haclient) 6、配置heartbeat xuegod63 和xuegod64xuegod63上生成heartbeat配置文件 将xuegod63配置好的文件拷贝的xuegod64上1拷贝配置文件[rootxuegod63 ~]# cp /usr/share/doc/heartbeat-3.0.6/ha.cf /etc/ha.d/ #主配置文件 [rootxuegod63 ~]# cp /usr/share/doc/heartbeat-3.0.6/authkeys /etc/ha.d/ #主备节点间通信时所使用的验证文件保证安全性 [rootxuegod63 ~]# cp /usr/share/doc/heartbeat-3.0.6/haresources /etc/ha.d/ #浮动资源的配置文件 2设置主备节点间通信时所使用的验证文件保证安全性。主备节点配置要求一致。 [rootxuegod63 ~]# vim /etc/ha.d/authkeys 改 为 auth 3 #1 crc #2 sha1 HI! 3 md5 mkkey [rootxuegod63 ~]# chmod 600 /etc/ha.d/authkeys #此文件权限必须是600否则启动不成功 注:/etc/ha.d/authkeys 文件决定了认证密钥。共有三种认证方式crcmd5和sha1。 三种认证方法的用途 如果Heartbeat运行于安全网络之上如本例中的交叉线可以使用crc从资源的角度来看这是代价最低的方法。如果网络并不安全但也希望降低CPU使用则使用md5。最后如果想得到最好的认证而不考虑CPU使用情况则使用sha1它在三者之中最难破解。 3修改浮动资源注意添加的顺序::表示分隔符 [rootxuegod63 ~]#vim /etc/ha.d/haresources 改 44 #node-name resource1 resource2 ... resourceN 为 xuegod63.cn IPaddr::192.168.1.200/24/eth0 Filesystem::192.168.1.62:/wwwdir::/var/www/html::nfs httpd 注node-name填的是主服务器的主机名。 xuegod64上不需要修改。这样资源默认会加一这个主机上。当xuegod63坏了xuegod64会再接管。 IPaddr::192.168.1.200/24/eth0 #指定VIP及绑定到哪个网卡上 Filesystem::192.168.1.62:/wwwdir::/var/www/html::nfs #指定要挂载的存储 httpd #指定要启动的服务。这个服务必须是/etc/init.d下可以通过service去启动或关闭4测试1测试 手动加载VIP 192.168.1.200到eth0上 [rootxuegod63 ~]# ll /etc/ha.d/resource.d/IPaddr -rwxr-xr-x 1 root root 2273 Jul 29 20:49 /etc/ha.d/resource.d/IPaddr [rootxuegod63 ~]# /etc/ha.d/resource.d/IPaddr 192.168.1.200/24/eth0 start IPaddr[7116]: INFO: Success INFO: Success 查看 VIP [rootxuegod63 x86_64]#ip addr #可以看到 eth0:0 的IP为192.168.1.200 注在Linux rhel6.2版本中可以使用ifconfig查看但是在rhel6.5上只能用ip addr 命令来查看到2测试 手动加载NFS存储资源到/var/www/html [rootxuegod63 ~]# /etc/ha.d/resource.d/Filesystem 192.168.1.62:/wwwdir /var/www/html/ nfs start Filesystem[23575]: INFO: Running start for 192.168.1.62:/wwwdir on /var/www/html Filesystem[23567]: INFO: Success INFO: Success [rootxuegod63 ~]# mount 192.168.1.62:/wwwdir on /var/www/html type nfs (rw,vers4,addr192.168.1.62,clientaddr192.168.1.63) 3测试 手动启动httpd服务 [rootxuegod63 ~]# /etc/init.d/httpd restart [rootxuegod63 ~]# grep /etc/ha.d/ha.cf #默认只开启了以下两行内容 logfacility local0 auto_failback on 注释#auto_failback on #为on时主结点恢复正常后资源自动转给主结点。 建议设为auto_failback off 等主节点恢复正常后在业务不繁忙时切换回来。防止主节点恢复正常时回切时再次影起网络中断。 5修改配置文件[rootxuegod63 ~]# vim /etc/ha.d/ha.cf #将配置文件中以下内容前面的#号去掉 24 debugfile /var/log/ha-debug 29 logfile /var/log/ha-log 48 keepalive 2 #设定heartbeat之间的时间间隔为2秒。 56 deadtime 30 #在30秒后宣布节点死亡。 61 warntime 10 #在日志中发出“late heartbeat“警告之前等待的时间单位为秒。 71 initdead 120 #在某些系统上系统启动或重启之后需要经过一段时间网络才能正常工作该选项用于解决这种情况产生的时间间隔。取值至少为deadtime的两倍。 76 udpport 694 #使用端口694进行bcast和ucast通信。这是默认的并且在IANA官方注册的端口号。 121 ucast eth0 192.168.1.64 #表示从本机的eth0接口发心跳消息给对方节点写另一端的IP地址 。 这是单播地址。 xuegod64上改为192.168.1.63 。心跳网卡 如果你有两个网卡可以写成eth1 注配置文件中 91 #bcast eth0 #表示在eth0接口上使用广播heartbeat将eth1替换为eth0eth2或者您使用的仸何接口。 157 auto_failback on #当auto_failback设置为on时一旦主节点重新恢复联机将从从节点取回所有资源。若该选项设置为off主节点便不能重新获得资源。 211 node xuegod63.cn #该选项是必须配置的。集群中机器的主机名与“uname –n”的输出相同。 212 node xuegod64.cn 修改以下内容改223 #ping 10.10.10.254 为223 ping 192.168.1.1 #通过ping命令来实现仲裁 改256 #respawn hacluster /usr/lib/heartbeat/ipfail 为256 respawn hacluster /usr/libexec/heartbeat/ipfail 改262 #apiauth ipfail gidhaclient uidhacluster 为 apiauth ipfail gidhaclient uidhacluster 到此修改结束保存退出。 7、xuegod64上配置heartbeat 1复制配置文件到xuegod64上 rootxuegod63 ~]# cd /etc/ha.d/ [rootxuegod63 ha.d]# scp ha.cf haresources authkeys 192.168.1.64:/etc/ha.d/ [rootxuegod64 ~]# chmod 600 /etc/ha.d/authkeys #此文件必须是600否则heartbeat启动不成功 #修改单播地址 [rootxuegod64 ~]# vim /etc/ha.d/ha.cf 改 ucast eth0 192.168.1.64 为 ucast eth0 192.168.1.63 2两机器启动heartbeat服务 [rootxuegod63 ~]# /etc/init.d/heartbeat restart [rootxuegod64 ~]# /etc/init.d/heartbeat restart #等待资源接管完成。 takeover接管。 当启动停留在这个界面时xuegod64会接管所有浮动资源。等下面启动成功时xuegod64会释放资源浮云资源再次加载。 3在xuegod63查看集群资源 [rootxuegod63 ~]# ifconfig eth0:0 Link encap:Ethernet HWaddr 00:0C:29:12:EC:1E inet addr:192.168.1.200 Bcast:192.168.1.255 Mask:255.255.255.0 [rootxuegod63 ~]# df -h /dev/sr0 3.4G 3.4G 0 100% /mnt 192.168.1.62:/wwwdir 9.7G 3.4G 5.8G 37% /var/www/html [rootxuegod63 ~]# /etc/init.d/httpd status httpd (pid 23641) is running... 4xuegod64上查看没有任何浮云资源 [rootxuegod64 ~]# ifconfig [rootxuegod64 ~]# df -h [rootxuegod63 ~]# /etc/init.d/httpd status httpd is stopped 8、测试 两台主机都开启时所有请求到转到了xuegod63上。访问http://192.168.1.200/ 正常 。当掉xuegod63关闭网卡等30秒钟所有请求都转到xuegod64上了 1主挂掉验证备[rootxuegod63 ha.d]# ifdown eth0 [rootxuegod64 ~]# ifconfig eth0:0 Link encap:Ethernet HWaddr 00:0C:29:48:80:95 inet addr:192.168.1.200 Bcast:192.168.1.255 Mask:255.255.255.0 [rootxuegod64 ~]# df -h 192.168.1.62:/wwwdir 9.7G 3.4G 5.8G 37% /var/www/html [rootxuegod64 ~]# service httpd status httpd (pid 6375) is running... 2主再次复活验证抢占功能---xuegod63上把eth0网卡再次开启 [rootxuegod63 ~]# ifup eth0 等30秒后查看 [rootxuegod63 ~]# df -h 192.168.1.62:/wwwdir 9.7G 3.4G 5.8G 37% /var/www/html #已经加载了httpd资源 [rootxuegod63 ~]# service httpd status httpd (pid 27097) is running... [rootxuegod63 ~]# ifconfig eth0:0 Link encap:Ethernet HWaddr 00:0C:29:12:EC:1E inet addr:192.168.1.200 Bcast:192.168.1.255 Mask:255.255.255.0 资源已经回切过来。 3在xuegod64上查看释放资料 [rootxuegod64 ~]# ifconfig #查看不到eth00 192.168.1.200 这个IP地址 [rootxuegod64 ~]# df -h Filesystem 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 [rootxuegod64 ~]# service httpd status httpd is stopped 转载于:https://blog.51cto.com/blxueyuan/1927500