Web应用通过Heartbeat实现高可用初探
一、环境介绍说明:所有案例在虚拟机(VMware)上完成
操作系统:centos 6.5 64bit
高可用软件:heartbeat 3.0.4
Web应用服务器:apache httpd 2.2.15
二、heartbeat服务主机资源规划
主机名称接口IP用途
heartbeat-mastereth0172.18.10.227外网管理IP,用于WAN数据转发
eth1172.18.0.227心跳线,用于服务器间心跳连接(直连)
172.18.10.200提供应用服务A的虚拟IP(VIP)
heartbeat-slaveeth0172.18.10.228外网管理IP,用于WAN数据转发
eth1172.18.0.228心跳线,用于服务器间心跳连接(直连)
172.18.10.201提供应用服务B的虚拟IP(VIP)
三、安装配置(主从服务器配置相同配置)
1 配置主机名称和hosts文件
1.1 将主机名替换为heartbeat-master
1
2
# sed -i 's#HOSTNAME=Base#HOSTNAME=heartbeat-master#g' /etc/sysconfig/network
# hostname heartbeat-master
退出重新登录系统(ctrl+d)
1
#
1.2 配置hosts文件
1
2
3
4
5
6
7
# cat >>/etc/hosts<<eof
> 172.18.0.227 heartbeat-master # 将主机名称与心跳线进行绑定
> 172.18.0.228 heartbeat-slave # 将主机名称与心跳线进行绑定
> eof
# tail -2 /etc/hosts
172.18.0.227 heartbeat-master
172.18.0.228 heartbeat-slave
1.3 检查配置是否正确
1
2
3
4
5
# tail -2 /etc/hosts
172.18.0.227 heartbeat-master
172.18.0.228 heartbeat-slave
# ping heartbeat-master
# ping heartbeat-slave
2 增加主机路由
2.1 在heartbeat-master主机上配置如下:
1
2
3
4
5
6
7
8
9
10
11
# route add -host 172.18.0.228 dev eth1
# echo "/sbin/route add -host 172.18.0.228 dev eth1" >> /etc/rc.local
# tail -1 /etc/rc.local
/sbin/route add -host 172.18.0.228 dev eth1
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.18.0.228 0.0.0.0 255.255.255.255 UH 0 0 0 eth1
172.18.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
172.18.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
......省略
2.2 在heartbeat-slave主机上配置如下:
1
2
3
4
5
6
7
8
9
10
11
# route add -host 172.18.0.227 dev eth1
# echo "/sbin/route add -host 172.18.0.227 dev eth1" >> /etc/rc.local
# tail -1 /etc/rc.local
/sbin/route add -host 172.18.0.227 dev eth1
# route -n #检查路由是否配置正确
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.18.0.227 0.0.0.0 255.255.255.255 UH 0 0 0 eth1
172.18.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
172.18.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
......省略
四、安装Heartbeat软件
需要说明,CentOS5.8系统自带heartbeat软件,通过yum方式直接可以安装,但是CentOS6版本之后就不再支持通过yum方式安装heartbeat了,需要首先安装epel包,通过epel包安装heartbeat软件。
1 创建软件存放目录(良好的习惯)
1
2
# mkdir -p /home/oldcat/tools
# cd /home/oldcat/tools/
2 下载并安装epel包
1
2
3
4
5
6
7
8
9
10
11
12
# wget http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
--2016-01-08 00:16:00--http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
正在解析主机 mirrors.ustc.edu.cn... 202.141.160.110, 2001:da8:d800:95::110
正在连接 mirrors.ustc.edu.cn|202.141.160.110|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
......省略
# rpm -ivh epel-release-6-8.noarch.rpm
warning: epel-release-6-8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ###########################################
1:epel-release ###########################################
# rpm -qa|grep epel
epel-release-6-8.noarch
3 yum安装heartbeat
1
2
3
4
5
6
7
# yum install heartbeat -y
Loaded plugins: fastestmirror, security
base | 3.7 kB 00:00
base/primary_db | 4.6 MB 00:00
epel/metalink | 4.5 kB 00:00
epel
......省略
五、安装Apache httpd软件
1 yum安装httpd
1
# yum install httpd -y
六、配置高可用服务
1 配置heartbeat
1.1 heartbeat通过yum方式安装后默认配置文件目录为/etc/ha.d/,但是默认不包含配置文件,需要将
/usr/share/doc/heartbeat-3.0.4/目录下的ha.cf、authkeys和haresources拷贝到/etc/ha.d/目录
1
2
3
4
5
6
7
8
9
10
11
12
# cp authkeys ha.cf haresources /etc/ha.d/
# cd /etc/ha.d/
# ll
总用量 44
-rw-r--r--. 1 root root 645 1月 8 01:03 authkeys
-rw-r--r--. 1 root root 10502 1月 8 01:03 ha.cf
-rwxr-xr-x. 1 root root 745 12月3 2013 harc
-rw-r--r--. 1 root root5905 1月 8 01:03 haresources
drwxr-xr-x. 2 root root4096 1月 8 00:30 rc.d
-rw-r--r--. 1 root root 692 12月3 2013 README.config
drwxr-xr-x. 2 root root4096 1月 8 00:30 resource.d
-rw-r--r--. 1 root root2082 11月 10 19:00 shellfuncs
1.2 编辑authkeys文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# cat authkeys
auth 1
1 sha1 49234s43k345lkj3lk239sljdf23478cxj90s8lwe5rll # 密钥串为自定义,尽量长即可
配置说明:sha1为加密方式,可选的加密方式还有crc及md5,但是官方并不建议使用crc方式。
#
# Authentication file.Must be mode 600
#
#
# Must have exactly one auth directive at the front.
# auth send authentication using this method-id
#
# Then, list the method and key that go with that method-id
#
# Available methods: crc sha1, md5.Crc doesn't need/want a key.
#
# You normally only have one authentication method-id listed in this file
#
# Put more than one to make a smooth transition when changing auth
# methods and/or keys.
#
#
# sha1 is believed to be the "best", md5 next best.
#
# crc adds no security, except from packet corruption.
# Use only on physically secure networks.
# chmod 600 authkeys# authkeys文件的权限必须为600!!
# ll
总用量 40
-rw-------. 1 root root 60 1月 8 01:09 authkeys
-rw-r--r--. 1 root root 10502 1月 8 01:03 ha.cf
-rwxr-xr-x. 1 root root 745 12月3 2013 harc
-rw-r--r--. 1 root root 99 1月 8 01:13 haresources
drwxr-xr-x. 2 root root4096 1月 8 00:30 rc.d
-rw-r--r--. 1 root root 692 12月3 2013 README.config
drwxr-xr-x. 2 root root4096 1月 8 00:30 resource.d
-rw-r--r--. 1 root root2082 11月 10 19:00 shellfuncs
1.3 编辑haresources文件
1
2
3
4
5
6
7
# cat >> haresources <<eof
> heartbeat-master IPaddr::172.18.10.200/24/eth0 httpd
> heartbeat-slave IPaddr::172.18.10.201/24/eth0
> eof
# cat haresources
heartbeat-master IPaddr::172.18.10.200/24/eth0 httpd
heartbeat-slave IPaddr::172.18.10.201/24/eth0
配置说明:
a.172.18.10.200为初始绑定在heartbeat-master主机上的vip,同理172.18.10.201为初始绑定
在heartbeat-slave主机上的vip。
b.虽然同时配置了master和slave的vip,但是初始绑定只会绑定属于本机的vip,只有在配置的时间内
接收不到对端服务器的心跳数据才会接管对端主机的vip及所有资源。
c.httpd参数是将httpd服务的启动停止操作委托给heartbeat负责。
1.4 编辑ha.cf文件
1
2
3
4
5
6
7
8
9
10
11
12
13
# cat ha.cf
debugfile /var/log/ha-debug.log
logfile /var/log/ha.log
logfacility local1
keepalive 2
deadtime 30
warntime 10
initdead 90
mcast eth1 225.0.0.100 694 1 0
auto_failback on
node heartbeat-master
node heartbeat-slave
crm no
页:
[1]