用Dockerfile部署zabbix
用Dockerfile部署
zabbix-server部署阶段
要用docker
跑zabbix-server
,需要以下几个组件
组件名称 | 作用 |
---|---|
数据库 | MySQL 或是PostgreSQL |
Zabbix Java gateway | Java管理扩展,可以不添加 |
Zabbix server | zabbix 服务端 |
zabbix-web | 与MySQL服务器实例和Zabbix server实例关联 |
zabbix-agent | zabbix客户端 |
MySQL数据库
数据库我选择MySQL
,还是得写Dockerfile
,改一下时区,时间不对很麻烦,字符集也改一下吧
# mkdir /data/docker/mysql -p /data/docker/mysql]# vim Dockerfile FROM mysql:5.7 ENV LANG en_US.utf8 RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone \
构建镜像
#docker build -t debian-mysql5.7 .
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debian-mysql5.7 latest 3d3c7dc9608c 14 seconds ago 372MB
mysql 5.7 563a026a1511 3 weeks ago 372MB
启动容器
# docker run --hostname mysql --name debian-mysql-5.7 -t \ -e MYSQL_USER="zabbix" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_PASSWORD="passwd" \ -e MYSQL_ROOT_PASSWORD="passwd" \ -v /data/mysql:/var/lib/mysql:rw \ -d debian-mysql5.7 9a98a2ba0896a8a146d515764d2d8eef4bc32a9f0dbea445c360fab5668a5cfe # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9a98a2ba0896 debian-mysql5.7 "docker-entrypoint.s…
变量说明
变量 | |
---|---|
MYSQL_USER | 启动时要添加的用户 |
MYSQL_DATABASE | 启动时要创建的数据库 |
MYSQL_PASSWORD | 指定添加用的的密码 |
MYSQL_ROOT_PASSWORD | root用户的密码 |
Zabbix Java gateway
这个看个人,如果不监控java
应用服务器可以不安装这个,还是得写Dockerfile
,改一下时区和编码。
/data/docker/java-gateway]# cat Dockerfile FROM zabbix/zabbix-java-gateway:latest ENV LANG en_US.utf8 RUN apk add -U tzdata RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone \ /data/docker/java-gateway]# docker build -t zabbix-java-gateway . /data/docker/java-gateway]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zabbix-java-gateway latest 7948dffa9af0 35 seconds ago 80.1MB
启动容器
需要映射一下10052
端口
# docker run --hostname zabbix-java-gateway \ -name zabbix-java-gateway -t \ -p 10052:10052 \ -d zabbix-java-gateway 5e2434e7a4c6f3f947e6408ff5a90097e802c6a36282c36ce7e4cd5819474f7a # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e2434e7a4c6 zabbix-java-gateway "docker-entrypoint.sh" 9 seconds ago Up 7 seconds 0.0.0.0:10052->10052/tcp zabbix-java-gateway 9a98a2ba0896 debian-mysql5.7 "docker-entrypoint.s…" 33 minutes ago Up 33 minutes 3306/tcp, 33060/tcp debian-mysql-5.7
Zabbix server
编写Dockerfile
/data/docker/zabbix-server]# cat Dockerfile FROM zabbix/zabbix-server-mysql:latest ENV LANG en_US.utf8 RUN apk add -U tzdata RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone \ /data/docker/zabbix-server]# docker build -t zabbix-server . /data/docker/zabbix-server]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zabbix-server latest 531aa3116f61 About a minute ago 62.3MB
需要映射10051
端口,指定数据库服务器,连接MySQL
和zabbix-java-gateway
容器
10051:10051 \
--hostname zabbix-server \
-e DB_SERVER_HOST="debian-mysql-5.7" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="passwd" \
-e MYSQL_ROOT_PASSWORD="passwd" \
-e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
--link debian-mysql-5.7:mysql \
--link zabbix-java-gateway:zabbix-java-gateway \
-d zabbix-server
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a45ef352cc68 zabbix-server "docker-entrypoint.sh" 9 minutes ago Up 8 minutes 0.0.0.0:10051->10051/tcp zabbix-server
zabbix-web
编写Dockerfile
/data/docker/zabbix-web]# cat Dockerfile FROM zabbix/zabbix-web-nginx-mysql:latest ENV LANG en_US.utf8 RUN apk add -U tzdata RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone \ /data/docker/zabbix-web]# docker build -t zabbix-web . /data/docker/zabbix-web]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zabbix-web latest 6ff51e2f0545 About a minute ago 164MB
启动容器
需要映射80端口到宿主机,连接数据库,连接zabbix-server
容器
# docker run --name zabbix-web -t \ -p 80:80 \ --hostname zabbix-web \ -e PHP_TZ="Asia/Shanghai" \ -e DB_SERVER_HOST="debian-mysql-5.7" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="zabbix" \ -e MYSQL_PASSWORD="passwd" \ -e MYSQL_ROOT_PASSWORD="passwd" \ --link debian-mysql-5.7:mysql \ --link zabbix-server:zabbix-server \ -d zabbix-web f0c33f227992ce220730da9454df2f354565ed0b3d0813513a87797c2b752702 # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f0c33f227992 zabbix-web "docker-entrypoint.sh" 13 seconds ago Up 10 seconds 0.0.0.0:80->80/tcp, 443/tcp zabbix-web
以上方式安装的是最新版本,因为在指定镜像的时候没有指定版本号,默认的就是laster
,也就是最新版本,建议zabbix-agent
版本要和服务端一致。
看下安装的容器
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4b2524953b4 zabbix-web "docker-entrypoint.sh" About an hour ago Up About an hour 0.0.0.0:80->80/tcp, 443/tcp zabbix-web d5a45e58b69d zabbix-server "docker-entrypoint.sh" 2 hours ago Up 2 hours 0.0.0.0:10051->10051/tcp zabbix-server cd16d62b98c4 zabbix-java-gateway "docker-entrypoint.sh" 2 hours ago Up 2 hours 0.0.0.0:10052->10052/tcp zabbix-java-gateway 6a4b9ce12398 debian-mysql5.7 "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306
/tcp, 33060/tcp debian-mysql-5.
访问:http://宿主机IP:80
zabbix-agent
安装(用官方yum源)
#rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm #yum -y install zabbix-agent-4.0.1
#vim /etc/zabbix/zabbix_agentd.conf Server=192.168.130.6 UnsafeUserParameters=1 Include=/etc/zabbix/zabbix_agentd.d/*.conf # systemctl start zabbix-agent
注意:如果要对宿主机进行监控,请编辑宿主机的zabbix_agentd.conf
,将Server
地址,默认127.0.0.1
,修改为docker-server
容器地址,否则不通;如果是对其他主机的监控则使用docker-zabbix-server宿主机的IP。
查看容器Zabbix-server的地址
# docker exec -it zabbix-server ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 23: eth0@if24: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever
要用docker
跑zabbix-server
,需要以下几个组件
组件名称 | 作用 |
---|---|
数据库 | MySQL 或是PostgreSQL |
Zabbix Java gateway | Java管理扩展,可以不添加 |
Zabbix server | zabbix 服务端 |
zabbix-web | 与MySQL服务器实例和Zabbix server实例关联 |
zabbix-agent | zabbix客户端 |
MySQL数据库
数据库我选择MySQL
,还是得写Dockerfile
,改一下时区,时间不对很麻烦,字符集也改一下吧
# mkdir /data/docker/mysql -p /data/docker/mysql]# vim Dockerfile FROM mysql:5.7 ENV LANG en_US.utf8 RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone \
构建镜像
#docker build -t debian-mysql5.7 .
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
debian-mysql5.7 latest 3d3c7dc9608c 14 seconds ago 372MB
mysql 5.7 563a026a1511 3 weeks ago 372MB
启动容器
# docker run --hostname mysql --name debian-mysql-5.7 -t \ -e MYSQL_USER="zabbix" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_PASSWORD="passwd" \ -e MYSQL_ROOT_PASSWORD="passwd" \ -v /data/mysql:/var/lib/mysql:rw \ -d debian-mysql5.7 9a98a2ba0896a8a146d515764d2d8eef4bc32a9f0dbea445c360fab5668a5cfe # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9a98a2ba0896 debian-mysql5.7 "docker-entrypoint.s…
变量说明
变量 | |
---|---|
MYSQL_USER | 启动时要添加的用户 |
MYSQL_DATABASE | 启动时要创建的数据库 |
MYSQL_PASSWORD | 指定添加用的的密码 |
MYSQL_ROOT_PASSWORD | root用户的密码 |
Zabbix Java gateway
这个看个人,如果不监控java
应用服务器可以不安装这个,还是得写Dockerfile
,改一下时区和编码。
/data/docker/java-gateway]# cat Dockerfile FROM zabbix/zabbix-java-gateway:latest ENV LANG en_US.utf8 RUN apk add -U tzdata RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone \ /data/docker/java-gateway]# docker build -t zabbix-java-gateway . /data/docker/java-gateway]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zabbix-java-gateway latest 7948dffa9af0 35 seconds ago 80.1MB
启动容器
需要映射一下10052
端口
# docker run --hostname zabbix-java-gateway \ -name zabbix-java-gateway -t \ -p 10052:10052 \ -d zabbix-java-gateway 5e2434e7a4c6f3f947e6408ff5a90097e802c6a36282c36ce7e4cd5819474f7a # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5e2434e7a4c6 zabbix-java-gateway "docker-entrypoint.sh" 9 seconds ago Up 7 seconds 0.0.0.0:10052->10052/tcp zabbix-java-gateway 9a98a2ba0896 debian-mysql5.7 "docker-entrypoint.s…" 33 minutes ago Up 33 minutes 3306/tcp, 33060/tcp debian-mysql-5.7
Zabbix server
编写Dockerfile
/data/docker/zabbix-server]# cat Dockerfile FROM zabbix/zabbix-server-mysql:latest ENV LANG en_US.utf8 RUN apk add -U tzdata RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone \ /data/docker/zabbix-server]# docker build -t zabbix-server . /data/docker/zabbix-server]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zabbix-server latest 531aa3116f61 About a minute ago 62.3MB
需要映射10051
端口,指定数据库服务器,连接MySQL
和zabbix-java-gateway
容器
10051:10051 \
--hostname zabbix-server \
-e DB_SERVER_HOST="debian-mysql-5.7" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="passwd" \
-e MYSQL_ROOT_PASSWORD="passwd" \
-e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
--link debian-mysql-5.7:mysql \
--link zabbix-java-gateway:zabbix-java-gateway \
-d zabbix-server
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a45ef352cc68 zabbix-server "docker-entrypoint.sh" 9 minutes ago Up 8 minutes 0.0.0.0:10051->10051/tcp zabbix-server
zabbix-web
编写Dockerfile
/data/docker/zabbix-web]# cat Dockerfile FROM zabbix/zabbix-web-nginx-mysql:latest ENV LANG en_US.utf8 RUN apk add -U tzdata RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo Asia/Shanghai > /etc/timezone \ /data/docker/zabbix-web]# docker build -t zabbix-web . /data/docker/zabbix-web]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zabbix-web latest 6ff51e2f0545 About a minute ago 164MB
启动容器
需要映射80端口到宿主机,连接数据库,连接zabbix-server
容器
# docker run --name zabbix-web -t \ -p 80:80 \ --hostname zabbix-web \ -e PHP_TZ="Asia/Shanghai" \ -e DB_SERVER_HOST="debian-mysql-5.7" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="zabbix" \ -e MYSQL_PASSWORD="passwd" \ -e MYSQL_ROOT_PASSWORD="passwd" \ --link debian-mysql-5.7:mysql \ --link zabbix-server:zabbix-server \ -d zabbix-web f0c33f227992ce220730da9454df2f354565ed0b3d0813513a87797c2b752702 # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f0c33f227992 zabbix-web "docker-entrypoint.sh" 13 seconds ago Up 10 seconds 0.0.0.0:80->80/tcp, 443/tcp zabbix-web
以上方式安装的是最新版本,因为在指定镜像的时候没有指定版本号,默认的就是laster
,也就是最新版本,建议zabbix-agent
版本要和服务端一致。
看下安装的容器
# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4b2524953b4 zabbix-web "docker-entrypoint.sh" About an hour ago Up About an hour 0.0.0.0:80->80/tcp, 443/tcp zabbix-web d5a45e58b69d zabbix-server "docker-entrypoint.sh" 2 hours ago Up 2 hours 0.0.0.0:10051->10051/tcp zabbix-server cd16d62b98c4 zabbix-java-gateway "docker-entrypoint.sh" 2 hours ago Up 2 hours 0.0.0.0:10052->10052/tcp zabbix-java-gateway 6a4b9ce12398 debian-mysql5.7 "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306
/tcp, 33060/tcp debian-mysql-5.
访问:http://宿主机IP:80
zabbix-agent
安装(用官方yum源)
#rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm #yum -y install zabbix-agent-4.0.1
#vim /etc/zabbix/zabbix_agentd.conf Server=192.168.130.6 UnsafeUserParameters=1 Include=/etc/zabbix/zabbix_agentd.d/*.conf # systemctl start zabbix-agent
注意:如果要对宿主机进行监控,请编辑宿主机的zabbix_agentd.conf
,将Server
地址,默认127.0.0.1
,修改为docker-server
容器地址,否则不通;如果是对其他主机的监控则使用docker-zabbix-server宿主机的IP。
查看容器Zabbix-server的地址
# docker exec -it zabbix-server ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 23: eth0@if24: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever