前言:
本文章转自我的个人博客 http://www.anyisalin.com 欢迎大家访问
heartbeat是什么:
Heartbeat 项目是 Linux-HA 工程的一个组成部分,它实现了一个高可用集群系统。
为什么要使用heartbeat:
在Linux中实现服务的高可用需要搭建HA集群,HA集群需要提供Messaging Layer和Cluster Resource Manager功能的软件
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集群中的节点能够解析各节点的主机名
[[email protected] ~]# 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
[[email protected] ~]# scp -p /etc/hosts node1.anyisalin.com:/etc/ #同步两个节点之间的hosts文件
2.使用公钥认证实现主机之间互信通信
[[email protected] ~]# ssh-keygen -t rsa -P ‘‘ -f ~/.ssh/id_rsa [[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub node1.anyisalin.com
[[email protected] ~]# ssh-keygen -t rsa -P ‘‘ -f ~/.ssh/id_rsa [[email protected] ~]# ssh-copy-id -t ~/.ssh/id_rsa node2.anyisalin.com
3.安装配置heartbeat,httpd
[[email protected] ~]# wget -O /etc/yum.repos.d/centos.repo https://lug.ustc.edu.cn/wiki/_export/code/mirrors/help/centos?codeblock=2 ##使用USTC镜像源 [[email protected] ~]# yum install epel-rpm-macros.noarch -y ##安装epel镜像源 [[email protected] ~]# yum instsall heartbeat httpd -y ##安装heartbeat和httpd [[email protected] ~]# ssh node2.anyisalin.com "yum install -y epel-rpm-macros.noarch" [[email protected] ~]# ssh node2.anyisalin.com "yum install -y heartbeat httpd" [[email protected] ~]# cp /usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources} /etc/ha.d/ ##复制配置文件模板 [[email protected] ~]# chmod 600 /etc/ha.d/authkeys ##修改认证文件权限
[[email protected] ~]# vim /etc/ha.d/authkeys
##在最后添加两行
auth 1
1 md5 d41d8cd98f00b204e9800998ecf8427e -
##保存退出
[[email protected] ~]# 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 #同上
##保存退出
[[email protected] ~]# vim /etc/ha.d/haresources
##在最后添加一行
node1.anyisalin.com IPaddr::172.16.100.1/24/eth0 httpd
##结束
[[email protected] ~]# scp /etc/ha.d/{authkeys,haresources,ha.cf} node2.anyisalin.com:/etc/ha.d/ [[email protected] ~]# echo "This is HA node1" > /var/www/html/index.html #创建网页文件 [[email protected] ~]# ssh node2.anyisalin.com "echo "This is HA node2" > /var/www/html/index.html" #为node2创建网页文件 [[email protected] ~]# service heartbeat start #启动heartbeat服务 [[email protected] ~]# ssh node2.anyisalin.com "service heartbeat start" #为node2启动heartbeat服务
[[email protected] ~]# 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
[[email protected] ~]# 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.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服务器是否实现高可用
[[email protected] ~]# curl 172.16.100.1 #测试Web服务器,页面显示为主节点node1的网页文件
This is HA node 1
[[email protected] ~]# /usr/share/heartbeat/hb_standby #使用heartbeat内置脚本模拟FailOver(故障转移)
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# curl 172.16.100.1 #重新发起请求,页面显示为从节点node2的网页文件
This is HA node2