[TOC]
0x00 需求与环境 (1)项目需求
实现 zabbix/数据库 (宕机自动切换到从)高可用架构;
实现 zabbix-proxy 分布式代理接入;
实现 zabbix 的邮件与钉钉预警;
实现 zabbix 的数据可视化利用grafana工具;
(2)项目环境 Zabbix主(Master) :CentOS Linux release 7.6.1810 (Core) Zabbix备(Slave) :CentOS Linux release 7.6.1810 (Core) Zabbix-proxy(分布式) Zabbix-agent(4台-主/从),其中宿主机windows7一台与proxyagent主机一台
我们这里将在Zabbix主(Master)中安装zabbix-server以及zabbix-agent和grafana可视化; 在zabbix备(slave)中安装zabbix-server/agent以及用docker-compose实现安装的docker-zabbix-proxy; 我将我的宿主机/matser/slave/proxyagent机器连接到docker-zabbix-proxy代理中;
基础环境搭建:
[TOC]
0x00 需求与环境 (1)项目需求
实现 zabbix/数据库 (宕机自动切换到从)高可用架构;
实现 zabbix-proxy 分布式代理接入;
实现 zabbix 的邮件与钉钉预警;
实现 zabbix 的数据可视化利用grafana工具;
(2)项目环境 Zabbix主(Master) :CentOS Linux release 7.6.1810 (Core) Zabbix备(Slave) :CentOS Linux release 7.6.1810 (Core) Zabbix-proxy(分布式) Zabbix-agent(4台-主/从),其中宿主机windows7一台与proxyagent主机一台
我们这里将在Zabbix主(Master)中安装zabbix-server以及zabbix-agent和grafana可视化; 在zabbix备(slave)中安装zabbix-server/agent以及用docker-compose实现安装的docker-zabbix-proxy; 我将我的宿主机/matser/slave/proxyagent机器连接到docker-zabbix-proxy代理中;
基础环境搭建:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 Zabbix(备)$hostname Szabbix Zabbix(主)$hostname Mzabbix $vim /etc/hosts92.168.1.98 Szabbix 192.168.1.99 Mzabbix 192.168.1.100 proxagent 192.168.1.200 zabbix.weiyigeek.com 192.168.56.101 Windows7 enp0s3: 192.168.1.200 enp0s9: 192.168.56.200 Zabbix主: 1. enp0s3 - 192.168.1.98 2. ens0s9 - 192.168.56.103 Zabbix从: 1. enp0s3 - 192.168.1.99 2. ens0s9 - 192.168.56.102 3. docker - 172.18.0.2 windows7: 192.168.56.101 proxyagent: 192.168.1.100
(3)项目架构
weiyigeek.top-AWS图
0x01 项目安装流程 Step1.根据我前面写的磁盘高可用和服务高可用进行安装heartbeat与drbd注意这里是Centos7版本的.(Drbd磁盘格式化是个坑)1 [root@Mzabbix/Szabbix]$usermod -G root hacluster
安装好drbd手动主从切换测试:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 mysql> update mysql.user set password=password('System@123' ) where user='root' ; mysql> grant all privileges on *.* to 'root' @'%' identified by "System@123" with grant option; mysql> grant all privileges on zabbix.* to zabbix@'%' identified by 'zabbix' ; mysql> grant all privileges on zabbix_proxy.* to zabbix_proxy@'%' identified by 'zabbix' ; mysql> flush privileges; [root@Mzabbix lib] drbdadm secondary data [root@Szabbix lib] mount /dev/drbd0 /var/lib/mysql/ chown -R mysql:mysql /var/lib/mysql/ service mariadb restart [root@Szabbix lib]$mysql -uzabbix -pzabbix Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.60-MariaDB MariaDB Server MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | test | +--------------------+
weiyigeek.top-drbd同步ok
(1)heartbeat配置文件 主/备机器共用:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 auth 1 1 sha1 zabbix logfile /var/log /ha-log logfacility local0 keepalive 2 deadtime 15 warntime 5 initdead 30 udpport 694 mcast enp0s3 225.0.0.255 694 1 0 auto_failback on node Mzabbix node Szabbix ping 192.168.1.1 Mzabbix IPaddr::192.168.1.200/24/enp0s3 drbddisk::data Filesystem::/dev/drbd0::/var/lib/mysql::ext4 mysqld zabbix Mzabbix IPaddr::192.168.56.200/24/enp0s9
注意:(坑)需要将/etc/目录中drbddisk复制到heartbeat源码安装的目录之中: cp /etc/ha.d/resource.d/drbddisk /usr/local/heartbeat/etc/ha.d/resource.d/
将下列的脚本加入到/usr/local/heartbeat/etc/ha.d/resource.d/之中mysqld 资源脚本: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #!/bin/bash chown -R mysql:mysql /var/lib/mysql sleep 1 if [ $# -eq 0 ];then echo "Usage:mysqld [start|stop|status]" exit fi action=$1 case $action in "start" ) service mariadb start ;; "stop" ) service mariadb stop ;; "status" ) service mariadb status esac
zabbix 资源脚本: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #!/bin/bash action=$1 if [ $# -eq 0 ];then echo "Usage:zabbix [start|stop|status]" exit fi case $action in "start" ) systemctl start zabbix-server httpd ;; "stop" ) systemctl stop zabbix-server httpd ;; "status" ) systemctl status zabbix-server httpd esac
(2)drbd配置文件: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 [root@Szabbix ha.d] global { usage-count no; } common { protocol C; startup { degr-wfc-timeout 60; } disk { on-io-error detach; } net { cram-hmac-alg md5; shared-secret "testdrbd" ; } syncer { rate 100M; verify-alg crc32c; } } resource data{ disk { on-io-error detach; } on Mzabbix { device /dev/drbd0; disk /dev/sdb1; address 192.168.56.103:7788; meta-disk /dev/sdb2[0]; } on Szabbix { device /dev/drbd0; disk /dev/sdb1; address 192.168.56.102:7788; meta-disk /dev/sdb2[0]; } }
完成以上配置后执行: $chown -R root:haclient /usr/local/heartbeat/etc/ha.d/
Step 2. 按照[zabbix入门学习]安装与配置Zabbix-server和zabbix-web以及mariadb数据库的安装; 由于我已经安装并配置好主节点zabbix的zabbix-web,我们将一些文件进行中直接复制到从节点中改IP指向我们VIP即可;1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [root@Mzabbix ~]$ scp /etc/zabbix/zabbix_server.conf root@192.168.1.98:/etc/zabbix/zabbix_server.conf [root@Mzabbix ~] LogFileSize=10 LogFile=/var/log /zabbix/zabbix_server.log PidFile=/var/run/zabbix/zabbix_server.pid SocketDir=/var/run/zabbix DBHost=192.168.1.200 DBName=zabbix DBUser=zabbix DBPassword=zabbix DBPort=3306 SNMPTrapperFile=/var/log /snmptrap/snmptrap.log Timeout=4 AlertScriptsPath=/usr/lib/zabbix/alterscripts ExternalScripts=/usr/lib/zabbix/externalscripts FpingLocation=/usr/sbin/fping LogSlowQueries=3000 AllowRoot=1 User=root StatsAllowedIP=192.168.1.0/24
复制主中zabbix-web项目文件到从机 /usr/share/zabbix 和etc/zabbix/web中config的php配置文件到从机:(非常重要:不用重新去初始化zabbix-web)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [root@Mzabbix web]$scp /etc/zabbix/web/zabbix.conf.php root@192.168.1.98:/etc/zabbix/web/ [root@Szabbix web]$chown -R apache:apache zabbix.conf.php <?php // Zabbix GUI configuration file. global $DB ; $DB ['TYPE' ] = 'MYSQL' ;$DB ['SERVER' ] = '192.168.1.200' ; //VIP$DB ['PORT' ] = '3306' ;$DB ['DATABASE' ] = 'zabbix' ;$DB ['USER' ] = 'zabbix' ;$DB ['PASSWORD' ] = 'zabbix' ;// Schema name. Used for IBM DB2 and PostgreSQL. $DB ['SCHEMA' ] = '' ;$ZBX_SERVER = '192.168.1.200' ; //从节点设置从即刻$ZBX_SERVER_PORT = '10051' ;$ZBX_SERVER_NAME = 'zabbix.weiyigeek.com' ;$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
weiyigeek.top-zabbix.conf.php
Step 3.安装zabbix-proxy-mysql并将sql文件导入数据中::1 2 3 4 5 6 7 8 9 10 $ yum install -y zabbix-proxy-mysql 已安装: zabbix-proxy-mysql.x86_64 0:4.2.1-1.el7 mysql_db:zabbix_proxy mysql_user:zabbix_proxy mysql_pass:zabbix $zcat /usr/share/doc/zabbix-proxy-mysql-4.2.1/schema.sql.gz | mysql -h192.168.1.200 -u'zabbix_proxy' -p'zabbix' zabbix_porxy
weiyigeek.top-导入成功
Step 4.利用docker-compose构建zabbix-proxy环境1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 [root@Szabbix zabbix-proxy] .: -rw-r--r-- 1 root root 462 5月 15 15:55 docker-compose.yaml drwxr-xr-x 3 root root 51 5月 15 15:05 etc ./etc: drwxr-xr-x 2 root root 30 5月 15 15:03 enc -rw-r--r-- 1 root root 123 5月 15 15:05 env_mysql -rw-r--r-- 1 root root 205 5月 15 15:04 env_proxy ./etc/enc: -rw-r--r-- 1 root root 65 5月 15 14:29 zabbix_proxy.psk https://hub.docker.com/r/zabbix/zabbix-proxy-mysql $docker network create zabbixversion: "3.0" services: zabbix_proxy: image: zabbix/zabbix-proxy-mysql:alpine-4.2.1 container_name: zabbix_proxy ports: - "10055:10051" volumes: - /etc/localtime:/etc/localtime:ro - ./etc/enc:/var/lib/zabbix/enc env_file: - ./etc/env_proxy - ./etc/env_mysql networks: - zabbix networks: zabbix: external: true DB_SERVER_HOST=192.168.1.200 DB_SERVER_PORT=3306 MYSQL_DATABASE=zabbix_porxy MYSQL_USER=zabbix_proxy MYSQL_PASSWORD=zabbix ZBX_SERVER_HOST=192.168.1.200 ZBX_SERVER_PORT=10051 ZBX_TLSCONNECT=psk ZBX_TLSPSKIDENTITY=ZABBIXPROXY ZBX_TLSPSKFILE=zabbix_proxy.psk ZBX_HOSTNAME=ZabbixProxy ZBX_CONFIGFREQUENCY=60 ZBX_PROXYOFFLINEBUFFER=10 ZBX_TLSCONNECT=psk ZBX_TLSACCEPT=psk ZBX_DATASENDERFREQUENCY=30 ZBX_PROXYLOCALBUFFER=10 docker-compose -f docker-compose.yaml up -d docker-compose up docker-compose logs zabbix_proxy | 186:20190515:161355.527 proxy zabbix_proxy | 161:20190515:161455.932 received configuration data from server at "192.168.1.200" , datalen 3707
选择 管理 -> agent代理程序 -> 创建代理:
weiyigeek.top-zabbixproxy-hostname
进行zabbix-proxy加密设置:
weiyigeek.top-建立zabbix-proxy
Step 5. 接入主机到zabbix-proxy中:1 2 3 4 5 6 7 8 9 10 $egrep -v '^#' /etc/zabbix/zabbix_agentd.confServer=192.168.1.98 ListenPort=10050 ServerActive=192.168.1.98:10055 Hostname=proxy-agent [root@Master-data1 ~]$ service zabbix-agent start Starting Zabbix agent: [ OK ]
weiyigeek.top-zabbix-proxy访问成功
接入其他需要监控的主机到server中1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 注意:在机器数量较少的时候可以采用被动主动,当机器变多的时候建议agent采用采用主动发送,以减少server的负载; Server192.168.1.98 ListenPort=10050 ServerActive=192.168.1.98:10055 Hostname=Mzabbix Hostname=Szabbix Server192.168.56.102 ListenPort=10050 ServerActive=192.168.56.102:10055 Hostname=Windows7
step 6. Grafana应安装在zabbix-server服务器上并进行以下配置1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 $wget https://dl.grafana.com/oss/release/grafana-5.4.2-1.x86_64.rpm $sudo yum -y localinstall grafana-5.4.2-1.x86_64.rpm已安装: grafana.x86_64 0:5.4.2-1 $systemctl daemon-reload$systemctl start grafana-server$yum -y install fontconfig freetype* urw-fonts已安装: freetype-demos.x86_64 0:2.8-12.el7_6.1 freetype-devel.x86_64 0:2.8-12.el7_6.1 作为依赖被安装: libpng-devel.x86_64 2:1.5.13-7.el7_2 $yum -y install fontconfig freetype* urw-fonts $systemctl start grafana-server
启动Grafana后,在浏览器中输入http://ip:3000,可访问(3000为默认端口,账号密码都是admin/weiyigeek),登录成功后需要您更改默认密码,然后进行插件安装: 1 2 3 4 5 6 7 8 9 grafana-cli plugins list-remote grafana-cli plugins list-remote |grep zabbix grafana-cli plugins install alexanderzobnin-zabbix-app systemctl restart grafana-server
weiyigeek.top-grafana安装zabbix插件
Grafana的zabbix配置:1 2 3 4 <1> 点击zabbix图标,启用zabbix插件 <2> 点击Enable <3> 点击Add data source ,添加数据源 (首页) <4> 点击zabbix,添加zabbix插件的数据源
weiyigeek.top-数据源
<5> 配置数据源 Name为自定义,要可区分 URL为zabbix的API地址,默认为:http://zabbix-server的IP/zabbix/api_jsonrpc.php 选择ACCESS为Browser Username,Password为zabbix的用户及对应的密码(可在zabbix上设置用户) 注意选择zabbix version为你自己的版本 - 确认无误则保存;
weiyigeek.top-配置数据源
<7>指示板进行查看并导入
weiyigeek.top-
<8>如何不正正确的链接到zabbix需要查看本地是否能正常连接到zabbix以及,绑定的zabbix的mysql的
weiyigeek.top-绑定myqsl
<9>最后grafana数据展现大成
weiyigeek.top-
业务整体架构:
weiyigeek.top-zabbix架构