Linux高可用web集群之heartbeat
一、环境描述
系统版本:centos7.4 x64
nfs共享存储IP:192.168.1.200
node1(主节点)IP: 192.168.1.101 主机名:lb01
node2(从节点)IP: 192.168.1.102 主机名:lb02
虚拟IP地址(VIP):192.168.1.100
(node1) 仅为主节点配置
(node2) 仅为从节点配置
(node1,node2) 为主从节点共同配置
二、环境准备
1、更改主机名和hosts记录
#lb01配置
[root@lb01 ~]# cat /etc/hostname
lb01
[root@lb01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.102 lb02
192.168.1.101 lb01
#lb02配置
[root@lb02 ~]# cat /etc/hostname
lb02
[root@lb02 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.102 lb02
192.168.1.101 lb01
2、双机互信
#lb01
[root@lb01 ~]# ssh-keygen -t rsa -P ''
...
[root@lb01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.102
#测试
[root@lb01 ~]# ssh 192.168.1.102 'ifconfig'
#lb02
[root@lb02 ~]# ssh-keygen -t rsa -P ''
...
[root@lb02 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.101
#测试
[root@lb02 ~]# ssh 192.168.1.101 'ifconfig'
3、检查iptables,关闭防火墙和selinux
#lb01
[root@lb01 ~]# setenforce 0
[root@lb01 ~]# systemctl stop firewalld
[root@lb01 ~]# getenforce
Disabled
[root@lb01 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
#lb02
[root@lb02 ~]# setenforce 0
[root@lb02 ~]# systemctl stop firewalld
[root@lb02 ~]# getenforce
Disabled
[root@lb02 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
4、时间同步
#本地搭建了ntp时间服务,这里以本地的时间服务器为准
[root@lb01 ~]# vi /etc/ntp.conf
server 192.168.1.200
Fudge 192.1681.200 stratum 8
[root@lb01 ~]# ntpdate 192.168.1.200
[root@lb02 ~]# vi /etc/ntp.conf
server 192.168.1.200
Fudge 192.1681.200 stratum 8
[root@lb02 ~]# ntpdate 192.168.1.200
三。安装heartbeat
1、安装heartbeat
#环境准备
[root@lb01 ~]# yum install -y bzip2 autoconf automake libtool glib2-devel libxml2-devel bzip2-devel libtool-ltdl-devel asciidoc libuuid-devel psmisc
#准备安装包
[root@lb01 ~]# wget http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2
[root@lb01 ~]# wget https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz
[root@lb01 ~]# wget http://hg.linux-ha.org/dev/archive/STABLE-3.0.6.tar.bz2
#解压安装包
[root@lb01 ~]# tar -vxjf 0a7add1d9996.tar.bz2
[root@lb01 ~]# tar -zvxf v3.9.6.tar.gz
[root@lb01 ~]# tar -vxjf STABLE-3.0.6.tar.bz2
#添加用户,创建安装目录,更改所属组和用户
[root@lb01 ~]# groupadd haclient
[root@lb01 ~]# useradd -g haclient hacluster
[root@lb01 ~]# mkdir /usr/local/heartbeat
[root@lb01 ~]# chown hacluster /usr/local/heartbeat/
[root@lb01 ~]# chgrp haclient /usr/local/heartbeat/
#安装glue
[root@lb01 ~]# cd Reusable-Cluster-Components-glue--0a7add1d9996/
# ./autogen.sh
# ./configure --prefix=/usr/local/heartbeat/
# make
# make install
#安装Resource Agents
[root@lb01 ~]# cd resource-agents-3.9.6/
# ./autogen.sh
# export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
# vi /etc/ld.so.conf.d/heartbeat.conf
/usr/local/heartbeat/lib
# ./configure --prefix=/usr/local/heartbeat/
# ldconfig
# make
# make install
#安装heartbeat
[root@lb01 ~]# cd Heartbeat-3-0-STABLE-3.0.6/
# ./bootstrap
# export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
# ./configure --prefix=/usr/local/heartbeat/
# vi /usr/local/heartbeat/include/heartbeat/glue_config.h
/*define HA_HBCONF_DIR “/usr/local/heartbeat/etc/ha.d/”*/ (注意这行用/**/注释掉)
# make
# make install
#复制配置文件
# cp /usr/local/heartbeat/share/doc/heartbeat/ha.cf /usr/local/heartbeat/etc/ha.d
# cp /usr/local/heartbeat/share/doc/heartbeat/authkeys /usr/local/heartbeat/etc/ha.d
# cp /usr/local/heartbeat/share/doc/heartbeat/haresources /usr/local/heartbeat/etc/ha.d
***********************************************
#lb02一样操作
2、文件配置
#配置authkeys
[root@lb01 ~]# vi /usr/local/heartbeat/etc/ha.d/authkeys
#添加如下内容
auth 1
1 md5 ee7fd3a4c8ee5a79626350ee31a40202
#通过dd if=/dev/random count=1 bs=512 | md5sum命令来获取随机数
#复制到lb02
[root@lb01 ha.d]# scp authkeys root@192.168.1.102:/usr/local/heartbeat/etc/ha.d/
#配置ha.cf
[root@lb01 ha.d]# vi ha.cf
debugfile /var/log/ha-debug #表示调试的日志文件 一般测试建议开启
logfile /var/log/ha-log #表示系统的的日志文件路径
logfacility local0 #表示使用系统日志与上面只能开启一个
keepalive 2 #主备之间的心跳间隔时间单位:s
deadtime 30 #表示如果连接对方30s还无法连接,表示节点死亡需要考虑vip转移
warntime 10 #表示10s时间未收到心跳时发出警告日志
initdead 120 #有时机器启动后需要一段时间网卡才能正常工作 需要预留一定的时间后,再开始判断心跳检测
udpport 694 #多播的udp端口
#baud 19200 #串行端口的波特率
#serial /dev/ttyS0 # Linux #串口的接口名
#serial /dev/cuaa0 # FreeBSD
#serial /dev/cuad0 # FreeBSD 6.x
#serial /dev/cua/a # Solaris
#bcast eth0 # Linux #传播心跳的广播网卡信息
#bcast eth1 eth2 # Linux
#bcast le0 # Solaris
#bcast le1 le2 # Solaris
#mcast eth0 225.0.0.1 694 1 0 #多播传送心跳的网卡 多播组 端口 跃点数 是否回环内传送
ucast ens33 192.168.1.102 #设置单播心跳,设置对方的ip地址,此处使用单播
auto_failback on #表示如果主机停止后,从机接管设置为on当主机从新启动后,主机立即接管vip off从机不会释放vip给主机
node lb01 #配置主从的节点信息,要与uname -n保持一致
node lb02
ping 192.168.1.1
#ping组的所有主机
#ping_group group1 10.10.10.254 10.10.10.253
#respawn userid /path/name/to/run
#指定与heartbeat一同启动和关闭的进程,该进程被自动监视,遇到故障则重新启动。最常用的进程是ipfail,该进程用于检测和处理网络故障,需要配合ping语
句指定的ping node来检测网络连接。如果你的系统是64bit,请注意该文件的路径。
respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
####配置haresources
[root@lb01 ha.d]# vi haresources
lb01 IPaddr::192.168.1.100/24/ens33 httpd
##将配置文件复制到lb02备节点上
scp -rp /usr/local/heartbeat/etc/ha.d/* root@lb02:/usr/local/heartbeat/etc/ha.d/
需要将ha.cf 配置文件更改下
[root@lb02 ha.d]# vi ha.cf
ucast ens33 192.168.1.101 #设置单播心跳,设置对方的ip地址,此处使用单播
#建立软链接
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 -svf /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/
ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/RAExec/* /usr/local/heartbeat/lib/heartbeat/plugins/RAExec/
#权限设置
chmod 600 authkeys
#编写httpd启动脚本
[root@node1 ~]# vi /usr/local/heartbeat/etc/ha.d/resource.d/httpd
#!/bin/bash
/bin/systemctl $1 httpd
3、测试
cd /usr/local/heartbeat/etc/ha.d/resource.d
./IPaddr 192.168.1.100/24/ens33 start
ifconfig #可以查看添加的VIP
#关闭
./IPaddr 192.168.31.150/24/ens33 stop
四、添加nfs服务,为httpd提供共享目录
1、安装nfs服务
[root@lb01 ~]# yum install nfs-utils -y
[root@lb02 ~]# yum install nfs-utils -y
[root@nfs ~]# yum install nfs-utils -y
#创建共享目录测试文件,并给相应权限
[root@nfs ~]# mkdir /wwwdir
[root@nfs ~]# echo 'Heartbeat Web test!'> /wwwdir/index.html
[root@nfs ~]# chmod 777 -R /wwwdir/
[root@nfs ~]# vi /etc/exports
/wwwdir 192.168.1.0/24(ro)
[root@nfs ~]# systemctl stop firewalld
[root@nfs ~]# setenforce 0
#启动服务
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# systemctl restart nfs
2、修改主备节点配置文件
[root@lb01 ha.d]# vi haresources
lb01 IPaddr::192.168.1.100/24/ens33 Filesystem::192.168.1.200:/wwwdir::/var/www/html::nfs httpd
[root@lb01 ha.d]# scp -rp haresources root@lb02:/usr/local/heartbeat/etc/ha.d/