zabbix监控keepalived主备状态

zabbix监控keepalived主备状态


环境说明

系统信息主机名IPVIP服务
RedHat8zabbix192.168.226.132lamp架构
zabbix_server
zabbix agent
RedHat8h1192.168.226.131192.168.226.150haproxy
master keepalived
RedHat8h2192.168.226.134192.168.226.150haproxy
slave keepalived
zabbix agent
RedHat8rs1192.168.226.128httpd
RedHat8rs2192.168.226.138httpd

HAProxy部署httpd负载均衡:https://blog.csdn.net/qq_59363371/article/details/133758281?spm=1001.2014.3001.5502

监控服务zabbix部署:https://blog.csdn.net/qq_59363371/article/details/133251980?spm=1001.2014.3001.5502

1. 配置主keepalived

keepalived监控主备状态的脚本,keepalived通过脚本监控负载均衡机的状态

1.1 在master主机上编写脚本

该脚本是为了得知master主机上是否存在haproxy服务进程,如果没有则说明服务出现了问题,无法正常提供服务,所以我们写入判断,当haproxy进程小于1时则关闭keepalived服务,自动释放内存

[root@h1 ~]# mkdir /scripts && cd /scripts
[root@h1 scripts]# vim check_n.sh
[root@h1 scripts]# cat check_n.sh
#!/bin/bash

haproxy_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhaproxy\b'|wc -l)
if [ $haproxy_status -lt 1 ];then
    systemctl stop keepalived
fi

[root@h1 scripts]# chmod +x check_n.sh
[root@h1 scripts]# ll
total 4
-rwxr-xr-x 1 root root 148 Oct 13 21:21 check_n.sh

1.2 配置主keepalived配置文件

[root@h1 ~]# vim /etc/keepalived/keepalived.conf  
[root@h1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id haproxy1
}

vrrp_script haproxy_check {
    script "/scripts/check_n.sh"
    interval 1
    fall 3
    weight -40
}

vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 80
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 610000
    }
    virtual_ipaddress {
        192.168.226.150
    }
    track_script {
        haproxy_check
    }
}

virtual_server 192.168.226.150 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.195.131 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
          }
        }
    real_server 192.168.195.134 80 {
        weight 1
        TCP_CHECK {
            connect_port 80 
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
          }
        }
}

[root@h1 ~]# systemctl restart keepalived.service

2. 配置从keepalived

2.1 在slave主机上编写脚本

该脚本是为了得知本主机是处于哪种状态(mastert|slave),当本主机变成master主机后,则进行第一个判断,当haproxy服务进程数小于1时,开启haproxy服务,继续进行负载均衡;而当本主机变回slave主机后,则进行第二个判断,当haproxy服务进程大于0时,关闭haproxy服务,避免与master主机上的haproxy服务产生冲突,从而导致流量无法正确转移到后端的web页面主机

[root@h2 ~]# mkdir /scripts && cd /scripts
[root@h2 scripts]# vim notify.sh
[root@h2 scripts]# cat notify.sh 
#!/bin/bash

case "$1" in
  master)
        haproxy_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhaproxy\b'|wc -l)
        if [ $haproxy_status -lt 1 ];then
            systemctl start haproxy
        fi
  ;;
  backup)
        haproxy_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bhaproxy\b'|wc -l)
        if [ $haproxy_status -gt 0 ];then
            systemctl stop haproxy
        fi
  ;;
  *)
        echo "Usage:$0 master|backup VIP"
  ;;
esac
[root@h2 scripts]# chmod +x notify.sh
[root@h2 scripts]# ll
total 4
-rwxr-xr-x 1 root root 461 Oct 13 21:25 notify.sh

2.2 配置备keepalived配置文件

[root@h2 ~]# vim /etc/keepalived/keepalived.conf 
[root@h2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id haproxy2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 80
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 610000
    }
    virtual_ipaddress {
        192.168.226.150
    }
    notify_master "/scripts/notify.sh master"
    notify_backup "/scripts/notify.sh backup"
}

virtual_server 192.168.195.150 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.226.131 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
          }
        }
    real_server 192.168.195.134 80 {
        weight 1
        TCP_CHECK {
            connect_port 80 
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
          }
        }
}

[root@h2 ~]# systemctl restart keepalived.service

2.3测试

测试前查看服务状态

master主机
//keepalived服务和haproxy服务正常运行,查看vip
[root@h1 ~]# systemctl is-active haproxy
active
[root@h1 ~]# systemctl is-active keepalived
active
[root@h1 ~]# ip a show ens160 | grep 192.168.226.150
    inet 192.168.226.150/32 scope global ens160


slave主机
//haproxy服务关闭,keepalved服务保持开启
[root@h2 ~]# systemctl is-active haproxy
inactive
[root@h2 ~]# systemctl is-active keepalived
active
[root@h2 ~]# ip a show ens160 | grep 192.168.226.150

模拟主keepalived主机故障haproxy服务关闭,查看VIP,备keepalived已经接管VIP,客户机通过VIP访问正常

[root@h1 ~]# systemctl stop haproxy.service
[root@h1 ~]# systemctl is-active haproxy.service
inactive
[root@h1 ~]# systemctl is-active keepalived.service
inactive
[root@h1 ~]# ip a show ens160 | grep 192.168.226.150


[root@h2 ~]# systemctl is-active keepalived
active
[root@h2 ~]# systemctl is-active haproxy.service
active
[root@h2 ~]# ip a show ens160 | grep 192.168.226.150
    inet 192.168.226.150/32 scope global ens160

3. 对keepalived进行监控

对keepalived服务的监控应在备用服务器上进行,通过添加zabbix自定义监控进行

3.1配置监控端配置文件,配置监控脚本

监控脚本如下:

[root@h2 ~]# mkdir -p /zabbix/scripts
[root@h2 ~]# cd /zabbix/scripts
[root@h2 scripts]# vim check_keepalived.sh
[root@h2 scripts]# cat check_keepalived.sh
#!/bin/bash

if [ `ip a show ens160 | grep 192.168.226.150 | wc -l` -ne 0 ]
then
    echo "1"
else
    echo "0"
fi
[root@h2 scripts]# chmod +x check_keepalived.sh
[root@h2 ~]# chown -R zabbix:zabbix /zabbix
[root@h2 scripts]# ll
total 8
-rwxr-xr-x 1 root root 115 Oct 16 15:33 check_keepalived.sh
-rwxr-xr-x 1 root root 444 Oct 15 20:31 notify.sh
网卡要改成你自己的网卡名称,VIP也要改成你自己的VIP,脚本赋予执行权限,且要修改/zabbix/scripts目录的属主属组为zabbix

进入配置文件,自定义监控打开,创建自定义监控任务

[root@h2 scripts]# vim /usr/local/etc/zabbix_agentd.conf
.....
UnsafeUserParameters=1
UserParameter=check_keepalived,/bin/bash /zabbix/scripts/check_keepalived.sh
......
[root@h2 scripts]# systemctl restart zabbix_agentd.service

在服务端验证新建的监控项

[root@zabbix ~]# zabbix_get -s 192.168.226.134 -k check_keepalived
0  //显示0说明该主机上没有vip
//该脚本得到的是主机上是否存在vip,如果slave主机(h2)上存在vip,则说明master主机(h1)上的haproxy服务出现问题,返回值报1,说明服务出现问题。

3.2 创建监控主机

在这里插入图片描述

3.3 创建监控项

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3.4 添加触发器

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3.7 测试

模拟master主机(h1)的haproxy服务异常关闭

测试前服务状态:

[root@h1 scripts]# systemctl is-active haproxy
active
[root@h1 scripts]# systemctl is-active keepalived
active
[root@h1 scripts]# ip a show ens160 | grep 192.168.226.150
    inet 192.168.226.150/32 scope global ens160

[root@h2 scripts]# systemctl is-active haproxy
inactive
[root@h2 scripts]# systemctl is-active keepalived
active
[root@h2 scripts]# ip a show ens160 | grep 192.168.226.150

测试master主机(h1)的haproxy服务异常关闭:

[root@h1 ~]# systemctl stop haproxy.service
[root@h1 ~]# systemctl is-active haproxy.service
inactive
[root@h1 ~]# systemctl is-active keepalived.service
inactive
[root@h1 ~]# ip a show ens160 | grep 192.168.226.150


[root@h2 ~]# systemctl is-active keepalived
active
[root@h2 ~]# systemctl is-active haproxy.service
active
[root@h2 ~]# ip a show ens160 | grep 192.168.226.150
    inet 192.168.226.150/32 scope global ens160

在这里插入图片描述

监控成功触发告警

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值