# Prometheus+grafana监控配置经验分享

> 作者/来源: UCloud 运营管理员
> 发布时间: 2023-01-11T05:20:00.000Z
> 分类: 云主机
> 标签: AI, 数据库, Kubernetes
> 原文链接: http://117.50.162.249:3000/yun/articles/976

---

# Prometheus+grafana监控配置经验分享

> 来源: https://www.ucloud.cn/yun/129925.html
> 作者: IT那活儿
> 发布日期: 发布于2023-01-11 13:20

Prometheus+grafana监控配置经验分享

一、监控目标

1. 基础监控；服务器基础指标（cpu load、内存、用户线程数、端口连接数、存储）

   语言相关比如jvm以及go、php等
2. 基础中间件监控；redis、mysql、rabbitmq
3. 应用监控；应用接口调用频率、应用接口响应时间、应用接口异常次数
4. 根据监控指标可配置定制化的阈值告警

二、prometheus概述

Prometheus（普罗米修斯）是一个prometheus是一个最初在SoundCloud上构建的开源系统监控和警报工具包 。从2012年开始，许多公司和组织开始使用Prometheus，该项目拥有非常活跃的开发人员和用户社区。目前它是一个独立的开源项目，并且不依赖与任何公司为了强调这一点，并澄清项目的治理结构，Prometheus在2016年加入Cloud Native Computing Foundation，作为kubernetes之后的第二个托管项目。

## **特点：**

1、多维数据模型(时序列数据有metric和一组key/value组成)

2、在多维度上灵活的查询语言(PromQl)

3、不依赖分布式存储（内置数据库），单主节点工作.

4、基于HHTP的pull方式采集时间序列数据

5、可以通过pushgateway进行时序列数据推送(pushing)

6、可以通过服务发现或者静态配置去获取要采集的目标服务器

7、多种可视化图表及仪表盘支持（grafana）

## **数据采集：**

Prometheus通过HTTP接口的方式从各种客户端获取数据，这些客户端必须符合Prometheus监控数据格式；

通常有两种方式：

**1、侵入式埋点监控（直接采集）**，通过在客户端集成，如果Kubernetes API直接通过引入Prometheus go client，提供/metrics接口查询kubernetes API各种指标；这一类Exporter直接内置了对Prometheus监控的支持，比如cAdvisor，Kubernetes，Etcd，Gokit等，都直接内置了用于向Prometheus暴露监控数据的端点。

**2、通过exporter方式（间接采集）**，在外部将原来各种中间件的监控支持转化为Prometheus的监控数据格式，如redis exporter将Reids指标转化为Prometheus能够识别的HTTP请求；间接采集，原有监控目标并不直接支持Prometheus，因此我们需要通过Prometheus提供的Client Library编写该监控目标的监控采集程序。例如：Mysql Exporter，JMX Exporter，Consul Exporter等。

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_000.png)

HTTP返回Header和Body如上图所示，指标前面两行#是注释，标识指标的含义和类型。指标和指标的值通过空格分割，开发者通常不需要自己拼接这种个数的数据， Prometheus提供了各种语言的SDK支持。

Prometheus并没有采用json的数据格式，而是采用text/plain纯文本的方式，这是它的特殊之处。

## **主要组件：**

prometheus生态系统由多个组件组成，其中许多组件是可选的。

**prometheus server：**主要获取和存储时间序列数据

**exporters：**主要是作为agent收集数据发送到prometheus server，不同的数据收集由不同的exporters实现，如监控主机有node-exporters，mysql有MySQL server exporters。

**pushgateway：**允许短暂和批处理的jobs推送它们的数据到prometheus；由于这类工作的存在时间不够长，所以需要他们主动将数据推送到pushgateway，然后由pushgateway将数据发送的prometheus。

**alertmanager：**实现prometheus的告警功能。

## **架构：**

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_001.png)

prometheus直接或通过pushgateway抓取数据。（短周期任务数据）

将数据存储在本地，并对这些数据运行规则，以便从现有数据聚合和记录新时间序列，或者生成警报。

Prometheus targets:采集mysql数据，

三、Prometheus安装

## **1. 下载最新版的Prometheus 2.17.1**

wget -P /usr/local/src https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz

## **2. 直接解压并建立软连接**

```
▼▼▼
```

## **3. 创建用于运行Prometheus的组和用户**

group add prometheus

useradd -g prometheus -s /sbin/nologinprometheus

## **4. 给Prometheus主目录赋用户Prometheus权限**

chown -R prometheus:prometheus /usr/local/prometheus/

## **5. 将Prometheus加入到系统管理程序中**

```
▼▼▼
```

## **6.将客户端加入到Prometheus监控中**

注意：将配置文件中的ip地址改成你的被监控客户端的ip，(node\_exporter的)端口号默认是9100。

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_002.png)

## **7.  启动Prometheus服务**

./prometheus --config.file=prometheus.yml

查看日志

## **8. 验证prometheus的Web页面**

prometheus默认的端口号是9090，

浏览器输入http://192.168.\*\*.\*\*\*:9090/

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_003.png)

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_004.png)

四. node\_exporter  客户端部署

1. 下载并解压node\_exporter
2. wget -P /usr/local/srchttps://github.com/prometheus/node\_exporter/releases/download/v1.0.0-rc.0/node\_exporter-1.0.0-rc.0.linux-amd64.tar.gz

   tar -zxvf /usr/local/src/node\_exporter-1.0.0-rc.0.linux-amd64.tar.gz -C /usr/local
3. 建立软连接

   ln -s /usr/local/node\_exporter-1.0.0-rc.0.linux-amd64/ /usr/local/node\_exporter
4. 添加用户名和用户组

   group add prometheus

   useradd -g prometheus -s /sbin/nologinprometheus
5. 给node\_exporter主目录赋权限

   chown -R prometheus:prometheus /usr/local/node\_exporter/
6. 启动并设置开机自启

   systemctl start node\_exporter

   systemctl enable node\_exporter
7. 检查node\_exporter是否已启动node\_exporter默认的端口是9100

   systemctl status node\_exporter

   ss -ntl |grep 9100
8. 直接关闭防火墙

   systemctl stop firewalld

   systemctl disable firewalld
9. 在prometheus的web上检查是否监控到了本机

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_005.png)

五. Prometheus配置文件

默认使用的配置文件是prometheus.yml

global:

```
scrape_interval: 15s //全局配置，多久搜集一次evaluation_interval: 15s //全局配置，多久搜集一次rule_files: //规则文件# - "first.rules"# - "second.rules"scrape_configs: //搜集的配置- job_name: prometheus //监控的服务名称static_configs:- targets: [localhost:9090] //监控入口
```

多个地址依次添加即可。

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_006.png)

**六. Prometheus+Grafana 打造监控系统**

1. 下载Grafana的rpm包

   wgethttps://dl.grafana.com/oss/release/grafana-6.7.2-1.x86\_64.rpm
2. 安装Grafana

   yum install grafana-6.7.2-1.x86\_64.rpm
3. 启动grafana，并设置其开机自启

   systemctl restart grafana-server

   systemctl enable grafana-server
4. 浏览器打开Grafana主页 http://192.168.48.150:3000/
5. 配置数据源类型Prometheus，并设置相关参数

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_007.png)

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_008.png)

**总结：**上文提到 Prometheus 是一款基于时序数据库的监控系统，时序数据库常简写为 **TSDB（Time Series Database）**。很多流行的监控系统都在使用时序数据库来保存数据，这是因为时序数据库的特点和监控系统不谋而合。本次分享到此结束，如有错误之处，还请多多指出。

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_009.png)

END

更多精彩干货分享

点击下方名片关注

IT那活儿

![](https://ucloud-blog.cn-bj.ufileos.com/articles/129925/images/129925_010.png)