美文网首页
zabbix高可用解决方案

zabbix高可用解决方案

作者: zwb_jianshu | 来源:发表于2019-08-14 21:01 被阅读0次

    一、实验环境

    [root@agent ~]# cat /etc/redhat-release
    CentOS Linux release 7.6.1810 (Core) 
    [root@agent ~]# sestatus
    SELinux status:                 disabled
    [root@agent ~]# systemctl stop firewalld.service
    ssh优化:
    GSSAPIAuthentication no
    UseDNS no
    

    二、本次实验一共准备了三台机器

    主机名                   ip                                  软件环境
    zabbix-server01          10.0.0.61                    httpd+php+keepalived+sshpass
                                                                 zabbix server+agent     
    zabbix-server02          10.0.0.62                    httpd+php+keepalived+sshpass
                                                                 zabbix server+agent 
    agent                           10.0.0.51                    zabbix agent+mysql
    
    VIP规划:10.0.0.66
    

    三、实验介绍

    本文中用到了sshpass免密钥交互,也可以做ssh相互免密钥登录
    zabbix server的高可用,我试过在agent配置文件中,使用VIP,发现不能正常监控,于是才有本文。
    我的做法agent中的配置文件,Server和ServerActive中分别都配置了两个ip,就是为了保证,zabbix-server01宕机了,还可以把监控数据发往zabbix-server02,由于两个zabbix-server共用一个数据库,肯定不能同时往数据库写数据,于是我想到了使用keepalived来控制zabbix server的开关,做到VIP在哪,那台机器的zabbix server就启动,另外一台关闭,而zabbix的web页面使用VIP去访问
    本文把zabbix数据库独立出来,为了方便做mysql高可用集群
    

    四、配置zabbix-server

    首先在zabbix-server上
    rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
    vim /etc/yum.repos.d/zabbix.repo
    
    修改两处bashurl
    baseurl=http://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
    baseurl=http://mirrors.aliyun.com/zabbix/non-supported/rhel/7/x86_64/
    
    因为zabbix4.0需要php5.4以上,这里安装一个php的yum源
    rpm -ivh http://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    
    安装httpd和php
    yum install httpd -y
    yum install php56w php56w-mysql php56w-common php56w-gd php56w-mbstring php56w-mcrypt php56w-devel php56w-xml php56w-bcmath -y
    
    安装zabbix
    yum install -y zabbix-agent.x86_64 zabbix-get.x86_64 zabbix-server-mysql.x86_64 zabbix-web.noarch zabbix-web-mysql.noarch
    \cp -R /usr/share/zabbix/ /var/www/html/
    
    sed -i 's#^post_max_size = 8M#post_max_size = 16M#g' /etc/php.ini
    sed -i 's#^max_execution_time = 30#max_execution_time = 300#g' /etc/php.ini
    sed -i 's#^max_input_time = 60#max_input_time = 300#g' /etc/php.ini
    sed -i '883a date.timezone = Asia/shanghai' /etc/php.ini
    sed -i '276a ServerName 127.0.0.1:80' /etc/httpd/conf/httpd.conf
    
    数据库迁移
    mysqldump -B  zabbix > zabbix.sql
    mysql  -uroot -p123456 -h 10.0.0.51 < zabbix.sql
    

    五、在agent上

    yum install -y zabbix-agent.x86_64
    yum install -y mysql-server mysql-devel mysql
    systemctl   start  mariadb
    mysql -e "create database zabbix character set utf8 collate utf8_bin;"
    mysql -e "show databases;"
    mysql -e "grant all on zabbix.* to zabbix@'10.0.0.%' identified by '123456';"
    mysql -e "select user,host from mysql.user"
    zcat /opt/create.sql.gz |mysql -uzabbix -p123456 zabbix
    
    在zabbix-server01上:
    sed -i '81a DBHost=10.0.0.61' /etc/zabbix/zabbix_server.conf
    sed -i '116a DBPassword=123456' /etc/zabbix/zabbix_server.conf
    systemctl   restart  httpd 
    systemctl   restart  zabbix-server 
    systemctl   restart  zabbix-agent
    打开浏览器进行安装
    安装完成后
    scp /etc/zabbix/web/zabbix.conf.php root@10.0.0.62:/opt
    
    在zabbix-server02
    rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
    vim /etc/yum.repos.d/zabbix.repo
    
    修改两处bashurl
    baseurl=http://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
    baseurl=http://mirrors.aliyun.com/zabbix/non-supported/rhel/7/x86_64/
    rpm -ivh http://mirror.webtatic.com/yum/el7/latest.rpm
    
    安装httpd和php
    yum install httpd -y
    yum install php56w php56w-mysql php56w-common php56w-gd php56w-mbstring php56w-mcrypt php56w-devel php56w-xml php56w-bcmath -y
    
    安装zabbix
    yum install -y zabbix-agent.x86_64 zabbix-get.x86_64 zabbix-server-mysql.x86_64 zabbix-web.noarch zabbix-web-mysql.noarch
    \cp -R /usr/share/zabbix/ /var/www/html/
    
    sed -i 's#^post_max_size = 8M#post_max_size = 16M#g' /etc/php.ini
    sed -i 's#^max_execution_time = 30#max_execution_time = 300#g' /etc/php.ini
    sed -i 's#^max_input_time = 60#max_input_time = 300#g' /etc/php.ini
    sed -i '883a date.timezone = Asia/shanghai' /etc/php.ini
    sed -i '276a ServerName 127.0.0.1:80' /etc/httpd/conf/httpd.conf
    sed -i '81a DBHost=10.0.0.62' /etc/zabbix/zabbix_server.conf
    sed -i '116a DBPassword=123456' /etc/zabbix/zabbix_server.conf
    \mv /opt/zabbix.conf.php /etc/zabbix/web/
    systemctl  restart  httpd
    vi /etc/zabbix/zabbix_agentd.conf
    修改如下三处:
    Server=10.0.0.61,10.0.0.62
    ServerActive=10.0.0.61,10.0.0.62
    Hostname=10.0.0.62
    systemctl  restart  zabbix-agent
    

    六、在zabbix-server01和zabbix-server02上分别安装keepalived的

    zabbix-server01
    配置文件如下
    cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
    router_id LVS_DEVEL 29
    }
    
    vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    
    virtual_ipaddress {
    10.0.0.66
    }
    
    notify_master /opt/to_master.sh
    }
    
    cat /opt/to_master.sh
    #!/bin/bash
    sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@10.0.0.61
    "systemctl  stop  zabbix-server"
    systemctl  restart  zabbix-server
    
    zabbix-server02
    配置文件如下
    cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
    router_id LVS_DEVEL 30
    }
    vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    10.0.0.66
    }
    notify_master /opt/to_master.sh
    }
    chmod  +x  /opt/to_master.sh
    
    [root@zabbix3 zabbix]# cat /opt/to_master.sh
    #!/bin/bash
    sshpass -p 123456 ssh -o StrictHostKeyChecking=no root@10.0.0.61 "systemctl  stop  zabbix-server"
    systemctl  restart  zabbix-server
    
    下面是测试
    两台zabbix server都启动keepalived的
    zabbix-server01 启动zabbix server
    zabbix-server02不启动zabbix server
    

    zabbix-server01状态如下

    image.png

    zabbix-server02状态如下

    image.png

    模拟zabbix-server01宕机,keepalived不工作了

    image.png image.png image.png

    再测试飘回来

    image.png image.png

    查看数据历史间隔,发现会丢失个别监控项的一次监控数据

    image.png

    由于两台zabbix-server只会同时开启一台,所有agent的日志如下

    image.png

    相关文章

      网友评论

          本文标题:zabbix高可用解决方案

          本文链接:https://www.haomeiwen.com/subject/snonjctx.html