0%

从零搭建运维监控系统-Prometheus

Prometheus是一个用 Golang 编写的开源的系统监控和警报工具包,能够收集和处理来自各种目标的指标,还可以查询、查看、分析指标并根据阈值收到警报。

一、基础介绍

  1. Prometheus是一个用 Golang 编写的开源的系统监控和警报工具包,能够收集和处理来自各种目标的指标,还可以查询、查看、分析指标并根据阈值收到警报。自2012年启动以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员和用户社区。它现在是一个独立的开源项目,独立于任何公司进行维护。Prometheus于2016年加入云原生计算基金会,成为继Kubernetes之后的第二个托管项目。
    • 在Prometheus的架构设计中,Prometheus Server并不直接服务监控特定的目标,其主要任务负责数据的收集,存储并且对外提供数据查询支持。因此为了能够监控到某些东西,如主机的CPU使用率,我们需要使用到Exporter。Prometheus周期性的从Exporter暴露的HTTP服务地址(通常是/metrics)拉取监控样本数据。
  2. Exporter是Prometheus的指标数据收集组件。它负责从目标Jobs收集数据,并把收集到的数据转换为Prometheus支持的时序数据格式。和传统的指标数据收集组件不同的是,他只负责收集,并不向Server端发送数据,而是等待Prometheus Server 主动抓取,node-exporter 默认的抓取url地址:http://ip:9100/metrics。
    • Prometheus可以使用Pushgateway这个组件推送node-exporter的指标数据到远端Prometheus,node-exporter用于采集node的运行指标,包括node的cpu、load、filesystem、meminfo、network等基础监控指标,类似于zabbix监控系统的的zabbix-agent。node-exporter由Prometheus官方提供、维护,属于监控指标收集类UNIX内核操作系统的必备的exporter。
  3. ‌‌Grafana是一个开源的‌数据可视化工具,它提供了强大的数据可视化功能、灵活的告警系统、用户和角色管理、插件系统等核心功能。‌
    • Grafana的架构由前端、后端和数据源组成。前端是基于‌React的单页应用,后端是基于‌Go语言的服务器,数据源通过插件与Grafana集成。这种架构设计使得Grafana能够灵活地适应不同的数据源和用户需求。
    • Grafana支持与多种数据源(如Elasticsearch、InfluxDB、Prometheus等)集成,使得它能够展示来自这些数据源的数据。

二、安装使用

  1. 安装prometheus
1
2
3
4
5
6
7
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus-2.54.1/prometheus --storage.tsdb.path=/usr/local/prometheus-2.54.1/data --config.file=/usr/local/prometheus-2.54.1/prometheus.yml
[Install]
WantedBy=multi-user.target
  • systemctl start prometheus
  • systemctl status prometheus
  • systemctl restart prometheus
  • systemctl stop prometheus
  1. 安装go环境
    • cd /usr/local/src
    • wget https://go.dev/dl/go1.23.0.linux-386.tar.gz
    • tar -zxvf go1.23.0.linux-386.tar.gz
    • mv go /usr/local
    • export GOROOT=/usr/local/go
    • export GOPATH=/home/gopath
    • export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
      • GOROOT 是 go 的安装目录
      • GOPATH 是 go 的工作目录
      • PATH 是环境变量
    • go version
  2. 安装node_exporter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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"
static_configs:
- targets: ["localhost:9090"]
- job_name: "node-exporter"
metrics_path: /metrics
static_configs:
- targets: ["localhost:9100"]
  • ./prometheus –config.file=prometheus.yml
  • 浏览器访问:http://ip:9090/targets
  • 添加到系统服务:vi /usr/lib/systemd/system/node_exporter.service
1
2
3
4
5
6
7
8
[Unit]
Description=Prometheus node_exporter
[Service]
User=nobody
ExecStart=/usr/local/node_exporter-1.8.2/node_exporter --log.level=error
ExecStop=/usr/bin/killall node_exporter
[Install]
WantedBy=default.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Install  1 Package

Total size: 122 M
Installed size: 460 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : grafana-enterprise-11.2.0-1.x86_64 1/1
Running scriptlet: grafana-enterprise-11.2.0-1.x86_64 1/1
### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemd
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable grafana-server.service
### You can start grafana-server by executing
sudo /bin/systemctl start grafana-server.service

POSTTRANS: Running script

Verifying : grafana-enterprise-11.2.0-1.x86_64 1/1

Installed:
grafana-enterprise-11.2.0-1.x86_64

Complete!
  • systemctl enable grafana-server
  • systemctl start grafana-server
  • systemctl restart grafana-server
  • systemctl stop grafana-server
  • 访问:http://101.200.15.11:3000
    • 默认:admin/admin,登录成功后会提示修改密码
    • 修改:admin/youshu123!@#
  • 配置prometheus数据源
    • add your first data source
    • prometheus
    • 输入链接
    • save & test
  • 配置显示面板
    • New
    • New dashboard
    • Import a dashboard
    • 输入:8919,详见
      1. 监听其他服务器

三、参考

  1. https://www.prometheus.io/
  2. https://prometheus.io/download/
  3. https://github.com/prometheus/prometheus
  4. https://grafana.com/