手把手教你如何通过docker-compose部署Zabbix监控

Posted bixiaoyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手把手教你如何通过docker-compose部署Zabbix监控相关的知识,希望对你有一定的参考价值。

软件环境部署

技术图片

一、部署docker-ce版本以及pip工具,并安装docker-compose工具

#yum install -y docker-ce python36-setuptools  python36-pip 
#pip3 install docker-compose==1.23.2

二、编写docker-compose的yaml文件

version: 3.7
services:
 zabbix-server:
  image: docker-registry.hexun.com/system_ops/zabbix-server-mysql:centos-4.0-latest
  ports:
   - "10051:10051"
  volumes:
   - /etc/localtime:/etc/localtime:ro
   - /etc/timezone:/etc/timezone:ro
   - ./zbx_data/usr/lib/zabbix/alertscripts:/usr/lib/zabbix/alertscripts:ro
   - ./zbx_data/usr/lib/zabbix/externalscripts:/usr/lib/zabbix/externalscripts:ro
   - ./zbx_data/var/lib/zabbix/modules:/var/lib/zabbix/modules:ro
   - ./zbx_data/var/lib/zabbix/enc:/var/lib/zabbix/enc:ro
   - ./zbx_data/var/lib/zabbix/ssh_keys:/var/lib/zabbix/ssh_keys:ro
   - ./zbx_data/var/lib/zabbix/mibs:/var/lib/zabbix/mibs:ro
   - ./zbx_data/var/lib/zabbix/snmptraps:/var/lib/zabbix/snmptraps:ro
  links:
   - mysql-server:mysql-server
   - zabbix-java-gateway:zabbix-java-gateway
  ulimits:
   nproc: 65535
   nofile:
    soft: 20000
    hard: 40000
  deploy:
   resources:
    limits:
      cpus: 4
      memory: 3G
    reservations:
      cpus: 3
      memory: 2G
  env_file:
   - .env_db_mysql
   - .env_srv
  user: root
  depends_on:
   - mysql-server
   - zabbix-java-gateway
   - zabbix-snmptraps
  networks:
   zbx_net:
     aliases:
      - zabbix-server
      - zabbix-server-mysql
      - zabbix-server-centos-mysql
      - zabbix-server-mysql-centos
   zbx_net:
  stop_grace_period: 30s
  sysctls:
   - net.ipv4.ip_local_port_range=1024 65000
   - net.ipv4.conf.all.accept_redirects=0
   - net.ipv4.conf.all.secure_redirects=0
   - net.ipv4.conf.all.send_redirects=0
  labels:
   com.zabbix.description: "Zabbix server with MySQL database support"
   com.zabbix.company: "Zabbix SIA"
   com.zabbix.component: "zabbix-server"
   com.zabbix.dbtype: "mysql"
   com.zabbix.os: "centos"
 zabbix-web-nginx-mysql:
  image: docker-registry.hexun.com/system_ops/zabbix-web-nginx-mysql:centos-4.0-latest
  ports:
   - "80:80"
   - "443:443"
  links:
   - mysql-server:mysql-server
   - zabbix-server:zabbix-server
  volumes:
   - /etc/localtime:/etc/localtime:ro
   - /etc/timezone:/etc/timezone:ro
   - ./zbx_data/etc/ssl/nginx:/etc/ssl/nginx:ro
  deploy:
   resources:
    limits:
      cpus: 4
      memory: 3G
    reservations:
      cpus: 2
      memory: 2G
  env_file:
   - .env_db_mysql
   - .env_web
  user: root
  depends_on:
   - mysql-server
   - zabbix-server
  healthcheck:
   test: ["CMD", "curl", "-f", "http://localhost"]
   interval: 10s
   timeout: 5s
   retries: 3
   start_period: 30s
  networks:
   zbx_net:
    aliases:
     - zabbix-web-nginx-mysql
     - zabbix-web-nginx-centos-mysql
     - zabbix-web-nginx-mysql-centos
   zbx_net:
  stop_grace_period: 10s
  sysctls:
   - net.core.somaxconn=65535
  labels:
   com.zabbix.description: "Zabbix frontend on Nginx web-server with MySQL database support"
   com.zabbix.company: "Zabbix SIA"
   com.zabbix.component: "zabbix-frontend"
   com.zabbix.webserver: "nginx"
   com.zabbix.dbtype: "mysql"
   com.zabbix.os: "centos"
 zabbix-java-gateway:
  image: docker-registry.hexun.com/system_ops/zabbix-java-gateway:centos-4.0-latest
  ports:
   - "10052:10052"
  deploy:
   resources:
    limits:
      cpus: 0.5
      memory: 512M
    reservations:
      cpus: 0.25
      memory: 256M
  env_file:
   - .env_java
  user: root
  networks:
   zbx_net:
    aliases:
     - zabbix-java-gateway
     - zabbix-java-gateway-centos
  stop_grace_period: 5s
  labels:
   com.zabbix.description: "Zabbix Java Gateway"
   com.zabbix.company: "Zabbix SIA"
   com.zabbix.component: "java-gateway"
   com.zabbix.os: "centos"
 zabbix-snmptraps:
  image: docker-registry.hexun.com/system_ops/zabbix-snmptraps:centos-4.0-latest
  ports:
   - "162:162/udp"
  volumes:
   - ./zbx_data/var/lib/zabbix/snmptraps:/var/lib/zabbix/snmptraps:rw
  deploy:
   resources:
    limits:
      cpus: 0.5
      memory: 256M
    reservations:
      cpus: 0.25
      memory: 128M
  user: root
  networks:
   zbx_net:
    aliases:
     - zabbix-snmptraps
   zbx_net:
  stop_grace_period: 5s
  labels:
   com.zabbix.description: "Zabbix snmptraps"
   com.zabbix.company: "Zabbix SIA"
   com.zabbix.component: "snmptraps"
   com.zabbix.os: "ubuntu"
 mysql-server:
  image: docker-registry.hexun.com/system_ops/mysql:5.7.26
  command: [mysqld, --character-set-server=utf8, --collation-server=utf8_bin, --default-authentication-plugin=mysql_native_password]
  volumes:
   - ./zbx_data/var/lib/mysql:/var/lib/mysql:rw
  env_file:
   - .env_db_mysql
  deploy:
   resources:
    limits:
      cpus: 4
      memory: 3G
    reservations:
      cpus: 3
      memory: 2G
  ports:
   - "3306:3306"
  user: root
  stop_grace_period: 1m
  networks:
   zbx_net:
    aliases:
     - mysql-server
     - zabbix-database
     - mysql-database
 db_data_mysql:
  image: busybox
  volumes:
   - ./zbx_data/var/lib/mysql:/var/lib/mysql:rw
networks:
  zbx_net:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "false"

三、编写各个容器对应的env_file环境变量文件

3.1:编写mysql对应env_file文件

#vim /data/zabbix-system/zabbix-apps/.env_db_mysql 

# DB_SERVER_HOST=mysql-server
# DB_SERVER_PORT=3306
# MYSQL_USER=zabbix
MYSQL_USER=zabbix
# MYSQL_PASSWORD=zabbix
MYSQL_PASSWORD=4Z8tQ5hbXIxR4be
# MYSQL_ROOT_PASSWORD=
MYSQL_ROOT_PASSWORD=4Z8tQ5hbXIxR4be
# MYSQL_ALLOW_EMPTY_PASSWORD=false
# MYSQL_DATABASE=zabbix
MYSQL_DATABASE=zabbix

#增大最大连接数
#show variables like max_connections;(查可以看当前的最大连接数)
#
# set global max_connections=1000;
# max_connections=1500
# innodb_buffer_pool_instances=5
# innodb_buffer_pool_size=6G
# key_buffer_size=6G

 3.2: 设置zabbix-server容器的env_file环境变量

# vim /data/zabbix-system/zabbix-apps/.env_srv

ZBX_LISTENIP=127.0.0.1
ZBX_STARTPOLLERS=30
ZBX_IPMIPOLLERS=1
ZBX_STARTPREPROCESSORS=30
ZBX_STARTPOLLERSUNREACHABLE=30
ZBX_STARTTRAPPERS=30
ZBX_STARTPINGERS=20
ZBX_STARTDISCOVERERS=20
ZBX_STARTHTTPPOLLERS=20
ZBX_STARTTIMERS=20
ZBX_STARTESCALATORS=30
ZBX_STARTALERTERS=20
ZBX_JAVAGATEWAY_ENABLE=true
ZBX_JAVAGATEWAY=zabbix-java-gateway
ZBX_JAVAGATEWAYPORT=10052
ZBX_STARTJAVAPOLLERS=1
ZBX_STARTVMWARECOLLECTORS=1
ZBX_VMWAREFREQUENCY=50
ZBX_VMWAREPERFFREQUENCY=60
ZBX_VMWARECACHESIZE=80M
ZBX_VMWARETIMEOUT=10
ZBX_ENABLE_SNMP_TRAPS=true
ZBX_HOUSEKEEPINGFREQUENCY=6
ZBX_MAXHOUSEKEEPERDELETE=200000
ZBX_SENDERFREQUENCY=30
ZBX_CACHESIZE=4024M
ZBX_CACHEUPDATEFREQUENCY=60
ZBX_STARTDBSYNCERS=30
ZBX_HISTORYCACHESIZE=2048M
ZBX_HISTORYINDEXCACHESIZE=888M
ZBX_TRENDCACHESIZE=888M
ZBX_VALUECACHESIZE=4096M
ZBX_TIMEOUT=30
ZBX_TRAPPERIMEOUT=300
ZBX_UNREACHABLEPERIOD=45
ZBX_UNAVAILABLEDELAY=60
ZBX_UNREACHABLEDELAY=1
ZBX_LOGSLOWQUERIES=3000
ZBX_STARTPROXYPOLLERS=20
ZBX_PROXYCONFIGFREQUENCY=600
ZBX_PROXYDATAFREQUENCY=1

3.3:设置Nginx-web容器的env_file环境变量

#vim /data/zabbix-system/zabbix-apps/.env_web

ZBX_SERVER_HOST=192.168.60.100
ZBX_SERVER_PORT=10051
ZBX_SERVER_NAME=tjdl-zabix-server
ZBX_MAXEXECUTIONTIME=600
ZBX_MEMORYLIMIT=512M
ZBX_POSTMAXSIZE=50M
ZBX_UPLOADMAXFILESIZE=2M
ZBX_MAXINPUTTIME=300
ZBX_SESSION_NAME=zbx_sessionid
php_TZ=Asia/Shanghai

3.4: 设置java-gateway容器环境变量

# vim /data/zabbix-system/zabbix-apps/.env_java

ZBX_START_POLLERS=5
ZBX_TIMEOUT=3
# Possible values: trace, debug, info, want, error, all, off
ZBX_DEBUGLEVEL=info

以上编写好yaml文件以及环境变量,可尝试重启各个容器服务组件,

[root@hostname-60100 zabbix-apps]#docker-compose -f zabbix-apps-compose.yaml up -d#创建容器并启动
WARNING: Some services (mysql-server, zabbix-java-gateway, zabbix-server, zabbix-snmptraps, zabbix-web-nginx-mysql) use the deploy key, which will be ignored. Compose does not support deploy configuration - use `docker stack deploy` to deploy to a swarm.
Restarting zabbix-apps_zabbix-web-nginx-mysql_1 ... done
Restarting zabbix-apps_zabbix-server_1          ... done
Restarting zabbix-apps_mysql-server_1           ... done
Restarting zabbix-apps_zabbix-snmptraps_1       ... done
Restarting zabbix-apps_db_data_mysql_1          ... done
Restarting zabbix-apps_zabbix-java-gateway_1    ... done

检查一下docker容器是否都正常启动,如果有容器无法正常启动,可查看某个容器日志进行排错,例如下面的zabbix-server没有启动,为退出状态

技术图片

报错:connenction to database ‘zabbix‘ failed: too many connections

 docker-compose -f zabbix-apps-compose.yaml logs -f  zabbix-server  #查看容器日志

技术图片

# docker exec -it zabbix-apps_mysql-server_1 bash  可进入mysql容器设置默认最大连接数

root@fdbebd4882df:/# egrep -v "#|^$" etc/mysql/mysql.conf.d/mysqld.cnf  #增加max_connections=1000参数,修改配置文件之后需重启mysql服务,因为mysql已经容器化,只需要重启重启即可生效
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
symbolic-links=0
max_connections=1000

Ps:还有一种方法可以在mysql命令行中设置最大连接数,但只是临时生效(set global max_connections=1000;)

mysql> show variables like ‘max_connections‘;  #查看mysql最大连接数;
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 1000 |
+-----------------+-------+
1 row in set (0.00 sec)

最后再次查看zabbix-server容器日志,已经没有异常信息提示

#docker-compose -f zabbix-apps-compose.yaml logs -ft  zabbix-server  

 四:进入lnmp容器修改php对应的配置文件

 #docker exec -it zabbix-apps_zabbix-web-nginx-mysql_1 bash

#cat  /usr/share/zabbix/conf/zabbix.conf.php   #注意这里的php配置文件一定要与前面之前编写的.env_srv环境内容对应
<?php
// Zabbix GUI configuration file.
global $DB, $HISTORY;

$DB[TYPE]     = MYSQL;
$DB[SERVER]   = mysql-server;
$DB[PORT]     = 3306;
$DB[DATABASE] = zabbix;
$DB[USER]     = zabbix;
$DB[PASSWORD] = 4Z8tQ5hbXIxR4be;

// Schema name. Used for IBM DB2 and PostgreSQL.
$DB[SCHEMA] = ‘‘;

$ZBX_SERVER      = 192.168.60.100;
$ZBX_SERVER_PORT = 10051;
$ZBX_SERVER_NAME = tjdl-zabix-server;

$IMAGE_FORMAT_DEFAULT    = IMAGE_FORMAT_PNG;

// Elasticsearch url (can be string if same url is used for all types).
$HISTORY[url]   = ‘‘;
// Value types stored in Elasticsearch.
$HISTORY[types] = [];

当一切配置修改完毕之后,再次通过docker-compose启动各个容器组件,确保每个配置均已生效

 docker-compose -f zabbix-apps-compose.yaml restart

技术图片

 访问zabbix监控url:http://192.168.60.100/zabbix.php?action=dashboard.view

五、更新本地agent端配置:

 #docker inspect zabbix-apps_zabbix-server_1 | grep -i ipaddr  #查看zabbix-server容器的IP地址,此IP用于agent配置文件中的server地址
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "172.18.0.2",  

agent配置文件如下:

[root@hostname-60100 zabbix-apps]# egrep -v "#|^$" /etc/zabbix/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=172.18.0.2
ListenIP=192.168.60.100
Include=/etc/zabbix/zabbix_agentd.d/*.conf1

#systemctl restart zabbix-agent #重启zabbix-agent

如果agent端监控成功,那么zabbixserver端获取的图形式乱码状态,需要中文乱码问题

技术图片

  #docker cp DejaVuSans.ttf zabbix-apps_zabbix-web-nginx-mysql_1:/usr/share/zabbix/assets/fonts/  #将windows下的中文字体拷贝到web容器相应的目录中,最后重启容器刷新即可

  #docker-compose -f zabbix-apps-compose.yaml restart zabbix-web-nginx-mysql

技术图片

 END!

以上是关于手把手教你如何通过docker-compose部署Zabbix监控的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你通过 Docker 部署前后端分离项目(亲测可用)

手把手教你LNMP和Zabbix部署搭建

手把手教你创建Azure ARM Template

手把手教你安装SSL证书升级https

一个成熟的前端项目是如何诞生的?手把手教你从创建项目到自动化部署!

一个成熟的前端项目是如何诞生的?手把手教你从创建项目到自动化部署!