heartbeat实现web服务器高可用

1 heartbeat概述:

heartbeat的工作原理:heartbeat最核心的包括两个。部分,心跳监测部分和资源接管部分,心跳监测可以通过网络链路和串口进行,而且支持冗余链路,它们之间相互发送报文来告诉对方自己当前的状态,如果在指定的时间内未收到对方发送的报文,那么就认为对方失效,这时需启动资源接管模块来接管运行在对方主机上的资源或者服务

下载heartbeat

http://www.linux-ha.org/wiki/Download
Heartbeat-3.X版本以后被分为了4个模块,这些安装包都可以从官网:
http://www.linux-ha.org/wiki/Downloads下载得到:
目前的这些版本是:
ClusterLabs-resource-agents-v3.9.2-0-ge261943.tar.gz #集群实验资源代理
Heartbeat-3-0-7e3a82377fa8.tar.bz2 # 心跳主程序包
pacemaker-1.1.9-1512.el6.src.rpm # 起搏器
ˈpāsˌmākər 起搏器
Reusable-Cluster-Components-glue–glue-1.0.9.tar.bz2 #可重复使用的群集组件

[root@yunzu62 ~]# cat /etc/services | grep 694
在这里插入图片描述

2 用heartbeat实现web服务器高可用

Heartbeat网络图:
在这里插入图片描述
yunzu63 主web
yunzu64 从web
yunzu62 NFS

1 准备工作:yunzu63,yunzu64
  1. 修改主机名,永久生效
    #vim /etc/sysconfig/network
    HOSTNAME=yunzu63.cn
  2. 解析
    #vim /etc/hosts
    192.168.1.63 yunzu63.cn
    192.168.1.64 yunzu64.cn
  3. 关防火墙,关掉selinux
    #iptables -F
    #getenforce
    Permissive
2 配置yunzu62为NFS服务器,提供存储资源

安装NFS服务器:(3台主机均安装)
[root@yunzu62 ~]# yum -y install nfs-utils
[root@yunzu62 ~]# mkdir /wwwdir
[root@yunzu62 ~]# echo ‘Heartbeat Web test!’> /wwwdir/index.html
[root@yunzu62 ~]#cat /etc/exports
/wwwdir 192.168.1.0/24(rw)
配置好目录权限:
[root@yunzu62 ~]# ll -d /wwwdir/
drwxr-xr-x 2 root root 4096 May 29 18:52 /wwwdir/
[root@yunzu62 ~]# chmod 777 -R /wwwdir/
[root@yunzu62 ~]# ll -d /wwwdir/
drwxrwxrwx 2 root root 4096 May 29 18:52 /wwwdir/
开启nfs服务
[root@yunzu62 ~]# systemctl start nfs
[root@yunzu62 ~]# systemctl enable nfs
[root@yunzu62 ~]# showmount –e 查看一下

3 yunzu63测试nfs 存储挂载并安装httpd web服务器

[root@yunzu63 ~]# yum install nfs-utils httpd -y
[root@yunzu63 ~]# systemctl start nfs
[root@yunzu63 ~]# showmount -e 192.168.1.62
Export list for 192.168.1.62:
/wwwdir 192.168.1.0/24
[root@yunzu63 ~]# mount -t nfs 192.168.1.62:/wwwdir /var/www/html/
[root@yunzu63 ~]# systemctl start httpd
测试:
[root@yunzu63 ~]# yum install elinks -y
[root@yunzu63 ~]# elinks --dump 192.168.1.63
Heartbeat Web test!
卸载资源:后期这些资源通过heartbeat直接加载
[root@yunzu63 ~]# umount /var/www/html/
[root@yunzu63 ~]# systemctl stop httpd
[root@yunzu63 ~]# systemctl disable httpd

4 yunzu64测试nfs 存储挂载并安装httpd web服务器:

[root@yunzu64 ~]# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=yunzu64.cn
[root@yunzu63 ~]# scp /etc/hosts 192.168.1.64:/etc/
[root@yunzu64 ~]# yum install nfs-utils httpd -y
[root@yunzu63 ~]# systemctl start nfs
[root@yunzu64 ~]# showmount -e 192.168.1.62
Export list for 192.168.1.62:
/wwwdir 192.168.1.0/24
[root@yunzu64 ~]# mount -t nfs 192.168.1.62:/wwwdir /var/www/html/
[root@yunzu64 ~]# systemctl start httpd
测试:
[root@yunzu64 ~]# yum install elinks -y
[root@yunzu64 ~]# elinks --dump 192.168.1.64
Heartbeat Web test!
卸载资源:后期这些资源通过heartbeat直接加载
[root@yunzu64 ~]# umount /var/www/html/
[root@yunzu64 ~]# systemctl stop httpd
[root@yunzu64 ~]# systemctl disable httpd

5 yunzu63安装heartbeat

heartbeat在Ceontos7下编译步骤
下载:http://www.linux-ha.org/wiki/Downloads,从官方下载最新的HeartBeat版本:
Heartbeat 3.0.6
Cluster Glue 1.0.12
Resource Agents 3.9.6

1.配置基础环境(配置好网络yum源)

安装依赖包
yum install -y bzip2 bzip2-devel gcc gcc-c++ autoconf automake libtool e2fsprogs-devel
glib2-devel libxml2 libxml2-devel libtool-ltdl-devel asciidoc libuuid-devel docbook
如果LTDL包安装不成功,下载rpm包安装:
rpm -ivh libtool-ltdl-devel-2.4.2-22.el7_3.x86_64.rpm

2.编译Cluster Glue

#tar jxf Reusable-Cluster-Components-glue.tar.bz2
#cd Reusable-Cluster-Components-glue–0a7add1d9996/
#groupadd haclient 创建用户组
#useradd -g haclient hacluster 创建用户
#./autogen.sh
#./configure --prefix=/usr/local/heartbeat/
#make && make install

3.编译Resource Agents

#tar -zxf resource-agents-3.9.6.tar.gz
#cd resource-agents-3.9.6
#./autogen.sh
#./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster
–with-daemon-group=haclient --enable-fatal-warnings=no LIBS=’/lib64/libuuid.so.1’
#make
#make install

4.编译Heartbeat

#tar -jxvf Heartbeat-3.0.6.tar.bz2
#cd Heartbeat-3-0-958e11be8686/
#./bootstrap
#export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"

./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS=’/lib64/libuuid.so.1’

#make
#make install

5.生成配置文件:

#cd /usr/local/heartbeat/etc/ha.d/
#cp /root/Heartbeat-3-0-958e11be8686/doc/{ha.cf,haresources,authkeys} .

chkconfig --add heartbeat

chkconfig heartbeat on

chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys

mkdir -pv /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/

cp /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/

#ln -sv /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/

6.Heartbeat配置

Heartbeat的配置主要涉及到ha.cf、haresources、authkeys这三个文件。
ha.cf: 主配置文件,
haresource: 用来配置要让Heartbeat托管的服务,
authkey: 是用来指定Heartbeat的认证方式。

配置ha.cf----主配置文件

vim /usr/local/heartbeat/etc/ha.d/ha.cf (注:前面数字是行号)

24 debugfile /var/log/ha-debug ##用于记录heartbeat的调试信息
29 logfile/var/log/ha-log ##用于记录heartbeat的日志信息
34 logfacilitylocal0 ##设置heartbeat的日志,这里用的是系统日志
48 keepalive 2 ##设定心跳(监测)时间时间为2秒
56 deadtime 30 ##指定若备用节点在30秒内未收到主节点心跳信号,则接管主服务器资源
61 warntime 10 ##指定心跳延迟的时间为10秒,10秒内备节点不能接收主节点心跳信号,即往日志写入警告日志,但不会切换服务
71 initdead 60 ##系统启动或重启后预留的忽略时间段,取值至少为deadtime的两倍
76 udpport 694 ##广播/单播通讯使用的Udp端口
91 #bcast ens32 # Linux ##使用网卡eno32发送心跳检测
113 #mcast eth0 225.0.0.1 694 1 0 ##采用网卡eth0的Udp多播来组织心跳,一般在备用节点
Bcast、ucast和mcast分别代表广播、单播和多播,是组织心跳的的方式,任选其一
121 ucast ens32 192.168.2.65 ##采用网卡eth32的udp单播来组织心跳,后面跟的IP地址为双机对方IP地址
157 auto_failback on ##定义当主节点恢复后,是否将服务自动切回
211 node yunzu63.cn ##主节点名称
212 node yunzu64.cn ##备用节点名称
220 ping 192.168.2.1 ##通过ping网关检测心跳是否正常,仅用来测试网络
253 respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail ##指定和heartbeat一起启动、关闭的进程
259 apiauth ipfail gid=haclient uid=hacluster ##设置启动IPfail的用户和组

配置haresources-----资源文件

Haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址及启动服务集群资源,文件每一行可包含一个或多个资源脚本名,资源间使用空格隔开,参数间使用两个冒号隔开,主节点和备份节点中资源文件haresources要完全一样
一般格式为:
node-name network
node-name表示主节点的主机名,必须和ha.cf文件中指定的节点名一致。network用于设定集群的
IP地址、子网掩码和网络设备标识等。resource-group用于指定需Heartbeat托管的服务(即这些
服务可由Heartbeat来启动和关闭)。
注意:这里指定的IP地址就是集群对外服务的IP地址
如要托管这些服务,必须将服务写成可通过start/stop来启动或关闭的脚本,放到/etc/init.d/
或/etc/ha.d/resource.d/目录下,Heartbeat会根据脚本名称自动去/etc/init.d或者
/etc/ha.d/resource.d目录下找到相应脚本进行启动或关闭操作。
例:

vim /usr/local/heartbeat/etc/ha.d/haresources (注:前面数字为行号)

44 yunzu63.cn IPaddr::192.168.2.111/24/ens32 Filesystem::192.168.2.62:/wwwdir::/var/www/html::nfs httpd
注:yunzu63.cn是主服务器的主机名, yunzu64上不需要修改。这样资源默认会加一这个主机上。当yunzu63坏了,yunzu64会再接管。
IPaddr::192.168.2.111/24/eth32 #指定VIP及绑定到哪个网卡上
Filesystem::192.168.2.62:/wwwdir::/var/www/html::nfs #指定要挂载的存储
httpd #指定要启动的服务。这个服务必须是在/etc/init.d下或者/usr/local/heartbeat/etc/ha.d/resource.d目录下

配置authkeys-----心跳密钥验证文件

#vim /usr/local/heartbeat/etc/ha.d/authkeys
auth 3
3 md5 Hello!
注:auth后填序号,可任意填写,但第二行开头必须为序号名,然后为验证方式,支持三种( crc md5 sha1 )方式验证,最后面是自定义密钥。我应该选哪种验证?
如果Heartbeat运行于安全网络之上,如本例中的交叉线,可以使用crc,从资源的角度来看,这是代价最低的方法。如果网络并不安全,但也希望降低CPU使用,则使用md5。最后,如果想得到最好的认证,而不考虑CPU使用情况,则使用sha1,它在三者之中最难破解

编写httpd启动脚本

vim /usr/local/heartbeat/etc/ha.d/resource.d/httpd

#!/bin/bash
/bin/systemctl KaTeX parse error: Expected 'EOF', got '#' at position 14: 1 httpd #̲chmod 755 !

复制配置文件至备机

#scp –rp /usr/local/heartbeat/etc/ha.d/* root@192.168.2.64:/usr/local/heartbeat/etc/ha.d/

测试:

手动加载VIP 192.168.1.111到ens32上
[root@yunzu63 ~]# cd /usr/local/heartbeat/etc/ha.d/resource.d
[root@yunzu63 resource.d]# ./IPaddr 192.168.1.111/24/ens32 start
IPaddr[7116]: INFO: Success
INFO: Success
查看 VIP:
[root@yunzu63 ~]# ifconfig #可以看到 ens32:1 的IP为:192.168.1.111

手动加载NFS存储资源到/var/www/html (加载需要fuser支持,yum install psmisc -y)

[root@yunzu63 resource.d]# ./Filesystem 192.168.1.62:/wwwdir /var/www/html/ nfs start
Filesystem[23567]: INFO: Success
INFO: Success
[root@yunzu63 ~]# ls /var/www/html/index.html
/var/www/html/index.html
[root@yunzu63 ~]# df -h
………
192.168.2.62:/wwwdir 18G 1.1G 17G 6% /var/www/html

手动启动httpd服务

[root@yunzu63 ~]# systemctl start httpd

测试打开主服务页面http://192.168.1.63

6.yunzu64上配置heartbeat

修改文件权限:
[root@yunzu64 ~]# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys #此文件必须是600权限 否则heartbeat启动不成功
修改单播地址:
[root@yunzu64 ~]# vim /usr/local/heartbeat/etc/ha.d/ha.cf
改:
ucast ens32 192.168.1.64
为:
ucast ens32 192.168.1.63
挂载需要fuser支持,yum install psmisc -y

7.两机器启动heartbeat服务:

[root@yunzu63 ~]# /etc/init.d/heartbeat restart
[root@yunzu64 ~]# /etc/init.d/heartbeat restart
查看端口号:
[root@yunzu63 ~]# netstat -antup | grep 694
udp 0 0 0.0.0.0:694 0.0.0.0:* 6895/heartbeat: wr
查看端口号:
[root@yunzu64 ~]# netstat -antup | grep 694
udp 0 0 0.0.0.0:694 0.0.0.0:* 6895/heartbeat: wri

在yunzu63查看集群资源:
[root@yunzu63 ~]# ifconfig #查看VIP加载
ens32:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.111 netmask 255.255.255.0 broadcast 192.168.1.255
ether 00:0c:29:f0:fc:2c txqueuelen 1000 (Ethernet)
[root@yunzu63 ~]# df -h
[root@yunzu63 ~]# df -h #查看挂载

192.168.2.62:/wwwdir 18G 1.1G 17G 6% /var/www/html
[root@yunzu63 ~]# netstat -anput | grep httpd #查看httpd

yunzu64上查看,没有任务资源:
[root@yunzu64 ~]# ifconfig
[root@yunzu64 ~]# df -h
[root@yunzu64 ~]# netstat -anput | grep httpd

通过VIP测试:
两台主机都开启时,所有请求到转到了yunzu63上。访问:http://192.168.1.111/ 正常

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值