宕机检测、重启、告警开发工具

一、直接给代码

1. keepalived.conf

安装详见 keepalived安装教程
配置前请备份好原文件,新建文件夹/etc/keepalived并将副本keepalived.conf复制到/etc/keepalived即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
!Configuration File for keepalived
global_defs {
router_id master
notification_email {
******@163.com
}
notification_email_from ******@126.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
}

vrrp_script chk_http_port {
# 脚本检测工具,见 3.nginx_check.sh
script "/usr/local/src/nginx_check.sh"
interval 2 #(检测脚本执行的间隔)
weight -20
}

vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP
## 具体网卡请 键入命令 ip a 查看有哪些来进行配置
interface eth0 //网卡
virtual_router_id 1 # 主、备机的 virtual_router_id 必须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1

authentication {
auth_type PASS
auth_pass root123
}

track_script{ # 这里调用检测脚本执行,一秒检测一次
chk_http_port
}

# 根据 内网子网 设置虚拟地址,用于转发,keepalived集群不在此章节
virtual_ipaddress {
192.168.0.100/24 dev eth0 label eth0:1
}

#配置keepalived发生故障转移时,触发执行的脚本
notify_master "/etc/keepalived/notify.sh master "
notify_backup "/etc/keepalived/notify.sh backup "
notify_fault "/etc/keepalived/notify.sh fault "

}

2. notify.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
#接收者邮箱,多个以空格分隔
contact=(******@163.com ******@qq.com)
#本机ip
HOST_IP=192.168.0.100/24
notify() {
#邮件主题
mailsubject="vip转移,$HOST_IP keepalived to be $1"
#邮件正文
mailbody="$(date +'%F %T'): 节点切换, $HOST_IP keepalived changed to be $1"
for receiver in ${contact[*]}
do
#发送邮件
echo "$mailbody" | mail -s "$mailsubject" $receiver
done
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage:{master|backup|fault}"
exit 1
;;
esac

3.nginx_check.sh

nginx为例,脚本用于宕机检测和重启

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env bash
echo ' a'
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
# 开启nginx服务
/usr/local/nginx/sbin/nginx
sleep 1
# nginx没有启动成功
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
# 杀死keepalived进程
killall keepalived
fi
fi

4.mail.rc

告警邮件发送配置,需要安装相关依赖

1
yum -y install mailx

配置/etc/mail.rc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# This is the configuration file for Heirloom mailx (formerly
# known under the name "nail".
# See mailx(1) for further options.
# This file is not overwritten when 'make install' is run in
# the mailx build process again.

# Sccsid @(#)nail.rc 2.11 (gritter) 8/2/08

# Do not forward to mbox by default since this is likely to be
# irritating for most users today.


# 末尾追加6行配置即可

#发送邮件的邮箱
set from=******@126.com
#smtp邮件服务器
set smtp=smtp.126.com
#发送邮件的邮箱用户
set smtp-auth-user=******@126.com
#邮箱授权码
set smtp-auth-password=******
set smtp-auth=login
set ssl-verify=ignore

二、效果展示

keepalived 服务

  • 启动
    1
    systemctl start keepalived
  • 查看状态
    1
    systemctl status keepalived
  • 关闭
    1
    systemctl stop keepalived
  • 重启
    1
    systemctl restart keepalived
    手动关闭nginx服务,keepalived自动检测并重启

    注意

检测周期必须大于notify.sh脚本睡眠时间。

告警邮件

在这里插入图片描述

三、结束语

评论区可留言,可私信,可互相交流学习,共同进步,欢迎各位给出意见或评价,本人致力于做到优质文章,希望能有幸拜读各位的建议!

专注品质,热爱生活。
交流技术,寻求同志。