Prometheus
1.Prometheus部署
1.1准备一台虚拟机(过程略)
1.2下载Prometheus文件
https://prometheus.io/download/
1.2部署(参考:https://prometheus.io/docs/prometheus/latest/getting_started/)
- 上传jar包到服务器
- 解压文件
tar -zxvf prometheus-2.31.1.linux-amd64.tar.gz
- 配置文件 prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
- 启动
./prometheus --config.file=prometheus.yml &
- 访问Prometheus http://192.168.40.100:9090/
2.监控
2.1监控远程主机
1.在被监控的主机上安装插件 node_exporter (官网下载 https://prometheus.io/download/)
这个插件主要收集系统信息
要收集其它信息需要找对应插件,例如MySQL是mysqld_exporter
- 解压文件并重名
tar -zxvf node_exporter-1.2.2.linux-amd64.tar.gz
mv node_exporter-1.2.2.linux-amd64 node_exporter
2.启动 node_exporter
# 默认使用的是9100端口
nohup /root/prometheus/exporter/node_exporter/node_exporter &
3.查看被监控端数据
# http://被监控端IP:9100/metrics
http://192.168.40.110:9100/metrics
http://192.168.40.120:9100/metrics
http://192.168.40.130:9100/metrics
4.将需要监控的机器加入Prometheus配置文件
- 修改Prometheus的配置文件prometheus.yml
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# 新增内容↓
- job_name: "hadoop"
static_configs:
- targets: ["192.168.40.110:9100", "192.168.40.120:9100", "192.168.40.130:9100"]
- 重启Prometheus
pkill prometheus
- 热更新配置
每次改配置重启显然太蠢
为了能够热更新启动时需要加上参数 --web.enable-lifecycle
./prometheus --config.file=prometheus.yml --web.enable-lifecycle &
curl -X POST http://localhost:9090/-/reload
2.2监控MySQL
1.在被监控的主机上安装插件 mysqld_exporter(官网下载 https://prometheus.io/download/)
2.解压并重名
# 解压
tar -zxvf mysqld_exporter-0.13.0.linux-amd64.tar.gz
# 重命名
mv mysqld_exporter-0.13.0.linux-amd64 mysqld_exporter
3.在mysqld_exporter文件夹中配置数据库
vim my.conf
[client]
user=root
password=123456
4.启动mysql_exporter
# 进入解压后的文件夹,指定配置文件启动
nohup ./mysqld_exporter --config.my-cnf=my.conf &
5.将需要监控的数据库加入Prometheus配置文件
# 修改Prometheus的配置文件prometheus.yml
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# 新增内容
- job_name: "mysql"
static_configs:
- targets: ["192.168.0.122:9104"]
6.更新配置
curl -X POST http://localhost:9090/-/reload