返回列表 发帖

CentOS8_在Docker中安装Zabbix4.4.5

查看Docker的版本信息:
[root@centos8 ~]# docker -v
Docker version 20.10.21, build baeda1f
[root@centos8 ~]#


从公网下载镜像:( 以下是下载CentOS8的镜像 )
[root@centos8 ~]# docker pull centos:centos8
centos8: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:centos8
docker.io/library/centos:centos8
[root@centos8 ~]#


查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       centos8   5d0da3dc9764   20 months ago   231MB
[root@centos8 ~]#


使用镜像“centos:centos8”创建并启动容器“Rhel8_1”:
[root@centos8 ~]# docker run --privileged -itd --name Rhel8_1 -p 80:80 -v /opt/share:/home/dir_1 centos:centos8 /usr/sbin/init
cd89128a399ac6b6c1071b55f18f4d93252c3d30028bd1fd38494fcd0efaa353
[root@centos8 ~]#

注释:
--privileged + /usr/sbin/init : 获取systemctl权限;不加上的话,在容器中就无法使用命令systemctl
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 第一个80是宿主机的端口,暴露给外部直接访问;第二个80是容器的端口。
-v 挂载目录 : 这里是把宿主机的目录/opt/share挂载到容器的目录/home/dir_1;假如目录不存在,就会自动创建。
centos:centos8 : 镜像名称:版本号


在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID   IMAGE            COMMAND            CREATED          STATUS          PORTS                               NAMES
cd89128a399a   centos:centos8   "/usr/sbin/init"   24 seconds ago   Up 23 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   Rhel8_1
[root@centos8 ~]#

注释:宿主机的TCP 80端口,映射到容器“Rhel8_1”的TCP 80端口。



###

进入容器“Rhel8_1”:
[root@centos8 ~]# docker exec -it Rhel8_1 /bin/bash
[root@cd89128a399a /]#

[root@cd89128a399a /]# pwd
/
[root@cd89128a399a /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@cd89128a399a /]#


查看容器“Rhel8_1”的系统版本信息:
[root@cd89128a399a /]# cat /etc/redhat-release
CentOS Linux release 8.4.2105
[root@cd89128a399a /]#
[root@cd89128a399a /]# uname -r
4.18.0-193.el8.x86_64
[root@cd89128a399a /]#


查看当前语言环境:(英文)
[root@cd89128a399a /]# echo $LANG
en_US.UTF-8
[root@cd89128a399a /]# cat /etc/locale.conf
LANG="en_US.UTF-8"
[root@cd89128a399a /]#


SELinux默认是不可用:
[root@cd89128a399a /]# getenforce
bash: getenforce: command not found
[root@cd89128a399a /]# cat /etc/selinux/config
cat: /etc/selinux/config: No such file or directory
[root@cd89128a399a /]#



[root@cd89128a399a /]# wget
bash: wget: command not found
[root@cd89128a399a /]#

不可以直接使用公网Yum源:
[root@cd89128a399a /]# yum -y install wget
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream                                                                    23  B/s |  38  B     00:01   
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist

[root@cd89128a399a /]#


要使用CentOS8的新公网Yum源,可参考:CentOS8使用CentOS-Vault源+EPEL

确认可以使用公网Yum源安装rpm软件包:
[root@cd89128a399a ~]# yum -y install wget





###

在容器“Rhel8_1”中安装Zabbix4.4.5:

安装Apache:
dnf -y install http*

启动Apache:
systemctl start httpd

启动容器时自动启动Apache:
systemctl enable httpd


安装MariaDB:
dnf -y install mariadb-server mariadb

启动MariaDB:
systemctl start mariadb

启动容器时自动启动MariaDB:
systemctl enable mariadb


安装PHP:
yum -y install php php-fpm php-mysqlnd php-gd php-xml php-mbstring



安装依赖软件包:
[root@cd89128a399a ~]# yum -y install gcc gcc-c++ wget autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel make

[root@cd89128a399a ~]# dnf -y install net-snmp-devel libxml2-devel libevent libevent-devel libcurl mysql-devel curl-devel php-ldap *bcmath* --allowerasing



编译安装Zabbix:
tar -zxvf zabbix-4.4.5.tar.gz
cd zabbix-4.4.5
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2
make install



配置数据库:( 使用数据库管理员root@localhost )
[root@cd89128a399a ~]# mysql -u"root" -h"localhost"
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>



创建库zabbix:
MariaDB [(none)]> Create database zabbix character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]>


查看库zabbix的字符集:
MariaDB [(none)]> Show create database zabbix;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| zabbix   | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.000 sec)

MariaDB [(none)]>

笺注:库zabbix的字符集为 utf8_general_ci 



切换到库zabbix:
MariaDB [(none)]> Use zabbix;
Database changed
MariaDB [zabbix]>

导入Zabbix的表:
MariaDB [zabbix]> source /root/zabbix-4.4.5/database/mysql/schema.sql;
MariaDB [zabbix]> source /root/zabbix-4.4.5/database/mysql/images.sql;
MariaDB [zabbix]> Source /root/zabbix-4.4.5/database/mysql/data.sql;



授权zabbix@localhost对库zabbix有完全控制的权限,密码:168
MariaDB [zabbix]> Grant all privileges on zabbix.* to zabbix@localhost identified by '168';
Query OK, 0 rows affected (0.000 sec)

MariaDB [zabbix]>



创建系统用户zabbix:
[root@cd89128a399a ~]# useradd -s /sbin/nologin zabbix

创建Zabbix的日志存放目录:
[root@cd89128a399a ~]# mkdir /usr/local/zabbix/logs

更改目录的属主、属组:
[root@cd89128a399a ~]# chown -R zabbix:zabbix /usr/local/zabbix



配置Zabbix本机的客户端:
[root@cd89128a399a ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf
修改:
PidFile=/usr/local/zabbix/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf



配置Zabbix本机的服务端:
[root@cd89128a399a ~]# vi /usr/local/zabbix/etc/zabbix_server.conf
修改:
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=168
DBSocket=/var/lib/mysql/mysql.sock
Timeout=4
LogSlowQueries=3000
Include=/usr/local/zabbix/etc/zabbix_server.conf.d/*.conf

备注:
[root@cd89128a399a ~]# find / -name mysql.sock
/var/lib/mysql/mysql.sock



启动Zabbix本机的客户端和服务端:
[root@cd89128a399a ~]# /usr/local/zabbix/sbin/zabbix_agentd
[root@cd89128a399a ~]# /usr/local/zabbix/sbin/zabbix_server

启动容器时,自动启动Zabbix本机的客户端和服务端:
[root@cd89128a399a ~]# echo '/usr/local/zabbix/sbin/zabbix_agentd' >> /etc/rc.local
[root@cd89128a399a ~]# echo '/usr/local/zabbix/sbin/zabbix_server' >> /etc/rc.local
[root@cd89128a399a ~]# chmod a+x /etc/rc.d/rc.local



复制Zabbix的Web应用程序至Apache网站根目录:
mkdir -p /var/www/html/zabbix/
cp -r /root/zabbix-4.4.5/frontends/php/* /var/www/html/zabbix/


修改Apache的默认首页文件:
[root@cd89128a399a ~]# vi /etc/httpd/conf/httpd.conf
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

修改为:
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>


重启Apache:
[root@cd89128a399a ~]# systemctl restart httpd



修改PHP的配置文件:(这里不修改的话,在Web部分安装时会失败的)
[root@cd89128a399a ~]# vi /etc/php.ini
修改:
max_execution_time = 300
post_max_size = 16M
max_input_time = 300
date.timezone = Asia/Shanghai

追加:
extension=bcmath.so


重启php-fpm:
[root@cd89128a399a ~]# systemctl restart php-fpm




###

在Windows客户端进行Web部分的安装:(建议使用Frefox浏览器)
http://192.168.168.154/zabbix/
图片1.png
注释:实验中,Docker宿主机的IP地址为192.168.168.154


如果提示其他错误,可以修改PHP的配置文件:
图片2.png


这里只需要输入数据库用户zabbix@localhost的密码:
图片3.png


这里保持默认:
图片4.png


图片5.png


来到这里会有问题:
图片6.png

解决方法:
1. 下载文件;
2. 把文件复制到指定位置:
[root@cd89128a399a ~]# cp zabbix.conf.php /var/www/html/zabbix/conf/zabbix.conf.php

3.点击本页面底下的“Finish”按键。


来到这里,点击“Finish”按键就完成安装了:
图片7.png



Admin的登录密码默认为:zabbix
图片8.png

Zabbix的主页:
图片9.png


容器“Rhel8_1”里的Zabbix可以监控其他远程主机:
图片10.png

图片11.png

图片12.png


监测》图形:(查看主机c6的图形)
图片13.png

图片14.png

图片15.png


监测》仪表板:(查看主机c6的告警)
图片16.png

图片17.png
System time is out of sync (diff with Zabbix server > 60s)
中文翻译:
系统时间不同步(与Zabbix服务器的差异>60秒)


在主机c6上进行时间同步:
[root@zabbix_client ~]# ntpdate ntp1.aliyun.com
20 May 23:50:09 ntpdate[112968]: step time server 120.25.115.20 offset 103465536.227679 sec
[root@zabbix_client ~]#


仪表板的告警自动消失:
图片18.png





###

在Zabbix服务器上使用命令zabbix_get检测被监控主机的TCP端口是否处于侦听状态:(TCP 80端口正在侦听时)
[root@cd89128a399a ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.130 -k net.tcp.listen[80]
1
[root@cd89128a399a ~]#


在Zabbix服务器上使用命令zabbix_get检测被监控主机的TCP端口是否处于侦听状态:(TCP 22端口正在侦听时)
[root@cd89128a399a ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.130 -k net.tcp.listen[22]
1
[root@cd89128a399a ~]#


在Zabbix服务器上使用命令zabbix_get检测被监控主机的TCP端口是否处于侦听状态:(TCP 21端口未侦听时)
[root@cd89128a399a ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.168.130 -k net.tcp.listen[21]
0
[root@cd89128a399a ~]#



###

备注:
实验中,容器“Rhel8_1”重启后,Zabbix一切正常。


笺注:
Docker宿主机的防火墙其实无需特意打开任何TCP端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports

[root@centos8 ~]#





相关文章:
CentOS8_在Docker中安装Nagios
CentOS8_在Docker中安装Zabbix5.0.12

CentOS8安装LAMP+phpMyAdmin
CentOS8编译安装Zabbix4.4.5
Zabbix使用Agent监控CentOS6/Redhat6

返回列表