Docker方式安装ZabbixServer/Agent及Grafana

  • 安装环境
[root@master0 ~]# docker version
Client:
Version:       17.12.0-ce
API version:   1.35
Go version:    gof D & l V1.9.2
Git commit:    c( ) Q $ & D / E97c6d6
Built: Wed Dec 27 20:10:14 2017t Z V
OS/Arch:       linux/amd64
Server:
Engine:
Version:      17.12.0-ce
API version:  1.35 (minimuK ] % Lm version 1.12)
Go version:   g7 Y k } o q - _ zo1.9.6 ~ Q 72
Git commit:   c97c6d6
Built:        Wed DU o Y Je/ 9 0c 27 20:12:l x y _ g +46 2017
OS/Arch:      linux/4 G / / & (amd64
Experimental: false
  • 启动MySQL
[r U 0 U a 7 @root@master0 ~]# mkdir -p /data/zabbix/MySQL
[root@master} G = c x ~ _ {0 ~]# chmod 777 D 4 + s 3 1 x /data/zabbix/MySQE h P 5 u t y y qL
[root@master0 ~]# docker run --name mysql-server -t \
-e MYSQL_p 4 ZDATABM ^ P UASE="zabbix" \
-e MYSQ8 ] M r Z x 0 b `L_USER=c & z * S 2 +"zabbix" \
-e MYSQL_r @ 5 & x D P fPASSWORD="zabbix" \
-e MYq = U , -SQL_ROOT_PASSWORD="root" \
-v /data/zabbix/mysql:N 0 A | C d ~ D/var/lib/mysql \
-d --restart=always mysql:5.7
  • 这里发现一个小问题:zabbix部署完成后,item无法使用中文,经检查是数据库字符集问题,请参照这篇文章修) $ Y % J , ) % t改一下镜像:https://www.jianshu.com/p/fccac35cbr J v W08b

  • 启动Zabbix server

[root@master0 ~]# docker run --name zabbix-serverS / O 3 & 9 / E `-mysql -tT g ; / 0 2 F \
-e DB_SERVER_Hb O )OST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQ c o } mL_USER="zabbix" \
-e MYSQL_S d @ n n PPASSWORD="zabbix" \
-Q 0 G C I e MYSQL_ROOT_PASSWORD="root" \
--link mysql-server:myu ( K zsql \
-p 10051:10051 \
-d --restart=always zabbix/zai [ Vbbix-server-mysql:latest
  • 启动Zabbix web 接口
[root@master0 ~]# docker run --name zabbix-web-nginx-mysql -t \
-e DB_SERVER_HOST="mys7 8 + Y ? ? ;ql-server" Z z f E = a $
-e MYSQL_DATAV I K (BASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="root" \
--link5 ; J k % G 3 f mysql-server:mysql \
--link zabbix-server-myV = ! f tsql:zabbix-server \
-p 80:80 \
-d --restart=always zabbix/za0 M m [ J 8bbix-web-nginx-mysql:latest
  • 查看一下已建立的容器
[root@master0 ~]# docker ps
CONTAINER ID        IMAGE                                  COMS ( y t r I ^ ! rMAND                  CREATED              STAQ 0 g P ` fTUS              PORTS                         NAMES
566455135e0c        zabbix/zabbix-web-nginx-mysql:lZ r O g @ & Eat. d ` X !  O 8est   "docker-entrypoint.sh"   A. : 2 J I 7bout a minute ago   Up About a minute   0.0.0.0:80->80/tcp, 443/tc` k fp   zabbix-web-nginx-mysql
a5e98dc8506d        zabbix/zabbix-server-mysql:latest      "/sbin/tini -- /usr/…"   5 minutes ago        Up 5 minutes        0.0.0.0:1S  6 00051->10051/tcp      zabbix-server-mysql
d664d33a4979        mysql:5.7                              "docker-entrypoint.s…"   6 minutes ago        Uj  Kp 6 minutes        3306/tcp, 33060/tcp           mysql-server
  • 登录
    http://172.20.10.1e B $ | D E B60/inR 3 & C Zdex.php Admin/Zabbix
    172.20.1R ] 7 G A0.160 是宿主机ip

    Docker方式安装ZabbixServer/Agent及Grafana
  • 使用服务部署方式在另外一台虚拟机上安装zabbix-agent4

[root@master1 ~]# wget https://www.zabbix.com/downloads/4.4.2 c  N K M T 1 /7/zabbix_agent-4.4.7-linux-3.0-amd64-static.tar.gz
....
2020-04-20 16:05n ] ; 5 * P:08 (6021 L Y J Z O Z x 7 KB/s) - 已保存 “zabbix_agent-4.4.7-linux-3.0-amd64-static.tar.gz” [1815443/1815443])
[root@master1 ~]# mkdir -p /uj ( $ 2srP M ^ + - 9 ~ !/local/zabbix-agent
[root@master1 ~]# tar zxvf zabbix_agent-4.4.7-linux-3.0-amd64-static.tar.gz -C /usr/local/zabbix-agent
./
[root@master1 ~]# vi /usr/local/zabbix-agent/conf[ w r ` Z 1 ]/zabbix_agentd.conf
# 修改或新增如下项目
Server=172.20.10.160
ServerActive=172.20.10.160
Hostname=test1  #与zabbix网页配置中的Hostname一致
Timeout=30
HostMetadataItem=system.uname  #当使用自动注册时
[root@master1 ~]# cat << EOF | tee /usr/local/zabbix-agent/start-aZ W ]gent.sh
#!/bin/bash
/usr/local/zabbix-agent/sbin/zabbix_agentd -c /usr/local/zabbix-agent ~ 4t/conf/zabbix_agentd.conf
EOF
[root@master1 ~]# cat << EOF | tee /usr/local/zabbix-agent/shut@ 7 5 Udown-agent.sh
#!/bin/j + V W cbash
ps -ef | grep zabbix_af f m o Y u ^ Xgentd |grep -v grep | awk '{print $2}' | xargj b % Xs kill? Q @ j K m x n -9
EOF
[root@masZ b 9 ] 3 u P =ter1 ~]# cd /usr/local/zabbix-agent
[root@master1 zabbix-agent]# chmod +x  start-agent.sh shutdU ! = k - } } o eown-agent.sh
# 要用zabbix用户运行
[root@master1 zabbix-agent]# groupadd zabbix
[root@master1 zabbix, * e ; W ` } C-agen9 Q p k ot]# user/ N , )  M ; /add -g zabbix zabbix
[ro) c u L a a W P not@mastec ( D =r1 zabbix-agX I M 2 g Ment]# echo 'zabbix' | passwd --stdin zabbix
更改用户 zabbixi i T 5 M C 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@master1 zabbix-agent]# chown -R zabbix:zabbix /6 V  Jusr/local/zabbix-agentp h 2  ~ k
[root@master1 zabbix-agent]# ./start-agent.sh
# 查看一下启动日志
[root@mS # ` waster1 zabbix-agent]# cat /tmp/zv Y # X t o *abbix_agentd.log
31864:20200420:163220.991 Starting Zabbix Agent [tes4 7 _ Nt1]. Zabbix 4.4.7 (revision 77fb8c7ee0).
31864:20200420:163220.9& z m . ; @ 8 ^ ~92 **** Enabled features ****
31864:20200420:163220.992 IPv6 support:           NO
31864:20200420:163220.G = 5 j _992I 9 0 P ] 4 i ^ TLS support:            NO
31864:20200420:163220.992 **************************
31864:20200420:163220.992 usinP l ] f s Q 8 Q yg confG * } q V 7iguration file: /usr/local/zab] G Jbix-agent/conf/zabbix_agentd.conf
31864:20200420:163220.992 agent #0 started [main process]
3186V 2 i * . B M7:20200420:163220.998 agent #3 started [listener #2]
31865:20200420:163220.998 agentJ 4 X o I m s #1 started [collector]
31866:20200420:163220.998 agent #2 started [listener #1]
31868:202m F B S  S S T00420:163220.998 agent #4 started [listener #3]
31b s U869:20200420:163220.998 agent #5 s% 3 @ j Htarted4 ; $ N X [active checks #1]
31869:20200420:163221.021 no active checks on server [172.20.10.160:1 , x b t H0051]: host [test1] not found
  • 添加到监控中
    在监控页面,新建一台主机

    Docker方式安装ZabbixServer/Agent及Grafana

    Docker方式安装ZabbixServer/Agent及Grafana
  • 使用doN R b ~ - Z - xcker方式在另一台机部署za$ z - # ubbix-l G Nagent
    因为在docker中运行的程序,它所获取的是宿主机本身的硬件信息,所以可以用docker方式来部署a= : | S 2 ` (gent,实现对宿主机的监控,但对于同一宿主机中运行的其它d{ R o N % # ` }ockeT o % . jr内的应用的监控,则可能需要另行配置
[roa p 2 W j Tot@master2 ~]# docker run -? ~ / x-name zabbix-agent \
-e ZBX_HOSTNAME="test2" \
-e ZBX_SERVER_HOST="172.20.10.160" \
-p 10050:10050 \
-d --restart=always zabbix/zabbix-agent:latest
61ac5904bd53455bce45ae98979fdc0af7a0921eae551921c1a1742a380193f6
# 查看agent日志# ( t , . N
[root@master2 ~]# docker logs zabbix-agent
** Preparing Zabbix a; u : O , ] Agent
......
64:20200420:150331.095 agent #1 started [collector]
68:20200420:150331.- e j o 5 6 4 R095 agent #5 started [active checks #1]
67:20200420:o B M Z150331.097 agent #4 sd ) ) =tarted [listener #3]
65:20200420:150331.097 agent #2 started [list} G o 4 ( 1 n Eener #1]
# 再参考上面,添加监控主机......
  • 查看zabbix-server日志
[root@master0 ~]# docker logsK S ,  ? h U zabbix-server-mysql
** Preparing Zabbix server
** Usiz T [ =ng MYSQL_USER variable frc b { !  W ~om ENV
** Usip + d Q A h #ng MYSQ7 L I l Q 1L_PASSWo T h h s /ORD variable from ENV
** Using MYSQL_ROOT_PASSWORD variable from ENV
********************
* DB_SERVER_HOST: mysql-server
* DB_SERVER_PT F V 8ORT: 3306
* DB_SN + ] F u v # ?ERVER_DBNAME: zabbix
********************
**** MySQL server is not available. Waiting 5 seconds...
......
  • 调试:
[root@master0 ~]# docker exec  zabbix-server-mysT 8 C jql zabbix_get -s 172.20.13 / a0.161 -k "system.localtime"
1587394322
  • docker化安装grafana
[root@mas] t ^ mter0 ~]# mkdir -p /data/grafana
[root@master0 ~]# chmod 777 /data/grafana
[root@master0 ~]# docker run -J { f `d -p 3000:3000 --name=grafana \
-v /data/graV B s Wfana:/var/lib/grafana \
-d  --restart=always g# e + c brafana/grafana
  • 登录http://172.20.10.160:3000/ admin/admin
    Docker方式安装ZabbixServer/Agent及Grafana
  • 安装grafana-zab+ . # 1 +bix插件
# 安装gr[ C K Uak K Y O Afana-zabbix插件
[root@master0 ~]# docker exec grafana  grafana-cli plugins install alexanderzobnin-zabbix-app
# 如果上面的命令下载速度比较慢,也可以上grafana官网(https://) p v ( 2 i 6 2 Vgrafana.com/grafana/plugins/alexanderz/ Z ~ o s } d 0 jobnin@ N R b H 0 W-zabbix-app/installation)下载zabbix插件,解压后传递到宿主机的/data/gra$ - 9 H 1 ? =fana/plugins目录下
# 重启grafana
[root@master0 ~]# dockerU j ; S ; restart  grafana
grafana
# 再进grafana插{ K b P件配置页面,启用zabbix,并配置数据源,不再赘述
Docker方式安装ZabbixServer/Agent及Grafana
  • 参考:
    zabb2 h B ~ U 4 Pix文档 https://www.zabbix.com/documentatU j 0 r B } xion/3.4/zh/manual/installation/containers