目录
使用命令存在的问题
- 学习成本高
- 只能操作一台机器
- 发送了命令才能知道运行情况
- 开发人员不一定能直接触碰生产环境
诉求
- 出现问题自动告警
- 可视化图表展示,上手成本低
- 能够监控多台服务器的情况
- Zabbix、Nagios、open-falcon...
Zabbix是什么
- 开源监控软件,可用来监控和跟踪各种网络服务,服务器和其他网络硬件的状态
- 使用到几个组件
- Server:Zabbix的Server端
- Database:Zabbix数据库
- Agent:采集数据
- 主动模式:由 Agent 主动简历TCP链接并向 Server端发送请求
- 被动模式:由 Server 建立TCP链接并向 Agent端发送请求
- Proxy:代理多台机器
- Web UI:界面,也叫Zabbix frontend
- Server:Zabbix的Server端
- 使用到几个组件
安装Zabbix
1. 选择您Zabbix服务器的平台, 生成指令
2. 为您的平台安装和配置Zabbix服务器
- 安装 Zabbix 的 repository
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
yum clean all
如果网络实在太差,可使用阿里云提供的镜像地址安装:
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
yum clean all
# 将 /etc/yum.repos.d/zabbix.repo中的http://repo.zabbix.com替换成https://mirrors.aliyun.com/zabbix
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
3. 安装Zabbix Server与agent
yum install zabbix-server-mysql zabbix-agent -y
4. 安装Zabbix frontend (Zabbix界面)
- 启用Red Hat软件集
yum install centos-release-scl -y
- 修改 /etc/yum.repos.d/zabbix.repo, 并启用Zabbix frontend
vim /etc/yum.repos.d/zabbix.repo
[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1 ## 从 0 修改成 1, 表示开启仓库
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
- 安装zabbix-frontend的包
yum install zabbix-web-mysql-scl zabbix-nginx-conf-scl -y
5. 初始化数据,导入MySQL
- 创建数据库与用户,并授权
进入容器
docker exec -it mysql8 bash
root@1e5ac696cabd:/# mysql -uroot -pabc123456
数据库终端
-- 创建库:zabbix
mysql> create database zabbix character set utf8 collate utf8_bin;
-- 创建用户:zabbix 密码:password
mysql> create user zabbix@localhost identified with mysql_native_password by 'password';
mysql> create user zabbix@192.168.8.108 identified with mysql_native_password by 'password';
-- 为用户:zabbix 授权zabbix这个库的权限
mysql> grant all privileges on zabbix.* to zabbix@localhost;
mysql> grant all privileges on zabbix.* to zabbix@192.168.8.108;
-- 退出
mysql> quit;
导入初始架构和数据,系统提示您输入新创建的账户密码
-- 查看初始数据在哪里
[root@8_100 ~]# find / -name create.sql.gz
/usr/share/doc/zabbix-server-mysql-5.0.12/create.sql.gz
-- 宿主机复制到容器里面
[root@8_100 ~]# docker cp /usr/share/doc/zabbix-server-mysql-5.0.12/create.sql.gz mysql8:/
-- 进入容器
[root@8_100 ~]# docker exec -it mysql8 bash
root@1e5ac696cabd:/# ls
bin create.sql.gz docker-entrypoint-initdb.d etc lib logs mnt proc run srv tmp var
boot dev entrypoint.sh home lib64 media opt root sbin sys usr ~
-- 停顿完后,导入成功!
root@1e5ac696cabd:/# zcat create.sql.gz | mysql -uzabbix -p zabbix
Enter password:
root@1e5ac696cabd:/#
修改PHP页面连接MySQL的DB参数
[root@localhost ~]# cd /usr/share/zabbix/conf/
[root@localhost conf]# cp zabbix.conf.php.example zabbix.conf.php
[root@localhost conf]# vim zabbix.conf.php
<?php
// Zabbix GUI configuration file.
$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = '192.168.8.100';
$DB['PORT'] = '11337';
$DB['DATABASE'] = 'zabbix';
$DB['USER'] = 'zabbix';
$DB['PASSWORD'] = 'password';
....
PHP 连接 MySQL8.0 出现the server requested authentication method unknown to the client
6. 修改Zabbix Server配置文件
-- 搜索文件在哪里
[root@8_100 ~]# find / -name zabbix_server.conf
/etc/zabbix/zabbix_server.conf
-- 修改 DBPassword 和 DBUser
[root@8_100 ~]# vim /etc/zabbix/zabbix_server.conf
...
# 设置成zabbix数据库的密码
DBPassword=password
DBPort=11337
DBUser=zabbix
DBName=zabbix
DBHost=192.168.8.100
...
:wq
7. 按需为Zabbix frontend配置PHP
- 按需修改Nginx配置文件,将以下两行的注释解开
[root@8_100 ~]# find / -name zabbix.conf
/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
/etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf
[root@8_100 ~]# vim /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf
server {
# listen 80;
# server_name example.com;
listen 8067;
server_name 127.0.0.1;
[root@localhost ~]# netstat -ntlp | grep 8067
tcp 0 0 0.0.0.0:8067 0.0.0.0:* LISTEN 16581/nginx: master
- 配置 PHP
[root@8_100 ~]# vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
listen.acl_users = apache 追加 listen.acl_users = apache,nginx
; php_value[date.timezone] = Europe/Riga 修改时区 php_value[date.timezone] = Asia/Shanghai
8. 启动Zabbix server和agent进程
启动Zabbix server和agent进程,并为它们设置开机自启:
systemctl restart zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
9. 访问Zabbix
- http://192.168.8.108:8067
- Next step
- Next step
- 填写数据库相关信息 ==> Next step
- Name输入:test ==> Next step
- Next step
- Finish
- 账户密码:Admin/zabbix ==> sign in
添加监控机器
想要监控的机器需要安装zabbix-agent
1. 安装Zabbix的repository
rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
yum clean all
如果网络实在太差,可使用阿里云提供的镜像地址安装:
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
yum clean all
# 将 /etc/yum.repos.d/zabbix.repo中的http://repo.zabbix.com替换成https://mirrors.aliyun.com/zabbix
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
2. 安装Zabbix Server与agent
yum install zabbix-agent -y
3. 配置
[root@k8s-node1 ~]# vim /etc/zabbix/zabbix_agentd.conf
# 被动模式下必填,填写zabbix-server端的ip
Server=192.168.8.108
# 主动模式下必填,填写zabbix-server端的ip
ServerActive=192.168.8.108
# 主机名,需要与zabbix-的网页上创建的主机名一致
Hostname=test
4. 启动
systemctl start zabbix-agent
systemctl status zabbix-agent
5. WEB配置,添加监控机器
- Configuration
- Host
- Host name : test ## 需要和zabbix_agentd.conf下的Hostname保持一致
- Agent : 192.168.8.242 ## 填写内网IP
- Groups : Linux servers
- Templates
- Select ==> Templates/Operating systems
- 扣选 ==> Template OS Linux by Zabbix agent (绿色就可以了)
- Host
TIPS
如果你的Zabbix要监控的机器非常多,还需要安装Zabbix Proxy, Proxy可以认为是一个“太监版”的Zabbix Server, 安装方式和Server比较类似
参考文档:https://www.cnblogs.com/irockcode/p/6754659.html