突然有一天早上,发现公司服务挂了,查看后zabbix与服务一起挂
了,这对于监控来说是绝对不可以容忍得。因此,出了以下得zabbix
高可用方案。
1. 使用keepalived对zabbix上的组件进行监控
2. 使用inotify+unison进行文件同步
3. 使用MHA提供数据库服务
在这里不细说zabbix部署
请参考https://blog.csdn.net/weixin_51477878/article/details/119410649
1, 首先下载一个keepalived的包(master和master同步)
tar -zxvf keepalived-1.2.18.tar.gz
cd keepalived-1.2.18
./configure --prefix=/usr/local/keepalived
make && make install
将keepalived安装成linux默认服务
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
2.修改配置文件(master和slave同步修改):
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email { # 邮件
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1 #邮件协议等配置,可以不用配置
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_zabbix { # 要执行的
script "/etc/keepalived/zabbix_check.sh" # 脚本位置
interval 2 # 监测间隔
weight -20 # 成立降低多少权重
}
vrrp_instance VI_1 {
state BACKUP
interface ens33 # 网口
virtual_router_id 33
mcast_src_ip 192.168.43.148 # 本机ip
priority 100 #权重
nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
advert_int 1 # 两个节点之间的通信间隔
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_zabbix # 执行
}
virtual_ipaddress {
192.168.43.150 # vip(可以多个)
}
}
3.编写监控脚本
vim/etc/keepalived/zabbix_check.sh
#!/bin/bash
#php监测
ss -ntal | grep 9000 > /dev/null
if [ $? -ne 0 ];then
pkill keepalived
fi
#zabbix监测
ss -ntal | grep 10051 > /dev/null
if [ $? -ne 0 ];then
pkill keepalived
fi
#nginx监测
ss -ntal | grep 1995 > /dev/null
if [ $? -ne 0 ];then
pkill keepalived
fi
4.给权限
chmod +x /etc/keepalived/zabbix_check.sh
5.启动keepalived
service keepalived start
1.配置ssh免密登录(两台机器同时)
ssh-keygen -t rsa ---> 一阵回车
ssh-copy-id 目标ip
ssh root@目标ip # 测试
2,安装三个源码包(两台机器同时)
包请访问主页
tar xf ocaml-3.10.2.tar.gz
cd ocaml-3.10.2
./configure
make world opt
make install
tar xvf unison-2.13.16.tar.gz
cd unison-2.13.16
make UISTYLE=text THREADS=true STATIC=true
cp unison /usr/local/bin
注意:报错请自行百度,缺什么yum什么即可
tar xvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
3.测试(两台机器同时)
unison -batch /opt/test ssh://192.168.105.43/opt/test
4.创建同步脚本(两台机器同时)
#/bin/bash
ip1="192.168.105.43" # 对方ip
src1="/usr/share/nginx/html"
dst1="/usr/share/nginx/html"
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src1 | while read line; do
/usr/local/bin/unison -batch $src1 ssh://$ip1/$dst1
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
同步其他目录(机器)自行修改,与此类似
5.让脚本后台运行(两台机器同步)
nohup /opt/web_master.sh &
ps -ef | grep web_master
可以看到进程时代表成功
这里是使用dba提供的MHA服务,不做过多阐述,请自行查看Mysql官方文档
至此zabbix高可用基本配置完成,将我们所有的组件全部启动即可.
在web上添加一台主机,然后挂掉master的nginx,通过查看nginx日志发现,我们再另一台机器上的web中也发现了这台主机
web/数据库完美同步
为了让zabbix能够以keepalive的vip进行外部访问,需将配置文件(zabbix_server.conf)中的SourceIP=
写入vip