Heartbeat实现web服务器高可用

一、Heartbeat概述:

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

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 # 起搏器
  Reusable-Cluster-Components-glue--glue-1.0.9.tar.bz2 #可重复使用的群集组件

端口号:694
[[email protected] ~]# cat /etc/services | grep 694
ha-cluster 694/tcp # Heartbeat HA-cluster
ha-cluster 694/udp # Heartbeat HA-cluster

扩展: 谁管理着TCP/UDP公共服务的端口定义
  IANA 就是指(Internet Assigned Numbers Authority) ,Internet号分配的机构。负责对IP地址分配规划以及对TCP/UDP公共服务的端口定义。
  IANA的所有任务可以大致分为三个类型:
    一、域名。IANA管理DNS域名根和.int,.arpa域名以及IDN(国际化域名)资源。
    二、数字资源。IANA协调全球IP和AS(自治系统)号并将它们提供给各区域Internet注册机构。
      注: AS自治系统号,是BGP路由协议中的号。
    三、协议分配。IANA与各标准化组织一同管理协议编号系统。
  官网:http://www.iana.org/

二、实战:使用Heartbeat实现web服务器高可用

1.Heartbeat 拓扑图

  xuegod63   主web
  xuegod64    从web
  xuegod62    NFS

2.准备工作:xuegod63,xuegod64

1. 修改主机名,永久生效
# vim /etc/sysconfig/network
HOSTNAME=xuegod63.cn
2. 解析
# vim /etc/hosts
192.168.1.63   xuegod63.cn
192.168.1.64   xuegod64.cn

3. 关防火墙,关掉selinux
# iptables -F
# getenforce

3.配置xuegod62为NFS服务器,提供存储资源  

[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# mkdir /wwwdir
[[email protected] ~]# echo ‘Heartbeat web test‘> /wwwdir/index.html
[[email protected] ~]# cat !$
cat /wwwdir/index.html
Heartbeat web test
[[email protected] ~]# vim /etc/exports
[[email protected] ~]# cat !$
cat /etc/exports
/wwwdir	192.168.1.0/24(rw)
[[email protected] ~]# chmod 777 -R /wwwdir/
[[email protected] ~]# ll -d /wwwdir/
drwxrwxrwx 2 root root 24 Jul  3 15:19 /wwwdir/
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[[email protected] ~]# showmount -e
Export list for xuegod70.cn:
/wwwdir 192.168.1.0/24

4.xuegod63,64测试nfs 存储挂载并安装httpd web服务器(基本操作是一致)

[[email protected] ~]# yum install nfs-utils httpd -y
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# showmount -e 192.168.1.62
Export list for 192.168.1.62:
/wwwdir 192.168.1.0/24
[[email protected] ~]# mount -t nfs 192.168.1.62
mount: 在 /etc/fstab 中找不到 192.168.1.62
[[email protected] ~]# mount -t nfs 192.168.1.62:/wwwdir /var/www/html/
[[email protected] ~]# df -h
文件系统               容量  已用  可用 已用% 挂载点
/dev/sda3               19G  1.9G   17G   11% /
devtmpfs               479M     0  479M    0% /dev
tmpfs                  489M     0  489M    0% /dev/shm
tmpfs                  489M  6.7M  482M    2% /run
tmpfs                  489M     0  489M    0% /sys/fs/cgroup
/dev/sr0               4.3G  4.3G     0  10% /mnt
/dev/sda1              497M  113M  385M   23% /boot
tmpfs                   98M     0   98M    0% /run/user/0
192.168.1.62:/wwwdir   19G  1.5G   18G    8% /var/www/html
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
[[email protected] ~]# systemctl restart httpd
[[email protected] ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[[email protected] ~]# elinks --dump 192.168.1.62   #测试挂载情况
   Heartbeat web test
#卸载资源:后期这些资源通过heartbeat直接加载
[[email protected] ~]# umount /var/www/html/
[[email protected] ~]# systemctl stop httpd
[[email protected] ~]# systemctl disable httpd

5.xuegod63安装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的认证方式。
    (1) 配置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.1.64   ##采用网卡eth32的udp单播来组织心跳,后面跟的IP地址为双机对方IP地址
157 auto_failback on                 ##定义当主节点恢复后,是否将服务自动切回
211 node xuegod63.cn                     ##主节点名称
212 node xuegod64.cn                     ##备用节点名称
220 ping 192.168.1.1                ##通过ping网关检测心跳是否正常,仅用来测试网络
253 respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail   ##指定和heartbeat一起启动、关闭的进程
259 apiauth ipfail gid=haclient uid=hacluster     ##设置启动IPfail的用户和组

    (2) 配置haresources-----资源文件
Haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址及启动服务集群资源,文件每一行可包含一个或多个资源脚本名,资源间使用空格隔开,参数间使用两个冒号隔开,主节点和备份节点中资源文件haresources要完全一样

一般格式为:
node-name network  <resource-group>
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 xuegod63.cn IPaddr::192.168.2.111/24/ens32 Filesystem::192.168.2.62:/wwwdir::/va
r/www/html::nfs  httpd
xuegod71.cn IPaddr::192.168.10.111/24/ens33 Filesystem::192.168.10.70:/wwwdir::/var/www/html::nfs  httpd
注:xuegod63.cn是主服务器的主机名, xuegod64上不需要修改。这样资源默认会加一这个主机上。当xuegod63坏了,xuegod64会再接管。
IPaddr::192.168.1.111/24/eth32  #指定VIP及绑定到哪个网卡上
Filesystem::192.168.1.62:/wwwdir::/var/www/html::nfs   #指定要挂载的存储
httpd  #指定要启动的服务。这个服务必须是在/etc/init.d下或者/usr/local/heartbeat/etc/ha.d/resource.d目录下
    (3) 配置authkeys-----心跳密im钥验证文件
#vim /usr/local/heartbeat/etc/ha.d/authkeys
auth 3
3 md5 Hello!
注:auth后填序号,可任意填写,但第二行开头必须为序号名,然后为验证方式,支持三种( crc md5 sha1 )方式验证,最后面是自定义密钥。我应该选哪种验证?
如果Heartbeat运行于安全网络之上,如本例中的交叉线,可以使用crc,从资源的角度来看,这是代价最低的方法。如果网络并不安全,但也希望降低CPU使用,则使用md5。最后,如果想得到最好的认证,而不考虑CPU使用情况,则使用sha1,它在三者之中最难破解。

    (4) 编写httpd启动脚本
# vim /usr/local/heartbeat/etc/ha.d/resource.d/httpd
#!/bin/bash
/bin/systemctl $1 httpd
# chmod 755 !$

    ⑸ 复制配置文件至备机
# scp -rp /usr/local/heartbeat/etc/ha.d/* [email protected]:/usr/local/heartbeat/etc/ha.d/  

三、测试

1.手动加载VIP 192.168.1.111到ens32上

[[email protected] ~]# cd /usr/local/heartbeat/etc/ha.d/resource.d
[[email protected] resource.d]# ./IPaddr 192.168.1.111/24/ens32 start
IPaddr[7116]: INFO:  Success
INFO:  Success
查看 VIP:
[[email protected] ~]# ifconfig    #可以看到 ens32:1  的IP为:192.168.1.111

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

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

手动启动httpd服务
[[email protected] ~]# systemctl start httpd

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

3.xuegod64上配置heartbeat

修改文件权限:
[[email protected] ~]# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys  #此文件必须是600权限 否则heartbeat启动不成功
修改单播地址:
[[email protected] ~]# 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

4.两机器启动heartbeat服务

[[email protected] ~]# /etc/init.d/heartbeat restart
[[email protected] ~]# /etc/init.d/heartbeat restart

查看端口号:
[[email protected] ~]# netstat -antup | grep 694
udp        0      0 0.0.0.0:694        0.0.0.0:*           6895/heartbeat: wr
查看端口号:
[[email protected] ~]# netstat -antup | grep 694
udp        0      0 0.0.0.0:694        0.0.0.0:*           6895/heartbeat: wri

5.查看集群  

在xuegod63查看集群资源:
[[email protected] ~]# 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) [[email protected] ~]# df -h

[[email protected] ~]# df  -h #查看挂载
………
192.168.2.62:/wwwdir    18G  1.1G   17G    6% /var/www/html

[[email protected] ~]# netstat -anput | grep httpd   #查看httpd

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

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

6.故障设定  

当掉xuegod63,关闭网卡,等30秒钟,所有请求都转到xuegod64上了
[[email protected] ha.d]# ifdown ens32
[[email protected] ~]# ifconfig
...
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:48:80:95
          inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0
[[email protected] ~]# df -h
...
192.168.1.62:/wwwdir  9.7G  3.4G  5.8G  37% /var/www/html
[[email protected] ~]#systemctl status httpd
httpd (pid  6375) is running...

xuegod63上把ens32网卡再次开启:
[[email protected] ~]# ifup ens32
等30秒后,查看:
[[email protected] ~]# df -h
...
192.168.1.62:/wwwdir  9.7G  3.4G  5.8G  37% /var/www/html    #已经加载了httpd资源
[[email protected] ~]# systemctl status httpd
httpd (pid  27097) is running...
[[email protected] ~]# ifconfig
。。。
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:12:EC:1E
          inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0
资源已经回切过来。
在xuegod64上查看释放资料:
[[email protected] ~]# ifconfig   #查看不到ens32:0  192.168.1.111这个IP地址
[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             9.7G  3.7G  5.5G  41% /
tmpfs                 569M     0  569M   0% /dev/shm
/dev/sda1             194M   28M  157M  15% /boot
/dev/sr0              3.4G  3.4G     0 100% /mnt
[[email protected] ~]# service httpd status
httpd is stopped

扩展:
heartbeat自带的断网切换的工具-ipfail
  ipfail断网切换的原理

  关于ipfail这个断网切换的原理很简单,首先heartbeat要判断自己的网络是否正常其实就是通过ping某个ip,如果可以ping的通,说明网络是通的,如果ping不通了,说明是网络断了,或者是主服务器的网卡坏了,然后执行切换的动作。

ping一个group的ipfail配置:

ping_group group1 172.16.103.254 172.16.103.212

原文地址:https://www.cnblogs.com/happy1983/p/9258645.html

时间: 2024-08-29 14:46:35

Heartbeat实现web服务器高可用的相关文章

用Heartbeat实现web服务器高可用

用Heartbeat实现web服务器高可用 heartbeat概述: Heartbeat 项目是 Linux-HA 工程的一个组成部分,它实现了一个高可用集群系统.心跳服务和集群通信是高可用集群的两个关键组件,在 Heartbeat 项目里,由 heartbeat 模块实现了这两个功能. 端口号:694 1)heartbeat的工作原理: heartbeat最核心的包括两个.部分,心跳监测部分和资源接管部分,心跳监测可以通过网络链路和串口进行,而且支持冗余链路,它们之间相互发送报文来告诉对方自己

heartbeat 配置两台web服务器高可用集群案例

通过heartbeat 配置两台web服务器高可用集群案例: 拓扑: web1:192.168.0.101 hostname: web1.example.com web2:192.168.0.102 hostname:web2.example.com vip:192.168.0.254 nfs服务器:192.168.0.100(存放web页面) 注意: 1主机名要与uname -n 保持一致 2.web1与web2主机的date时间保持同步 3.web1与web2,ssh双机互信通信 4.关闭i

Heartbeat实现web服务高可用(三)

六:Heartbeat实现WEB服务高可用案例 6.1 部署准备 资源环境:继续使用我们之前已经部署好Heartbeat的两台服务器node01.cn和node02.cn,两台机器heartbeat是双主模式我们再捋一捋    node01.cn   eth0 172.10.25.26 外网管理IP                      eth1 10.25.25.16  心跳直连                      VIP  172.10.25.18        node02.cn

Heartbeat实现web服务高可用

在之前的部署过程中,成功的部署并配置了heartbeat,而且也能实现两节点之间VIP的高可用,这里来配置并实现httpd服务的高可用. 一.安装Apache 两台heartbeat节点服务器都先停止heartbeat服务. /etc/init.d/heartbeat stop 分别在heartbeat01和heartbeat02上安装httpd服务 yum -y install httpd 分别在两个节点上执行 echo $HOSTNAME >>/var/www/html/index.htm

HA搭建web服务器高可用

拓扑:QJ02 主web==192.168.2.131QJ03 从web==192.168.2.132QJ01 NFS====192.168.2.1301.准备工作:(1).解析域名 #[[email protected] ~]# vim /etc/hosts 192.168.2.131 QJ02.cn 192.168.2.132 QJ03.cn [[email protected] ~]# vim /etc/hosts 192.168.2.131 QJ02.cn 192.168.2.132 Q

高可用集群技术之heartbeat+NFS实现web服务高可用(文本方式配置接口--gui图形配置)

一.高可用集群基本概念   什么是高可用技术呢?在生产环境中我既要保证服务不间断的服务又要保证服务器稳定不down机,但是异常还是会发生,比如说:服务器硬件损坏...导致服务器down机,我该如何保证服务器down机后继续提供服务呢?这时我就应该请出高可用技术来帮忙了,当我们的服务器发生故障后不能继续时,高可用集群技术解决将业务及服务自动转移至其他主机服务器上继续服务,保证服务架构不间断运行. 高可用集群的架构层次: 后端主机层: 这一层主要是正在运行在物理主机上的服务. 2.Message l

heartbeat + httpd + nfs 实现高可用web服务器

一  环境准备 二 拓扑结构 三 前提条件 四 安装相关软件 五 配置heartbeat 六 测试web集群 七 问题汇总 八 共享存储 一 环境准备 操作系统 centos 6.4 x86_64  最小化安装 如使用yum 安装的方式 centos5.5 安装的是V2.X   ,centos 6.4 安装的是V3.X YUM 安装   Vim  man   ntp  "development tools" "server platform development"

heartbeat httpd nfs 实现高可用web集群

一 环境准备 二 拓扑结构 三 前提条件 四 安装相关软件 五 配置heartbeat 六 测试web集群 七 问题汇总 八 共享存储 一 环境准备 操作系统 centos 6.4 x86_64 最小化安装 如使用yum 安装的方式 centos5.5 安装的是V2.X ,centos 6.4 安装的是V3.X YUM 安装 Vim man ntp "development tools" "server platform development" "des

Heartbeat+DRBD实现文件高可用

一.需求分析: 本文结合之前heartbeat与drbd两篇文章,实现磁盘文件高可用.当MySQL01(主数据库服务器)宕机后,MySQL02(备数据库服务器)可以通过heartbeat立刻检测到MySQL01挂了,MySQL02接管VIP,MySQL02的DRBD服务会自动从Secondary切换到Primary状态,然后自动加载DRBD逻辑盘:/dev/drbd0到/data目录,从而实现文件持续在线--即高可用状态:当MySQL01恢复后,VIP返回MySQL01,MySQL01的DRBD