前言:
该文章花费了笔者大量时间编写,转载请回复征得笔者同意!
实现目标:
使用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/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.100.7 node1.anyisalin.com
172.16.100.8 node2.anyisalin.com
[root@node2 ~]# scp -p /etc/hosts node1.anyisalin.com:/etc/
2.使用公钥认证实现主机之间互信通信
[root@node2 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa [root@node2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub node1.anyisalin.com [root@node1 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa [root@node1 ~]# ssh-copy-id -t ~/.ssh/id_rsa node2.anyisalin.com
3.安装配置heartbeat,httpd
[root@node1 ~]# wget -O /etc/yum.repos.d/centos.repo https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/centos?codeblock=2 ##使用USTC镜像源 [root@node1 ~]# yum install epel-rpm-macros.noarch -y [root@node1 ~]# yum instsall heartbeat httpd -y [root@node1 ~]# ssh node2.anyisalin.com "yum install -y epel-rpm-macros.noarch" [root@node1 ~]# ssh node2.anyisalin.com "yum install -y heartbeat httpd" [root@node1 ~]# cp /usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources} /etc/ha.d/ [root@node1 ~]# chmod 600 /etc/ha.d/authkeys
[root@node1 ~]# vim /etc/ha.d/authkeys
##在最后添加两行
auth 1
1 md5 d41d8cd98f00b204e9800998ecf8427e -
##保存退出
[root@node1 ~]# vim /etc/ha.d/ha.cf
##在结尾添加几行
bcast eth0
logfile /var/log/ha-log
keepalive 1
node node1.anyisalin.com #这里的主机名必须和uname -n显示的一致
node node2.anyisalin.com #同上
##保存退出
[root@node1 ~]# vim /etc/ha.d/haresources
##在最后添加一行
node1.anyisalin.com IPaddr::172.16.100.1/24/eth0 httpd
##结束
[root@node1 ~]# scp /etc/ha.d/{authkeys,haresources,ha.cf} node2.anyisalin.com:/etc/ha.d/ [root@node1 ~]# echo "This is HA node1" > /var/www/html/index.html #创建网页文件 [root@node1 ~]# ssh node2.anyisalin.com "echo "This is HA node2" > /var/www/html/index.html" #为node2创建网页文件 [root@node1 ~]# service heartbeat start #启动heartbeat服务 [root@node1 ~]# ssh node2.anyisalin.com "service heartbeat start" #为node2启动heartbeat服务
[root@node1 ~]# tail /var/log/ha-log #监控日志文件
ResourceManager(default)[8766]:2016/01/02_06:22:00 info: Running /etc/ha.d/resource.d/IPaddr 172.16.100.1/24/eth0 start
IPaddr(IPaddr_172.16.100.1)[8916]:2016/01/02_06:22:00 INFO: Adding inet address 172.16.100.1/24 with broadcast address 172.16.100.255 to device eth0
IPaddr(IPaddr_172.16.100.1)[8916]:2016/01/02_06:22:00 INFO: Bringing device eth0 up
IPaddr(IPaddr_172.16.100.1)[8916]:2016/01/02_06:22:00 INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 -p /var/run/resource-agents/send_arp-172.16.100.1 eth0 172.16.100.1 auto not_used not_used
/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_172.16.100.1)[8890]:2016/01/02_06:22:00 INFO: Success
ResourceManager(default)[8766]:2016/01/02_06:22:00 info: Running /etc/init.d/httpd start
[root@node1 ~]# ip a ##node1为主节点
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:cf:c0:42 brd ff:ff:ff:ff:ff:ff
inet 172.16.100.7/24 brd 172.16.100.255 scope global eth0
inet 172.16.100.1/24 brd 172.16.100.255 scope global secondary eth0
inet6 fe80::20c:29ff:fecf:c042/64 scope link
valid_lft forever preferred_lft forever
4.测试web服务器是否实现高可用
[root@DataBase ~]# curl 172.16.100.1 #测试Web服务器,页面显示为主节点node1的网页文件
This is HA node 1
[root@node1 ~]# /usr/share/heartbeat/hb_standby #使用heartbeat内置脚本模拟FailOver(故障转移)
[root@node1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:cf:c0:38 brd ff:ff:ff:ff:ff:ff
inet 172.16.100.7/24 brd 172.16.100.255 scope global eth0
inet6 fe80::20c:29ff:fecf:c038/64 scope link
valid_lft forever preferred_lft forever
[root@node2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:cf:c0:42 brd ff:ff:ff:ff:ff:ff
inet 172.16.100.8/24 brd 172.16.100.255 scope global eth0
inet 172.16.100.1/24 brd 172.16.100.255 scope global secondary eth0
inet6 fe80::20c:29ff:fecf:c042/64 scope link
valid_lft forever preferred_lft forever
[root@DataBase ~]# curl 172.16.100.1 #重新发起请求,页面显示为从节点node2的网页文件
This is HA node2
实验总结:
heartbeat和haresources实现Web集群的高可用,在主节点故障的情况下能够通过CRM将资源转移到备用节点上,但是该实验还有很多未考虑的因素,在此不过多进行解释
转载于:https://blog.51cto.com/anyisalin/1738210
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109160.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...