Docker quickly builds and uses Zabbix

Docker builds and uses Zabbix

0 zabbix basics

insert image description here

  1. zabbix-server
    The server side of zabbix is ​​responsible for receiving the monitoring data sent by the agent and providing all the core functions of zabbix.
  2. database
    is a database used to store monitoring data and configuration information. Currently, there are two commonly used databases, mysql and postgresql.
  3. zabbix-web
    The UI side of zabbix, which provides functions such as operation console and monitoring display.
  4. zabbix-java-gateway
    is used to monitor the JVM status of Java programs. Zabbix itself cannot directly obtain monitoring indicators from jvm, so it needs to use this gateway to achieve. [So zabbix-java-gateway does not have to be installed, if you want to monitor java programs, you need to install it]
  5. zabbbix-agent
    The agent of zabbix is ​​deployed on the target host to collect the monitoring data of the host and provide it to the zabbix server.

注意:The required environment for zabbix6.0 requires centos8.0

# 查看centos版本
cat /etc/centos-release
# 查看linux版本
cat /proc/version

1 Install and start docker

yum install -y yum-utils
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
yum install docker
systemctl start docker

2 Pull mysql, zabbix-server and other images

docker pull mysql:5.7
docker pull docker.io/zabbix/zabbix-web-nginx-mysql:centos-5.4-latest
docker pull docker.io/zabbix/zabbix-server-mysql:centos-5.4-latest

① run mysql

  1. Create a storage volume for persistent mysql data
docker volume create -d local mysql_data  # 存放mysql的数据
docker volume create -d local mysql_logs  # 存放mysql的日志
docker volume create -d local mysql_conf  # 存放mysql的配置文件

Note: The default storage path of the storage volume is: /var/lib/docker/volume/${volume_name}.

  1. Create and run the mysql8 container
docker run --name mysql-server -t \
   -v mysql_data:/var/lib/mysql \
      -v mysql_logs:/var/log/mysql \
      -v mysql_conf:/etc/mysql \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix_pwd" \
      -e MYSQL_ROOT_PASSWORD="123456" \
      --restart=unless-stopped \
      -d mysql:8.0 \
      --character-set-server=utf8 --collation-server=utf8_bin \
      --default-authentication-plugin=mysql_native_password

Note: The zabbix6.x version requires the use of mysql8.0.

②Run zabbix-java-gateway

docker run --name zabbix-java-gateway -t \
   --restart=unless-stopped \
      -d zabbix/zabbix-java-gateway:alpine-6.2-latest

③Run zabbix-server

  1. Create a storage volume for storing zabbix configuration files
docker volume create -d local zabbix_server
  1. Create and start zabbix-server

Run the zabbix-server container and open port 10051/TCP for receiving monitoring data. Add the –link parameter to realize the mutual communication between the mysql and java-gateway containers.

docker run --name zabbix-server-mysql -t \
    -v zabbix_server:/etc/zabbix \
      -e DB_SERVER_HOST="mysql-server" \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix_pwd" \
      -e MYSQL_ROOT_PASSWORD="123456" \
      -e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
      --link mysql-server:mysql \
      --link zabbix-java-gateway:zabbix-java-gateway \
      --restart=unless-stopped \
      -p 10051:10051 \
      -d zabbix/zabbix-server-mysql:alpine-6.2-latest

注意:This method is suitable for all containers to be deployed on the same host. If they are deployed separately, the relevant ports need to be opened in the previous steps and connected through the LAN.

④ run zabbix-web

Run the zabbix-web container, which exposes port 80 by default

docker run --name zabbix-web-nginx-mysql -t \
   -e PHP_TZ="Asia/Shanghai" \
   -e ZBX_SERVER_HOST="zabbix-server-mysql" \
      -e DB_SERVER_HOST="mysql-server" \
      -e MYSQL_DATABASE="zabbix" \
      -e MYSQL_USER="zabbix" \
      -e MYSQL_PASSWORD="zabbix_pwd" \
      -e MYSQL_ROOT_PASSWORD="123456" \
      --link mysql-server:mysql \
      --link zabbix-server-mysql:zabbix-server \
      -p 80:8080 \
      --restart unless-stopped \
      -d zabbix/zabbix-web-nginx-mysql:alpine-6.2-latest

If it prompts that the port is occupied:
Method 1: delete the mirror first, and then replace the unoccupied port (for example: port 80 of zabbix-web-nginx-mysql is already occupied)

  • Delete the image: docker rm zabbix-web-nginx-mysql

  • -p 80:8080更换为-p 9000:8080Just change the mapped port

Method 2: Kill the process that occupies the port

  • Execute the command to find the process number that occupies the port (list open files: lsof command can list the opened files and network connections in the current system, including detailed information such as file name, process ID, user, file type, file descriptor):sudo lsof -i :80
  • Kill the process:sudo kill 进程号(processID)

Results of the:
insert image description here

3 run zabbix-agent

If we want to monitor information on the zabbix-server server, then we need to install zabbix-agent

3.1 Deploy through the installation package (recommended)

  1. Install zabbix corresponding warehouse through rpm
rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/7/x86_64/zabbix-release-6.0-4.el7.noarch.rpm

# 清除本地yum缓存
yum clean all
  1. Install zabbix-agent
yum install -y zabbix-agent
  1. Start zabbix-agent
systemctl restart zabbix-agent

# 设置开机自启
# systemctl enable zabbix-agent

3.2 Deploy through docker

If we deploy zabbix-agent in docker, what zabbix-server monitors is the status of the container, not the status of the host

  • Through docker deployment, we need to modify the ZBX_Server_HOST of the configuration file in the container, but if the modification is restarted, the modification will be lost, so we need to write a DockerFile, and then build our own zabbix-agent image
    • Some commands:
#编写DockerFile
vi Dockerfile
# DockerFile中填写下面两行信息
FROM zabbix/zabbix-agent:alpine-6.2-latest
COPY zabbix_agentd.conf /etc/zabbix/zabbix_agentd.conf
# 构建自己的镜像
docker build -t my-zabbix-agent 
#通过自己的镜像运行容器
docker run -d \
--name zabbix-agent \
 -v /var/log/zabbix:/var/log/zabbix \
 -p 10050:10050 \
 --restart=unless-stopped \
 --privileged \
 my-zabbix-agent

Deploy via Docker:

# ZBX_SERVER_HOST更换为自己zabbix-server的所在地址
# 并且在zabbix的web页面中也需要填写agent的容器地址
docker run -d \
   --name zabbix-agent \
   -v /etc/zabbix \
   -v /var/log/zabbix:/var/log/zabbix \
   -e ZBX_HOSTNAME="client-01" \
   -e ZBX_SERVER_HOST="10.253.50.145" \
   -e ZBX_SERVER_PORT=10051 \
   -p 10050:10050 \
   --restart=unless-stopped \
   --privileged \
   zabbix/zabbix-agent:alpine-6.2-latest

3.3 Set web as Chinese and bug handling

  1. Modify the web page of zabbix, and set the page to be displayed in Chinese

User settings - Profile - Language - Chinese, and finally click update below

insert image description here
2. Modify the monitoring address of zabbix to the agent address

Detection - host - select our zabbix-server - mouse click and select configuration

insert image description here
Replace the address of the agent with the address of zabbix-server

Because we are installing a zabbix-agent on the zabbix-server machine at this moment, the purpose is to detect the status of the machine where our zabbix-server is located.

  • If you installed zabbix-agent on other machines, just replace it with the corresponding machine ip address

insert image description here
3. Wait a few minutes to refresh the page

bug1:

If it is found that the availability still does not turn green, and the page reports an error
Asuming that agent dropped connect because of access permision, it means that we have not enabled the corresponding permissions

  • First check to see if the firewall is turned off
    • Turn off the firewall command: systemctl stop firewalld
  • If you are using a cloud server, check whether the corresponding security group policy of the cloud server is enabled
  • Insufficient permissions: modify the configuration file of zabbix-agent
    • vim /etc/zabbix/zabbix_agentd.conf
    • Find the location of the Server, and modify it to the IP address of your own server plus the network segment, such as: 172.159.92.3/24; for convenience, you can also modify it to 0.0.0.0/0 (representing all server accesses) After completion
      insert image description here
      , :wqSave and exit, systemctl restart zabbix-agent.servicerestart zabbix-agent, wait for a few minutes and then refresh the zabbix web page
  • If none of the above methods work, check the error message in the log and deal with it accordingly:
    Check the log information: tail -f /var/log/zabbix/zabbix_agentd.log
    • 报错信息:Unable to connect to [127.0.0.1]:10051 [cannot connect to [[127.0.0.1]:10051]: [111] Connection refused]
    • Indicates that the server address configuration is wrong, vim /etc/zabbix/zabbix_agentd.conf, change the ServerActive in the configuration file to the address of zabbix-server, such as: 10.253.50.145, remember that only the ip needs to be modified, and the port port is not required
      insert image description here

bug2:

If you query the log file of the agent and find an error message:
23946:20230722:121107.962 no active checks on server [10.253.50.145:10051]: host [Zabbix server] not found

  • Query log file command: tail -f /var/log/zabbix/zabbix_agentd.log
  • Solution: Modify the Hostname in the agent configuration file,
    • Check the HostName in the configuration file:
      cat /etc/zabbix/zabbix_agentd.conf | grep Hostname
    • Change the Host name on the zabbix web page Monitoring->Configuration->Hosts page to be the same as the Hostname in zabbix_agentd.conf.
      insert image description here
      insert image description here
  • restart zabbix-agent

Demand combat (HTTP proxy + zabbix5)

Customers need to monitor the status of our services, so we only need to provide a custom template (xml file) + custom alarm template

  • Solution implementation: We expose an API interface to Zabbix, which is used to obtain data through HTTP proxy. [So there is no need to install zabbix-agent]

① Environment construction: zabbix-server, web, mysql

  1. Install and start docker
yum install -y yum-utils
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
yum install docker
systemctl start docker

Pull the docker of mysql and zabbix, because zabbix 6 or above requires centos 8, limited by the environment, here is a demonstration of zabbix 5 version

  1. Pull the image and create a data volume
# 拉取镜像
docker pull mysql:5.7
docker pull docker.io/zabbix/zabbix-web-nginx-mysql:centos-5.4-latest
docker pull docker.io/zabbix/zabbix-server-mysql:centos-5.4-latest
# 拉取完成后,开始安装
docker volume create -d local  mysql_data #存放mysql数据
docker volume create -d local  mysql_logs #存放mysql日志
docker volume create -d local  mysql_conf #存放mysql配置文件 
  1. Create corresponding containers (mysql, zabbix-server, zabbix-web)
docker run --name mysql-server -t \
-v mysql_data:/var/lib/mysql \
-v mysql_logs:/var/log/mysql \
-v mysql_conf:/etc/mysql \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="123456" \
-e MYSQL_ROOT_PASSWORD="123456" \
--restart=unless-stopped \
-d mysql:5.7 \
--character-set-server=utf8 \
--collation-server=utf8_bin \
--default-authentication-plugin=mysql_native_password

docker volume create -d local  zabbix_server
docker run --name zabbix-server-mysql -t \
-v zabbix_server:/etc/zabbix \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="123456" \
-e MYSQL_ROOT_PASSWORD="123456" \
--link mysql-server:mysql \
--restart=unless-stopped \
-p 10051:10051 \
--hostname zabbix-server \
-d zabbix/zabbix-server-mysql:centos-5.4-latest

docker run --name zabbix-web-nginx-mysql -t \
-e PHP_TZ="Asia/Shanghai" \
-e ZBX_SERVER_HOST="zabbix-server-mysql" \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="123456" \
-e MYSQL_ROOT_PASSWORD="123456" \
--link mysql-server:mysql \
--link zabbix-server-mysql:zabbix-server \
-p 80:8080 \
--restart unless-stopped \
--hostname zabbix-web \
-d zabbix/zabbix-web-nginx-mysql:centos-5.4-latest
  1. Once created, visit http://ip/zabbix. You can log in with Admin and zabbix. Note that Admin starts with a capital
    insert image description here

② Create automatic discovery and custom templates

  1. Automatic discovery
    Because the customer deploys too many of our machines, it is used zabbix的自动发现to automatically discover the host
  • Create automatic discovery (fill in the corresponding ip range and protocol port)
  • Configure association actions (add the discovered host to the host group and add it to the custom template we provide to customers)
  1. Custom template (Template)
    We provide templates required by customers to monitor the data of specified services (call the interface provided by us to obtain the information that needs to be monitored)
  • Create monitoring items under the custom template, such as: our interface returns multiple Agent information, then create a monitoring item called Agent Item
  • Create automatic discovery (HTTP proxy), monitoring item prototype (agent information, etc., such as: name, cpu usage) and triggers under the custom template Template [automatically create multiple Each Agent contains the prototype of the corresponding monitoring item: name, cpu usage, etc.] When the collected data is unreasonable, the corresponding trigger will be triggered

③Email alert

  1. Create an alarm medium (pop/smtp needs to be enabled for email), and the account needs to be configured as the smtp identification code corresponding to the email
  2. email template
//模板一
邮件主题:OCP Agent Problem: {
    
    EVENT.NAME}

邮件内容:<b>Problem started</b> at {
    
    EVENT.TIME} on {
    
    EVENT.DATE}<br><b>Problem name:</b> {
    
    EVENT.NAME}<br><b>Host:</b> {
    
    HOST.NAME}<br><b>Severity:</b> {
    
    EVENT.SEVERITY}<br><b>Operational data:</b> {
    
    EVENT.OPDATA}<br><b>Original problem ID:</b> {
    
    EVENT.ID}<br>{
    
    TRIGGER.URL}



//模板二
主题:

故障{
    
    TRIGGER.STATUS},服务器:{
    
    HOSTNAME1},发生: {
    
    TRIGGER.NAME}故障!

内容:

告警主机:{
    
    HOSTNAME1}
告警时间:{
    
    EVENT.DATE} {
    
    EVENT.TIME}
告警等级:{
    
    TRIGGER.SEVERITY}
告警信息:{
    
    TRIGGER.NAME}
告警项目:{
    
    TRIGGER.KEY1}
问题详情:{
    
    ITEM.NAME}:{
    
    ITEM.VALUE}
当前状态:{
    
    TRIGGER.STATUS}:{
    
    ITEM.VALUE1}
事件ID:{
    
    EVENT.ID}

Reference article: https://blog.csdn.net/chang_chunhua/article/details/127846551

Guess you like

Origin blog.csdn.net/weixin_45565886/article/details/131773129