Linux系统高可用集群软件之HeartBeat

     Heartbeat是Linux-HA工程的一个组件,最核心的功能是心跳监测部分和资源接管部分.心跳监测可以通过网络链路和串口进行,而且支持冗余链路,它们之间相互发送报文来告诉对方自己当前的状态,如果在指定的时间内未收到对方发送的报文,那么就认为对方失效,这时需启动资源接管模块来接管运行在对方主机上的资源或者服务.Heartbea是基于主机名来切换节点,通过字符串加密来认证两台主机连接,与keepalived不同是它可以基于脚本资源切换,文件共享,是通过tcp协议对资源切换.

 

服务器环境:

node1:192.168.1.100    10.0.0.1

node2:192.168.1.102    10.0.0.2

服务:apache

 

1.配置系统的网络环境

node1节点:

[root@node1 yum.repos.d]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# vim ifcfg-eth0        --配置本机的IP地址

DEVICE=eth0
HWADDR=08:00:27:EE:3D:F6
TYPE=Ethernet
UUID=f1fcda7b-0fa1-458d-800c-aeed3d56aad0
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0

GATEWAY=192.168.1.1

[root@node1 network-scripts]# vim ifcfg-eth1     --配置心跳的IP地址

DEVICE=eth1
HWADDR=08:00:27:06:79:47
TYPE=Ethernet
UUID=f1fcda7b-0fa1-458d-800c-aeed3d56aad0
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.0.0.1
NETMASK=255.255.255.0

[root@node1 network-scripts]# vim /etc/hosts     --node1和node2解析IP

192.168.1.100 node1
192.168.1.102 node2

[root@node1 network-scripts]#

 

node2节点:

[root@node2 ha.d]# cd /etc/sysconfig/network-scripts/
[root@node2 network-scripts]# vim ifcfg-eth0          --配置节点2的IP地址

DEVICE=eth0
HWADDR=08:00:27:A5:94:4C
TYPE=Ethernet
UUID=fd898d33-eed6-4333-a5cd-a6961d9f4226
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.1.102
NETMASK=255.255.255.0
GATEWAY=192.168.1.1

[root@node2 network-scripts]# vim ifcfg-eth1     --配置节点2的心跳IP地址

DEVICE=eth1
HWADDR=08:00:27:5B:2C:6E
TYPE=Ethernet
UUID=fd898d33-eed6-4333-a5cd-a6961d9f4226
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.0.0.2
NETMASK=255.255.255.0

[root@node2 network-scripts]# vim /etc/hosts      --配置IP地址解析

192.168.1.100 node1
192.168.1.102 node2

[root@node1 network-scripts]# ping node2 -c1     --检查两台服务器是否ping通
PING node2 (192.168.1.102) 56(84) bytes of data.
64 bytes from node2 (192.168.1.102): icmp_seq=1 ttl=64 time=1.97 ms

--- node2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 1.974/1.974/1.974/0.000 ms

[root@node2 network-scripts]# ping node1 -c 1
PING node1 (192.168.1.100) 56(84) bytes of data.
64 bytes from node1 (192.168.1.100): icmp_seq=1 ttl=64 time=0.524 ms

--- node1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 1ms
rtt min/avg/max/mdev = 0.524/0.524/0.524/0.000 ms
[root@node2 network-scripts]#

 

2.下载epel源,用epel源安装heartbeat软件

node1节点:

[root@node1 network-scripts]# wget http://mirrors3.ustc.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm           --下载epel源

[root@node1 yum.repos.d]# rpm -ivh epel-release-6-8.noarch.rpm     --安装epel源 
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]

[root@node1 yum.repos.d]# vim epel.repo       --启用epel源

将#baseurl前面的#号去掉,enabled=0修改为enabled=1

[root@node1 yum.repos.d]# vim epel-testing.repo

将#baseurl前面的#号去掉,enabled=0修改为enabled=1

[root@node1 yum.repos.d]# yum repolist

[root@node1 yum.repos.d]# yum install heartbeat* -y          --安装heartbeat软件

[root@node1 yum.repos.d]# scp epel* node2:/etc/yum.repos.d/      --将epel源复制到node2中
root@node2's password:
epel-release-6-8.noarch.rpm                             100%      14KB     14.2KB/s      00:00   
epel.repo                                                         100%       954       0.9KB/s        00:00   
epel-testing.repo                                              100%       1054     1.0KB/s        00:00   
[root@node1 yum.repos.d]#

 

node2节点:

[root@node2 yum.repos.d]# yum repolist

[root@node2 yum.repos.d]# yum install heartbeat* -y          --在node2节点上安装heartbeat软件

 

3.在两个节点安装apache服务测试

node1节点:

[root@node1 yum.repos.d]# yum install httpd -y           --安装apache服务

[root@node1 yum.repos.d]# echo "node1" > /var/www/html/index.html        --写出测试页

[root@node1 yum.repos.d]# /etc/init.d/httpd stop         --关闭httpd服务

[root@node1 yum.repos.d]# chkconfig  httpd off           --httpd服务开机不启动

 

node2节点:

[root@node2 yum.repos.d]# yum install httpd -y           --在node2安装apache服务

[root@node2 yum.repos.d]# echo "node2" > /var/www/html/index.html       --写出首页

[root@node2 yum.repos.d]# /etc/init.d/httpd stop          --关闭服务

[root@node2 yum.repos.d]# chkconfig  httpd off            --开机不启动

 

4.配置HeartBeat集群软件

[root@node1 yum.repos.d]# cd /etc/ha.d/

[root@node1 ha.d]# cp -a /usr/share/doc/heartbeat-3.0.4/authkeys .      --复制配置文件模板

[root@node1 ha.d]# cp -a /usr/share/doc/heartbeat-3.0.4/haresources .

[root@node1 ha.d]# cp -a /usr/share/doc/heartbeat-3.0.4/ha.cf  .

[root@node1 ha.d]# vim authkeys     --修改认证信息

auth 3                  --添加这两行

3 md5 1111

[root@node1 ha.d]# chmod 600 authkeys    --修改权限

[root@node1 ha.d]# vim ha.cf

debugfile /var/log/ha-debug         --去年前面的#号,开启调试日志

logfile /var/log/ha-log                  --去年前面的#号,开启运行和访问日志

keepalive 2             --去年前面的#号,心跳探测为2秒

deadtime 30            --去年前面的#号,死亡时间

warntime 10            --去年前面的#号,等待时间10秒

initdead 120            --去年前面的#号,确定死亡时间

udpport        694     --监听的端口

bcast eth1              --添加这行,组播地址绑定在哪个网卡(bcast--广播  mcast--多播或者组播 ucast--单播)

auto_failback on      --如果主节点正常就恢复到主节点上

node node1            --添加这行,写上两台服务器的主机名 

node node2

ping_group 192.168.1.102 192.168.1.103 192.168.1.1    --检查主机是否宕机

compression     bz2      --压缩集群事物信息

compression_threshold 2     --大于2K就压缩

[root@node1 ha.d]# vim haresources      --定义资源

node1 192.168.1.105/24/eth1:0 httpd        --添加这行,node1主服务器的主机名,192.168.1.105虚拟IP,24子网掩码,eth1:0是vip的网络接口,httpd是应用服务

[root@node1 ha.d]# scp authkeys  ha.cf  haresources  node2:/etc/ha.d/     --将三个配置文件复制到node2中
root@node2's password:
authkeys                                                    100%         663           0.7KB/s       00:00   
ha.cf                                                          100%        10KB        10.3KB/s      00:00   
haresources                                               100%         5941         5.8KB/s       00:00   
[root@node1 ha.d]#

 

5.启动服务,测试是否正常

node1节点:

[root@node1 ha.d]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO:  Resource is stopped
Done.

[root@node1 ha.d]# netstat -autnp |more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      958/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1178/master        
tcp        0     52 192.168.1.100:22            192.168.1.93:51656          ESTABLISHED 1216/sshd          
tcp        0      0 :::80                       :::*                        LISTEN      7720/httpd         
tcp        0      0 :::22                       :::*                        LISTEN      958/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1178/master        
udp        0      0 0.0.0.0:34964               0.0.0.0:*                               7271/heartbeat: wri
udp        0      0 0.0.0.0:694                 0.0.0.0:*                               7271/heartbeat: wri

[root@node1 ha.d]# ifconfig  eth1:0         --node1节点的vip启动
eth1:0    Link encap:Ethernet  HWaddr 08:00:27:06:79:47 
          inet addr:192.168.1.105  Bcast:0.0.0.0  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

[root@node1 ha.d]#

 

node2节点:

[root@node2 ha.d]# /etc/init.d/heartbeat  start
Starting High-Availability services: INFO:  Resource is stopped
Done.

[root@node2 ha.d]# netstat -antup |more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      956/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1176/master        
tcp        0     52 192.168.1.102:22            192.168.1.93:51657          ESTABLISHED 1214/sshd          
tcp        0      0 :::22                       :::*                        LISTEN      956/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1176/master        
udp        0      0 0.0.0.0:42151               0.0.0.0:*                               4581/heartbeat: wri
udp        0      0 0.0.0.0:694                 0.0.0.0:*                               4581/heartbeat: wri
[root@node2 ha.d]#

 

测试访问:

wKiom1RhpROiMWheAADxnB2Bt7Y088.jpg

 

测试故障转移:

node1节点:

[root@node1 ha.d]# /etc/init.d/httpd stop
Stopping httpd:                                            [  OK  ]
[root@node1 ha.d]#

 

node2节点:

[root@node2 ha.d]# netstat -antup |more        --关闭node1的httpd服务后,node2的httpd就自动启动了
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      956/sshd           
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1176/master        
tcp        0     52 192.168.1.102:22            192.168.1.93:51657          ESTABLISHED 1214/sshd          
tcp        0      0 :::80                       :::*                        LISTEN      4918/httpd         
tcp        0      0 :::22                       :::*                        LISTEN      956/sshd           
tcp        0      0 ::1:25                      :::*                        LISTEN      1176/master        
udp        0      0 0.0.0.0:42151               0.0.0.0:*                               4581/heartbeat: wri
udp        0      0 0.0.0.0:694                 0.0.0.0:*                               4581/heartbeat: wri
[root@node2 ha.d]#

测试:

 wKiom1RhrAGg4sX0AADgtahLdRM171.jpg

你可能感兴趣的:(linux,heartbeat,高可用集群)