「Linux」- 搭建 Prometheus 监控(单机,安装,部署,快速开始,实验环境)

问题描述

Prometheus 是开源的系统监控和告警工具集(官方解释)。但是我们提到 Prometheus 术语时,多半是指存储监控数据的服务。

在 Prometheus Monitoring System 中:Exporters 负责采集指标,并通过 HTTP 来暴露关键指标;Prometheus 定期请求 Exporters 以拉取指标并存储,然后评估是否达到告警阈值;Alertmanager 负责发送来自 Prometheus 的告警请求;Grafana 提供 Web 界面,读取 Prometheus 数据来展示各种指标。

该笔记将记录:在 Linux 中,如何部署 Prometheus 监控,以实验体验为目的,以及相关问题的解决思路。

解决方案

注意事项:
1)该笔记仅记录如何部署整套监控系统,但并未涵盖如何使用及如何配置,因为这涉及相当多的内容;
2)这是我们早期在刚接触云原生时记录的实验笔记。现在,我们已经在 Kubernetes 环境中部署监控;

官方提供多种部署方式(这些并非我们常用部署方式):
1)Using pre-compiled binaries
2)From source
3)Using Docker
4)Using configuration management systems
5)通过 Minikube 提供的内置部署方式;

环境信息

操作系统:Debian GNU/Linux 10 (buster)
软件版本:Node Exporter 0.17.0;Alertmanager 0.15.3;Prometheus 2.7.1;Grafana 7.5.7;

部署 Node Exporter 服务

apt-get install -y prometheus-node-exporter

systemctl enable prometheus-node-exporter.service
systemctl start prometheus-node-exporter.service

# 请求 Node Exporter 服务,以检查服务正常运行
curl http://127.0.0.1:9100/metrics

补充说明:Node Exporter 是众多 Exporter 中的某一个,还有比如 MySQL server exporter、Memcached exporter、JIRA exporter 等等,参考 Exporters and integrations 页面。

部署 Alertmanager 服务

apt-get install -y prometheus-alertmanager

systemctl enable prometheus-alertmanager
systemctl start prometheus-alertmanager

# 请求 Alertmanager 服务,以检查服务正常运行
curl http://127.0.0.1:9093/-/ready

部署 Prometheus 服务

apt-get install -y prometheus

修改配置文件,以自动抓取 Node Exporter 暴露的指标:

...
scrape_configs:
...
  - job_name: node
    static_configs:
      - targets: ['localhost:9100']
...

修改配置文件,以将告警信息发送到 Alertmanger 服务:

...
alerting:
  alertmanagers:
  - static_configs:
    - targets: ['localhost:9093']
...

启动服务:

systemctl enable prometheus.service
systemctl start prometheus.service

# 请求 Prometheus 服务,以检查服务正常运行
curl http://127.0.0.1:9090

部署 Grafana 服务

apt-get install -y apt-transport-https
apt-get install -y software-properties-common wget

wget -q -O - https://packages.grafana.com/gpg.key | apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | tee -a /etc/apt/sources.list.d/grafana.list

apt-get update
apt-get install -y grafana

systemctl daemon-reload
systemctl enable grafana-server.service
systemctl start grafana-server

# 请求 Grafana 服务,以检查服务正常运行
curl http://127.0.0.1:3000

为了能使 Grafana 能够显示 Prometheus 中的数据,在访问 Grafana 界面后:
1)Configuration => Data Sources => Add data source => Prometheus => Select
2)根据提示,填写相关信息,Save & Test
3)在 Explore 中,输入某个指标,并 Run Query 查看结果。

然后,为了能够图形化显示 Node Exporter 暴露的指标,我们需要在 Grafana 中配置 Dashboard(本质上还是配置查询)。当然已经有开箱即用的 Node Exporter Dashboard 配置,因此我们能够直接在 Grafana 中导入该 Dashboard 配置:
1)访问 Node Exporter Full dashboard for Grafana 下载该 Dashboard 的 .json 配置文件;
2)在 Grafana 中,Create(+)=> Import => Upload JSON file => Import;
3)然后,我们便能够查看该 Dashboard 的信息,以及其展示的 Node Exporter 指标;

学习路线

                     +------ Grafana
                     |
                     |
Exporter ------- Prometheus ------> Alertmanager

参考文献

Install on Debian/Ubuntu | Grafana Labs
FIRST STEPS WITH PROMETHEUS
Getting started | Prometheus
Prometheus – Monitoring system & time series database