欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

heartbeat v2基于haresources实现HA Web

程序员文章站 2022-04-22 10:59:30
前言: 该文章花费了笔者大量时间编写,转载请回复征得笔者同意! 实现目标: 使用heartbeat和haresources实现Web集群的高可用 术语解释: heartbeat是什么...

前言:

该文章花费了笔者大量时间编写,转载请回复征得笔者同意!

实现目标:

使用heartbeat和haresources实现Web集群的高可用

术语解释:

heartbeat是什么:

Heartbeat 项目是 Linux-HA 工程的一个组成部分,它实现了一个高可用集群系统

为什么要使用heartbeat:

在Linux中实现服务的高可用需要搭建HA集群,HA集群需要提供Messaging Layer和Cluster Resource Manager功能的软件

实现CRM的组件:

heartbeat V2版本中有两个组件可以实现CRM的功能,分别是haresources,crm.在此我们使用haresources来实现对于各节点资源的管理

实验环境:

System: CentOS6.7

host:

Primary: node1.anyisalin.com

Standby: node2.anyisalin.com

Network Configure:

node1: 172.16.100.7/24 eth0

node2: 172.16.100.8/24 eth0

float IP: 172.16.100.1/24 #对外提供服务的IP

实验步骤:

1.使用hosts文件实现HA集群中的节点能够解析各节点的主机名

2.使用公钥认证实现主机之间互信通信

3.安装配置heartbeat,httpd

4.测试web服务器是否实现高可用

1.使用hosts文件实现HA集群中的节点能够解析各节点的主机名

[root@node2~]#vim/etc/hosts127.0.0.1localhostlocalhost.localdomainlocalhost4localhost4.localdomain4::1localhostlocalhost.localdomainlocalhost6localhost6.localdomain6172.16.100.7node1.anyisalin.com172.16.100.8node2.anyisalin.com[root@node2~]#scp-p/etc/hostsnode1.anyisalin.com:/etc/

2.使用公钥认证实现主机之间互信通信

[root@node2~]#ssh-keygen-trsa-P''-f~/.ssh/id_rsa
[root@node2~]#ssh-copy-id-i~/.ssh/id_rsa.pubnode1.anyisalin.com
[root@node1~]#ssh-keygen-trsa-P''-f~/.ssh/id_rsa
[root@node1~]#ssh-copy-id-t~/.ssh/id_rsanode2.anyisalin.com

3.安装配置heartbeat,httpd

[root@node1~]#wget-O/etc/yum.repos.d/centos.repohttps://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/centos?codeblock=2##使用USTC镜像源
[root@node1~]#yuminstallepel-rpm-macros.noarch-y
[root@node1~]#yuminstsallheartbeathttpd-y
[root@node1~]#sshnode2.anyisalin.com"yuminstall-yepel-rpm-macros.noarch"
[root@node1~]#sshnode2.anyisalin.com"yuminstall-yheartbeathttpd"
[root@node1~]#cp/usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources}/etc/ha.d/
[root@node1~]#chmod600/etc/ha.d/authkeys
[root@node1~]#vim/etc/ha.d/authkeys##在最后添加两行auth11md5d41d8cd98f00b204e9800998ecf8427e-##保存退出
[root@node1~]#vim/etc/ha.d/ha.cf##在结尾添加几行bcasteth0logfile/var/log/ha-logkeepalive1nodenode1.anyisalin.com#这里的主机名必须和uname-n显示的一致nodenode2.anyisalin.com#同上##保存退出
[root@node1~]#vim/etc/ha.d/haresources##在最后添加一行node1.anyisalin.comIPaddr::172.16.100.1/24/eth0httpd##结束
[root@node1~]#scp/etc/ha.d/{authkeys,haresources,ha.cf}node2.anyisalin.com:/etc/ha.d/
[root@node1~]#echo"ThisisHAnode1">/var/www/html/index.html#创建网页文件
[root@node1~]#sshnode2.anyisalin.com"echo"ThisisHAnode2">/var/www/html/index.html"#为node2创建网页文件
[root@node1~]#serviceheartbeatstart#启动heartbeat服务
[root@node1~]#sshnode2.anyisalin.com"serviceheartbeatstart"#为node2启动heartbeat服务
[root@node1~]#tail/var/log/ha-log#监控日志文件ResourceManager(default)[8766]:2016/01/02_06:22:00info:Running/etc/ha.d/resource.d/IPaddr172.16.100.1/24/eth0startIPaddr(IPaddr_172.16.100.1)[8916]:2016/01/02_06:22:00INFO:Addinginetaddress172.16.100.1/24withbroadcastaddress172.16.100.255todeviceeth0IPaddr(IPaddr_172.16.100.1)[8916]:2016/01/02_06:22:00INFO:Bringingdeviceeth0upIPaddr(IPaddr_172.16.100.1)[8916]:2016/01/02_06:22:00INFO:/usr/libexec/heartbeat/send_arp-i200-r5-p/var/run/resource-agents/send_arp-172.16.100.1eth0172.16.100.1autonot_usednot_used/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_172.16.100.1)[8890]:2016/01/02_06:22:00INFO:SuccessResourceManager(default)[8766]:2016/01/02_06:22:00info:Running/etc/init.d/httpdstart
[root@node1~]#ipa##node1为主节点1:lo:mtu65536qdiscnoqueuestateUNKNOWNlink/loopback00:00:00:00:00:00brd00:00:00:00:00:00inet127.0.0.1/8scopehostloinet6::1/128scopehostvalid_lftforeverpreferred_lftforever2:eth0:mtu1500qdiscpfifo_faststateUPqlen1000link/ether00:0c:29:cf:c0:42brdff:ff:ff:ff:ff:ffinet172.16.100.7/24brd172.16.100.255scopeglobaleth0inet172.16.100.1/24brd172.16.100.255scopeglobalsecondaryeth0inet6fe80::20c:29ff:fecf:c042/64scopelinkvalid_lftforeverpreferred_lftforever

4.测试web服务器是否实现高可用

[root@DataBase~]#curl172.16.100.1#测试Web服务器,页面显示为主节点node1的网页文件ThisisHAnode1[root@node1~]#/usr/share/heartbeat/hb_standby#使用heartbeat内置脚本模拟FailOver(故障转移)[root@node1~]#ipa1:lo:mtu65536qdiscnoqueuestateUNKNOWNlink/loopback00:00:00:00:00:00brd00:00:00:00:00:00inet127.0.0.1/8scopehostloinet6::1/128scopehostvalid_lftforeverpreferred_lftforever2:eth0:mtu1500qdiscpfifo_faststateUPqlen1000link/ether00:0c:29:cf:c0:38brdff:ff:ff:ff:ff:ffinet172.16.100.7/24brd172.16.100.255scopeglobaleth0inet6fe80::20c:29ff:fecf:c038/64scopelinkvalid_lftforeverpreferred_lftforever
[root@node2~]#ipa1:lo:mtu65536qdiscnoqueuestateUNKNOWNlink/loopback00:00:00:00:00:00brd00:00:00:00:00:00inet127.0.0.1/8scopehostloinet6::1/128scopehostvalid_lftforeverpreferred_lftforever2:eth0:mtu1500qdiscpfifo_faststateUPqlen1000link/ether00:0c:29:cf:c0:42brdff:ff:ff:ff:ff:ffinet172.16.100.8/24brd172.16.100.255scopeglobaleth0inet172.16.100.1/24brd172.16.100.255scopeglobalsecondaryeth0inet6fe80::20c:29ff:fecf:c042/64scopelinkvalid_lftforeverpreferred_lftforever
[root@DataBase~]#curl172.16.100.1#重新发起请求,页面显示为从节点node2的网页文件ThisisHAnode2

实验总结:

heartbeat和haresources实现Web集群的高可用,在主节点故障的情况下能够通过CRM将资源转移到备用节点上,但是该实验还有很多未考虑的因素,在此不过多进行解释