Keeplived脚本检测
主机环境
IP | 服务 | 描述 |
---|---|---|
192.168.197.154 | keepalived httpd |
keepalive主服务器 |
192.168.197.155 | keepalived httpd |
keepalive备服务器 |
步骤
- 编写主服务器检测脚本
[root@localhost scripts]# vim check_httpd.sh
#!/bin/bash
httpd_status=$(ps -ef|grep -Ev "grep|$0"|grep 'httpd'|wc -l)
if [ $httpd_status -lt 1 ];then
systemctl stop keepalived
fi
[root@localhost scripts]# chmod +x check_httpd.sh
- 测试脚本
[root@localhost scripts]# systemctl start keepalived
## 打开http 和 keepalive服务 ,此时vip在主服务器上
[root@localhost scripts]# ip a
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:4e:98:e3 brd ff:ff:ff:ff:ff:ff
inet 192.168.197.154/24 brd 192.168.197.255 scope global dynamic noprefixroute ens33
valid_lft 1676sec preferred_lft 1676sec
inet 192.168.197.250/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::c733:208:3487:d373/64 scope link noprefixroute
valid_lft forever preferred_lft forever
## 模拟关闭httpd服务,执行脚本,此时keepalived服务停止
[root@localhost scripts]# systemctl stop httpd
[root@localhost scripts]# ./check_httpd.sh
[root@localhost scripts]# ip a
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:4e:98:e3 brd ff:ff:ff:ff:ff:ff
inet 192.168.197.154/24 brd 192.168.197.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe4e:98e3/64 scope link
valid_lft forever preferred_lft forever
# keepalived自动停止
[root@localhost scripts]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: inactive (dead)
- 编写状态切换脚本
[root@localhost scripts]# vim notify.sh
#!/bin/bash
VIP=$2
case "$1" in
master)
httpd_status=$(ps -ef|grep -Ev "grep|$0"|grep 'httpd'|wc -l)
if [ $httpd_status -lt 1 ];then
systemctl start httpd
fi
sendmail
;;
backup)
httpd_status=$(ps -ef|grep -Ev "grep|$0"|grep 'httpd'|wc -l)
if [ $httpd_status -gt 0 ];then
systemctl stop httpd
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
- 配置主备keepalive配置文件
# 编辑master服务器配置文件
[root@localhost scripts]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script httpd_check {
script "/scripts/check_httpd.sh"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.197.250/24
}
track_script {
httpd_check
}
notify_master "/scripts/notify.sh master 192.168.197.250"
notify_backup "/scripts/notify.sh backup 192.168.197.250"
}
virtual_server 192.168.197.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.197.154 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.197.155 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
# 编辑backup服务器配置文件
[root@localhost scripts]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.197.250/24
}
notify_master "/scripts/notify.sh master 192.168.197.250"
notify_backup "/scripts/notify.sh backup 192.168.197.250"
}
virtual_server 192.168.197.250 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.197.154 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.197.155 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
- 脚本测试
# master上模拟停止http服务
[root@localhost scripts]# systemctl stop httpd
## 查看ip,发现vip已经不存在,且keepalived停止
[root@localhost scripts]# ip a
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:4e:98:e3 brd ff:ff:ff:ff:ff:ff
inet 192.168.197.154/24 brd 192.168.197.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe4e:98e3/64 scope link
valid_lft forever preferred_lft forever
[root@localhost scripts]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: inactive (dead)
# 查看backup主机上的ip,VIP已经出现,且keepalived服务启动
[root@localhost scripts]# ip a
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:06:50:c1 brd ff:ff:ff:ff:ff:ff
inet 192.168.197.155/24 brd 192.168.197.255 scope global dynamic noprefixroute ens33
valid_lft 1536sec preferred_lft 1536sec
inet 192.168.197.250/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::d665:370e:61d8:385c/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost scripts]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2020-12-04 19:18:03 CST; 12min ago
keepalived + zabbix监控
环境
IP | 服务 | 描述 |
---|---|---|
192.168.197.154 | keepalived httpd |
keepalive主服务器 |
192.168.197.155 | keepalived httpd zabbix_agent |
keepalive备服务器,zabbix客户端 |
192.168.197.138 | zabbix_agent zabbix_server |
zabbix服务器 |
步骤
- 搭建LAMP及zabbix_server
...略
- zabbix客户端配置
# 安装依赖包
[root@localhost zabbix-5.2.0]# yum -y install pcre-devel
#创建系统用户
[root@localhost zabbix-5.2.0]# useradd -M -r -s /sbin/nologin zabbix
# 编译zabbix,客户机只用编译agent
[root@localhost zabbix-5.2.0]# ./configure --enable-agent
......
LDAP support: no
IPv6 support: no
***********************************************************
* Now run 'make install' *
* *
* Thank you for using Zabbix! *
* <http://www.zabbix.com> *
***********************************************************
[root@localhost zabbix-5.2.0]# make install
......
2.配置agent配置文件
[root@localhost etc]# vim /usr/local/etc/zabbix_agentd.conf
......
# Server=
Server=192.168.197.138 # 配置zabbix_server IP地址
......
# ServerActive=
ServerActive=192.168.197.138 # 配置zabbix_server IP地址
......
# Hostname=
Hostname=zabbix_test # 配置Hostname,此名字对应web页新增host名字
......
# 开启服务
[root@localhost etc]# zabbix_agentd
[root@localhost etc]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
- zabbix添加主机
- zabbix_agent主机添加自定义监控脚本
[root@localhost scripts]# vim check_keepalived.sh
#!/bin/bash
if [ `ip a show ens33 |grep 192.168.197.250|wc -l` -ne 0 ]
then
echo "1"
else
echo "0"
fi
[root@localhost scripts]# chmod +x check_keepalived.sh
# 添加到配置文件中key
[root@localhost scripts]# vim /usr/local/etc/zabbix_agentd.conf
......
# TLSCipherAll=
UserParameter=check_keepalived,/bin/bash /scripts/check_keepalived
## 重启服务
[root@localhost zabbix]# pkill zabbix
[root@localhost zabbix]# zabbix_agentd
## 服务端验证脚本可行性
[root@localhost src]# zabbix_get -s 192.168.197.155 -k check_keepalived
0
- web页添加监控项及触发器
- 测试
## master服务器关掉httpd服务
[root@localhost scripts]# systemctl stop httpd
root@localhost scripts]# ip a
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:4e:98:e3 brd ff:ff:ff:ff:ff:ff
inet 192.168.197.154/24 brd 192.168.197.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe4e:98e3/64 scope link
valid_lft forever preferred_lft forever
- zabbix发生报警