zabbix监控keepalived高可用脑裂

zabbix监控keepalived高可用脑裂

    • 1. 搭建lamb架构(用脚本安装,过程略)
    • 2. 配置zabbix
    • 3. 配置keepalived
    • 4. 配置zabbix监控,对脑裂进行监控

1. 搭建lamb架构(用脚本安装,过程略)

2. 配置zabbix

[root@zml ~]# yum -y install net-snmp-devel libevent-devel
[root@zml ~]# cd /usr/src/
[root@zml src]# tar -xf zabbix-4.0.3.tar.gz 
[root@zml src]# groupadd -r zabbix
[root@zml src]# useradd -r -M -s /sbin/nologin -g zabbix zabbix
[root@zml src]# mysql -uroot -pzml123!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.24 sec)

mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.21 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.25 sec)

mysql> exit
Bye
[root@zml src]# cd zabbix-4.0.4/database/mysql/
[root@zml mysql]# ls
data.sql  images.sql  Makefile.am  Makefile.in  schema.sql
[root@zml mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@zml mysql]#  mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@zml mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@zml mysql]# cd ../..
[root@zml zabbix-4.0.3]# ./configure --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
[root@zml zabbix-4.0.3]# make install
[root@zml zabbix-4.0.3]# ls /usr/local/etc/
zabbix_agentd.conf  zabbix_agentd.conf.d  zabbix_server.conf  zabbix_server.conf.d
[root@zml zabbix-4.0.3]# vim /usr/local/etc/zabbix_server.conf
#
# Mandatory: no
# Default:
 DBPassword=zabbix123!

### Option: DBSocket
[root@zml zabbix-4.0.3]# zabbix_server 
[root@zml zabbix-4.0.3]# zabbix_agentd 
[root@zml zabbix-4.0.3]# ss -antl
State       Recv-Q Send-Q        Local Address:Port                       Peer Address:Port              
LISTEN      0      128                       *:22                                    *:*                  
LISTEN      0      100               127.0.0.1:25                                    *:*                  
LISTEN      0      128                       *:10050                                 *:*                  
LISTEN      0      128                       *:10051                                 *:*                  
LISTEN      0      128               127.0.0.1:9000                                  *:*                  
LISTEN      0      128                      :::80                                   :::*                  
LISTEN      0      128                      :::22                                   :::*                  
LISTEN      0      100                     ::1:25                                   :::*                  
LISTEN      0      80                       :::3306                                 :::*                  
[root@zml ~]# cd /usr/src/zabbix-4.0.3
[root@localhost ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@localhost ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini[root@localhost ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@localhost ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@localhost ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@localhost ~]# cd /usr/src/zabbix-4.0.3
[root@localhost zabbix-4.0.3]# mkdir /usr/local/apache/htdocs/zabbix
[root@localhost zabbix-4.0.3]# cp -a frontends/php/* /usr/local/apache/htdocs/zabbix/
[root@localhost zabbix-4.0.3]# chown -R apache.apache /usr/local/apache/htdocs[root@localhost zabbix-4.0.3]# 
[root@localhost zabbix-4.0.3]# vim /etc/httpd24/httpd.conf 
[root@localhost zabbix-4.0.3]# apachectl -t
Syntax OK
[root@localhost zabbix-4.0.3]# chmod 777 /usr/local/apache/htdocs/zabbix/conf
[root@localhost zabbix-4.0.3]# ll -d /usr/local/apache/htdocs/zabbix/conf
drwxrwsrwx. 2 apache apache 81 12月 20 19:24 /usr/local/apache/htdocs/zabbix/conf
[root@localhost zabbix-4.0.3]# apachectl stop
httpd (no pid file) not running
[root@localhost zabbix-4.0.3]# apachectl start

//安装zabbix客户端
[root@2 ~]# yum -y install net-snmp-devel libevent-devel
[root@2 ~]# cd /usr/src/
[root@2 src]# ls
apr-1.6.5            apr-util-1.6.1.tar.bz2    httpd-2.4.37.tar.bz2
apr-1.6.5.tar.bz2    apr-util-1.6.1.tar.bz2.1  kernels
apr-1.6.5.tar.bz2.1  debug                     zabbix-4.0.3.tar.gz
apr-util-1.6.1       httpd-2.4.37
[root@2 src]# tar -xf zabbix-4.0.3.tar.gz 
[root@2 src]# cd zabbix-4.0.3
[root@2 zabbix-4.0.3]# groupadd -r zabbix
[root@2 zabbix-4.0.3]# useradd -r -M -s /sbin/nologin -g zabbix zabbix
[root@2 zabbix-4.0.3]# ./configure --enable-agent
[root@2 zabbix-4.0.3]# make install
[root@2 ~]# cd /usr/local/etc/
[root@2 etc]# ls
zabbix_agentd.conf  zabbix_agentd.conf.d
[root@2 etc]# sed -i 'Server=127.0.0.1/s/127.0.0.1/192.168.^C
[root@2 etc]# vim zabbix_agentd.conf
[root@2 etc]# sed -i 'Server=127.0.0.1/s/127.0.0.1/192.168.194.132/g' zabbix_agentd.conf
sed: -e expression #1, char 1: unknown command: `S'
[root@2 etc]# sed -i '/Server=127.0.0.1/s/127.0.0.1/192.168.194.132/g' zabbix_agentd.conf
[root@2 etc]# sed -i '/ServerActive=127.0.0.1/s/127.0.0.1/192.168.194.132/g' zabbix_agentd.conf
[root@2 etc]# sed -i '/Hostname=Zabbix server/s/Zabbix server/agent1/g' zabbix_agentd.conf
[root@2 etc]# zabbix_agentd 
[root@2 etc]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128         *:22                      *:*                  
LISTEN     0      128         *:10050                   *:*                  
LISTEN     0      128        :::80                     :::*                  
LISTEN     0      128        :::22                     :::*                  
[root@2 etc]#

3. 配置keepalived

//主keepalived
[root@localhost htdocs]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost htdocs]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost htdocs]#  yum -y install epel-release vim wget gcc gcc-c++
[root@localhost htdocs]# yum -y install keepalived
[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
[root@localhost ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass zml123!
    }
    virtual_ipaddress {
        192.168.194.99
    }
}

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

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

    real_server 192.168.194.138 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# vim check_n.sh
[root@localhost scripts]# chmod +x check_n.sh
[root@localhost scripts]# cat 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@localhost scripts]# vim notify.sh
[root@localhost scripts]# chmod +x notify.sh
[root@localhost scripts]# cat 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@localhost scripts]# cat 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@localhost ~]# vim /etc/keepalived/keepalived.conf 
[root@localhost ~]# cat /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 zml123!
    }
    virtual_ipaddress {
        192.168.194.99
    }
    track_script {
        nginx_check
    }
    notify_master "/scripts/notify.sh master 192.168.194.99"
    notify_backup "/scripts/notify.sh backup 192.168.194.99"
}
}

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

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

    real_server 192.168.194.138 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:35:c2:61 brd ff:ff:ff:ff:ff:ff
    inet 192.168.194.132/24 brd 192.168.194.255 scope global dynamic ens33
       valid_lft 1668sec preferred_lft 1668sec
    inet 192.168.194.99/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::a256:aa88:345c:c691/64 scope link 
       valid_lft forever preferred_lft forever
       
//备keepalived
[root@2 ~]# sed -i '/SELINUX=enforcing/s/enforcing/disabled/g' /etc/selinux/config 
[root@2 ~]# systemctl stop firewalld
[root@2 ~]# 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@2 ~]# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@2 ~]# setenforce 0
[root@2 ~]# yum -y install epel-release vim wget gcc gcc-c++
[root@2 ~]# yum -y install keepalived
[root@2 ~]# vim /etc/keepalived/keepalived.conf 
[root@2 ~]# cat /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 wangqing
    }
    virtual_ipaddress {
        192.168.194.99
    }
}

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

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

    real_server 192.168.194.140 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@2 ~]# mkdir /scripts
[root@2 ~]# cd /scripts/
[root@2 scripts]# vim notify.sh
[root@2 scripts]# chmod +x notify.sh
[root@2 scripts]# vim /etc/keepalived/keepalived.conf 
[root@2 scripts]# cat /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 wangqing
    }
    virtual_ipaddress {
        192.168.194.99
    }
    notify_master "/scripts/notify.sh master 192.168.194.99"
    notify_backup "/scripts/notify.sh backup 192.168.194.99"
}

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

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

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

4. 配置zabbix监控,对脑裂进行监控

//在备keepalived上写监控脚本
[root@2 ~]# cd /scripts/
[root@2 scripts]# vim check_keepalived.sh
[root@2 scripts]# cat check_keepalived.sh
#!/bin/bash

while true
do
if [ `ip a show ens33 |grep 192.168.194.99|wc -l` -ne 0 ]
then
    echo 1
else
    echo 0
fi
done

//在zabbix客户端上配置
[root@2 ~]# cd /scripts/
[root@2 scripts]# chown zabbix.zabbix check_keepalived.sh 
[root@2 scripts]# ll
total 8
-rw-r--r--. 1 zabbix zabbix 125 Mar  9 22:17 check_keepalived.sh
-rwxr-xr-x. 1 root   root   662 Mar  9 21:46 notify.sh
[root@2 scripts]# sed -i '/# UnsafeUserParameters=0/s/#//g' /usr/local/etc/zabbix_agentd.conf
[root@2 scripts]# sed -i '/UnsafeUserParameters=0/s/0/1/g' /usr/local/etc/zabbix_agentd.conf
[root@2 scripts]# echo "UserParameter=check_keepalived,/scripts/check_keepalived.sh" >> /usr/local/etc/zabbix_agentd.conf
[root@2 scripts]# pkill zabbix
[root@2 scripts]# zabbix_agentd
  • 添加监控项
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190309223449368.png
  • 添加触发器
    在这里插入图片描述

你可能感兴趣的:(zabbix监控keepalived高可用脑裂)