prometheus下载地址_监控系列讲座(三)Prometheus概览

1. Prometheus快速搭建

1.1. 下载

下载地址,这里面的地址都是来自于github,我们也可以去github中的release直接下载。

我们这里使用的是2.18.1版本

wget https://github.com/prometheus/prometheus/releases/download/v2.18.1/prometheus-2.18.1.linux-amd64.tar.gz

1.2. 安装

不管哪个版本,我都建议看安装文档来安装。这里我们为了讲课方便,我会把安装步骤写下来。

首先解压他

tar xvfz prometheus-*.tar.gz
cd prometheus-*

1.3. 配置Prometheus监控他自己

Prometheus收集来自从targets的endpoints中的metrics。因为Prometheus同样会暴露一些关于自己的指标,他也可以抓取并且监控他自己的监控。

虽然Prometheus服务器只收集自身的数据在实践中不是很有用,但它是一个很好的开始示例。将以下基本Prometheus配置保存为名为Prometheus.yml

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# 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'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

1.4. 启动Prometheus

使用新创建的配置文件来启动Prometheus,进入到包含Prometheus二进制文件的目录运行

# Start Prometheus.
# By default, Prometheus stores its database in ./data (flag --storage.tsdb.path).
./prometheus --config.file=prometheus.yml

2. 架构

2.1. 架构图

architecture.png

v2-b12e9305e1ca1f9f57ff3c7b8029f341_b.jpg

2.2. 一些概念

3. 初步认识Prometheus

3.1. 使用表达式浏览

我们来看一下Prometheus收集的关于他自己的数据。使用Prometheus内建的浏览器,打开浏览器输入 http://localhost:9090/graph然后选择Console来浏览“Graph”标签。

我们可以从这个地址来采集metric localhost:9090/metrics, Prometheus所暴露的他自己的指标中,有一个metric叫 prometheus_target_interval_length_seconds (两次抓取指标的时间间隔). 我们把他输入到查询框中,然后执行Execute

image-20200607222651752.png

v2-9b255972a127a87bb6ec6eb852b63cc4_b.jpg

他会返回一系列不同的时间(每个后面都有一个value),所有的metrics都叫 prometheus_target_interval_length_seconds但是标签不同。这些标签标示不同的延迟百分比和目标组的间隔时间

如果我们只对“99th percentile latencies”这一行感兴趣,我们就可以这样来取出这一行

prometheus_target_interval_length_seconds{quantile="0.99"}

如果想统计返回的时间序列的个数,就使用count函数

count(prometheus_target_interval_length_seconds)

3.2. 使用图形接口

使用图形接口的话,就输入http://localhost:9090/graph然后使用“Graph”标签

比如,输入下面的表达式可以显示出最近每分钟产生的chunks的数量

rate(prometheus_tsdb_head_chunks_created_total[1m])

image-20200608103548306.png

v2-1ed693ce210b167e44a3dd2a0be8804f_b.jpg

3.3. 启动一些样例的target

我们可以创建一些样例的targets给Prometheus去抓取

Go客户端的库里面包含了一个样例,为三个不同延迟的服务暴露了虚构的RPC延迟

确保我们有Go语言编译器和Go语言编译环境(配置GOPATH

从Prometheus网站下载Go客户端的库来运行这三个样例

# 下载客户端的库并且编译
git clone https://github.com/prometheus/client_golang.git
cd client_golang/examples/random
go get -d
go build

# 在三个端口启动这三个样例
./random -listen-address=:8080
./random -listen-address=:8081
./random -listen-address=:8082

现在我们就有一些样例的数据暴露在端口上, http://localhost:8080/metrics, http://localhost:8081/metrics, 和 http://localhost:8082/metrics.

3.4. 配置Prometheus去监控这些样例

现在我们配置Prometheus去抓取这些新的目标。我们在这三个后端节点放在一个job中,叫做 example-random。我们前两个节点想象为生产的节点,第三个节点是一个金丝雀的实例。我们可以在Prometheus中添加多个端点到一个组,然后为每个组添加额外的标签。在下面的例子当中,我们会给第一组添加group="production"标签,给第二组添加group="canary"标签。

我们需要在prometheus.yml 文件的scrape_configs添加job的定义,然后重启Prometheus实例

scrape_configs:
  - job_name:       'example-random'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:8080', 'localhost:8081']
        labels:
          group: 'production'

      - targets: ['localhost:8082']
        labels:
          group: 'canary'

去浏览器的表达式中查看,确认Prometheus已经拥有这些端点暴露出来的时间序列,比如 rpc_durations_seconds

image-20200608173434649.png

v2-bc8703d7e20486d5cb75fb2cf808e0bc_b.jpg

3.5. 配置把抓取的数据聚合到新的时间序列

在我们刚才的例子成功的条件下,查询那些由上千条时间序列聚合而成的数据的时候可能很慢,他们需要一条一条的计算。为了让他更有效率,Prometheus允许我们通过规则预先设定一个表达式rate(rpc_durations_seconds_count[5m])来创建一个新的时间段。比如,我们对于所有的实例中,平均每秒钟的RPC请求 (rpc_durations_seconds_count) 感兴趣,并且观察的维度(groub by)是jobservice,每五分钟计算一次,我们就可以写成下面的形式

avg(rate(rpc_durations_seconds_count[5m])) by (job, service)

image-20200608173621247.png

v2-053f2fcbfc875d8b761ed48093bc3525_b.jpg

3.6. 创建新的规则

然后,我们想把刚才的时间序列变成一个新的metrics,叫做job_service:rpc_durations_seconds_count:avg_rate5m,就创建一个规则文件,并且保存成prometheus.rules.yml

groups:
- name: example
  rules:
  - record: job_service:rpc_durations_seconds_count:avg_rate5m
    expr: avg(rate(rpc_durations_seconds_count[5m])) by (job, service)

为了让Prometheus识别刚才的规则,需要在prometheus.yml中添加一个 rule_files 。配置文件如下:

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.
  evaluation_interval: 15s # Evaluate rules every 15 seconds.

  # Attach these extra labels to all timeseries collected by this Prometheus instance.
  external_labels:
    monitor: 'codelab-monitor'

rule_files:
  - 'prometheus.rules.yml'

scrape_configs:
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']

  - job_name:       'example-random'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:8080', 'localhost:8081']
        labels:
          group: 'production'

      - targets: ['localhost:8082']
        labels:
          group: 'canary'

用新的配置文件重启Prometheus并且确保使用新的metrics名字的时间序列job_service:rpc_durations_seconds_count:avg_rate5m可以被浏览器查询

image-20200608175311646.png

v2-0c3765303f3ed59ff7e7bf9c71a3fa19_b.jpg 为了方便大家学习,请大家加我的微信,我会把大家加到微信群(微信群的二维码会经常变)和qq群821119334,问题答案云原生技术课堂,有问题可以一起讨论
  • 个人微信

640.jpeg

  • 腾讯课堂 640-20200506145837072.jpeg

  • 微信公众号 640-20200506145842007.jpeg

  • 专题讲座

2020 CKA考试视频 真题讲解 https://www.bilibili.com/video/BV167411K7hp

2020 CKA考试指南 https://www.bilibili.com/video/BV1sa4y1479B/

2020年 5月CKA考试真题 https://mp.weixin.qq.com/s/W9V4cpYeBhodol6AYtbxIA

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值