监控告警 Prometheus+Grafana+cAdvisor

220 阅读3分钟

监控告警工具

更新时间:2024年3月27日

简单介绍

  • Exporter 主机监控工具
  • cAdvisor 容器监控工具
  • Prometheus 时序数据库,用来存储和查询监控数据
  • Grafana 仪表盘,显示数据

简单理解:Exporter 和 cAdvisor 监控目标(主机,容器等)并生成监控数据(cpu 使用率,内存使用率等);Prometheus 获取 Exporter 和 cAdvisor 产生的监控数据,并且作为时序数据库,它存储监控数据;Grafana 从 prometheus 时序数据库中读取监控数据 并 展示到前端页面。

图片1-1711531455788-3.png

图 1:组件结构

在实际使用中,根据需求进行组合,例如:

需求 1:监控主机数据,通过代码获取监控数据

  • 使用 prometheus + exporter

需求 2:可视化容器资源占用

  • 使用 grafana + prometheus + cAvisor

备注:还有很多优秀的监控告警工具,本文只列出作者常用的几个

部署配置步骤

Prometheus

  1. 拉取镜像
docker pull prom/prometheus

prometheus 的默认配置文件 prometheus.yml

  1. 配置文件

在这里假设我们保存的路径为 /opt/prometheus/prometheus.yml,内容如下

global:
  scrape_interval: 15s # 每15s采集一次数据
  evaluation_interval: 15s # 每15s做一次告警检测

alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  1. 启动容器
docker run --name prometheus -d -p 9090:9090 -v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
  1. 访问其Web管理页面

    地址为:http://localhost:9090

Exporter

  1. 下载 node-exporter

    根据系统信息选择响应的node-export,比如:node_exporter-1.7.0.linux-amd64.tar.gz

    下载连接为:github.com/prometheus/…

  2. 解压并启动

tar xvfz node_exporter-1.7.0.linux-amd64.tar.gz
cd node_exporter-1.7.0.linux-amd64
# 后台运行
./node_exporter &
  1. 访问 web 页面查看内容

    地址为:http://localhost:9100/metrics

  2. 配置并重启 prometheus

    /opt/prometheus/prometheus.yml 添加内容

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  # 收集主机的监控数据
  - job_name: 'myLinux'
    static_configs:
      - targets: ['localhost:9100']

备注:可以自行搜索 node-export 开机自启动教程

Grafana

  1. 拉取镜像 并 启动
docker pull grafana/grafana

docker run --name grafana -d -p 3000:3000 grafana/grafana
  1. 配置 grafana
  • 通过 http://localhost:3000 访问配置页面, 默认的账户:admin 密码: admin; 登录之后需要修改密码

image-20240327153635856.png

  • 添加监控数据源:Connections,搜索 prometheus 并 点击;

image-20240327154409777.png

再点击 add new data source

image-20240327154536718.png

在配置页面只需要输入 promethues 的网址 URL,例如 http://127.0.0.1:9090

image-20240327154730497.png

下滑点击 save & test

image-20240327154844757.png

可以设置搜索选项,例如:data source 设置为 prometheus

选择需要的模板,例如:node exporter full

image-20240327160553798.png

点击之后,会看到下面的界面,复制 ID 信息

image-20240327160614255.png

  • 添加仪表盘:

点击左侧的 dashboards \to new \to import

image-20240327160500758.png

输入模板ID,并点击 load

image-20240327160710574.png

选择 prometheus \to import

image-20240327160748165.png

最终成功

image-20240327160938168.png

cAdvisor

  1. 拉取镜像 并 启动容器
docker pull google/cadvisor

docker run	--volume=/:/rootfs:ro --volume=/var/run:/var/run:ro --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --volume=/dev/disk/:/dev/disk:ro --publish=8080:8080 --detach=true --name=cadvisor --privileged --device=/dev/kmsg google/cadvisor 
  1. 访问页面

    http://localhost:8080

  2. 在 promethus 中添加配置并重启

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  # 监控主机
  - job_name: 'myLinux'
    static_configs:
      - targets: ['localhost:9100']

	# 监控 容器
	- job_name: 'cAdvisor'
    static_configs:
      - targets: ['localhost:8080']
  1. 通过配置 Grafana 进行可视化

    比如 193 模板

image-20240327164106400.png