- 安装环境
[root@master0 ~]# docker version
Client:
Version: 17.12.0-ce
API version: 1.35
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:10:14 2017
OS/Arch: linux/amd64
Server:
Engine:
Version: 17.12.0-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:12:46 2017
OS/Arch: linux/amd64
Experimental: false
- 启动MySQL
[root@master0 ~]# mkdir -p /data/zabbix/MySQL
[root@master0 ~]# chmod 777 /data/zabbix/MySQL
[root@master0 ~]# docker run --name mysql-server -t \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="root" \
-v /data/zabbix/mysql:/var/lib/mysql \
-d --restart=always mysql:5.7
这里发现一个小问题:zabbix部署完成后,item无法使用中文,经检查是数据库字符集问题,请参照这篇文章修改一下镜像:https://www.jianshu.com/p/fccac35cb08b
启动Zabbix server
[root@master0 ~]# docker run --name zabbix-server-mysql -t \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="root" \
--link mysql-server:mysql \
-p 10051:10051 \
-d --restart=always zabbix/zabbix-server-mysql:latest
- 启动Zabbix web 接口
[root@master0 ~]# docker run --name zabbix-web-nginx-mysql -t \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="root" \
--link mysql-server:mysql \
--link zabbix-server-mysql:zabbix-server \
-p 80:80 \
-d --restart=always zabbix/zabbix-web-nginx-mysql:latest
- 查看一下已建立的容器
[root@master0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
566455135e0c zabbix/zabbix-web-nginx-mysql:latest "docker-entrypoint.sh" About a minute ago Up About a minute 0.0.0.0:80->80/tcp, 443/tcp zabbix-web-nginx-mysql
a5e98dc8506d zabbix/zabbix-server-mysql:latest "/sbin/tini -- /usr/…" 5 minutes ago Up 5 minutes 0.0.0.0:10051->10051/tcp zabbix-server-mysql
d664d33a4979 mysql:5.7 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 3306/tcp, 33060/tcp mysql-server
-
登录
http://172.20.10.160/index.php Admin/Zabbix
172.20.10.160 是宿主机ip
使用服务部署方式在另外一台虚拟机上安装zabbix-agent4
[root@master1 ~]# wget https://www.zabbix.com/downloads/4.4.7/zabbix_agent-4.4.7-linux-3.0-amd64-static.tar.gz
....
2020-04-20 16:05:08 (602 KB/s) - 已保存 “zabbix_agent-4.4.7-linux-3.0-amd64-static.tar.gz” [1815443/1815443])
[root@master1 ~]# mkdir -p /usr/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/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-agent.sh
#!/bin/bash
/usr/local/zabbix-agent/sbin/zabbix_agentd -c /usr/local/zabbix-agent/conf/zabbix_agentd.conf
EOF
[root@master1 ~]# cat << EOF | tee /usr/local/zabbix-agent/shutdown-agent.sh
#!/bin/bash
ps -ef | grep zabbix_agentd |grep -v grep | awk '{print $2}' | xargs kill -9
EOF
[root@master1 ~]# cd /usr/local/zabbix-agent
[root@master1 zabbix-agent]# chmod +x start-agent.sh shutdown-agent.sh
# 要用zabbix用户运行
[root@master1 zabbix-agent]# groupadd zabbix
[root@master1 zabbix-agent]# useradd -g zabbix zabbix
[root@master1 zabbix-agent]# echo 'zabbix' | passwd --stdin zabbix
更改用户 zabbix 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@master1 zabbix-agent]# chown -R zabbix:zabbix /usr/local/zabbix-agent
[root@master1 zabbix-agent]# ./start-agent.sh
# 查看一下启动日志
[root@master1 zabbix-agent]# cat /tmp/zabbix_agentd.log
31864:20200420:163220.991 Starting Zabbix Agent [test1]. Zabbix 4.4.7 (revision 77fb8c7ee0).
31864:20200420:163220.992 **** Enabled features ****
31864:20200420:163220.992 IPv6 support: NO
31864:20200420:163220.992 TLS support: NO
31864:20200420:163220.992 **************************
31864:20200420:163220.992 using configuration file: /usr/local/zabbix-agent/conf/zabbix_agentd.conf
31864:20200420:163220.992 agent #0 started [main process]
31867:20200420:163220.998 agent #3 started [listener #2]
31865:20200420:163220.998 agent #1 started [collector]
31866:20200420:163220.998 agent #2 started [listener #1]
31868:20200420:163220.998 agent #4 started [listener #3]
31869:20200420:163220.998 agent #5 started [active checks #1]
31869:20200420:163221.021 no active checks on server [172.20.10.160:10051]: host [test1] not found
-
添加到监控中
在监控页面,新建一台主机
- 使用docker方式在另一台机部署zabbix-agent
因为在docker中运行的程序,它所获取的是宿主机本身的硬件信息,所以可以用docker方式来部署agent,实现对宿主机的监控,但对于同一宿主机中运行的其它docker内的应用的监控,则可能需要另行配置
[root@master2 ~]# docker run --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日志
[root@master2 ~]# docker logs zabbix-agent
** Preparing Zabbix agent
......
64:20200420:150331.095 agent #1 started [collector]
68:20200420:150331.095 agent #5 started [active checks #1]
67:20200420:150331.097 agent #4 started [listener #3]
65:20200420:150331.097 agent #2 started [listener #1]
# 再参考上面,添加监控主机......
- 查看zabbix-server日志
[root@master0 ~]# docker logs zabbix-server-mysql
** Preparing Zabbix server
** Using MYSQL_USER variable from ENV
** Using MYSQL_PASSWORD variable from ENV
** Using MYSQL_ROOT_PASSWORD variable from ENV
********************
* DB_SERVER_HOST: mysql-server
* DB_SERVER_PORT: 3306
* DB_SERVER_DBNAME: zabbix
********************
**** MySQL server is not available. Waiting 5 seconds...
......
- 调试:
[root@master0 ~]# docker exec zabbix-server-mysql zabbix_get -s 172.20.10.161 -k "system.localtime"
1587394322
- docker化安装grafana
[root@master0 ~]# mkdir -p /data/grafana
[root@master0 ~]# chmod 777 /data/grafana
[root@master0 ~]# docker run -d -p 3000:3000 --name=grafana \
-v /data/grafana:/var/lib/grafana \
-d --restart=always grafana/grafana
- 登录http://172.20.10.160:3000/ admin/admin
- 安装grafana-zabbix插件
# 安装grafana-zabbix插件
[root@master0 ~]# docker exec grafana grafana-cli plugins install alexanderzobnin-zabbix-app
# 如果上面的命令下载速度比较慢,也可以上grafana官网(https://grafana.com/grafana/plugins/alexanderzobnin-zabbix-app/installation)下载zabbix插件,解压后传递到宿主机的/data/grafana/plugins目录下
# 重启grafana
[root@master0 ~]# docker restart grafana
grafana
# 再进grafana插件配置页面,启用zabbix,并配置数据源,不再赘述
- 参考:
zabbix文档 https://www.zabbix.com/documentation/3.4/zh/manual/installation/containers