搭建监控:prometheus grafana nodeexporter

prometheus

下载地址

  • prometheus.io/download/

image.png

启动/停止

# 解压目录
~/monitor/prometheus/prometheus-2.45.0.linux-amd64
1. 编写启动脚本
echo ' ./prometheus --config.file=./prometheus.yml &' > start.sh && chmod +x start.sh
1. 编写停止脚本
echo ' pkill prometheus' > stop.sh && chmod +x stop.sh
1. 启动
nohup sh start.sh &

配置文件

cat 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.
  1. scrape_timeout is set to the global default (10s).

1. Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          1. - alertmanager:9093

1. Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  1. - "first_rules.yml"
  1. - "second_rules.yml"

1. A scrape configuration containing exactly one endpoint to scrape:
1. Here it's Prometheus itself.
scrape_configs:
  1. The job name is added as a label `job=` to any timeseries scraped from this config.
  - job_name: "prometheus"

    1. metrics_path defaults to '/metrics'
    1. scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  1. 采集node exporter监控数据
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']
  - job_name: 'your application'
    scheme: https
    tls_config:
      insecure_skip_verify: true
    static_configs:
      - targets: [ 'localhost:9098' ]
        labels:
          application: 'your application'

注意:由于应用程序开启了https,可以简单通过tls_config跳过验证

验证

prometheus自带了前段页面。 可以在浏览器中输入:localhost:9090打开。记得打开9090端口的访问权限

image.png

image.png

grafna

下载

  • grafana.com/grafana/dow…
  • 两种文件格式,任选其一。tar.gz是二进制文件,可以直接执行。本文使用的是二进制文件。关于rpm的安装与启动,参考文末文档。

image.png

启动/停止

cd ~/monitor/grafana
nohup ./bin/grafana server &

访问

在浏览器输入localhost:3000。 会提示输入账号密码,默认都是admin。然后修改一下密码。

配置数据源

image.png

选择prometheus作为数据源

image.png

输入prometheus的地址 image.png

http方法改为get,保存 image.png 选择一个dashbboard image.png

然后就能看到效果了。

image.png

node-exporter

下载

  • prometheus.io/download/#n…

安装/启动/停止

cd ~/monitor/prometheus/node_exporter-1.6.1.linux-amd64

1. 编写启动脚本
echo ' ./node_exporter &' > start.sh && chmod +x start.sh
1. 编写停止脚本
echo ' pkill node_exporter ' > stop.sh && chmod +x stop.sh

nohup sh start.sh &

配置prometheus.yml

scrape_configs:
  1. The job name is added as a label `job=` to any timeseries scraped from this config.
  - job_name: "prometheus"

    1. metrics_path defaults to '/metrics'
    1. scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  1. 采集node exporter监控数据
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']

配置grafana dashboard

  • grafana.com/grafana/das…

image.png

拷贝模版号 image.png

  • 在grafana导入模版

image.png

输入拷贝的模板号,点击load

image.png

选择数据源,点击import image.png 查看效果

image.png

参考

-Prometheus+Grafana 监控服务器CPU、磁盘、内存等信息

-Configure Prometheus AlertManager

-Prometheus+Grafana+Alertmanager部署教程

-Systemd 入门教程:实战篇