实验环境

[root@zabbix_server ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

一、Docker

1、安装

[root@zabbix_server ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@zabbix_server ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@zabbix_server ~]# yum install -y docker-ce docker-ce-cli containerd.io

2、启动

[root@zabbix_server ~]# systemctl start docker
[root@zabbix_server ~]# systemctl enable  docker

二、Docker安装zabbix的两种方式

Zabbix官网手册:https://www.zabbix.com/documentation/4.0/zh/manual/installation/containers

1、直接使用docker run示例,官网提供了三个不同示例


2、使用docker-compose命令运行yaml脚本,本文主要也是说明这个。这个命令将会为每个 Zabbix 组件下载最新的 Zabbix 4.0 镜像,并以 detach 模式运行

三、Docker Compose

GitHub官网:https://github.com/docker/compose/releases

1、安装docker-compose

[root@zabbix_server ~]# curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose   
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0    495      0 --:--:--  0:00:01 --:--:--   496
100 15.4M  100 15.4M    0     0   177k      0  0:01:28  0:01:28 --:--:--  115k

#给二进制文件执行权限
[root@zabbix_server ~]# chmod +x /usr/local/bin/docker-compose

#查看版本
[root@zabbix_server ~]# docker-compose --version
docker-compose version 1.24.1, build 4667896b

2、GitHub下载脚本,这里选择了master节点下载

3、创建一个存放目录,把下载所有以点开头的环境变量文件和一个yaml文件上传到zabbix_server服务器(因为实验系统是centos,所以选择了docker-compose_v3_centos_mysql_latest.yaml,根据需求参考)

[root@zabbix_server ~]# mkdir  zabbix
[root@zabbix_server ~]# cd zabbix/

4、docker-compose_v3_centos_mysql_latest.yaml还可以根据需求进一步优化

本文选择安装:zabbix-server、zabbix-web-apache-mysql、zabbix-agent、zabbix-snmptraps、mysql-server

注意:在每个基础镜像下添加restart: always,要不容器退出之后无法自动启动

[root@zabbix_server zabbix]# vim docker-compose_v3_centos_mysql_latest.yaml
version: '3.5'
services:
 zabbix-server:
  image: zabbix/zabbix-server-mysql:centos-latest	#基础镜像
  restart: always	#指定容器退出后的重启策略为始终重启。该命令对保持服务始终运行十分有效,在生产环境中推荐配置为always或者unless-stopped。
  ports:
   - "10051:10051"	#暴露端口
  volumes:
   - /etc/localtime:/etc/localtime:ro	#时间,只读模式
   - /etc/timezone:/etc/timezone:ro 	#时区,只读模式
   - ./zbx_env/usr/lib/zabbix/alertscripts:/usr/lib/zabbix/alertscripts:ro	#映射数据卷
   - ./zbx_env/usr/lib/zabbix/externalscripts:/usr/lib/zabbix/externalscripts:ro
   - ./zbx_env/var/lib/zabbix/modules:/var/lib/zabbix/modules:ro
   - ./zbx_env/var/lib/zabbix/enc:/var/lib/zabbix/enc:ro
   - ./zbx_env/var/lib/zabbix/ssh_keys:/var/lib/zabbix/ssh_keys:ro
   - ./zbx_env/var/lib/zabbix/mibs:/var/lib/zabbix/mibs:ro
   - ./zbx_env/var/lib/zabbix/snmptraps:/var/lib/zabbix/snmptraps:ro
  links:		#容器之间建立互联(将删除,不推荐使用)
   - mysql-server:mysql-server	#第一个mysql-server是要链接的容器名称,第二个mysql-server是别名
  ulimits:		#指定容器的 ulimits 限制值
   nproc: 65535
   nofile:
    soft: 20000
    hard: 40000
  deploy:		#仅用于Swarm mode
   resources:
    limits:
      cpus: '0.70'
      memory: 1G
    reservations:
      cpus: '0.5'
      memory: 512M
  env_file:			#从文件中获取环境变量,可以为单独的文件路径或列表
   - .env_db_mysql	#这些就是刚才上传.开头的隐藏文件
   - .env_srv
  secrets:			#存储敏感数据,例如 mysql 服务密码
   - MYSQL_USER
   - MYSQL_PASSWORD
   - MYSQL_ROOT_PASSWORD
  user: root
  depends_on:		#解决容器的依赖、启动先后的问题。以下会先启动mysql-server和zabbix-snmptraps,再启动zabbix-server
   - mysql-server
   - zabbix-snmptraps
  networks:			#配置容器连接的网络
   zbx_net_backend:
     aliases:
      - zabbix-server
      - zabbix-server-mysql
      - zabbix-server-centos-mysql
      - zabbix-server-mysql-centos
   zbx_net_frontend:
#  devices:
#   - "/dev/ttyUSB0:/dev/ttyUSB0"
  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:			#为容器添加 Docker 元数据(metadata)信息。例如可以为容器添加辅助说明信息
   com.zabbix.description: "Zabbix server with MySQL database support"
   com.zabbix.company: "Zabbix LLC"
   com.zabbix.component: "zabbix-server"
   com.zabbix.dbtype: "mysql"
   com.zabbix.os: "centos"

 zabbix-web-apache-mysql:
  image: zabbix/zabbix-web-apache-mysql:centos-latest
  restart: always
  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_env/etc/ssl/apache2:/etc/ssl/apache2:ro
  deploy:
   resources:
    limits:
      cpus: '0.70'
      memory: 512M
    reservations:
      cpus: '0.5'
      memory: 256M
  env_file:
   - .env_db_mysql
   - .env_web
  secrets:
   - MYSQL_USER
   - MYSQL_PASSWORD
  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_backend:
    aliases:
     - zabbix-web-apache-mysql
     - zabbix-web-apache-centos-mysql
     - zabbix-web-apache-mysql-centos
   zbx_net_frontend:
  stop_grace_period: 10s
  sysctls:
   - net.core.somaxconn=65535
  labels:
   com.zabbix.description: "Zabbix frontend on Apache web-server with MySQL database support"
   com.zabbix.company: "Zabbix LLC"
   com.zabbix.component: "zabbix-frontend"
   com.zabbix.webserver: "apache2"
   com.zabbix.dbtype: "mysql"
   com.zabbix.os: "centos"

 zabbix-agent:
  image: zabbix/zabbix-agent:centos-latest
  restart: always
  ports:
   - "10050:10050"
  volumes:
   - /etc/localtime:/etc/localtime:ro
   - /etc/timezone:/etc/timezone:ro
   - ./zbx_env/etc/zabbix/zabbix_agentd.d:/etc/zabbix/zabbix_agentd.d:ro
   - ./zbx_env/var/lib/zabbix/modules:/var/lib/zabbix/modules:ro
   - ./zbx_env/var/lib/zabbix/enc:/var/lib/zabbix/enc:ro
   - ./zbx_env/var/lib/zabbix/ssh_keys:/var/lib/zabbix/ssh_keys:ro
  links:
   - zabbix-server:zabbix-server
  deploy:
   resources:
    limits:
      cpus: '0.2'
      memory: 128M
    reservations:
      cpus: '0.1'
      memory: 64M
   mode: global
  env_file:
   - .env_agent
  user: root
  privileged: true
  pid: "host"
  networks:
   zbx_net_backend:
    aliases:
     - zabbix-agent
     - zabbix-agent-passive
     - zabbix-agent-centos
  stop_grace_period: 5s
  labels:
   com.zabbix.description: "Zabbix agent"
   com.zabbix.company: "Zabbix LLC"
   com.zabbix.component: "zabbix-agentd"
   com.zabbix.os: "centos"
 zabbix-snmptraps:
  image: zabbix/zabbix-snmptraps:centos-latest
  ports:
   - "162:162/udp"
  volumes:
   - ./zbx_env/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_frontend:
    aliases:
     - zabbix-snmptraps
   zbx_net_backend:
  stop_grace_period: 5s
  labels:
   com.zabbix.description: "Zabbix snmptraps"
   com.zabbix.company: "Zabbix LLC"
   com.zabbix.component: "snmptraps"
   com.zabbix.os: "ubuntu"

 mysql-server:
  image: mysql:5.7
  restart: always
  command: [mysqld, --character-set-server=utf8, --collation-server=utf8_bin]
  volumes:
   - ./zbx_env/var/lib/mysql:/var/lib/mysql:rw
  env_file:
   - .env_db_mysql
  secrets:
   - MYSQL_USER
   - MYSQL_PASSWORD
   - MYSQL_ROOT_PASSWORD
  user: root
  stop_grace_period: 1m
  networks:
   zbx_net_backend:
    aliases:
     - mysql-server
     - zabbix-database
     - mysql-database

networks:
  zbx_net_frontend:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "false"
    ipam:
      driver: default
      config:
      - subnet: 172.16.238.0/24
  zbx_net_backend:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "false"
    internal: true
    ipam:
      driver: default
      config:
      - subnet: 172.16.239.0/24

secrets:
  MYSQL_USER:
    file: ./.MYSQL_USER
  MYSQL_PASSWORD:
    file: ./.MYSQL_PASSWORD
  MYSQL_ROOT_PASSWORD:
    file: ./.MYSQL_ROOT_PASSWORD

5、安装zabbix

因为是联网下载镜像所以会很慢。推荐搭建harbor镜像仓库,然后通过内网去下载

[root@zabbix_server zabbix]# docker-compose -f ./docker-compose_v3_centos_mysql_latest.yaml  up -d

6、查看容器

[root@zabbix_server zabbix]# docker ps -a
CONTAINER ID        IMAGE                                          COMMAND                  CREATED             STATUS                    PORTS                                      NAMES
6499dc6b25a7        zabbix/zabbix-web-apache-mysql:centos-latest   "docker-entrypoint.sh"   16 minutes ago      Up 16 minutes (healthy)   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   zabbix_zabbix-web-apache-mysql_1
9ce1cb810e49        zabbix/zabbix-agent:centos-latest              "/sbin/tini -- /usr/…"   16 minutes ago      Up 16 minutes                                                        zabbix_zabbix-agent_1
84bd8b1b6bcd        zabbix/zabbix-server-mysql:centos-latest       "/sbin/tini -- /usr/…"   16 minutes ago      Up 16 minutes             0.0.0.0:10051->10051/tcp                   zabbix_zabbix-server_1
af9a34a8172b        mysql:5.7                                      "docker-entrypoint.s…"   16 minutes ago      Up 16 minutes                                                        zabbix_mysql-server_1
9204a93d9f76        zabbix/zabbix-snmptraps:centos-latest          "/usr/bin/supervisor…"   16 minutes ago      Up 16 minutes             0.0.0.0:162->162/udp                       zabbix_zabbix-snmptraps_1

四、解决Zabbix agent is not available

1、浏览器登录http://192.168.14.216 如果无法登录,先关闭主机防火墙

2、接口IP不能是127.0.0.1,127.0.0.1是宿主机的。

3、查看zabbix_zabbix-agent_1容器的IP地址

4、点击进入Zabbix server修改IP地址,等会绿色就是正常

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐