什么是Prometheus?
Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB)。Prometheus使用Go语言开发,是Google BorgMon监控系统的开源版本。
2016年由Google发起Linux基金会旗下的原生云基金会(Cloud Native Computing Foundation), 将Prometheus纳入其下第二大开源项目。
Prometheus目前在开源社区相当活跃。
Prometheus和Heapster(Heapster是K8S的一个子项目,用于获取集群的性能数据。)相比功能更完善、更全面。Prometheus性能也足够支撑上万台规模的集群。

Prometheus的特点
多维度数据模型。
灵活的查询语言。
不依赖分布式存储,单个服务器节点是自主的。
通过基于HTTP的pull方式采集时序数据。
可以通过中间网关进行时序列数据推送。
通过服务发现或者静态配置来发现目标服务对象。
支持多种多样的图表和界面展示,比如Grafana等。
Prometheus监控基本原理
Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控。不需要任何SDK或者其他的集成过程。这样做非常适合做虚拟化环境监控系统,比如VM、Docker、Kubernetes等。输出被监控组件信息的HTTP接口被叫做exporter 。目前互联网公司常用的组件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux系统信息(包括磁盘、内存、CPU、网络等等)。

Prometheus服务过程
Prometheus Daemon 负责定时去目标上抓取metrics(指标)数据,每个抓取目标需要暴露一个http服务的接口给它定时抓取。Prometheus支持通过配置文件、文本文件、Zookeeper、Consul、DNS SRV Lookup等方式指定抓取目标。Prometheus采用PULL的方式进行监控,即服务器可以直接通过目标PULL数据或者间接地通过中间网关来Push数据。
Prometheus在本地存储抓取的所有数据,并通过一定规则进行清理和整理数据,并把得到的结果存储到新的时间序列中。
Prometheus通过PromQL和其他API可视化地展示收集的数据。Prometheus支持很多方式的图表可视化,例如Grafana、自带的Promdash以及自身提供的模版引擎等等。Prometheus还提供HTTP API的查询方式,自定义所需要的输出。
PushGateway支持Client主动推送metrics到PushGateway,而Prometheus只是定时去Gateway上抓取数据。
Alertmanager是独立于Prometheus的一个组件,可以支持Prometheus的查询语句,提供十分灵活的报警方式。

Prometheus 三大套件
Server 主要负责数据采集和存储,提供PromQL查询语言的支持。
Alertmanager 警告管理器,用来进行报警。
Push Gateway 支持临时性Job主动推送指标的中间网关。

  1. 安装 Prometheus Server
    1.1 运行用户创建
    groupadd prometheus
    useradd -g prometheus -m -d /opt/prometheus/ -s /sbin/nologin prometheus
    1.2 prometheus server安装
    wget http://10.200.77.3:90/Monitor/prometheus/prometheus-2.14.0.linux-amd64.tar.gz
    tar xzf prometheus-2.14.0.linux-amd64.tar.gz -C /opt/
    cd /opt/prometheus-2.14.0.linux-amd64
    1.3 prometheus配置语法校验
    建议每次修改prometheus配置之后, 都进行语法校验, 以免导致 prometheus server无法启动.

./promtool check config prometheus.yml
1.4 启动Prometheus
此时采用默认配置启动 prometheus server 看下界面, 稍后介绍如何监控Linux 服务器.

./prometheus --config.file=prometheus.yml
1.5 通过浏览器访问prometheus

发现 target 中只有 prometheus server, 因为我们还没有加入其他监控, 下面进行介绍, 后续博文中还将陆续介绍如何监控 redis, RabbitMQ, Kafka, nginx, java等常见服务.

prometheus默认配置:

复制代码

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’
    scrape_interval: 10s
    static_configs:

    • targets: [‘localhost:9090’]
      复制代码
      1.6 设置prometheus系统服务,并配置开机启动
      touch /usr/lib/systemd/system/prometheus.service
      chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service
      vim /usr/lib/systemd/system/prometheus.service
      将如下配置写入prometheus.servie

复制代码
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus

–storage.tsdb.path是可选项,默认数据目录在运行目录的./dada目录中

ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data --storage.tsdb.retention=60d
Restart=on-failure

[Install]
WantedBy=multi-user.target
复制代码
Prometheus启动参数说明
–config.file – 指明prometheus的配置文件路径
–web.enable-lifecycle – 指明prometheus配置更改后可以进行热加载
–storage.tsdb.path – 指明监控数据存储路径
–storage.tsdb.retention --指明数据保留时间
设置开机启动
systemctl daemon-reload
systemctl enable prometheus.service
systemctl status prometheus.service
systemctl restart prometheus.service
说明: prometheus在2.0之后默认的热加载配置没有开启, 配置修改后, 需要重启prometheus server才能生效, 这对于生产环境的监控是不可容忍的, 所以我们需要开启prometheus server的配置热加载功能.
在启动prometheus时加上参数 web.enable-lifecycle , 可以启用配置的热加载, 配置修改后, 热加载配置:

curl -X POST http://localhost:9090/-/reload
2. Prometheus 配置监控其他Linux主机
2.1 node_exporter安装配置
复制代码

运行用户添加

groupadd prometheus
useradd -g prometheus -m -d /usr/local/node_exporter/ -s /sbin/nologin prometheus

下载node_server

wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz

解压到指定目录并删除下载文件

tar -zxf node_exporter-0.18.1.linux-amd64.tar.gz
mv node_exporter-0.18.1.linux-amd64 /usr/local/
ln -sv /usr/local/node_exporter-0.18.1.linux-amd64 /usr/local/node_exporter
rm -f node_exporter-0.18.1.linux-amd64.tar.gz

系统服务配置 node_exporter

touch /usr/lib/systemd/system/node_exporter.service
chown prometheus:prometheus /usr/lib/systemd/system/node_exporter.service
chown -R prometheus:prometheus /usr/local/node_exporter*
vim /usr/lib/systemd/system/node_exporter.service
复制代码
在node_exporter.service中加入如下代码:

复制代码
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
复制代码
启动 node_exporter 服务并设置开机启动

复制代码
systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service
systemctl status node_exporter.service
systemctl restart node_exporter.service
systemctl start node_exporter.service
systemctl stop node_exporter.service
复制代码
node_exporter启动成功后, 你就可以通过如下api看到你的监控数据了(将下面的node_exporter_server_ip替换成你的node_exporter的IP地址, 放到浏览器中访问就可以了 ).

http://node_exporter_server_ip:9100/metrics
为了更好的展示, 接下来我们将这个api 配置到 prometheus server中, 并通过grafana进行展示.

将 node_exporter 加入 prometheus.yml配置中

  • job_name: ‘Linux’
    file_sd_configs:

    • files: [’/opt/prometheus/sd_cfg/Linux.yml’]
      refresh_interval: 5s
      并在文件/opt/prometheus/sd_cfg/Linux.yml中写入如下内容
  • targets: [‘IP地址:9100’]
    labels:
    name: Linux-node1[这里建议给每个主机打个有意义的标签,方便识别.]
    如果你按照上面的方式配置了, 但是使用工具 promtool检测prometheus配置时,没有通过, 那肯定是你写的语法有问题, 不符合yml格式. 请仔细检查下. 如有疑问, 可以在下方评论区留言.

这样做的好处是, 方便以后配置监控自动化, 规范化, 将每一类的监控放到自己的配置文件中, 方便维护.

当然, 如果你的服务器少, 要监控的组件少的话, 你也可以将配置都写入prometheus的主配置文件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.

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’
    scrape_interval: 10s
    static_configs:

    • targets: [‘localhost:9090’]
  • job_name: ‘Linux’
    static_configs:
    targets: [‘http://10.199.111.110:9100’]
    labels:
    group: ‘client-node-exporter’
    复制代码
    重载prometheus配置

curl -X POST http://localhost:9090/-/reload

3 数据展示Grafana安装配置
下载地址: https://grafana.com/grafana/download

wget https://dl.grafana.com/oss/release/grafana-6.5.1-1.x86_64.rpm
sudo yum localinstall grafana-6.5.1-1.x86_64.rpm
granafa默认端口为3000,可以在浏览器中输入http://localhost:3000/
granafa首次登录账户名和密码admin/admin,可以修改
配置数据源Data sources->Add data source -> Prometheus,输入prometheus数据源的信息,主要是输入name和url

添加 Dashboard -> New Dashboard -> Import Dashboard -> 输入11074,导入Linux监控模板. 并配置数据源为Prometheus,即上一步中的name
配置完保存后即可看到逼格非常高的系统主机节点监控信息,包括系统运行时间, 内存和CPU的配置, CPU、内存、磁盘、网络流量等信息, 以及磁盘IO、CPU温度等信息。

参考资料:

官网地址:https://prometheus.io/
GitHub: https://github.com/prometheus
官方文档中文版: https://github.com/Alrights/prometheus
官方监控agent列表:https://prometheus.io/docs/instrumenting/exporters

Prometheus+Grafana监控相关推荐

  1. 使用Prometheus+Grafana 监控MySQL/MONGODB

    使用Prometheus+Grafana 监控MySQL/MONGODB 之前就久仰 Prometheus 大名,因为有用zabbix 进行监控,就没去安装它.现在正好用上监控MONGO+MYSQL. ...

  2. 基于Prometheus+Grafana监控SQL Server数据库

    墨墨导读:本文整理了基于Prometheus+Grafana监控SQL Server数据库的全过程,分享至此,希望对大家有帮助. 搭建SQL Server环境 使用容器建立SQL Server环境非常 ...

  3. Prometheus+Grafana监控PostgreSQL

    Prometheus+Grafana监控PostgreSQL Prometheus:2.32.0 Grafana:8.3.3 PG:13.2 Linux:CentOS7.6 docker:18.06. ...

  4. prometheus+grafana监控以及企业微信告警

    prometheus+grafana监控以及企业微信告警(单机二进制部署) 一.下载部署包,更改其中两个包名称,放到/data下 1.安装包以及解压步骤 grafana-enterprise-8.4. ...

  5. Grafana监控系统之Prometheus+Grafana监控系统搭建

    Grafana监控系统之Prometheus+Grafana监控系统搭建 本文章内容较长,可通过右上角点击目录快速定位想看的内容 => => 一. 概述 1.1 Grafana介绍 Gra ...

  6. Prometheus+Grafana 监控 MySQL

    Prometheus 获取 MySQL 的监控数据,并通过 Grafana 展示的过程.首先来看整体架构图: 1 架构图 如上图,通过 mysql_exporter 获取 MySQL 的监控数据,通过 ...

  7. 搭建prometheus+grafana监控系统

    prometheus简介 Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包 .自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发 ...

  8. Linux安装prometheus+grafana监控

    一.在业务中遇到服务器负载过高问题,由于没有监控,一直没发现,直到业务方反馈网站打开速度慢,才发现问题.这样显得开发很被动.所以是时候搭建一套监控系统了. 由于是业余时间自己捯饬,所以神马业务层面的监 ...

  9. k8s prometheus/grafana 监控系统建设

    全栈工程师开发手册 (作者:栾鹏) 架构系列文章 prometheus架构 其中 1.pushgateway是用来接收业务推送的数据形成metrics接口. 2.exporter是用来监控组件(三方中 ...

最新文章

  1. 5图片展示_做跨境电商想拍出爆款产品图片,我只用这五招
  2. MPAndroidChart的具体属性方法
  3. Win10+vs2013+Caffe静态库配置自己的工程
  4. php 系统平均负载,Linux_解析Linux系统的平均负载概念,一、什么是系统平均负载(Load a - phpStudy...
  5. 三类计算机语言及特点,计算机语言分为哪三类,计算机语言有哪些
  6. tcpdump查看某个端口数据
  7. 陆奇下下选,YC上上签
  8. iphone远没有android好用,抛弃iPhone转投Android 我竟没有一丝留恋
  9. Security+ 学习笔记51 风险分析
  10. SQL server中如何使用return,break和continue
  11. 浅谈css常用伪类用法
  12. select引起的服务端程序崩溃问题
  13. MySql将一张表的数据copy到另一张表中
  14. 人脸表情识别相关研究
  15. windows下vscode + code runner + cmake + msbuild.exe搭建C++快速编译环境
  16. 深度学习水果识别系统-python
  17. symbian与uiq开发教程
  18. 【大学物理· 恒定电流的磁场】有磁介质时的安培环路定理和高斯定理 磁场强度
  19. springCloud Alibaba seata 分布式事务
  20. 【UWB 定位】高精度定位

热门文章

  1. Linux复制一个或多个文件到另一个目录下
  2. 三菱FX系列DPLSY指令使用
  3. 网络唤醒 php,php 远程唤醒电脑 php源码远程唤醒电脑 远程开机
  4. 前端:使用BootStrap搭建一个简单的网页
  5. 盘点:当今十大备份应用软件(转)
  6. python语法报错_Python语法总结
  7. 工业相机QE-量子转换效率
  8. 20届春秋招数据分析面筋分享
  9. 用旭日图展示数据的三种方法
  10. 算法:求小于N的最大素数