zoukankan      html  css  js  c++  java
  • 搭建双主zabbix

    架构图

    环境准备

    操作系统:Ubuntu1804,zabbix4.0
    172.31.1.30 zabbix-server1 rsync + inotify-tools
    172.31.1.31 zabbix-server2 rsync + inotify-tools
    172.31.1.32 mysql-server1
    172.31.1.33 mysql-server2
    172.31.1.34 keepalived + haproxy1
    172.31.1.35 keepalived + haproxy2
    

    安装haproxy和keepalived(两台都要)

    [root@zabbix-ha1 ~]# apt -y install haproxy keepalived
    

    查找模板

    [root@zabbix-ha1 ~]# find / -name "*keepalived.conf*"
    

    拷贝模板

    [root@zabbix-ha1 ~]# cp /usr/share/doc/keepalived/samples/keepalived.conf.vrrp /etc/keepalived/keepalived.conf
    

    配置keepalived

    [root@zabbix-ha1 ~]# cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         acassen
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       smtp_server 192.168.200.1
       smtp_connect_timeout 30
       router_id LVS_DEVEL
    }
    
    vrrp_instance apache {
        state MASTER
        interface eth0
        garp_master_delay 10
        smtp_alert
        virtual_router_id 55
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            172.31.1.100 dev eth0 label eth0:1
        }
    }
    
    vrrp_instance mysql {
        state BACKUP
        interface eth0
        garp_master_delay 10
        smtp_alert
        virtual_router_id 51
        priority 80
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 123456
        }
        virtual_ipaddress {
            172.31.1.200 dev eth0 label eth0:2
        }
    }
    

    重启

    [root@zabbix-ha1 ~]# systemctl restart keepalived.service
    

    查看会看到两个vip

    [root@zabbix-ha1 ~]# ip a
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
        link/ether 00:0c:29:d3:f0:ed brd ff:ff:ff:ff:ff:ff
        inet 172.31.1.34/21 brd 172.31.7.255 scope global eth0
           valid_lft forever preferred_lft forever
        inet 172.31.1.100/32 scope global eth0:1
           valid_lft forever preferred_lft forever
        inet 172.31.1.200/32 scope global eth0:2
    

    把配置文件拷贝到远端ha机器

    [root@zabbix-ha1 ~]# scp /etc/keepalived/keepalived.conf 172.31.1.35:/etc/keepalived/keepalived.conf
    

    修改内核参数

    [root@zabbix-ha1 ~]# vim /etc/sysctl.conf
    
    net.ipv4.ip_forward = 1
    net.ipv4.ip_nonlocal_bind = 1
    net.ipv6.ip_nonlocal_bind = 1
    

    执行

    [root@zabbix-ha2 ~]# sysctl -p
    

    修改haproxy配置文件

    [root@zabbix-ha1 ~]# cat /etc/haproxy/haproxy.cfg
    
    listen mysql-3306
      bind 172.31.1.200:3306
      mode tcp
      server 172.31.1.32 172.31.1.32:3306 check inter 2s fall 3 rise 5
      server 172.31.1.33 172.31.1.33:3306 check inter 2s fall 3 rise 5
    
    listen web-80
        bind 172.31.1.100:80
        mode tcp
        cookie SRV insert indirect nocache
        balance roundrobin
        server 172.31.1.30 172.31.1.30:80 check inter 3000 fall 2 rise 5
        server 172.31.1.31 172.31.1.31:80 check inter 3000 fall 2 rise 5
    
    listen zabbix-10051
        bind 172.31.1.100:10051
        mode tcp
        cookie SRV insert indirect nocache
        balance roundrobin
        server 172.31.1.30 172.31.1.30:10051 check inter 3000 fall 2 rise 5
        server 172.31.1.31 172.31.1.31:10051 check inter 3000 fall 2 rise 5
    

    重启服务

    [root@zabbix-ha1 ~]# systemctl restart haproxy.service
    

    把配置文件拷贝到远端ha机器

    [root@zabbix-ha1 ~]# scp /etc/haproxy/haproxy.cfg 172.31.1.35:/etc/haproxy/haproxy.cfg
    

    查看vip

    [root@zabbix-ha1 ~]# ip a
    inet 172.31.1.100/32 scope global eth0:1
    
    [root@zabbix-ha2 ~]# ip a
    inet 172.31.1.200/32 scope global eth0:2
    

    安装数据库(两台都要)

    [root@mysql ~]# apt install -y mysql-server
    

    修改配置

    [root@mysql ~]# vim /etc/mysql/mysql.conf.d/mysqld.cnf
    [mysqld]
    
    bind-address        = 0.0.0.0
    server-id = 32
    log-bin = /var/lib/mysql/master-log
    auto-increment-increment = 2
    auto-increment-offset = 1
    binlog_format = mixed
    sync_binlog=1
    binlog-do-db=zabbix_server  # 同步原数据库名
    binlog-ignore-db=mysql      # 不同步数据库名
    binlog-ignore-db=sys
    replicate-do-db=zabbix_server
    replicate-ignore-db=mysql
    replicate-ignore-db=sys
    

    重启服务

    [root@mysql ~]# systemctl restart mysql
    

    下载zabbix

    [root@zabbix-master ~]# wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb
    

    安装

    [root@zabbix-master ~]# dpkg -i zabbix-release_4.0-3+bionic_all.deb
    
    [root@zabbix-master ~]# apt update
    

    安装前端

    [root@zabbix-master ~]# apt -y install zabbix-server-mysql zabbix-frontend-php zabbix-agent
    

    登录zabbix数据库

    [root@zabbix-master ~]# mysql -uzabbix -p'centos.com' -h 172.31.1.32
    

    导入初始化数据

    [root@zabbix-master ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p'centos.com' -h 172.31.1.32 zabbix_server
    

    修改zabbix-server端配置

    [root@zabbix-master ~]# vim /etc/zabbix/zabbix_server.conf
    SourceIP=172.31.1.100
    DBHost=172.31.1.32
    DBName=zabbix_server
    DBUser=zabbix
    DBPassword=centos.com
    DBPort=3306
    

    修改php

    [root@zabbix-master ~]# vim /etc/zabbix/apache.conf
    
    php_value date.timezone Asia/Shanghai
    

    重启

    [root@zabbix-master ~]# systemctl restart zabbix-server.service apache2 zabbix-agent.service
    

    浏览器登录:http://172.31.1.30/zabbix

    然后就是web界面

    另外一台按同样步骤安装即可

    mysql数据库猪猪同步

    172.31.1.32(授权给对方)

    mysql> grant REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO replication@'172.31.1.33' identified by '123456';
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    
    mysql> show master status;
    +-------------------+----------+---------------+------------------+-------------------+
    | File              | Position | Binlog_Do_DB  | Binlog_Ignore_DB | Executed_Gtid_Set |
    +-------------------+----------+---------------+------------------+-------------------+
    | master-log.000002 |   403995 | zabbix_server | mysql,sys        |                   |
    +-------------------+----------+---------------+------------------+-------------------+
    1 row in set (0.00 sec)
    

    172.31.1.33(授权给对方)

    mysql> grant REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO replication@'172.31.1.32' identified by '123456';
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    
    mysql> show master status;
    +-------------------+----------+---------------+------------------+-------------------+
    | File              | Position | Binlog_Do_DB  | Binlog_Ignore_DB | Executed_Gtid_Set |
    +-------------------+----------+---------------+------------------+-------------------+
    | master-log.000002 |   448899 | zabbix_server | mysql,sys        |                   |
    +-------------------+----------+---------------+------------------+-------------------+
    1 row in set (0.00 sec)
    

    启动同步(在对方机器操作添加当前机器为master)

    172.31.1.33(33操作如下:)

    CHANGE MASTER TO
      MASTER_HOST='172.31.1.32',
      MASTER_USER='replication',
      MASTER_PASSWORD='123456',
      MASTER_LOG_FILE='master-log.000002',
      MASTER_LOG_POS=403995;
    

    启动

    mysql> start slave;
    Query OK, 0 rows affected (0.00 sec)
    

    查看

    mysql> show slave statusG
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.31.1.32
                      Master_User: replication
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: master-log.000002
              Read_Master_Log_Pos: 429267
                   Relay_Log_File: mysql2-relay-bin.000002
                    Relay_Log_Pos: 25593
            Relay_Master_Log_File: master-log.000002
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    

    172.31.1.32(32操作如下:)

    CHANGE MASTER TO
      MASTER_HOST='172.31.1.33',
      MASTER_USER='replication',
      MASTER_PASSWORD='123456',
      MASTER_LOG_FILE='master-log.000002',
      MASTER_LOG_POS=448899;
    

    启动

    mysql> start slave;
    Query OK, 0 rows affected (0.00 sec)
    

    查看

    mysql> show slave statusG
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.31.1.33
                      Master_User: replication
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: master-log.000002
              Read_Master_Log_Pos: 479863
                   Relay_Log_File: mysql-relay-bin.000002
                    Relay_Log_Pos: 31285
            Relay_Master_Log_File: master-log.000002
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
    

    文件同步(这里还不完整需要在查查)

    建议使用rsync + sersync

    安装

    [root@zabbix-master ~]# apt -y install rsync
    

    默认安装但是不开启,修改配置文件即可

    [root@zabbix-master ~]# vim /etc/default/rsync
    
    #RSYNC_ENABLE=false
    RSYNC_ENABLE=true
    

    增加配置

    [root@zabbix-master ~]# cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
    

    修改配置文件

    [root@zabbix-master ~]# vim /etc/rsyncd.conf
    
    [zabbix]
    uid=root
    gid=root
    max connections=100
    use chroot=true
    log file=/var/log/rsyncd.log
    motd file = /etc/rsyncd.motd
    transfer logging = true
    hosts allow= 172.31.1.31
    [zabbix]
    path=/etc/zabbix/
    exclude = web
    comment = etc zabbix
    read only = no
    list = yes
    auth users = root
    secrets file=/etc/rsyncd.pwd
    
    [web1]
    #path=/etc/httpd/conf/ # 这是CentOS系统的
    path=/etc/apache2/
    #comment = httpd conf  # 这是CentOS系统的
    comment = apache2 conf
    read only = no
    list = yes
    auth users = root
    secrets file=/etc/rsyncd.pwd
    [web2]
    #path=/etc/httpd/conf/ # 这是CentOS系统的
    path=/etc/apache2/
    #comment = httpd conf  # 这是CentOS系统的
    comment = apache2 conf
    read only = no
    list = yes
    auth users = root
    secrets file=/etc/rsyncd.pwd
    

    制作密码

    [root@zabbix-master ~]# cat /etc/rsyncd.pwd
    root:Root$130
    
    [root@zabbix-master ~]# cat /etc/rsyncd.pwd2
    Root$130
    
    

    rsyncd.conf文件与主机相同,只是 ‘ hosts allow= 172.31.1.30 ’
    同步的文件为/etc/zabbix/(不包括web)、/etc/httpd/conf/和/etc/httpd/conf.d/,可根据实际情况增减。

    验证主从连接状态

    [root@zabbix-master zabbix]# rsync -vzrtopg --progress root@172.31.1.31::zabbix --password-file=/etc/rsyncd.pwd2
    

    同步数据

    注意:
    –delete :以SRC为主,对DEST进行同步。多则删之,少则补之。

    [root@zabbix-master zabbix]# rsync -avzPt --delete --password-file=/etc/rsyncd.pwd2 root@172.31.1.31::zabbix /etc/zabbix/
    

    主备都要

    [root@zabbix-master ~]# nohup rsync --daemon --config=/etc/rsyncd.conf &
    

    文件同步测试

    [root@zabbix-master apache2]# cd /etc/apache2/ && mkdir dropme1 && cd /etc/apache2/ && mkdir dropme2 && cd /etc/zabbix/ && mkdir dropme3
    

    inotify 安装

    [root@zabbix-master ~]# apt -y install inotify-tools
    

    编写监控同步脚本

    #!/bin/bash
    #
    #********************************************************************
    #Author:        xuanlv
    #QQ:            360956175
    #Date:          2021-08-08
    #FileName:     zabbijk-scpits.sh
    #URL:           https://www.cnblogs.com/xuanlv-0413/
    #Description:      The test script
    #Copyright (C):     2021 All rights reserved
    #********************************************************************
    
    ZabbixPath=/etc/zabbix/
    WebPath=/etc/apache2/
    WebPath2=/etc/apache2/
    # server是写对方的IP
    Server=172.31.1.31
    
    /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib,move  $WebPath $WebPath2 $ZabbixPath |
    while read line
    do
        if [[ $line =~ $ZabbixPath ]];then
           rsync -vzrtopg --progress --delete  $ZabbixPath  root@$Server::zabbix  --password-file=/etc/rsyncd.pwd2
        elif [[ $line =~ $WebPath2 ]];then
           rsync -vzrtopg --progress --delete  $WebPath2        root@$Server::web2 --password-file=/etc/rsyncd.pwd2
        elif [[ $line =~ $WebPath ]];then
           rsync -vzrtopg --progress --delete  $WebPath  root@$Server::web1  --password-file=/etc/rsyncd.pwd2
        else
           echo $line >> /var/log/inotify.log
        fi
    done
    

    授权

    [root@zabbix-master ~]# chmod +x zabbijk-scpits.sh
    

    执行

    [root@zabbix-master ~]# nohup sh /root/zabbijk-scpits.sh &
    

    验证

    使用vip登录zabbix

    http://172.31.1.100/zabbix/

    需要创建zabbix用户

    先查看zabbix-server

    [root@zabbix-master ~]# id zabbix
    uid=113(zabbix) gid=115(zabbix) groups=115(zabbix)
    

    ha服务器创建zabbix用户

    [root@zabbix-ha2 ~]# groupadd -g 115 zabbix && useradd -u 115 -r -g 115 -s /usr/sbin/nologin zabbix
    
  • 相关阅读:
    powershell命令大全
    Android USB Connections Explained: MTP, PTP, and USB Mass Storage
    安装Windows Metasploit Framework
    Sublime Text2 jedi插件离线安装
    MySQL下载安装配置和Navicat for MySQL的安装配置
    Sublime中文编码问题
    Flask入门之结构重组(瘦身)-第13讲笔记
    Flask入门之SQLAlchemy配置与数据库连接
    Flask入门之flask-wtf表单处理
    Total Command使用笔记
  • 原文地址:https://www.cnblogs.com/xuanlv-0413/p/15171576.html
Copyright © 2011-2022 走看看