HeartBeat

 

如何使用Heartbeat,组建一个高可用性的Apache集群?

 

一、原理

Heartbeat提供了诸多集群基础架构服务,比如集群之间的消息传递、节点成员身份、IP地址分配和迁移,以及服务的开启和停止。Heartbeat 可以用来为Apache、Samba和Squid等企业应用系统构建几乎任何一种高可用性的集群。此外,它可以结合负载均衡软件使用,那样入站请求就可以 由所有集群节点来分担。

二、配置网络

Server6节点:

[root@server6 network-scripts]# ip addr show   ##eth0为本机的ip地址,eth1为心跳的ip地址

 

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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 52:54:00:86:4c:9a brd ff:ff:ff:ff:ff:ff

    inet 172.25.60.16/24 brd 172.25.60.255 scope global eth0

    inet6 fe80::5054:ff:fe86:4c9a/64 scope link 

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 52:54:00:5d:65:8f brd ff:ff:ff:ff:ff:ff

    inet 10.0.0.1/24 brd 10.0.0.255 scope global eth1

    inet6 fe80::5054:ff:fe5d:658f/64 scope link 

       valid_lft forever preferred_lft forever

[root@server6 network-scripts]# route -n    ##网关

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

172.25.60.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1

0.0.0.0         172.25.60.1     0.0.0.0         UG    0      0        0 eth0

 

 

[root@server7 network-scripts]# ip addr show   ##eth0为本机的ip地址,eth1为心跳的ip地址

 

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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 52:54:00:02:a0:76 brd ff:ff:ff:ff:ff:ff

    inet 172.25.60.17/24 brd 172.25.60.255 scope global eth0

    inet6 fe80::5054:ff:fe02:a076/64 scope link 

       valid_lft forever preferred_lft forever

3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 52:54:00:50:32:32 brd ff:ff:ff:ff:ff:ff

    inet 10.0.0.2/24 brd 10.0.0.255 scope global eth1

    inet6 fe80::5054:ff:fe50:3232/64 scope link 

       valid_lft forever preferred_lft forever

[root@server7 network-scripts]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

172.25.60.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1

0.0.0.0         172.25.60.1     0.0.0.0         UG    0      0        0 eth0

 

 

[root@server7 network-scripts]# ping server6  ##检查两台服务器是否ping

PING server6 (172.25.60.16) 56(84) bytes of data.

64 bytes from server6 (172.25.60.16): icmp_seq=1 ttl=64 time=0.224 ms

64 bytes from server6 (172.25.60.16): icmp_seq=2 ttl=64 time=0.157 ms

^C

--- server6 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1917ms

rtt min/avg/max/mdev = 0.157/0.190/0.224/0.036 ms

 

三、安装heartbeat软件

Server6节点:

[root@server6 ~]# ls

heartbeat-3.0.4-2.el6.x86_64.rpm        heartbeat-libs-3.0.4-2.el6.x86_64.rpm

heartbeat-devel-3.0.4-2.el6.x86_64.rpm  libnet-1.1.6-7.el6.x86_64.rpm

[root@server6 ~]# rpm -ivh libnet-1.1.6-7.el6.x86_64.rpm 

[root@server6 ~]# rpm -ivh heartbeat-*

Server7 节点同上

四、在两个节点安装apache服务测试

Server6节点:

[root@server6 ~]# /etc/init.d/httpd stop

停止 httpd:                                               [失败]

[root@server6 ~]# /etc/init.d/httpd status

httpd 已停

[root@server6 ~]# chkconfig httpd off

 

Server7节点:

[root@server7 ~]# /etc/init.d/httpd status

httpd 已停

[root@server7 ~]# chkconfig httpd off

[root@server7 ~]#  echo "node2" > /var/www/html/index.html

 

五、配置HeartBeat集群软件

Server6节点:

[root@server6 doc]# cp authkeys /etc/ha.d/

[root@server6 doc]# cp haresources /etc/ha.d/

[root@server6 doc]# cp ha.cf /etc/ha.d/

[root@server6 doc]# cd /etc/ha.d/

[root@server6 ha.d]# ls

authkeys  ha.cf  haresources

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

auth 3   ##添加这两行

3 md5 1111

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

[root@server6 ha.d]# vim ha

ha.cf        haresources  

[root@server6 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 server6          --添加这行,写上两台服务器的主机名 

node server7

ping_group 172.25.60.16 172.25.60.17 172.25.60.1    --检查主机是否宕机

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

compression_threshold 2     --大于2K就压缩

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

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

[root@server6 ha.d]# scp * server7:/etc/ha.d/  ##将三个配置文件复制到server7中

The authenticity of host 'server7 (172.25.60.17)' can't be established.

RSA key fingerprint is f7:67:1d:fd:81:8d:57:a4:cd:90:ed:de:3c:74:44:b5.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'server7,172.25.60.17' (RSA) to the list of known hosts.

root@server7's password: 

authkeys                                      100%  663     0.7KB/s   00:00    

ha.cf                                         100%   10KB  10.3KB/s   00:00    

haresources                                   100% 5943     5.8KB/s   00:00

六、启动服务,测试是否正常

Server6节点:

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

[root@server6 ha.d]# netstat -antlpe
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           
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@server6 ha.d]# ifconfig  eth1:0         --server6节点的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 

Server7节点:

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

[root@server7 ha.d]# netstat -antlpe
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         
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 

 

测试访问:

 

 

测试故障转移:

Server6节点:

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

 

Server7节点:

[root@server7 ha.d]# netstat -antlpe       --关闭server6httpd服务后,server7httpd就自动启动了
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          
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 

测试:

 

 

 

posted @ 2017-05-31 22:55  yangying  阅读(243)  评论(0编辑  收藏  举报