安装数据库
安装mariadb,略。参考https://yanyong.cc/post/docker-example-list/#mariadb
安装zabbix
参考https://www.zabbix.com/documentation/current/manual/installation/containers
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
| sudo docker network create --subnet 172.19.0.0/16 --gateway 172.19.0.1 zabbix-net
sudo docker run --name zabbix-java-gateway \
--network zabbix-net \
--ip 172.19.0.2 \
--restart unless-stopped \
-d zabbix/zabbix-java-gateway:alpine-5.0-latest
sudo docker run --name zabbix-server-mysql \
-e DB_SERVER_HOST="172.17.0.1" \
-e DB_SERVER_PORT="3306" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="1234" \
-e MYSQL_ROOT_PASSWORD="123456" \
-e ZBX_JAVAGATEWAY_ENABLE="true" \
-e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
-e ZBX_JAVAGATEWAYPORT="10052" \
-e ZBX_STARTJAVAPOLLERS="5" \
-v /data/zabbix/alertscripts:/usr/lib/zabbix/alertscripts \
-v /data/zabbix/externalscripts:/usr/lib/zabbix/externalscripts \
--network zabbix-net \
--ip 172.19.0.3 \
-p 10051:10051 \
-e TZ='Asia/Shanghai' \
--restart unless-stopped \
-d zabbix/zabbix-server-mysql:alpine-5.0-latest
sudo docker run --name zabbix-web-nginx-mysql \
-e ZBX_SERVER_HOST="zabbix-server-mysql" \
-e ZBX_SERVER_PORT="10051" \
-e DB_SERVER_HOST="172.17.0.1" \
-e DB_SERVER_PORT="3306" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="1234" \
-e PHP_TZ="Asia/Shanghai" \
--network zabbix-net \
--ip 172.19.0.4 \
-p 8080:8080 \
--restart unless-stopped \
-d zabbix/zabbix-web-nginx-mysql:alpine-5.0-latest
|
如果配置MYSQL_ROOT_PASSWORD则会尝试创建用户和数据库,也可以手动创建用和数据库则无需配置MYSQL_ROOT_PASSWORD
1
2
3
| mysql -h 127.0.0.1 -P 3306 -u root -p'123456' -e "create user zabbix@'%' identified by '1234';"~~
mysql -h 127.0.0.1 -P 3306 -u root -p'123456' -e "create database zabbix character set utf8 collate utf8_bin;"
mysql -h 127.0.0.1 -P 3306 -u root -p'123456' -e "grant all on zabbix.* to zabbix@'%';flush privileges;"
|
安装zabbix agent
zabbix agent部署在被监控目标上,zabbix server主机上也要安装zabbix agent因为主机也需要被监控,不建议容器安装zabbix agent
zabbix agent版本最好与zabbix server版本一致
zabbix agent下载地址
下载并解压
1
2
3
4
5
| mkdir ~/zabbix && cd ~/zabbix
wget https://cdn.zabbix.com/zabbix/binaries/stable/5.0/5.0.11/zabbix_agent-5.0.11-linux-3.0-amd64-static.tar.gz
tar zxf zabbix_agent-5.0.11-linux-3.0-amd64-static.tar.gz
|
修改并查看agent配置文件。zabbix server容器的地址为172.19.0.3,主机地址为10.20.70.113,Server
为允许访问agent的主机,ServerActive
配置后启用主动模式,以下示例为zabbix server上安装的agent配置文件,主动模式时Hostname
必须与server端创建主机时的名字一致,配置文件官方文档
1
2
3
| sed -i 's/^Server=.*/Server=172.19.0.3/' ~/zabbix/conf/zabbix_agentd.conf
sed -i 's/^ServerActive=.*/ServerActive=10.20.70.113:10051/' ~/zabbix/conf/zabbix_agentd.conf # 可选项
sed '/^#\|^ *$/d' ~/zabbix/conf/zabbix_agentd.conf # 显示非注释且非空行
|
LogFile=/tmp/zabbix_agentd.log
Server=172.19.0.3
ServerActive=10.20.70.113:10051
Hostname=Zabbix server
启动zabbix agent,默认监听端口10050
1
| ~/zabbix/sbin/zabbix_agentd -c ~/zabbix/conf/zabbix_agentd.conf
|
关闭zabbix agent
1
| ps -ef|awk '/zabbix_agent[d] -c/{print $2}'|xargs -i kill {}
|
可选步骤,开启防火墙
1
| sudo iptables -A INPUT -s 172.19.0.0/16 -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT
|
1
| sudo iptables -A INPUT -s 10.20.70.113 -p tcp -m state --state NEW -m tcp --dport 10050 -j ACCEPT
|
可选步骤,加入开机自启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #echo "sudo -u $USER $HOME/zabbix/sbin/zabbix_agentd -c $HOME/zabbix/conf/zabbix_agentd.conf" | sudo tee -a /etc/rc.local
#sudo chmod +x /etc/rc.local
#2022.6.22更新
##Debian系统
sudo tee /etc/rc.local << EOF
#!/bin/bash -e
sudo -u $USER $HOME/zabbix/sbin/zabbix_agentd -c $HOME/zabbix/conf/zabbix_agentd.conf
exit 0
EOF
sudo chmod +x /etc/rc.local
sudo systemctl restart rc-local
##CentOS系统,开头也必须指定#!/bin/bash,可以不用exit 0
echo "sudo -u $USER $HOME/zabbix/sbin/zabbix_agentd -c $HOME/zabbix/conf/zabbix_agentd.conf" | sudo tee -a /etc/rc.local
sudo chmod +x /etc/rc.local
sudo systemctl restart rc-local
|
监控zabbix server主机
主机IP:10.20.70.113,访问http://10.20.70.113:8080
,nginx反向代理配置一个域名访问也可以,默认用户名密码:Admin/zabbix
默认已经添加zabbix server主机(agent被动模式),因为zabbix server是容器安装的,所以默认的127.0.0.1:10050是找不到zabbix agent的,需要改成主机地址10.20.70.113或docker网桥地址172.17.0.1