阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 7下安装配置Heartbeat高可用集群

180次阅读
没有评论

共计 5939 个字符,预计需要花费 15 分钟才能阅读完成。

Heartbeat 项目是 Linux-HA 工程的一个组成部分,它实现了一个高可用集群系统。心跳服务和集群通信是高可用集群的两个关键组件,在 Heartbeat 项目里,由 Heartbeat 模块实现了这两个功能。

一、环境说明

#uname -r
3.10.0-862.9.1.el7.x86_64
# cat /etc/RedHat-release
CentOS Linux release 7.5.1804 (Core)

双节点互备集群:

节点 1:node1 node1.pjy.com 192.168.146.141

节点 2:node2 node2.pjy.com 192.168.146.150

VIP 地址:192.168.146.222

二、前提条件准备

在每个节点上做以下配置:

1. 关闭 firewalld

2. 关闭 selinux

3. 同步时间

4. 配置主机名

5. 配置主机间 ssh 互信,免密钥认证

以下配置是在节点 1(141)主机上的配置,节点 2(150)省略:

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i s#SELINUX=enforcing#SELINUX=disabled# /etc/selinux/config
ntpdate 10.0.0.100
hostname node1.pjy.com
echo “node1.pjy.com” >> /etc/hostname
ssh-keygen -q -t rsa -N ” -f ~/.ssh/id_rsa
ssh-copy-id root@192.168.146.150
echo -e “192.168.146.141 node1.pjy.com node1\n192.168.146.150 node2.pjy.com node2” >> /etc/hosts

三、开始安装

1. 安装基础环境包:

yum install gcc gcc-c++ autoconf automake libtool glib2-devel libxml2-devel bzip2 bzip2-devel e2fsprogs-devel libxslt-devel libtool-ltdl-devel asciidoc

2. 创建用户和组

groupadd haclient
useradd -g haclient hacluster

3. 下载软件包:Reusable-Components-glue、resource-agents、heartbeat

可以到 Linux 公社资源站下载:

—————————————— 分割线 ——————————————

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是 www.linuxidc.com

具体下载目录在 /2019 年资料 / 2 月 /26 日 /CentOS 7 下安装配置 Heartbeat 高可用集群 /

下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm

—————————————— 分割线 ——————————————

4. 安装 glue

tar xf 0a7add1d9996.tar.bz2
cd Reusable-Cluster-Components-glue–0a7add1d9996/
./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
echo $?
cd ..

5. 安装 Resource Agents

tar xf v3.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
echo $?
cd ..

6. 安装 HeartBeat

tar xf 958e11be8686.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
echo $?

7. 配置网卡支持插件文件

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/

# 注意:一般启动时会报错因为 ping 和 ucast 这些配置都需要插件支持 需要将 lib64 下面的插件软连接到 lib 目录 才不会抛出异常

ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/RAExec/* /usr/local/heartbeat/lib/heartbeat/plugins/RAExec/
ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/

# 以上在节点 1 上安装完成,在节点 2 上执行以上同样的步骤,此处省略 …

# 下面开始配置

四、配置 heartbeat

# 拷贝三个模版配置文件到 /usr/local/heartbeat/etc/ha.d 目录下

cp doc/{ha.cf,haresources,authkeys} /usr/local/heartbeat/etc/ha.d/

1. 配置 ha.cf 配置文件

# 该配置文件用于配置 心跳的核心配置

vim /usr/local/heartbeat/etc/ha.d/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.146.150 #设置单播心跳,设置对方的 ip 地址, 此处使用单播
auto_failback on  #表示如果主机停止后,从机接管设置为 on 当主机从新启动后,主机立即接管 vip off 从机不会释放 vip 给主机
node    node1.pjy.com  #配置主从的节点信息,要与 uname - n 保持一致
node    node2.pjy.com
#############################################
# 使用 ping 模式 有时当主机挂掉或者 heartbeat 挂掉后 vip 才会转移  有时出现某个进程挂掉 切换需要使用脚本
#ping 模式用于测试 如果网卡 ping 不同 某个主机 就认为当前断网 需要转移 vip
#respawn root    /usr/local/heartbeat/libexec/heartbeat/ipfail 表示当 ping 不通时 自动调用 ipfail 这个脚本
#apiauth ipfail gid=haclient uid=hacluster 表示有权限操作 ipfail 脚本的组和用户
############################################
ping 192.168.146.2
#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

2. 配置 authkeys 配置文件

# 该文件表示发送心跳时 机器用于验证的 key 的 hash 算法,节点之间必须配置成一致的密码

vim /usr/local/heartbeat/etc/ha.d/authkeys

auth 2  #表示使用 id 为 2 的验证 下边需要定义一个 2 的验证算法
2 sha1 1a2b3c  #ID 2 的验证加密为 shal, 并添加密码

# 更改权限为 600

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

3. 配置 haresources 配置文件

# 该文件表示资源的管理,如果是主机,当主机启动后自动加载该文件中配置的所有启动资源,资源脚本默认在 haresources 同级目录下的 resource.d 目录下

vim /usr/local/heartbeat/etc/ha.d/haresources
# 指定节点主机名,和 VIP 地址,以双冒号分隔资源,此处以 apache 为例进行配置
node1.pjy.com  192.168.146.222 apache::/etc/httpd/conf/httpd.conf

4. 节点 2 上准备配置文件

# 拷贝三个配置好的文件到节点 2 上,只需修改 ha.cf 配置文件中的单播地址为对方地址即可 (ucast ens33 192.168.146.141)。

scp authkeys ha.cf haresources root@node2:/usr/local/heartbeat/etc/ha.d/

5. 安装 httpd 资源服务

# 在每个节点上安装 httpd 服务并测试

yum install httpd
echo “<h1>node1.pjy.com</h1>” >>/var/www/html/index.html
systemctl start httpd
curl 192.168.146.141

# 测试 httpd 服务正常后关闭 httpd 服务并关闭自启动

systemctl stop httpd
systemctl disable httpd

6. 启动服务

# 启动每个节点上 heartbeat 服务

systemctl enable heartbeat
systemctl start heartbeat
ssh node2 ‘systemctl start heartbeat’

7. 测试结果

# curl http://192.168.146.222
<h1>node1.pjy.com</h1>
# 使用 heartbeat 自带脚本切换主备节点
# /usr/local/heartbeat/share/heartbeat/hb_standby
Going standby [all].
# curl http://192.168.146.222
<h1>node2.pjy.com</h1>

8. 配置 NFS 共享存储

# 在建立 NFS 的服务器上建立 NFS 存储:

yum install nfs-utils rpcbind
systemctl start rpcbind
systemctl start nfs
cat /etc/exports  #建立 NFS 共享存储
/data 192.168.146.0/24(ro)
mkdir /data  #建立测试文件
echo ‘<h1>nfs server</h1>’ > /data/index.html

# 然后修改 haresources 配置文件添加自动挂载 nfs 资源:

node1.pjy.com IPaddr::192.168.146.222/24/ens33 Filesystem::192.168.146.151:/data::/var/www/html::nfs::ro apache::/etc/httpd/conf/httpd.conf

# 重启 heartbeat 服务加载配置

systemctl restart heartbeat
ssh node2 ‘systemctl restart heartbeat’

# 测试是否加载资源:

ip addr
netstat -lntup
mount

# 资源转移测试:

/usr/local/heartbeat/share/heartbeat/hb_standby

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计5939字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中