一.环境准备
服务器 | ip |
Prometneus服务器 | 192.168.159.160 |
被监控服务器 | 192.168.159.161 |
grafana服务器 | 192.168.159.162 |
1. 静态ip(要求能上外网)
2. 主机名
各自配置好主机名
# hostnamectl set-hostname master
# hostnamectl set-hostname node1
# hostnamectl set-hostname node2
三台都互相绑定IP与主机名
# vim /etc/hosts
192.168.159.160 master
192.168.159.161 node1
192.168.159.162 node2echo "192.168.159.160 master
192.168.159.161 node1
192.168.159.162 node2">>/etc/hosts3. 时间同步(时间同步一定要确认一下)
[root@master ~]# yum install -y ntpdate && ntpdate time.windows.com4. 关闭防火墙,selinux
# systemctl stop firewalld
# systemctl disable firewalld
# iptables -F
二.安装prometheus
1.从 https://prometheus.io/download/ 下载相应版本,安装到服务器上
官网提供的是二进制版,解压就能用,不需要编译,下面是安装包下载地址
链接:https://pan.baidu.com/s/1A1e1UDMU5cMt6eKfq3IsEg
提取码:r435
上传prometheus-2.5.0.linux-amd64.tar.gz
root@master ~]# ls
prometheus-2.5.0.linux-amd64.tar.gz[root@master ~]# tar -zxvf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local/
[root@master ~]# mv /usr/local/prometheus-2.5.0.linux-amd64/ /usr/local/prometheus
[root@master local]# cd prometheus/
[root@master prometheus]# ls
console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool直接配置文件启动
/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
[root@master ~]# netstat -lntp | grep 9090
tcp6 0 0 :::9090 :::* LISTEN 19328/prometheus
2.prometheus界面
通过浏览器访问http://服务器IP:9090就可以访问到prometheus的主界面
默认只监控了本机一台,点Status --》点Targets --》可以看到只监控了本机
3.主机数据显示
通过http://服务器IP:9090/metrics可以查看到监控的数据
4.在web主界面可以通过关键字查询监控项
三.远程监控node1节点
① 在远程linux主机(被监控端agent1)上安装node_exporter组件
下载地址: https://prometheus.io/download/
上传node_exporter-1.8.2.linux-amd64.tar.gz
[root@node1 ~]# tar -zxvf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
node_exporter-0.16.0.linux-amd64/
node_exporter-0.16.0.linux-amd64/LICENSE
node_exporter-0.16.0.linux-amd64/node_exporter
node_exporter-0.16.0.linux-amd64/NOTICE
[root@node1 ~]# mv /usr/local/node_exporter-0.16.0.linux-amd64/ /usr/local/node_exporter[root@node1 ~]# cd /usr/local/node_exporter
里面就一个启动命令node_exporter,可以直接使用此命令启动
如果把启动node_exporter的终端给关闭,那么进程也会随之关闭。nohup命令会帮你解决这个问题
nohup ./node_exporter &
② 通过浏览器访问http://被监控端IP:9100/metrics就可以查看到 node_exporter在被监控端收集的监控信息
http://192.168.10.65:9100/metrics
③ 回到prometheus服务器的配置文件里添加被监控机器的配置段 在主配置文件最后加上下面三行
[root@master prometheus]# vim /usr/local/prometheus/prometheus.yml
- job_name: 'node1' # 取一个job名称来代 表被监控的机器
static_configs:
- targets: ['192.168.159.161:9100'] # 这里改成被监控机器 的IP,后面端口接9100
改完配置文件后,重启服务
[root@master prometheus]# ps -ef | grep prometheus
root 20436 17886 0 16:00 pts/0 00:00:01 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
root 23037 17886 0 16:33 pts/0 00:00:00 grep --color=auto prometheus
[root@master prometheus]# kill -9 20436
[root@master prometheus]# ps -ef | grep prometheus
root 23069 17886 0 16:34 pts/0 00:00:00 grep --color=auto prometheus#重启prometheus
[root@master prometheus]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
[root@master prometheus]# netstat -lntp |grep 9090
tcp6 0 0 :::9090 :::* LISTEN 23106/prometheus
④ 回到web管理界面 --》点Status --》点Targets --》可以看到多了一台监 控目标
四.远程监控node2 mysql
① 在被管理机node2上安装mysqld_exporter组件
下载地址: https://prometheus.io/download/
上传mysqld_exporter-0.15.1.linux-amd64.tar.gz
[root@node2 ~]# tar xf mysqld_exporter-0.11.0.linux-amd64.tar.gz -C /usr/local/
[root@node2 ~]# mv /usr/local/mysqld_exporter-0.11.0.linux-amd64/ /usr/local/mysqld_exporter
[root@node2 ~]# ls /usr/local/mysqld_exporter/
LICENSE mysqld_exporter NOTICE
#安装mariadb数据库,并授权
[root@node2 mysqld_exporter]# yum install mariadb\* -y
[root@node2 mysqld_exporter]# systemctl start mariadb
[root@node2 mysqld_exporter]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@node2 mysqld_exporter]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (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)]>
MariaDB [(none)]> grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '123';(注意:授权ip为localhost,因为不是prometheus服务器来直接找mariadb 获取数据,而是prometheus服务器找mysql_exporter,mysql_exporter 再找mariadb。所以这个localhost是指的mysql_exporter的IP)
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> exit
#创建一个mariadb配置文件,写上连接的用户名与密码(和上面的授权的用户名 和密码要对应)[root@node2 mysqld_exporter]# vim /usr/local/mysqld_exporter/.my.cnf
[client]
user=mysql_monitor
password=123#启动mysqld_exporter
[root@node2 mysqld_exporter]# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &
#查看启动端口(9104)
② 回到prometheus服务器的配置文件里添加被监控的mariadb的配置段
[root@master ~]# vim /usr/local/prometheus/prometheus.yml
- job_name: 'node2-mysql'
static_configs:
- targets: ['192.168.159.162:9104']
#改完配置文件后,重启服务
[root@master ~]# pkill prometheus
[root@master prometheus]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
#查看端口(9090)
③ 回到web管理界面 --》点Status --》点Targets --》可以看到监控mariadb了
mysql_global_status_threads_connected
五.Grafana可视化图形工具
直接从下面注意开始
yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.3-1.x86_64.rpm
wget https://dl.grafana.com/oss/release/grafana-5.3.4-1.x86_64.rpm
yum localinstall grafana-5.3.4-1.x86_64.rpm -y
systemctl start grafana-server
登录:ip:3000 用户名:admin 密码:admin
2.使用Grafana连接Prometheus
注意:上述的grafana版本过低 改为以下版本
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.3.2-1.x86_64.rpm
yum localinstall grafana-enterprise-9.3.2-1.x86_64.rpm -y
systemctl start grafana-server
二进制文件:
- Grafana 的主程序二进制文件通常位于
/usr/sbin/grafana-server
。配置文件:
- 配置文件默认情况下位于
/etc/grafana/grafana.ini
。这是您可以修改 Grafana 配置选项的地方,如数据库连接、监听端口等。日志文件:
- 日志文件一般保存在
/var/log/grafana/grafana.log
中。查看日志文件对故障排查和监控运行状态非常有用。插件和数据:
- 插件一般存放于
/var/lib/grafana/plugins
。- SQLite 数据库文件(如果没有使用其他数据库)也是存放在
/var/lib/grafana/grafana.db
。Web文件和静态资源:
- Web页面的相关静态文件存储在
/usr/share/grafana
。服务文件:
systemd
服务文件是/usr/lib/systemd/system/grafana-server.service
,它用于定义 Grafana 如何作为服务运行。
然后登录grafana页面
设置中文
选择Prometheus导入数据源
导入模板8919对应node_exports(安装node_exports机器)
可以看到如上页面
mysql_exports 12826模板
启动时间 time() - process_start_time_seconds
连接数 rate(mysql_global_status_connections[1m])
每一项需要找对应的查询命令