我相信大家都已经会再物理机上跑zabbix并且监控了,那么有没有想过在docker中跑zabbix?下面咱们来看看如何在docker中搭建zabbix并且监控
部署环境
2台物理机机器:
zabbix-server:192.168.254.13
zabbix-agent:192.168.254.17
docker版本:Server Version: 19.03.1
zabbix版本:4.0
docker的镜像网站(清华大学):https://mirrors.tuna.tsinghua.edu.cn/
安装docker
cd /etc/yum.repos.d/
wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce -y
部署zabbix
1.部署mysql数据库
docker run --name mysql-server -t \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix" \
-e MYSQL_ROOT_PASSWORD="root" \
-v /mysql/data/:/var/lib/mysql -v /mysql/conf/:/etc/my.cnf.d/ \
-d mariadb:10.2
2.部署zabbix-server
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 zabbix/zabbix-server-mysql:latest
3.部署apache web
docker run --name zabbix-web-apache-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 zabbix/zabbix-web-apache-mysql:latest
4.打开浏览器
用户名为Admin,密码为zabbix
5.安装zabbix客户端agent
docker run --name zabbix-agent \-e ZBX_HOSTNAME="self"\-e ZBX_SERVER_HOST="192.168.254.13"\-e ZBX_METADATA="client"\-p 10050:10050\-d zabbix/zabbix-agent:latest
6.在zabbix server端开启自动注册规则
过一会我们就可以看到宿主机192.168.254.17上的容器已经被注册进来了
最后说明一下上面用到的命令和参数解释
说明一下各个参数的意义:--name:代表容器的名字,或者标签-e:代表要执行的环境变量-p:代表映射的端口,:前面为宿主机,:后面代表容器的端口-d:代表后台运行-v:代表映射卷--character-set-server:字符集--collation-server:大小写是否敏感
MYSQL_USER:代表zabbix用户名
MYSQL_PASSWORD:代表zabbix的密码
MYSQL_DATABASE:zabbix创建的数据库名字为zabbixMYSQL_ROOT_PASSWORD:代表数据库root密码,没有指定USER是因为默认就是root
更多参数详情请参照以下网站:
mariadb10.2参考手册:https://hub.docker.com/_/mariadb
zabbix-server参考手册:https://hub.docker.com/r/zabbix/zabbix-server-mysql
zabbix-web-apache-mysql参考手册:https://hub.docker.com/r/zabbix/zabbix-web-apache-mysql
zabbix-agent参考手册:https://hub.docker.com/r/zabbix/zabbix-agent
用docker-compose部署zabbix
编辑docker-compose.yaml文件
[root@master ~]# catdocker-compose.yaml
version:'3.3'services:
mysql-server:
container_name: mariadb-server
image: mariadb:10.2restart: always
environment:
MYSQL_DATABASE: zabbix
MYSQL_USER: zabbix
MYSQL_PASSWORD: zabbix
MYSQL_ROOT_PASSWORD: root
volumes:- /data/mysql/data/:/var/lib/mysql- /data/mysql/conf/:/etc/my.cnf.d/server.cnf
zabbix-server:
container_name: zabbix-server
image: zabbix/zabbix-server-mysql
restart: always
environment:
DB_SERVER_HOST:"mysql-server"MYSQL_DATABASE:"zabbix"MYSQL_USER:"zabbix"MYSQL_PASSWORD:"zabbix"MYSQL_ROOT_PASSWORD:"root"ports:- 10051:10051links:- mysql-server
zabbix-web:
container_name: zabbix-web
image: zabbix/zabbix-web-apache-mysql
restart: always
depends_on:- mysql-server- zabbix-server
environment:
DB_SERVER_HOST:"mysql-server"MYSQL_DATABASE:"zabbix"MYSQL_USER:"zabbix"MYSQL_PASSWORD:"zabbix"MYSQL_ROOT_PASSWORD:"root"ports:- 80:80links:- mysql-server- zabbix-server
用docker-compose执行创建容器
#启动docker-compose.yaml文件中定义的容器,并且是后台运行
[root@master~]# docker-compose up -d
说明
docker-compose命令的用法
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Composefileconfig Validate and view the Composefilecreate Create services
down Stop and remove containers, networks, images, and volumes
events Receive realtimeevents from containers
exec Execute a commandina running container
help Get help on a command
images List imageskillKill containers
logs View output from containers
pause Pause services
port Print the public portfora port bindingpsList containers
pull Pull service images
push Push service images
restart Restart servicesrmRemove stopped containers
run Run a one-off command
scale Set number of containersfora service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
build 构建或重建服务
help 命令帮助
pause 暂停服务
exec 进入到一个docker容器的交互界面kill杀死容器
logs 显示docker-compose.yaml文件中容器的输出内容ps显示启动的容器
pull 拉取服务镜像
restart 重启服务rm删除停止的容器
run 运行容器
scale 设置服务的容器数目
start 开启服务
stop 停止服务
up 创建并启动容器config 测试docker-comose.yaml文件的语法是否ok
docker-compose.yaml文件中的语法参数说明
一个docker-compose.yaml文件大致分为:version,services,network
version
version可以是1,2,3这是大版本,还有小版本,比如1.0,2.2,3.3等,在定义yaml文件时必须制定version版本,每个版本的语法可能不太相同,可以参照文章下方给的官方文档了解详情
service
1.service中定义服务名称,service的下一级定义服务名称
version: '3.3'services:
服务名称:
2.在服务名称下一级标签有container_name,image,restart,privileged,environment,volumes,ports,links等
container_name:定义容器名称
image:镜像名称
restart
restart:"no"#不重启
restart: always #总是重启
restart: on-failure #失败时才重启
restart: unless-stopped #除非停止
privileged:获取最高权限
environment:设置系统环境变量
KEY1:VALUE1
KEY2:VALUE2
volumes 映射容器卷-HOST_PATH:CONTAINER_PATH-HOST_PATH:CONTAINER_PATH
ports: 映射容器端口-HOST_PORT: CONTAINER_PORT
links: 连接容器-SERVICE_NAME
depends_on:定义容器的先后顺序- zabbix-server- zabbix-web- mysql-server