zabbix监控keepalived脑裂

zabbix监控keepalived脑裂


文章目录

  • zabbix监控keepalived脑裂
    • 通过zabbix监控可能发生keepalived脑裂,将告警信息发送到你的邮箱中,验证收到该邮件

通过zabbix监控可能发生keepalived脑裂,将告警信息发送到你的邮箱中,验证收到该邮件


主节点 zabbix-server zabbix-server zabbix-agent lamp(80) keepalived nginx(8080) 192.168.89.151
备份节点 zabbix-agent zabbix-agent keepalived nginx(8080) 192.168.89.150
VIP: 192.168.100.100/32

//关闭防火墙和selinux
[root@server ~]# systemctl stop firewalld
[root@server ~]# systemctl disable firewalld
[root@server ~]# vim /etc/selinux/config
SELINUX=disabled
[root@server ~]# setenforce 0


[root@agent1 ~]# systemctl stop firewalld
[root@agent1 ~]# systemctl disable firewalld
[root@agent1 ~]# vim /etc/selinux/config
SELINUX=disabled
[root@agent1 ~]# setenforce 0


//配置yum源
//安装常用命令
yum -y install vim wget gcc gcc-c++


//安装keepalived
[root@server ~]# yum -y install keepalived

[root@agent1 ~]#  yum -y install keepalived


//在server节点安装nginx
[root@server ~]# yum -y install nginx
[root@server ~]# vim /etc/nginx/nginx.conf    //修改nginx默认端口号为8080
    server {
        listen       8080 default_server;
        listen       [::]:8080 default_server;
[root@server ~]# systemctl restart nginx
[root@server ~]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@server ~]#


//在nginx中添加测试网页
[root@server ~]#  cd /usr/share/nginx/html/
[root@server html]# echo "master node" > index.html


//在agent1节点安装nginx
[root@agent1 ~]# yum -y install nginx
[root@agent1 ~]# systemctl restart nginx
[root@agent1 ~]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@agent1 ~]#

//在nginx中添加测试网页
[root@agent1 ~]# cd /usr/share/nginx/html/
[root@agent1 html]# echo "slave node" > index.html

测试验证
zabbix监控keepalived脑裂_第1张图片
zabbix监控keepalived脑裂_第2张图片

//配置主keepalived
[root@server ~]# cd /etc/keepalived/
[root@server keepalived]# cp keepalived.conf keepalived.conf.bak
[root@server keepalived]# vim keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lc001
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.89.100
    }
}

virtual_server 192.168.89.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.89.151 8080 {
        weight 1
        TCP_CHECK {
                connect_port 80
            connect_timeout 3
                nb_get_retry 3
            delay_before_retry 3
        }
    }


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

//重启keepalived,并设置下次启动生效
[root@server keepalived]# systemctl restart keepalived.service
[root@server keepalived]# systemctl enable keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@server keepalived]#



//在agent1节点配置备份keepalived
[root@agent1 ~]# cd /etc/keepalived/
[root@agent1 keepalived]# cp keepalived.conf keepalived.conf.bak
[root@agent1 keepalived]# vim keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lc002
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.89.100
    }
}

virtual_server 192.168.89.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.89.151 8080 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }


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



//重启服务并设置下次启动生效
[root@agent1 keepalived]# systemctl restart keepalived.service
[root@agent1 keepalived]# systemctl enable keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /usr/lib/systemd/system/keepalived.service.
[root@agent1 keepalived]#


//在master中配置VIP
[root@server ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR1=192.168.89.100
PREFIX1=32
[root@server ~]# systemctl restart NetworkManager
[root@server ~]# nmcli connection up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)



//修改内核参数,开启侦听VIP功能
[root@server ~]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
[root@server ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1


[root@agent1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
[root@agent1 ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1


//让keepalived监控nginx负载均衡,在server上编写脚本
[root@server ~]# mkdir /scripts
[root@server ~]# cd /scripts/
[root@server scripts]# vim check.sh
#!/bin/bash
nginx_status=`ps -ef | grep -v "grep" | grep "nginx" | wc -l`
if [ $nginx_status -lt 1 ];then
        systemctl stop keepalived
fi
[root@server scripts]# chmod +x check.sh


[root@server scripts]# vim notify.sh
#!/bin/bash
VIP=$2
sendmail () {
        subject="${VIP}'s server keepalived state is translate"
        content="`date +'%F %T'`: `hostname`'s state change to master"
        echo $content | mail -s "$subject" [email protected]
}

case "$1" in
        master)
                nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
                if [ $nginx_status -lt 1 ];then
                        systemctl start nginx
                fi
                sendmail
                ;;
        backup)
                nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
                if [ $nginx_status -gt 0 ];then
                        systemctl stop nginx
                fi
                ;;
        *)
                echo "Usage:$0 master|backup VIP"
                ;;
esac
[root@server scripts]# chmod +x notify.sh



//在agent1上编写脚本
[root@agent1 ~]# mkdir /scripts
[root@agent1 ~]# cd /scripts/
[root@agent1 scripts]#  scp [email protected]:/scripts/check.sh .
[email protected]'s password:
check.sh                                                                                                                             100%  144   174.6KB/s   00:00
[root@agent1 scripts]#

[root@agent1 scripts]# scp [email protected]:/scripts/notify.sh .
[email protected]'s password:
notify.sh                                                                                                                            100%  574   641.3KB/s   00:00
[root@agent1 scripts]#
[root@agent1 scripts]# chmod +x check.sh
[root@agent1 scripts]# chmod +x notify.sh



//配置server的keepalived
[root@server scripts]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lc001
}

vrrp_script nginx_check {
    script "/scripts/check.sh"
    interval 10
    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 1111
    }
    virtual_ipaddress {
        192.168.89.100
    }
    track_script {
    nginx_check
    }
    notify_master "/scripts/notify.sh master 192.168.89.100"
    notify_backup "/scripts/notify.sh backup 192.168.89.100"
}

virtual_server 192.168.89.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.89.151 8080 {
        weight 1
        TCP_CHECK {
                connect_port 80
            connect_timeout 3
                nb_get_retry 3
            delay_before_retry 3
        }
    }


    real_server 192.168.89.150 80 {
        weight 1
        TCP_CHECK {
                connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@server scripts]# systemctl restart keepalived.service



//配置agent1的keepalived
[root@agent1 scripts]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lc002
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.89.100
    }
    notify_master "/scripts/notify.sh master 192.168.89.100"
    notify_backup "/scripts/notify.sh backup 192.168.89.100"
}

virtual_server 192.168.89.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.89.151 8080 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }


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


//重启服务
[root@agent1 scripts]# systemctl restart keepalived.service



//模拟故障
//启用keepalived,开启nginx服务
[root@server scripts]# systemctl restart keepalived.service
[root@server scripts]# systemctl enable keepalived
[root@server scripts]# systemctl restart nginx
[root@server scripts]# systemctl enable nginx

//在agent1中查看端口80是否开启
[root@agent1 scripts]# ss -antl
State              Recv-Q             Send-Q                           Local Address:Port                            Peer Address:Port             Process
LISTEN             0                  128                                    0.0.0.0:10050                                0.0.0.0:*
LISTEN             0                  128                                  127.0.0.1:9000                                 0.0.0.0:*
LISTEN             0                  128                                    0.0.0.0:22                                   0.0.0.0:*
LISTEN             0                  128                                       [::]:10050                                   [::]:*
LISTEN             0                  80                                           *:3306                                       *:*
LISTEN             0                  128                                       [::]:22                                      [::]:*

zabbix监控keepalived脑裂_第3张图片

//关闭server的nginx服务
[root@server scripts]# systemctl stop nginx

//在agent1中查看端口8080是否开启
[root@agent1 ~]# ss -antl
State              Recv-Q             Send-Q                           Local Address:Port                            Peer Address:Port             Process
LISTEN             0                  128                                  127.0.0.1:9000                                 0.0.0.0:*
LISTEN             0                  128                                    0.0.0.0:80                                 0.0.0.0:*
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                  80                                           *:3306                                       *:*
LISTEN             0                  128                                       [::]:80                                     [::]:*
LISTEN             0                  128                                       [::]:22                                      [::]:*
LISTEN             0                  128                                       [::]:10050                                   [::]:*

zabbix监控keepalived脑裂_第4张图片

Zabbix和agent1已经配置好了zabbix监控
接下来就给zabbix和agent1配置好keepalived高可用,但是注意的是nginx使用的端口改为8080

[root@agent1 ~]# vim /etc/nginx/nginx.conf       //修改nginx默认端口号为8080
    server {
        listen       8080 default_server;
        listen       [::]:8080 default_server;
[root@agent1 ~]# systemctl restart nginx
//在备份节点中,新建脚本,检查VIP
[root@agent1 ~]# cd /scripts/
[root@agent1 scripts]# vim check_backupip.sh
#!/bin/bash
a=`ip a show ens33 | grep 192.168.89.100 | wc -l`
if [ $a -eq 0 ];then
        echo "0"
else
        echo "1"
fi
[root@agent1 scripts]# chmod +x check_backupip.sh


//在备份节点添加自定义监控项
[root@agent1 scripts]# vim /etc/zabbix/zabbix_agentd.conf

//在最末尾加入这两行,自定义监控项
UnsafeUserParameters=1
UserParameter=check.backup,/scripts/check_backupip.sh

[root@agent1 scripts]# systemctl restart zabbix-agent.service



//在主节点中测试该监控项
[root@server ~]# yum -y install zabbix-get
[root@server ~]# zabbix_get -s 192.168.89.150 -k "check.backup"
0

zabbix监控keepalived脑裂_第5张图片

zabbix监控keepalived脑裂_第6张图片
zabbix监控keepalived脑裂_第7张图片
zabbix监控keepalived脑裂_第8张图片
zabbix监控keepalived脑裂_第9张图片
zabbix监控keepalived脑裂_第10张图片
zabbix监控keepalived脑裂_第11张图片
zabbix监控keepalived脑裂_第12张图片

//模拟脑裂,关闭主节点的nginx服务
[root@server ~]# systemctl stop nginx
[root@server ~]# systemctl restart keepalived.service

zabbix监控keepalived脑裂_第13张图片
zabbix监控keepalived脑裂_第14张图片
zabbix监控keepalived脑裂_第15张图片

你可能感兴趣的:(zabbix,linux,nginx)