Linux系统构架 - HA集群配置

2022-11-19,,,,

一、安装

HA 即(high available)高可用,又被叫做双机热备,用于关键性业务。

简单理解就是,有两台机器 A 和 B,正常是 A 提供服务,B 待命闲置,当 A 宕机或服务宕掉,会切换至 B 机器继续提供服务。

常用实现高可用的开源软件有 heartbeat 和 keepalived,其中keepalived 有负载均衡的功能。

1.准备两台机器,主的ip:192.168.1.116 从的ip:192.168.1.115

修改主的,从的名字:主叫master 从叫slave

[root@localhost ~]# hostname master
[root@localhost ~]# bash
[root@master ~]#
[root@localhost ~]# hostname slave
[root@localhost ~]# bash
[root@slave ~]#

2.关闭防火墙,两台同时操作

 vim /etc/selinux/config

找到
SELINUX=enforcing
改为
SELINUX=disabled
 setenforce 
iptables -F
service iptables save

3.编辑hosts

 vi /etc/hosts
添加
192.168.1.116 master
192.168.1.115 slave

4.安装heartbeat,两台机器都操作

查看是否有这个包,

yum list |grep heartbeat

安装扩展源。去www.lishiming.net搜索epel

wget www.lishiming.net/data/attachment/forum/epel-release--8_64.noarch.rpm
rpm -ivh epel-release--8_64.noarch.rpm
yum install -y heartbeat
yum install -y libnet

二、配置

主的

cd /usr/share/doc/heartbeat-3.0./
cp authkeys ha.cf haresources /etc/ha.d/
cd /etc/ha.d/
vim authkeys
两台机器通信,用来验证
#auth
# crc
# sha1 HI!
# md5 Hello!
改为
auth
# crc
# sha1 HI!
md5 Hello!
chmod  authkeys
vim haresources
默认全部注释
找到
#node1 10.0.0.170 Filesystem::/dev/sda1::/data1::ext2
改为
master 192.168.1.110//eth0: nginx
主的名字 流动ip 网段 网卡 服务
vim ha.cf
全部删掉,写入
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive
deadtime
warntime
initdead
udpport
ucast eth0 192.168.1.115
auto_failback on
node master
node slave
ping 192.168.1.1
respawn hacluster /usr/lib64/heartbeat/ipfail

debugfile /var/log/ha-debug //该文件保存 heartbeat 的调试信息
logfile /var/log/ha-log  //heartbeat 的日志文件
logfacility local0   //日志级别
keepalive 2   //多少时间去探测一次 2秒
deadtime 30  //如果30秒ping不通认为死掉了
warntime 10  //10秒ping不通发出警告
initdead 60  //预留60秒。可能对方在重启
udpport 694  //心跳线,通信端口
ucast eth0 192.168.1.115  //用单独网卡或者串口链接心跳线,对方ip
auto_failback on  //如果备启动了,主再次激活,备主动放弃。
node master
node slave  //指定主和从,各占一行,主在上从在下
ping 192.168.1.1 //仲裁的地址,判断双方是否是活的
respawn hacluster /usr/lib64/heartbeat/ipfail //以哪个用户身份去运行这个检查网络连接性脚本,对方不存活,把他拉起来的。

两台都安装scp命令才能拷贝

yum install openssh-clients -y
scp authkeys haresources ha.cf slave:/etc/ha.d/
yes/no:yes
root@slave's password:***

从的配置

cd /etc/ha.d/
vim ha.cf

ucast eth0 192.168.1.115
改为
ucast eth0 192.168.1.116

两台同时安装nginx

yum install nginx -y

 三、启动

/etc/init.d/heartbeat start
ifconfig
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:CE:99:EA
inet addr:192.168.1.110 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 流动ip
ps aux |grep ngin
nginx 启动成功

/etc/init.d/heartbeat start
ps aux|grep nginx
没有启动
ifconfig
没有eth0: 流动ip没有流动到从上

主:

echo '1111master' > /usr/share/nginx/html/index.html
可以用流动ip访问

从:

echo '2222222slave' > /usr/share/nginx/html/index.html

主:

/etc/init.d/heartbeat stop

ifconfig
ps
都没有了
刷新浏览器,
变成 222*slave

ifconfig
ps
启动

Linux系统构架 - HA集群配置的相关教程结束。

《Linux系统构架 - HA集群配置.doc》

下载本文的Word格式文档,以方便收藏与打印。