美文网首页java
Prometheus-入门教程

Prometheus-入门教程

作者: xsren2019 | 来源:发表于2020-06-22 11:52 被阅读0次

    参考文档:
    官方文档:https://prometheus.io/docs/introduction/overview/
    其他文章: http://dbaplus.cn/news-72-1462-1.html
    配合Grafana https://www.hi-linux.com/posts/25047.html

    OVERVIEW

    Prometheus 是一个开源的系统监控和警报工具包。非常适合记录时间序列数据,比如可以记录机器CPU、Memory的使用情况;也可以在微服务中收集各个维度的信息。

    特点:
    • 多维数据模型,时间序列由metric名字和K/V标签标识
    • 灵活的查询语言(PromQL)
    • 单机模式,不依赖分布式存储
    • 基于HTTP采用pull方式收集数据
    • 支持push数据到中间件(pushgateway)
    • 通过服务发现或静态配置发现目标
    • 多种图表和仪表盘
    组件:

    Prometheus生态系统由多个组件构成,其中多是可选的,根据具体情况选择

    • Prometheus server - 收集和存储时间序列数据
    • client library - 用于client访问server/pushgateway
    • pushgateway - 对于短暂运行的任务,负责接收和缓存时间序列数据,同时也是一个数据源
    • exporter - 各种专用exporter,面向硬件、存储、数据库、HTTP服务等
    • alertmanager - 处理报警
    • 其他各种支持的工具
    架构图

    INSTALL

    Prometheus的安装还是很简单的。有以下三种:

    • Using pre-compiled binaries
    • From source
    • Using Docker

    我使用的是第一种方式。在https://prometheus.io/download/下载对应操作系统的安装包,解压即可运行。

    CONFIGURATION

    Prometheus 支持使用YAML文件配置系统。下面的配置文件表示每15s收集一次prometheus server自身的数据。evaluation_interva: 15s 表示每15s检查一次是否ch

    global:
      scrape_interval:     15s
      evaluation_interval: 15s
    
    # 报警规则
    rule_files:
      # - "first.rules"
      # - "second.rules"
    
    scrape_configs:
      - job_name: prometheus
        static_configs:
          - targets: ['localhost:9090']
    

    RUN

    运行:

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

    访问http://localhost:9090,可以看到如下图:

    image.png

    QUERY

    Prometheus提供PromQL进行数据查询,首先需要了解一些概念:

    数据模型

    metric名字和标签集合确定一个唯一的时间序列;时间序列数据样本由一个毫秒精度的时间戳和一个float64类型的值组成。数据需要满足下列格式:

    metric_name [
      "{" label_name "=" `"` label_value `"` { "," label_name "=" `"` label_value `"` } [ "," ] "}"
    ] value [ timestamp ]
    

    一条完整的数据:

    # HELP http_requests_total The total number of HTTP requests.
    # TYPE http_requests_total counter
    http_requests_total{method="post",code="200"} 1027 1395066363000
    http_requests_total{method="post",code="400"}    3 1395066363000
    
    JOBS AND INSTANCE

    被监控的一个节点叫instance,这样的一组节点叫job

    • job: api-server
      ** instance 1: 1.2.3.4:5670
      ** instance 2: 1.2.3.4:5671
      ** instance 3: 5.6.7.8:5670
      ** instance 4: 5.6.7.8:5671
      生成数据时会自动加上job、instance的信息到labels中。

    所以一条完整的时间序列数据可能是下面这样的:

    http_request{method: "POST",host: "host-1"
    ,uri: "/index.htm",job: "api-server", instance:"1.2.3.4:5670"}  3 1395066363000
    

    查询参考http://caosiyang.github.io/2017/10/27/prometheus/,讲的非常好。

    查询示例

    VISUALIZATION

    Prometheus支持三种图形展示方式

    • Expression browser
      自带,比较简单,可以进行debug。
    • Grafana
      有丰富的图文显示。官方推荐。
    • Console templates
      使用 Go templating language,方便定制化和版本控制,但是有一定的门槛。

    Storage

    自身包含了一个基于本地磁盘的时间序列数据库,但是也是支持其他远程的存储系统。

    Local storage
    • --storage.tsdb.path 数据库位置,默认 data/
    • --storage.tsdb.retention 数据保留时间,默认15d

    占用空间计算公式

    needed_disk_space = retention_time_seconds * ingested_samples_per_second * bytes_per_sample
    
    Remote storage

    可以弥补local storage在扩展性和持久性方面的不足。
    通过两种方式和remote storage交互:

    • 可以通过 a remote URL 写数据
    • 可以通过 a remote URL 读数据

    可以集成下列数据库:

    • AppOptics: write
    • Chronix: write
    • Cortex: read and write
    • CrateDB: read and write
    • Gnocchi: write
    • Graphite: write
    • InfluxDB: read and write
    • OpenTSDB: write
    • PostgreSQL/TimescaleDB: read and write
    • SignalFx: write

    SECURITY

    不提供用户和权限认证功能。需要用户使用防火墙或者反向代理。

    服务自动发现

    官方支持Consul、etcd和K8S等。
    比较简单的方式是通过脚本定期更新文件来实现自动发现。

    FEDERATION

    Federation 允许一个Prometheus server从另外一个Prometheus server抓取数据。
    即可以横向扩展Prometheus,也可以结合多个系统的数据。

    配置举例(下列配置表示从 source-prometheus-{1,2,3}:9090 中获取 job="prometheus" 或者m etric name 以 job: 打头的数据):

    - job_name: 'federate'
      scrape_interval: 15s
    
      honor_labels: true
      metrics_path: '/federate'
    
      params:
        'match[]':
          - '{job="prometheus"}'
          - '{__name__=~"job:.*"}'
    
      static_configs:
        - targets:
          - 'source-prometheus-1:9090'
          - 'source-prometheus-2:9090'
          - 'source-prometheus-3:9090'
    

    相关文章

      网友评论

        本文标题:Prometheus-入门教程

        本文链接:https://www.haomeiwen.com/subject/ysqywctx.html