目录
一、node_export 监测服务器
下载路径:
链接:https://pan.baidu.com/s/1q1Lj-N_H2PV5DryLaTZF6A
提取码:0hu2
# 我以这个为例
# 我是使用这个网址去下载https://prometheus.io/download/#node_exporter 最新版本,然后放到linux机器上去解压安装
# 解压
tar -zxvf node_exporter-1.1.2.linux-amd64.tar.gz
# 修改名称为node_exporter
mv node_exporter-1.1.2.linux-amd64 node_exporter
# 切换到该目录
cd node_exporter
# 执行
./node_exporter
#启动后你将会看到下面的显示的界面且可以清晰看到此服务运行绑定端口号为9100如下所示:
浏览器可以访问此端口以查看效果(需开放此端口或者关闭防火墙):
http://192.168.233.129:9100
# 新增开机启动文件,
vim /lib/systemd/system/node_exporter.service
# 输入以下内容.注意下面那个路径要与实际文件一致性
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
ExecStart=/opt/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
:wq 保存
#############################################################
#重新载入配置文件
systemctl daemon-reload
#设置为开机自启
systemctl enable node_exporter.service
#其他常用命令
systemctl start node_exporter.service
#启动 node_exporter 服务
systemctl stop node_exporter.service
#关闭 node_exporter 服务
systemctl restart node_exporter.service
#重启 node_exporter 服务
二、prometheus
官方下载地址:Download | Prometheus
下载prometheus安装包:
# 下载
wget https://github.com/prometheus/prometheus/releases/download/v2.18.1/prometheus-2.18.1.linux-amd64.tar.gz
# 解压
tar -zxvf prometheus-2.18.1.linux-amd64.tar.gz
# 修改名称为prometheus
mv prometheus-2.18.1.linux-amd64 prometheus
# 切换到该目录
cd prometheus
# 发现yml文件没有权限
ls
chmod 777 prometheus.yml
vim prometheus.yml
# 在对应区域加入,注意格式这是yml
- job_name: 'export_test2'
static_configs:
- targets: ['10.211.55.5:9100']
labels:
instance: 'node2'
# 启动
./prometheus --config.file=prometheus.yml
恭喜你已经成功安装好了prometheus,浏览器IP+端口9090
# 新增开机启动文件
vim /lib/systemd/system/prometheus.service
# 输入以下内容
[Unit]
Description=Prometheus
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
[Install]
WantedBy=multi-user.target
:wq 保存
#############################################################
#重新载入配置文件
systemctl daemon-reload
#设置为开机自启
systemctl enable prometheus.service
#其他常用命令
systemctl start prometheus.service
#启动 prometheus 服务
systemctl stop prometheus.service
#关闭 prometheus 服务
systemctl restart prometheus.service
#重启 prometheus 服务
三、grafana
# 下载
wget https://dl.grafana.com/oss/release/grafana-6.7.2-1.x86_64.rpm
#安装
yum localinstall -y grafana-6.7.2-1.x86_64.rpm
# 启动服务并添加开机启动
systemctl start grafana-server
systemctl enablegrafana-server.service
四、influxdb
InfluxDB(时序数据库)常用的一种使用场景:服务器监控数据统计然后将数据统计汇总并借助Grafana进行图形化展示
官方下载地址:Downloads
# 下载
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.5.2.x86_64.rpm
# 安装
sudo yum localinstall influxdb-1.5.2.x86_64.rpm
# 启动InfluxDB服务、添加开机启动:
systemctl start influxdb
systemctl enable influxdb
# 当安装完毕后输入
influx
# 然后就弹出如下界面:
# 创建名称为prometheus的数据库实例
create database prometheus
# 切换数据库实例prometheus
use prometheus
# 创建用户名和密码都为node的用户,注意密码只能用''字符否则influxdb将会报错
create user "node" with password 'node'
配置 Remote storage adapter
这个是prometheus 官方提供的写适配器插件,通过Prometheus的远程写协议接收样本,并将它们存储在Graphite, InfluxDB, or OpenTSDB 中。
下载此插件需要机器拥有go环境,这样就可以自主编译remote_storage_adapter插件,关于go环境配置这里就不过多介绍读者可以从网上很多博客找到相关素材。
编译好后即可运行插件,如果没有go环境也不想编译此组件,也可以下载这个编译好的组件:remote_storage_adapter[存储在我的网盘]
./remote_storage_adapter --influxdb-url=http://127.0.0.1:8086/ --influxdb.database="prometheus" --influxdb.retention-policy=autogen
配置 prometheus.yml
# 远程写配置
remote_write:
- url: "http://localhost:9201/write"
# 配置连接influxdb连接的用户名与密码
basic_auth:
username: node
password: node
# 远程读配置
remote_read:
- url: "http://localhost:9201/read"
basic_auth:
username: node
password: node
五、mysqld_exporter
# 新增用户
useradd -m -s /bin/false mysqld_exporter
# 我以这个为例
# 我是使用这个网址去下载https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz ,然后放到linux机器上去解压,路径/opt
# 解压
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz
# 修改名称为mysqld_exporter
mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
# 文件许可
chown mysqld_exporter:mysqld_exporter /opt/mysqld_exporter
#进入容器,因为我mysql使用的是docker安装的。如果不是忽略 77打头是容器ID
sudo docker exec -it 775c7c9ee1e1 /bin/bash
或 sudo docker exec -it 0e09e7e8d3ac /bin/sh
#进入mysql
mysql -u root -p
# 输入密码 回车
CREATE USER 'mysqld_exporter'@'localhost' IDENTIFIED BY 'StrongPassword' WITH MAX_USER_CONNECTIONS 2;
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'mysqld_exporter'@'localhost';
grant all privileges on *.* to 'mysqld_exporter'@'%' identified by 'StrongPassword';
FLUSH PRIVILEGES;
quit
# 重启mysql服务,之前一直不行 重启服务就好了这个一般不需要service mysqld restart
service mysql restart
# 退出容器
exit
创建配置文件
# 创建配置文件
vi /etc/.mysqld_exporter.cnf
# 输入 账户密码端口根据实际来
[client]
host=127.0.0.1
port=3313
user=mysqld_exporter
password=StrongPassword
# :wq,保存 查看文件
ls -a
# 设置.mysqld_exporter.cnf文件的文件许可权
chown mysqld_exporter:mysqld_exporter /etc/.mysqld_exporter.cnf
vim /lib/systemd/system/mysqld_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=mysqld_exporter
Group=mysqld_exporter
Restart=always
ExecStart=/opt/mysqld_exporter/mysqld_exporter --config.my-cnf=/etc/.mysqld_exporter.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target
# 接下来,启动并启用mysqld_exporter服务
systemctl start mysqld_exporter
systemctl enable mysqld_exporter
#为了确保该服务正在运行,请执行:
systemctl status mysqld_exporter
ip+端口访问
如果启动异常,查看端口,然后kill 6517
修改 vim prometheus.yml
# 新增这个
- job_name: mysq1245 # 这个应该是监控mysq1
static_configs:
- targets: ['IP:9104']
labels:
instance: mysql245