上一篇文章介绍了如何通过docker的基本操作命令搭建运行一个zabbix系统,搭建运行zabbix涉及到了多个容器组成。操作起来相对繁琐,并且每次运行都要来一遍,很是不方便。
使用Docker Compose,不再需要使用shell脚本来启动容器。在配置文件中,所有的容器通过services来定义,然后使用docker-compose脚本来启动,停止和重启应用,和应用中的服务以及所有依赖服务的容器。Docker Compose是一个用于定义和运行多容器Docker应用程序的工具。[39]它使用YAML文件配置应用程序的服务,并使用单个命令执行所有容器的创建和启动过程。该docker-compose
CLI工具允许用户同时运行多个集装箱的命令。
一、安装docker-compose工具
1、快速安装docker
curl -fsSL https://get.docker.com/ | sh
2、启动docker
systemctl enable docker
systemctl start docker
3、安装docker-compose工具
如果系统中已经安装了docker则不需要再执行前两步,docker-compose需要单独安装。
yum install docker-compose -y
安装完毕后使用docker-compose version验证安装
二、规划设计zabbix系统环境依赖配置文件
1、主机安装目录规划
目录结构如上图,在/home下设置一个zabbix目录,然后在zabbix目录下创建三个环境依赖文件即可
.env_db_mysql #数据库运行依赖环境文件
.env_srv #zabbix-server运行依赖环境变量文件
.env_web #zabbix前端web运行依赖文件
2、.env_db_mysql文件内容
[root@localhost zabbix]# cat .env_db_mysql
# DB_SERVER_HOST=mysql-server
# DB_SERVER_PORT=3306
# MYSQL_USER=zabbix
MYSQL_USER=zabbix
# MYSQL_PASSWORD=zabbix
MYSQL_PASSWORD=zabbix
# MYSQL_ROOT_PASSWORD=
MYSQL_ROOT_PASSWORD=zabbix
# MYSQL_ALLOW_EMPTY_PASSWORD=false
# MYSQL_DATABASE=zabbix
MYSQL_DATABASE=zabbix
3、.env_srv文件内容如下
[root@localhost zabbix]# cat .env_srv
# ZBX_LISTENIP=
# ZBX_HISTORYSTORAGEURL=http://elasticsearch:9200/ # Available since 3.4.5
# ZBX_HISTORYSTORAGETYPES=uint,dbl,str,log,text # Available since 3.4.5
# ZBX_HISTORYSTORAGEDATEINDEX=0 # Available since 4.0.0
# ZBX_DEBUGLEVEL=3 #定义zabbix-server启动时的输出日志模式
# ZBX_STARTPOLLERS=5
# ZBX_IPMIPOLLERS=0
# ZBX_STARTPREPROCESSORS=3 # Available since 3.4.0
# ZBX_STARTPOLLERSUNREACHABLE=1
# ZBX_STARTTRAPPERS=5
# ZBX_STARTPINGERS=1
# ZBX_STARTDISCOVERERS=1
# ZBX_STARTHTTPPOLLERS=1
# ZBX_STARTTIMERS=1
# ZBX_STARTESCALATORS=1
# ZBX_STARTALERTERS=3 # Available since 3.4.0
ZBX_JAVAGATEWAY_ENABLE=false #在这里设置为true 可以支撑JAVA网关
# ZBX_JAVAGATEWAY=zabbix-java-gateway
# ZBX_JAVAGATEWAYPORT=10052
ZBX_STARTJAVAPOLLERS=5
# ZBX_STARTVMWARECOLLECTORS=0
# ZBX_VMWAREFREQUENCY=60
# ZBX_VMWAREPERFFREQUENCY=60
# ZBX_VMWARECACHESIZE=8M
# ZBX_VMWARETIMEOUT=10
ZBX_ENABLE_SNMP_TRAPS=false #在这里设置为true 可以支持snmp的traps
# ZBX_SOURCEIP=
# ZBX_HOUSEKEEPINGFREQUENCY=1
# ZBX_MAXHOUSEKEEPERDELETE=5000
# ZBX_SENDERFREQUENCY=30
# ZBX_CACHESIZE=8M
# ZBX_CACHEUPDATEFREQUENCY=60
# ZBX_STARTDBSYNCERS=4
# ZBX_HISTORYCACHESIZE=16M
# ZBX_HISTORYINDEXCACHESIZE=4M
# ZBX_TRENDCACHESIZE=4M
# ZBX_VALUECACHESIZE=8M
# ZBX_TIMEOUT=4
# ZBX_TRAPPERIMEOUT=300
# ZBX_UNREACHABLEPERIOD=45
# ZBX_UNAVAILABLEDELAY=60
# ZBX_UNREACHABLEDELAY=15
# ZBX_LOGSLOWQUERIES=3000
# ZBX_STARTPROXYPOLLERS=1
# ZBX_PROXYCONFIGFREQUENCY=3600
# ZBX_PROXYDATAFREQUENCY=1
# ZBX_LOADMODULE="dummy1.so,dummy2.so,dummy10.so"
# ZBX_TLSCAFILE=
# ZBX_TLSCRLFILE=
# ZBX_TLSCERTFILE=
# ZBX_TLSKEYFILE=
4、.env_web 文件内容
# ZBX_SERVER_HOST=zabbix-server
# ZBX_SERVER_PORT=10051
ZBX_SERVER_NAME=My Zabbix-Server #定义启动web页面后的TITLE
# ZBX_HISTORYSTORAGEURL=http://elasticsearch:9200/ # Available since 3.4.5
# ZBX_HISTORYSTORAGETYPES=['uint', 'dbl', 'str', 'text', 'log'] # Available since 3.4.5
# ZBX_MAXEXECUTIONTIME=600
# ZBX_MEMORYLIMIT=128M
# ZBX_POSTMAXSIZE=16M
# ZBX_UPLOADMAXFILESIZE=2M
# ZBX_MAXINPUTTIME=300
# Timezone one of: http://php.net/manual/en/timezones.php
PHP_TZ=Asia/Shanghai
三、规划设计zabbix系统compose的yaml配置文件
cat docker-compose.yaml
version: '3.5'
services:
zabbix-server:
image: zabbix/zabbix-server-mysql:centos-4.0-latest
ports:
- "10051:10051"
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- ./home/zabbix/alertscripts:/usr/lib/zabbix/alertscripts:ro
- ./home/zabbix/externalscripts:/usr/lib/zabbix/externalscripts:ro
- ./home/zabbix/modules:/var/lib/zabbix/modules:ro
- ./home/zabbix/enc:/var/lib/zabbix/enc:ro
- ./home/zabbix/ssh_keys:/var/lib/zabbix/ssh_keys:ro
- ./home/zabbix/mibs:/var/lib/zabbix/mibs:ro
- ./home/zabbix/snmptraps:/var/lib/zabbix/snmptraps:ro
links:
- mysql-server:mysql-server
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
deploy:
resources:
limits:
cpus: '0.70'
memory: 1G
reservations:
cpus: '0.5'
memory: 512M
env_file:
- .env_db_mysql
- .env_srv
user: root
depends_on:
- mysql-server
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:
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: zabbix/zabbix-web-nginx-mysql
ports:
- "8080:80"
- "8443:443"
links:
- mysql-server:mysql-server
- zabbix-server:zabbix-server
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- ./home/zabbix/ssl/nginx:/etc/ssl/nginx:ro
deploy:
resources:
limits:
cpus: '0.70'
memory: 512M
reservations:
cpus: '0.5'
memory: 256M
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_backend:
aliases:
- zabbix-web-nginx-mysql
- zabbix-web-nginx-centos-mysql
- zabbix-web-nginx-mysql-centos
zbx_net_frontend:
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"
mysql-server:
image: mysql:5.7
command: [mysqld, --character-set-server=utf8, --collation-server=utf8_bin]
volumes:
- ./home/zabbix/mysql:/var/lib/mysql:rw
env_file:
- .env_db_mysql
user: root
stop_grace_period: 1m
networks:
zbx_net_backend:
aliases:
- mysql-server
- zabbix-database
- mysql-database
db_data_mysql:
image: busybox
volumes:
- ./home/zabbix/mysql:/var/lib/mysql:rw
# elasticsearch:
# image: elasticsearch
# environment:
# - transport.host=0.0.0.0
# - discovery.zen.minimum_master_nodes=1
# networks:
# zbx_net_backend:
# aliases:
# - elasticsearch
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
配置文件内容介绍:
a、定义zabbix-server服务
zabbix-server服务只连通后端网络zbx_net_backend
b、定义zabbix-web-nginx-mysql服务
zabbix-web-nginx-mysql服务连接前后端两个网络,zbx_net_backend和zbx_net_frontend
c、定义mysql-server服务
mysql-server服务连接后端网络zbx_net_backend
d、定义db_data_mysql服务
e、定义了两个网络桥接服务
zbx_net_frontend 定义的ip范围是172.16.238.0/24
zbx_net_backend 定义的ip范围是172.16.239.0/24
四、一键启动zabbix服务
docker-compose -f docker-compose.yaml up -d
或者 docker-compose up -d 也可以
使用docker ps 查看已经启动的三个服务 ,mysql5.7 zabbix-server-mysql zabbix-web-nginx-mysql
五、验证zabbix启动效果
在地址栏输入http://133.3.103.2地址后查看登陆界面
六、一键关停zabbix服务
通过docker-compose down命令一键关停前面部署的多个docker服务及网络环境
总结:
compose会保留所有服务的volume data,这个volume data,我理解的是对容器做的所有修改。当再次运行docker-compose run
这条命令的时候,compose会找到之前运行过的所有容器,然后把它们的volumes,也就是修改后的容器状态,从老的容器中,拷贝到新的容器中,在这个过程中新老容器的CONTAINER ID不会变化。
Compose 文件是一个 YAML , 主要定义了 services , networks 和 volumes , 其默认路径是 ./docker-compose.yml 。service 定义包含了应用与每个容器的配置,很像给 docker run 传参,同样, network 和 volume 对于 docker network create 和 docker volume create 也类似