Prometheus查询表达式

2017-03-30  本文已影响10658人  YichenWong

Prometheus Querying

查询

prometheus提供了功能性表达式语言,可让用户对于时间序列的数据进行选择和聚合。通过表达式查询的结果可以绘制为曲线图,也可以在prometheus提供的表达式浏览器中显示为表格,也可以通过外部系统以HTTP API来调用使用。

expression language data types

prometheus 表达式语言中,有四种类型:

根据使用情况(例如绘图或者显示表达式的输出),这些类型中只有一些是由用户指定的表达式产生的结果而有效的,例如,即时向量表达式是可以绘图的唯一类型。

时间序列选择器

怎么使用prometheus监控容器?

prometheus监控不同的目标服务需要实现不同的exporter插件,早期的时候,官方出了container-exporter项目,但是现在项目已经停止。推荐使用谷歌的cAdvisor项目作为prometheus的exporter。cAdvisor作为一个监控单机容器的项目,数据较为全面,但是也有很大的问题,例如io等数据没有等等。结合prometheus后就能在整个集群监控查询容器。举个例子,你有一个项目有3个容器分布在三台机器,你怎么监控整个项目的流量,内存量,负载量的实时数据。这就是prometheus的多维度查询解决的问题,数据从3台机器的cadvisor得到每个容器的数据,它的多维度查询语法就能让你得到你想要的数据。

这里假设你有10台机器部署了容器需要监控,你在10台机器上分别部署cAdvisor容器

sudo docker run \
  --volume=/:/rootfs:ro \
  --volume=/var/run:/var/run:rw \
  --volume=/sys:/sys:ro \
  --volume=/var/lib/docker/:/var/lib/docker:ro \
  --publish=8080:8080 \
  --detach=true \
  --name=cadvisor \
  google/cadvisor:latest

找一台机器部署prometheus服务,这里依然使用容器部署:

docker run \
    -p 9090:9090 \
    --log-driver none \
    -v /hdd1/prometheus/etc/:/etc/prometheus/ \
    -v /hdd1/prometheus/data/:/prometheus/ \
    -v /etc/localtime:/etc/localtime \
    --name prometheus \
    prom/prometheus

创建/hdd1/prometheus/etc/prometheus.yml配置文件

    my global config
    global:
      scrape_interval:     15s # By default, scrape targets every 15 seconds.
      evaluation_interval: 15s # By default, scrape targets every 15 seconds.
      # scrape_timeout is set to the global default (10s).
      # Attach these labels to any time series or alerts when communicating with
      # external systems (federation, remote storage, Alertmanager).
      external_labels:
          monitor: 'container-monitor'
    # Load and evaluate rules in this file every 'evaluation_interval' seconds.
    rule_files:
       - "/etc/prometheus/rules/common.rules"
    # 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: 'container'
        static_configs:
        - targets: ['10.12.1.129:8080','10.12.1.130:8080','10.50.1.92:8080','10.50.1.93:8080','10.50.1.119:8080']

配置文件中 -targets中的端点填写你的实际cadvisor所在的ip和暴露的端口.正确启动后访问ip:9090就能查询数据了哦。

上一篇下一篇

猜你喜欢

热点阅读