CentOS6.5搭建HeartBeat(http服务)

时间:2024-03-14 14:55:54

一.环境

1.Linux系统,Vmware,两台虚拟机CentOS6.5系统,双网卡。

2.Heartbeat3.0.4

3.主机网卡:

主机服务器IPeth0192.168.27.143

主机心跳线IPeth110.5.36.41

4.备机网卡:

备机服务器IPeth0192.168.27.147

备机心跳线IPeth110.5.36.135

5.虚拟IPeth0:0192.168.27.200

6.监控裁判IP10.5.36.1

7.主机名字:host-A

8.备机名字:host-B

9. HA实现服务:httpd

二.搭建及测试

1.   配置网卡

实验要求主机和备机都需要两个网卡,所以需要先检查虚拟机网卡个数,如果不够则需要添加网卡,够了就跳过该步骤。

1)查看虚拟机网卡

首先通过ifconfig命令检查虚拟机网卡,一般配置的时候是单网卡即只有eth0,需要自己添加eth1.

2)添加网卡

VM菜单选择虚拟机—>设置—>添加—>选择网络适配器—>选择桥接模式。
CentOS6.5搭建HeartBeat(http服务)CentOS6.5搭建HeartBeat(http服务)

3)添加之后重启系统,通过ifconfig命令检查是否是双网卡。

ifconfig命令会显示每台机器的eth0eth1

令主备机的eth0 网卡IP为服务器IP(主机服务器:192.168.27.143,备机服务器192.168.27.147

令主备机的eth1网卡IP为监测心跳的IP(主机心跳线:10.5.36.41,备机心跳线:10.5.36.135

2.   设置主机名字

1)修改network文档

vim /etc/sysconfig/network

修改两台服务器的名字,主机改为host-A,备机改为host-B
HOSTNAME=host-A

CentOS6.5搭建HeartBeat(http服务)
CentOS6.5搭建HeartBeat(http服务)

2)修改hosts文档

主备机内容一样,添加服务器IP以及服务器名称。

vim /etc/hosts

CentOS6.5搭建HeartBeat(http服务)CentOS6.5搭建HeartBeat(http服务)

3)重启

init 6

4)检查修改结果

uname -n

命令为显示主机名字的命令,查看是否修改成功,主机为host-A,备机为host-B

CentOS6.5搭建HeartBeat(http服务)CentOS6.5搭建HeartBeat(http服务)CentOS6.5搭建HeartBeat(http服务)

3.   搭建http服务器

1)        安装HTTP

yum -y install httpd

2)        配置服务器

A)备份主配置文件

cd /etc/httpd/conf

cp httpd.conf httpd.conf.bak

B)修改主配置文件

vim /etc/httpd/conf/httpd.conf

...

 74 KeepAlive On

...

265 ServerName host-A:80//主机为host-A,备机为host-B

...

391 DirectoryIndex index.html index.php

C)设置index.html页面

修改/var/www/html/index.html

vim /var/www/html/index.html

主机改为This is host-A

CentOS6.5搭建HeartBeat(http服务)CentOS6.5搭建HeartBeat(http服务)

备机改为This is host-B

CentOS6.5搭建HeartBeat(http服务)CentOS6.5搭建HeartBeat(http服务)

D)检验安装结果

启动HTTP服务:service httpd restart

chkconfig httpd onCentOS6.5搭建HeartBeat(http服务)

浏览器分别输入主机和备机服务器的地址,显示如下表示搭建完毕。

CentOS6.5搭建HeartBeat(http服务)
CentOS6.5搭建HeartBeat(http服务)

CentOS6.5搭建HeartBeat(http服务)

 CentOS6.5搭建HeartBeat(http服务)

4.   安装heartbeat

环境

yum install e2fsprogs-devel libuuid-devel Libnetlibtool-ltdl-devel libxslt-devel bzip2-devel glib2-devel perl* glib*

yum install autoconf automake libtool glib2-develbzip2-devel glib-devel libxml2 libxml2-devel docbook-style-xsllibtool-ltdl-devel

yum install net-snmp* perl-Compress* perl-HTML* perl-libwww* perl-MailTools* perl-TimeDate* perl-URI*

yum install perl-IO-Socket* perl-Socket6* perl-Pod*perl-Test-Pod* libnet*

安装

yum -y install epel-release  

yum -y install heartbeat*

5.   配置heartbeat

Heartbeat主要有三个文件,ha.cfharesourcesauthkeys

主机

1)拷贝文件到相关路径

cp /usr/share/doc/heartbeat-3.0.4/ha.cf /etc/ha.d

cp /usr/share/doc/heartbeat-3.0.4/haresources /etc/ha.d

cp /usr/share/doc/heartbeat-3.0.4/authkeys /etc/ha.d

2)修改authkeys

修改文件权限

cd /etc/ha.d
chmod 600 authkeys

CentOS6.5搭建HeartBeat(http服务)

修改内容

vim authkeys
CentOS6.5搭建HeartBeat(http服务)CentOS6.5搭建HeartBeat(http服务)
3)设置虚拟IP 192.168.27.200端口eth0:0
修改haresources
vi haresources

CentOS6.5搭建HeartBeat(http服务)
4)修改ha.cf

1. debugfile /var/log/ha-debug 

2. logfile /var/log/ha-log  

3. logfacility    local0  

4. keepalive 2  

5. deadtime 30  

6. warntime 10  

7. initdead 60  

8. udpport 694  

9. ucast eth1 192.168.1.100 //备机的心跳线地址

10. auto_failback off  

11. node   host-A  

12. node   host-B  

13. ping 10.5.36.1  //监控裁判IP

14. respawn hacluster /usr/lib64/heartbeat/ipfail //注:有些机器为/usr/lib/heartbeat/ipfail

备机

和主机类似

1)        拷贝文档至相关路径

2)        authkeysharesources和主机一样

3)        ha.cf有一处不同

ucast eth1 10.5.36.41 主机的心跳线

6.   测试

首先关闭防火墙和Selinux
关闭防火墙 service iptables stop
关闭Selinux命令:setenforce 0
1)        两台机器同时关闭http服务
service httpd stop
CentOS6.5搭建HeartBeat(http服务)CentOS6.5搭建HeartBeat(http服务)

2)        开启主机Heartbeat服务

service heartbeat start
CentOS6.5搭建HeartBeat(http服务)

3)        等待大约1分钟,检查虚拟IP是否启动

ip a

CentOS6.5搭建HeartBeat(http服务)CentOS6.5搭建HeartBeat(http服务)
可以通过命令tial /var/log/ha-log查看运行日志
CentOS6.5搭建HeartBeat(http服务)

4)启动之后浏览器访问主机服务器IP 192.168.27.143或者虚拟IP 192.168.27.200都会显示This is host-A页面。

CentOS6.5搭建HeartBeat(http服务)

CentOS6.5搭建HeartBeat(http服务)


5)启动备机B

B此时不会启用虚拟IP。


6)模拟主机故障,即强制关闭AB监测到A故障会接替A服务,此时访问虚拟IP192.168.27.200会显示This is host-B页面。

查看日志,发现备机接替了主机的工作。

CentOS6.5搭建HeartBeat(http服务)

CentOS6.5搭建HeartBeat(http服务)


7)启动主机,开启HeartBeat服务,A重新服务,此时访问虚拟IP192.168.27.200会显示This is host-A页面。