一:环境规划:
主机名 | 主机地址 | 角色 |
node4 | 192.168.188.114 | prometheus客户端 |
node5 | 192.168.188.115 | prometheus服务端 |
二. 监控远程linux主机:
1. 解压node_exporter压缩包:
[root@node4 ~]# tar xf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
2. 对压缩后的文件做软连接:
[root@nnode4 ~]# ln -sv /usr/local/node_exporter-1.3.1.linux-amd64/ /usr/local/node_exporter
3. 创建用户:
[root@node4 ~]# useradd prometheus -M -s /sbin/nologin
4. 修改" /usr/local/node_exporter/ "目录属主:
[root@node4 ~]# chown -R prometheus /usr/local/node_exporter/*
5. 配置node_exporter服务启动文件:
[root@node4 ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
user=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WanteBy=multi-user.target
6. 启动node_exporter服务:
[root@node4 ~]# systemctl daemon-reload
[root@node4 ~]# systemctl enable --now node_exporter.service
## node_exporter默认端口是9100,查看9100是否监听:
[root@node4 ~]# ss -lntup | grep 9100
tcp LISTEN 0 4096 [::]:9100 [::]:* users:(("node_exporter",pid=22143,fd=3))
7. prometheus服务端测试是否能获取数据:
[root@node5 ~]# curl 192.168.188.114:9100/metrics
8. 修改prometheus服务端配置文件:
[root@node5 ~]# vim /usr/local/prometheus/prometheus.yml
scrape_configs:
## 添加的内容:添加监控的工作
- job_name: "node4"
static_configs:
- targets: ["192.168.188.114:9100"]
## 重启prometheus服务:
[root@node5 ~]# systemctl restart prometheus.service
9. 查看监控的工作是否添加:
三. 监控远程MySQL:
1. 安装数据库MariaDB:
[root@node4 ~]# yum install mariadb-server
2. 登录数据库,授权用户:
[root@node4 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-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)]> grant select,replication client,process on *.* to mysql_monitor@localhost identified by '123456';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> exit
Bye
注意:授权ip为localhost,因为不是prometheus服务器来直接找mariadb获取数据,
而是prometheus服务器找mysql_exporter,mysql_exporter再找mariadb. 所以这个
localhost是指的mysql_exporter所在主机的IP
3. 解压mysqld_exporter压缩包:
[root@node4 ~]# tar xf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local/
4. 对压缩后的文件做软连接:
[root@node4 ~]# ln -sv /usr/local/mysqld_exporter-0.14.0.linux-amd64/ /usr/local/mysqld_exporter
5. 配置mysqld_exporter服务启动文件:
[root@node4 ~]# vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target
6. 创建一个mariadb配置文件,写上连接的用户名与密码(授权的用户名和密码):
[root@node4 ~]# vim /usr/local/mysqld_exporter/.my.cnf
[client]
user=mysql_monitor
password=123456
7. 修改" /usr/local/mysqld_exporter/ "目录属主:
[root@node4 ~]# chown -R prometheus.prometheus /usr/local/mysqld_exporter/*
8. 启动mysqld_exporter服务:
[root@node4 ~]# systemctl daemon-reload
[root@node4 ~]# systemctl enable --now mysqld_exporter.service
## mysqld_exporter默认是9104端口,查看端口是否监听:
[root@node4 ~]# ss -lntup | grep 9104
tcp LISTEN 0 4096 [::]:9104 [::]:* users:(("mysqld_exporter",pid=17094,fd=3))
9. prometheus服务端测试是否能获取数据:
[root@node5 ~]# curl 192.168.188.114:9104/metrics
10. 修改prometheus服务端配置文件:
[root@node5 ~]# vim /usr/local/prometheus/prometheus.yml
scrape_configs:
## 添加的内容:添加监控的工作
- job_name: "node4-mariadb"
static_configs:
- targets: ["192.168.188.114:9104"]
## 重启prometheus服务:
[root@node5 ~]# systemctl restart prometheus.service
11. 查看监控的工作是否添加: