Use docker to build zabbix

Why can’t 80% of coders be architects? >>>  

Overview

Before again, we need to know what the main architecture of zabbix is. Zabbix has a server side which is the most important. It is mainly used to collect the data from the agent and then process it. Of course, there must be an agent side on the server side. To collect data, in order to display the data graphically for people to see, there must be a web side to display the data and facilitate people to configure zabbix, and then a single server side to collect data may cause too much pressure on the server side, so we There is also a proxy side to replace the server side to receive data to reduce the pressure on the server side. The last one is the database. We need to store the collected data in the database. Here I choose mysql. The total zabbix is ​​divided into the following modules

  • zabbix-server
  • zabbix-web
  • zabbix-proxy
  • zabbix-agent
  • mysql

After knowing this, we will use the docker image to build such an architecture, because I only use it for the server, so the proxy is not needed. The zabbix selected is version 4.0, and finally we need the following docker images

docker pull zabbix/zabbix-server-mysql:alpine-trunk docker pull zabbix/zabbix-web-nginx-mysql:alpine-trunk docker pull zabbix/zabbix-agent:alpine-trunk docker pull mysql:5.6

Note: All the above images do not support the arm architecture, so if you want to monitor the Raspberry Pi, you can only compile the agent on the Raspberry Pi

Overview of Experiment Resources

First of all, I use an Alibaba Cloud server, which is an Ubuntu system with docker installed.

Run the database first

Because the database is the most basic, you have to make it run first, otherwise where is the data of zabbix? First create a zabbix folder locally, and then all the data in the container will be stored in it. mkdir zabbixThen enter the following command to run the container docker run --name zabbix-mysql -d -v /root/zabbix/mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD="woyaoxuehuilinux" mysql:5.6. Explain the meaning of the above parameters –name is the name of the specified container, -d is to run the container in the background, -v is to map the /var/lib/mysql directory in the container with the /root/zabbix/mysql-data directory in the host, so that the file will not be lost if the container deletes it. The environment variable specified after -e is set mysql password, and finally the name of the image to be used

Run the server zabbix-server

After that, it is the core server side in zabbix. Enter the following command to run it docker run --name zabbix-server -p10051:10051 --link zabbix-mysql:mysql -v /root/zabbix/zabbix-server:/var/lib/zabbix -e DB_SERVER_HOST="zabbix-mysql" -e MYSQL_USER="root" -e MYSQL_PASSWORD="woyaoxuehuilinux" -d zabbix/zabbix-server-mysql:alpine-trunk. Explain that the parameter –name is to give a name to the container, and -p is to map the 10051 port of the host with the 10051 port in the container, –link It is to associate the zabbix container with the mysql container to prevent the ip address from being changed and unable to connect. -v is to map the main directory of the zabbix-server in the container and the /root/zabbix/zabbix-server in the host to prevent loss. For unstructured data, the -e parameter is the environment variable that specifies the database, such as the address of the database, because we have done the association with the mysql container before, so just write the name of the MySQL container, and then the mysql account and password , -d means let the container run in the background, and finally don’t forget the name of the image to be used, because the port 10051 of the security group corresponding to the Alibaba Cloud host needs to be opened

Run the server-side zabbix-web

The server is up, the next is the web side, enter the following command to run the web side docker run --name zabbix-web --link zabbix-mysql:mysql -p80:80 -p443:443 --link zabbix-server:zabbix-server -v /usr/share/zabbix -e DB_SERVER_HOST="zabbix-mysql" -e MYSQL_USER="root" -e MYSQL_PASSWORD="woyaoxuehuilinux" -e ZBX_SERVER_HOST="zabbix-server" -e PHP_TZ="Asia/Shanghai" -d zabbix/zabbix-web-nginx-mysql:alpine-trunkHere I have encountered a problem, that is, I use the – v /zabbix-web:/use/share/zabbix method to mount the zabbixweb site When it is in the directory, docker automatically deletes all the contents of the directory /usr/share/zabbix in the container. I don’t know if this is a problem of mirroring, so there is nothing in the final /zabbix-web directory, but I use docker In the managed volume mode, it can be successfully completed, so I used the following method to solve this problem. When I use the docker managed volume method to mount it, check the corresponding volume directory.docker inspect zabbix-web |grep Source

➜  zabbix docker inspect zabbix-web |grep Source
                "Source": "/var/lib/docker/volumes/2b327a18746700dcfb813b46aefeaddb9e04a526ca575d6a0a68d7097b55b72b/_data",
                "Source": "/var/lib/docker/volumes/57e343972c4eaf4875c5d830fe14d45bd97dbcbc69a105b183617f74916b07aa/_data",

Then copy the site directory in the volume cp -rf /var/lib/docker/volumes/2b327a18746700dcfb813b46aefeaddb9e04a526ca575d6a0a68d7097b55b72b/_data/ /root/zabbix/zabbix-web, delete the container, docker stop zabbix-web docker rm zabbix-webdelete the reserved volume docker volume prune, and then regenerate the container mount and docker run --name zabbix-web --link zabbix-mysql:mysql -p80:80 -p443:443 --link zabbix-server:zabbix-server -v /root/zabbix/zabbix-web/:/usr/share/zabbix -e DB_SERVER_HOST="zabbix-mysql" -e MYSQL_USER="root" -e MYSQL_PASSWORD="woyaoxuehuilinux" -e ZBX_SERVER_HOST="zabbix-server" -e PHP_TZ="Asia/Shanghai" -d zabbix/zabbix-web-nginx-mysql:alpine-trunkthen the problem is solved. The database is also connected to zabbix-server, -p is port mapping, -v is to mount the host directory to the container /usr/share/zabbix directory, -e is followed by the specified database host account and password, and the specified The host of zabbix-server also has the php time zone. -d is to put the container in the background to run and then we access the web interface of zabbix. The browser can enter the server ip and the default account Admin password zabbix

Run zabbix-agent on the server

In the end, it is relatively simple to run the agent. Run the following command docker run --name zabbix-agent --link zabbix-server:zabbix-server -e ZBX_HOST -d zabbix/zabbix-agent:alpine-trunkand then re-run zabbix-server docker run --name zabbix-server -p10051:10051 --link zabbix-mysql:mysql --link zabbix-agent:zabbix-agent -v /root/zabbix/zabbix-server:/var/lib/zabbix -e DB_SERVER_HOST="zabbix-mysql" -e MYSQL_USER="root" -e MYSQL_PASSWORD="woyaoxuehuilinux" -d zabbix/zabbix-server-mysql:alpine-trunk. I will first explain why I need to re-run zabbix-server, because I added –link zabbix-mysql:mysql, this is to avoid zabbix- After the agent container changes the ip, the zabbix-server cannot find the address of the zabbix-agent. When we configure the agent, click Configuration->Host->Zabbix server->agent agent interface in the web interface. Enter the DNS name here. zabbix-agent is just fine. Of course, to connect here, you must choose the DNS address port of 10050. The above parameter commands will not be explained. It is too simple and easy to understand. There are two ways to check zabbix, one is active mode and the other is passive mode. If it is monitoring the host, it is best to use passive mode

Welcome to follow Bboysoul’s blog www.bboysoul.com Have Fun

Reprinted in: https://my.oschina.net/u/3778921/blog/1799360

Leave a Comment

Your email address will not be published. Required fields are marked *