Heartbeat搭建Web高可用集群

Heartbeat 概述

Heartbeat主要由两个部分组成:
心跳检测部分和资源接管部分。心跳检测部分主要是通过互相发送报文信息来进行检测对方状态的,如果长时间未收到对方发送来的报文,则认为对方失效,那么就会开启资源接管部分,接管对方存在的服务以及资源

Heartbeat环境搭建需要如下组件:

软件包名称下载地址
Heartbeat 3.0.6http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2
Cluster Glue 1.0.12http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2
Resource Agents 3.9.6https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz

测试环境

主机IP地址
node1192.168.1.10
node2192.168.1.20
node3192.168.1.30

node1和node2 都是web服务器,node3为后端NFS服务器

搭建过程如下

所有节点执行以下命令

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# vi /etc/selinux/config 
......
SELINUX=disabled
......
[root@localhost ~]# setenforce 0

[root@localhost ~]# vi /etc/hosts      
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.10    node1
192.168.1.20    node2
192.168.1.30    node3

以上执行完成以后,我们将每一个的节点的名称进行修改

node3配置NFS服务器

[root@node3 ~]# yum install -y nfs-utils rpcbind   //安装nfs所需的软件包
[root@node3 ~]# mkdir /share     //创建共享目录
[root@node3 ~]# echo "this is test" >>/share/index.html 
[root@node3 ~]# vi /etc/exports   //编辑文件添加共享目录的相关权限
/share *(rw)    //赋予任何网段的主机可读可写权限
[root@node3 ~]# exportfs -r    //执行这个生效
[root@node3 ~]# systemctl start nfs 
[root@node3 ~]# systemctl enable nfs 
[root@node3 ~]# systemctl start rpcbind 
[root@node3 ~]# systemctl enable rpcbind 
[root@node3 ~]# showmount -e    //查看共享文件
Export list for node3:
/share *

node1 和 node2配置(两个配置一致,这里用node1来展示)

[root@node1 ~]# yum install -y nfs-utils  httpd    //安装nfs软件
[root@node1 ~]# showmount -e 192.168.1.30   //查看nfs服务器共享文件的情况
Export list for 192.168.1.30:
/share *
[root@node1 ~]# mount -t nfs 192.168.1.30:/share /var/www/html/    //挂载测试
[root@node1 ~]# df -h   //查看挂载
.....
192.168.1.30:/share   17G  1.3G   16G    8% /var/www/html

卸载资源:后期这些资源通过heartbeat直接加载
[root@node1 ~]# umount /var/www/html/

这个时候node1 和 node2 都没有启动httpd服务器以及挂载,这一些操作,后面我们可以自动通过heartbeat来实现,不需要手动进行

node1 和 node2 安装Heartbeat(两个配置一致)
提前下载好我们需要的软件包并且上传到Linux服务器上

[root@node1 opt]# ls
0a7add1d9996.tar.bz2  958e11be8686.tar.bz2  resource-agents-3.9.6.tar.gz
[root@node1 opt]# 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    //解决依赖关系

编译安装Cluster Glue
[root@node1 opt]# groupadd haclient    //创建组
[root@node1 opt]# useradd -g haclient hacluster  //创建用户并加入到该组
[root@node1 opt]# tar -xf 0a7add1d9996.tar.bz2   //解压
[root@node1 opt]# cd Reusable-Cluster-Components-glue--0a7add1d9996/
[root@node1 Reusable-Cluster-Components-glue--0a7add1d9996]# ./autogen.sh 
[root@node1 Reusable-Cluster-Components-glue--0a7add1d9996]# ./configure --prefix=/usr/local/heartbeat/
[root@node1 Reusable-Cluster-Components-glue--0a7add1d9996]# make && make install 

编译安装Resource Agents
[root@node1 opt]# tar -xf resource-agents-3.9.6.tar.gz 
[root@node1 opt]# cd resource-agents-3.9.6
[root@node1 resource-agents-3.9.6]# ./autogen.sh 
[root@node1 resource-agents-3.9.6]# ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
[root@node1 resource-agents-3.9.6]# make && make install 

编译安装Heartbeat
[root@node1 opt]# tar -xf 958e11be8686.tar.bz2 
[root@node1 opt]# cd Heartbeat-3-0-958e11be8686/
[root@node1 Heartbeat-3-0-958e11be8686]# ./bootstrap 
[root@node1 Heartbeat-3-0-958e11be8686]# export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
[root@node1 Heartbeat-3-0-958e11be8686]# ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
[root@node1 Heartbeat-3-0-958e11be8686]# make && make install

注node2配置到这里就可以了

node1生成相关文件

生成配置文件
heartbeat主要由三个文件组成
ha.cf:  主配置文件
haresource:  用来配置要让Heartbeat托管的服务
authkey:  是用来指定Heartbeat的认证方式

[root@node1 opt]# cd Heartbeat-3-0-958e11be8686/
[root@node1 Heartbeat-3-0-958e11be8686]# cp doc/{ha.cf,haresources,authkeys} /usr/local/heartbeat/etc/ha.d/
[root@node1 ~]# chkconfig --add heartbeat 
[root@node1 ~]# chkconfig heartbeat on   //设置为开机自动启动
[root@node1 ~]# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys   //权限必须为600否则启动不起
[root@node1 ~]# mkdir -pv /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/
[root@node1 ~]# cp /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/
[root@node1 ~]# ln -sv /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/     //不设置可能会报ping ucast 的错误

node1的配置(主要修改三大配置文件)

编辑 ha.cf 文件
[root@node1 ~]# vi /usr/local/heartbeat/etc/ha.d/ha.cf 
确保以下的代码存在且无注释
24 debugfile /var/log/ha-debug     //调式信息的存放地
29 logfile /var/log/ha-log      //日志文件存放地
34 logfacility     local0      
48 keepalive 2            //心跳检测的时间为2秒,每2秒发送报文信息
56 deadtime 30            //备用节点在30秒为收到主节点信息,则启动资源接管部分
61 warntime 10           //心跳延迟时间
71 initdead 60          //系统启动或重启后预留的忽略时间段,取值至少为deadtime的两倍
76 udpport 694          //侦听的端口号
121 ucast ens33 192.168.1.20    //采用ens33网卡单播与对应的IP地址进行通信
157 auto_failback on       //定义当主节点恢复后,是否将服务自动切回
211 node    node1        //节点的名称   需和使用unmae -n 的一致
212 node    node2        //节点的名称
220 ping 192.168.1.1       //主要监测网络的状态
253 respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail
259 apiauth ipfail gid=haclient uid=hacluster

编辑haresources资源文件
[root@node1 ~]# vi /usr/local/heartbeat/etc/ha.d/haresources 
node1 IPaddr::192.168.1.150/24/ens33 Filesystem::192.168.1.30:/share::/var/www/html::nfs httpd

node1:代表主机名称,node2的这个文件和这里一致
IPaddr::192.168.1.150/24/ens33 :绑定VIP地址到指定的网卡上
Filesystem::192.168.1.30:/share::/var/www/html::nfs :指定挂载的路径
httpd :表示启动httpd这个脚本文件(这个文件主要用于启动httpd服务器的)
httpd这个文件一定要存在于这个目录下才可:
/usr/local/heartbeat/etc/ha.d/resource.d

编辑authkeys心跳验证文件
[root@node1 ~]# vi /usr/local/heartbeat/etc/ha.d/authkeys 
auth 1
1 md5 hello!
注:auth 后面的序号可以任意写,不过换行了之后,最开始写的序号需和auth后面的一致,然后就是采用的算法了
算法的分类:crc md5 sha1
算法后面跟的就是密码了

编写httpd启动脚本
[root@node1 ~]# vi /usr/local/heartbeat/etc/ha.d/resource.d/httpd 
#!/bin/bash
/bin/systemctl $1 httpd
[root@node1 ~]# chmod 755 !$     //赋予执行权限
chmod 755 /usr/local/heartbeat/etc/ha.d/resource.d/httpd

以上配置完成以后,我们将配置的文件都发送node2,然后在再node2上进行修改即可
[root@node1 ~]# scp -rp /usr/local/heartbeat/etc/ha.d/* root@node2:/usr/local/heartbeat/etc/ha.d/

node2的配置

[root@node2 ~]# ln -sv /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/
[root@node2 ~]# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys 
[root@node2 ~]# vi /usr/local/heartbeat/etc/ha.d/ha.cf 
121 ucast ens33 192.168.1.10     //修改为node1 的IP地址

node1 和node2 启动heartbeat服务
在启动之前先下载这个两个软件包:yum install -y net-tools psmisc

node1 启动
[root@node1 ~]# /etc/init.d/heartbeat start 
Starting heartbeat (via systemctl):                        [  OK  ]
[root@node1 ~]# netstat -anlupt | grep 694
udp        0      0 0.0.0.0:694             0.0.0.0:*                           108303/heartbeat: w 

node2启动
[root@node2 ~]# /etc/init.d/heartbeat start 
Starting heartbeat (via systemctl):                        [  OK  ]
[root@node2 ~]# netstat -anlupt |grep 694
udp        0      0 0.0.0.0:694             0.0.0.0:*                           96290/heartbeat: wr 

默认状态下,node1 节点会自动生成VIP地址以及自动进行挂载

[root@node1 ~]# ifconfig 
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.150  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:d8:b0:c3  txqueuelen 1000  (Ethernet)
[root@node1 ~]# df -h
192.168.1.30:/share   17G  1.3G   16G    8% /var/www/html
[root@node1 ~]# ss -tan |grep 80
LISTEN     0      128       [::]:80                    [::]:*  

node2 查看什么也没有
[root@node2 ~]# ifconfig 
[root@node2 ~]# df -h
[root@node2 ~]# ss -tan | grep 80

浏览器访问测试
默认请求会交给node1去处理
在这里插入图片描述
故障检验
关掉node1节点的网卡,等待30秒去node2节点查看,然后浏览器访问看能否访问成功

[root@node1 ~]# ifdown ens33   //关掉网卡
[root@node2 ~]# ifconfig 
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.150  netmask 255.255.255.0  broadcast 192.168.1.255
        ether 00:0c:29:bd:6e:43  txqueuelen 1000  (Ethernet)
[root@node2 ~]# df -h
......
192.168.1.30:/share   17G  1.3G   16G    8% /var/www/html
[root@node2 ~]# ss -tan | grep 80
LISTEN     0      128       [::]:80                    [::]:*  

浏览器访问网页测试
在这里插入图片描述
现在我们把node1的网卡启动,看看主节点的资源会不会归为

[root@node2 ~]# ifup ens33   //开启网卡,稍微等待30秒左右
[root@node1 ~]# ip a   //查看VIP地址是否生成
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d8:b0:c3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.1.150/24 brd 192.168.1.255 scope global secondary ens33:2
[root@node1 ~]# df -h   //查看挂载情况
......
192.168.1.30:/share   17G  1.3G   16G    8% /var/www/html
[root@node1 ~]# netstat -anlupt | grep httpd    //查看httpd服务器是否启动
tcp6       0      0 :::80                   :::*                    LISTEN      1525/httpd   

使用curl来访问测试(在NFS服务器上进行)
[root@node3 ~]# curl 192.168.1.150
this is test
[root@node3 ~]# curl 192.168.1.150
this is test

整个实验搭建完成,高可用性达到

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ball-4444

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值