Zabbix监控nginx高可用是否脑裂

Zabbix监控nginx高可用是否脑裂

1. nginx高可用配置

lamp部署请参照lamp分离部署(此为部署在一台服务器上)
zabbix部署请参照zabbix部署

环境说明:

系统信息主机ip应用
rhel7192.168.100.100lamp
zabbix_server
zabbix_agentd
rhel7192.168.100.99keepalived
nginx_master
rhel7192.168.100.96keepalived
nginx_slave
zabbix_agentd

1.1 keepalived安装

配置主keepalived

# 关闭防火墙与SELINUX
[root@99 ~]# systemctl stop firewalld
[root@99 ~]# systemctl disable firewalld
[root@99 ~]# setenforce 0

# 配置网络源
[root@99 ~]# curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@99 ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@99 ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@99 ~]# yum -y install epel-release vim wget gcc gcc-c++
安装过程略.....

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

# 查看安装生成的文件
[root@99 ~]# rpm -ql keepalived
/etc/keepalived     //配置目录
/etc/keepalived/keepalived.conf     //此为主配置文件
/etc/sysconfig/keepalived
/usr/bin/genhash
/usr/lib/systemd/system/keepalived.service      //此为服务控制文件
/usr/libexec/keepalived
/usr/sbin/keepalived
.....此处省略N行

用同样的方法在备服务器上安装keepalived

# 关闭防火墙与SELINUX
[root@96 ~]# systemctl stop firewalld
[root@96 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@96 ~]# setenforce 0
[root@96 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

# 配置网络源
[root@96 ~]# curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@96 ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@96 ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@96 ~]# yum -y install epel-release vim wget gcc gcc-c++
安装过程略.....

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

1.2 在主备机上分别安装nginx

在主上安装nginx

[root@99 ~]# yum -y install nginx
[root@99 ~]# cd /usr/share/nginx/html/
[root@99 html]# ls
404.html  50x.html  index.html  nginx-logo.png  poweredby.png
[root@99 html]# mv index.html{,.bak}
[root@99 html]# echo 'slave' > index.html
[root@99 html]# ls
404.html  50x.html  index.html  index.html.bak  nginx-logo.png  poweredby.png
[root@99 html]# systemctl start nginx
[root@99 html]# systemctl enable nginx		

在备上安装nginx

[root@96 ~]# yum -y install nginx
[root@96 ~]# cd /usr/share/nginx/html/
[root@96 html]# ls
404.html  50x.html  index.html  nginx-logo.png  poweredby.png
[root@96 html]# mv index.html{,.bak}
[root@96 html]# echo 'slave' > index.html
[root@96 html]# ls
404.html  50x.html  index.html  index.html.bak  nginx-logo.png  poweredby.png
[root@96 html]# systemctl start nginx
[root@96 html]# systemctl disable nginx		## 备上面不能开机自动启动

在浏览器上访问试试,确保master上的nginx服务能够正常访问
在这里插入图片描述
在这里插入图片描述

1.3 keepalived配置

配置主keepalived

! Configuration File for keepalived

global_defs {
   router_id lb02
}

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

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

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

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


[root@99 html]# systemctl start keepalived
[root@99 html]# systemctl enable keepalived

配置备keepalived

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

global_defs {
   router_id lb01
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass shicailun
    }
    virtual_ipaddress {
        192.168.100.250
    }
}

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

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

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

[root@96 ~]# systemctl start keepalived
[root@96 ~]# systemctl enable keepalived

1.4 查看VIP在哪里

在主上查看

[root@99 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2f:89:15 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.99/24 brd 192.168.100.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.100.250/32 scope global ens33				## 现在在主这边说明没问题
       valid_lft forever preferred_lft forever
    inet6 fe80::92d4:d1c1:856a:e885/64 scope link 
       valid_lft forever preferred_lft forever

在备上查看

[root@96 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:ad:5e:54 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.96/24 brd 192.168.100.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever

停掉主上面的keepalived看备上面会不会抢占过去

[root@99 ~]# systemctl stop keepalived
[root@99 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2f:89:15 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.99/24 brd 192.168.100.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::92d4:d1c1:856a:e885/64 scope link 
       valid_lft forever preferred_lft forever

[root@96 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:ad:5e:54 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.96/24 brd 192.168.100.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.100.250/32 scope global ens33			## 备上面已经抢占过来了
       valid_lft forever preferred_lft forever


[root@99 ~]# systemctl start keepalived			## 开启主的keepalived时又会抢占回来
[root@99 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2f:89:15 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.99/24 brd 192.168.100.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.100.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::92d4:d1c1:856a:e885/64 scope link 
       valid_lft forever preferred_lft forever

1.5 keepalived通过脚本来监控nginx负载均衡机的状态

在主上编写脚本

[root@99 ~]# mkdir /scripts
[root@99 ~]# cd /scripts/
[root@99 scripts]# vim check_n.sh
#!/bin/bash
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
    systemctl stop keepalived
fi

[root@99 scripts]# chmod +x check_n.sh
[root@99 scripts]# ll
总用量 4
-rwxr-xr-x 1 root root 142 9月   4 13:50 check_n.sh

[root@99 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" 1539163444@qq.com
}
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@99 scripts]# chmod +x notify.sh
[root@99 scripts]# ll
总用量 8
-rwxr-xr-x 1 root root 142 9月   4 13:50 check_n.sh
-rwxr-xr-x 1 root root 662 9月   4 13:52 notify.sh

在备上编写脚本

[root@96 ~]# mkdir /scripts
[root@96 ~]# cd /scripts/
[root@96 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" 1539163444@qq.com
}
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@96 scripts]# chmod +x notify.sh
[root@96 scripts]# ll
-rwxr-xr-x 1 root root 594 Oct 20 03:24 notify.sh

1.6 配置keepalived加入监控脚本的配置

配置主keepalived

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

global_defs {
   router_id lb01
}

vrrp_script nginx_check {
    script "/scripts/check_n.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 shicailun
    }
    
    virtual_ipaddress {
        192.168.100.250
    }
    
    track_script {
        nginx_check
    }
    notify_master "/scripts/notify.sh master 192.168.100.250"
    notify_backup "/scripts/notify.sh backup 192.168.100.250"
}

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

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

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



[root@99 ~]# systemctl restart keepalived

配置备keepalived

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

global_defs {
   router_id lb02
}

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

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

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

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

[root@96 ~]# systemctl restart keepalived

停掉主的nginx服务备为自动成为主

[root@99 ~]# systemctl stop nginx && ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2f:89:15 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.99/24 brd 192.168.100.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::92d4:d1c1:856a:e885/64 scope link 
       valid_lft forever preferred_lft forever

[root@96 scripts]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:ad:5e:54 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.96/24 brd 192.168.100.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.100.250/32 scope global ens33
       valid_lft forever preferred_lft forever


启动主上的nginx和keepalived又会抢占回来

[root@99 ~]# systemctl start nginx keepalived

[root@99 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2f:89:15 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.99/24 brd 192.168.100.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.100.250/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::92d4:d1c1:856a:e885/64 scope link 
       valid_lft forever preferred_lft forever

2. 在备上面配置自定义监控

写脚本,给执行权限,改脚本存放目录的属主为zabbix

[root@96 ~]# cd /scripts/
[root@96 scripts]# vim check_keepalived.sh

#!/bin/bash

if [ `ip a show ens33 |grep 192.168.100.250|wc -l` -ne 0 ]
then
    echo "1"
else
    echo "0"
fi
## 当备上面出现vip的时候要么是出现了脑裂要么是主服务器挂了

[root@96 scripts]# bash check_keepalived.sh 
0
[root@96 scripts]# chmod +x check_keepalived.sh 
[root@96 scripts]# chown -R zabbix.zabbix .
[root@96 scripts]# ll -d
drwxr-xr-x 2 zabbix zabbix 50 9月   4 14:48 .

改客户端配置文件agentd.conf

[root@96 ~]# vim /usr/local/etc/zabbix_agentd.conf	## 末尾处添加
UnsafeUserParameters=1
UserParameter=check_keepalived,/usr/bin/bash /scripts/check_keepalived.sh

# 重启zabbix_agent
[root@96 ~]# pkill zabbix
[root@96 ~]# zabbix_agentd

在zabbix_server服务端手动测试

[root@100 ~]# zabbix_get -s 192.168.100.96 -k check_keepalived
0

配置网页界面,添加监控项以及触发器

  • 创建主机
    在这里插入图片描述
  • 配置监控项

在这里插入图片描述

  • 配置触发器
    在这里插入图片描述
  • 查看监控到的最新数据
    在这里插入图片描述
    在这里插入图片描述
  • 触发验证
# 停掉主上面的nginx服务
[root@99 ~]# systemctl stop nginx

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值