Pushgateway的安装和使用

介绍

pushgateway是Prometheus下的一个组件,用来当做采集对象和Prometheus的代理,Prometheus会定时的从gateway上面pull数据。
由于服务部署在容器中,服务的ip会经常的变化,导致Prometheus无法直接拉取到每个target的数据。
问题:
多个服务的数据,推送到pushgateway,如果push挂了,影响比较大

安装

从github上面 下载 ,已经编译好的二进制文件包。

wget https://github.com/prometheus/pushgateway/releases/download/v1.0.0/pushgateway-1.0.0.linux-amd64.tar.gz

tar -zxvf pushgateway-1.0.0.linux-amd64.tar.gz

mv pushgateway-1.0.0.linux-amd64 pushgateway-1.0.0

此时已经下载,安装完毕,进入到文件夹下执行就可以启动pushgateway了

[root@root pushgateway-1.0.0]# ll
总用量 16136
-rw-r--r-- 1 3434 3434    11357 10月 16 04:10 LICENSE
-rw-r--r-- 1 3434 3434      487 10月 16 04:10 NOTICE
-rwxr-xr-x 1 3434 3434 16505766 10月 16 03:58 pushgateway*
[root@root pushgateway-1.0.0]# ./pushgateway 
level=info ts=2019-12-17T06:56:17.315Z caller=main.go:81 msg="starting pushgateway" version="(version=1.0.0, branch=HEAD, revision=cc61f46971f5eb7a5be64e80c2ee03857ddbb41a)"
level=info ts=2019-12-17T06:56:17.315Z caller=main.go:82 build_context="(go=go1.13.1, user=root@58be538fc30e, date=20191015-19:58:18)"
level=info ts=2019-12-17T06:56:17.317Z caller=main.go:142 listen_address=:9091

访问 localhost:9091,出现一下界面为安装成功。
在这里插入图片描述

使用

client

TODO

http

  1. 提交一条数据到 {job=‘some_job’}
    echo "some_metric 3.14" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job
    在这里插入图片描述
    可以看到有个 UNTYPED 。因为没有提供了类型信息,所以some_metric将类型的无类型。
    因为我们只写了job的名称,所以可以看到instance那一列为 空字符串

  2. 下面我们加上instance的值
    echo "some_metrics 3.14" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance
    在这里插入图片描述
    可以看到pushgateway页面上产生了两个group,pgw是以job和instance分组的。用来更细力度的区分。

  3. 可以添加更多的标签,但是只会以job和instance区分
    官方的例子如下:

     cat <<EOF | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance
      # TYPE some_metric counter
      some_metric{label="val1"} 42
      # TYPE another_metric gauge
      # HELP another_metric Just an example.
      another_metric 2398.283
      EOF
    

    看起来太复杂,可以这么简单写
    echo "some_metrics{tag=\"test\"} 3.14" | curl --data-binary @- http://127.0.0.1:9091/metrics/job/some_job/instance/some_instance

    可以看到,这次并没有新增一个group,而且在同一个group下也没用多出来,而是把上一个覆盖了。

  4. 删除某个组下某实例的所有数据
    curl -XDELETE http://pushgateway.example.org:9091/metrics/job/some_job/instance/some_instance

  5. 删除某个组下的所有数据
    curl -X DELETE http://pushgateway.example.org:9091/metrics/job/some_job

  6. 删除所有,需要开启 --web.enable-admin-api
    curl -X PUT http://pushgateway.example.org:9091/api/v1/admin/wipe

QA

  1. prometheus 的配置
    因为 Prometheus 配置 pushgateway 的时候,指定 job 和 instance, 但是它只表示 pushgateway 实例,不能真正表达收集数据的含义。所以在 prometheus 中配置 pushgateway 的时候,需要添加 honor_labels: true 参数, 从而避免收集数据被push本身的 job 和 instance 被覆盖。不加 honor_labels: true ,会取gateway的job和instance,设置了的话会取push过来的数据,job必填,instance没有就为 “” 空字符串
- job_name: pushgetway	
  honor_labels: true
  honor_timestamps: true
  scrape_interval: 15s
  scrape_timeout: 10s
  metrics_path: /metrics
  scheme: http
  static_configs:
  - targets:
    - 127.0.0.1:9091
  1. pushgateway 的启动参数,快速启动下执行 ./pushgateway就可以启动一个实例。但是有时候我们需要定制化一些参数,此时就可以通过 ./pushgateway -h查看。
    效果如下:
[root@root pushgateway-1.0.0]# ./pushgateway  -h
usage: pushgateway [<flags>]

The Pushgateway

Flags:
  -h, --help                     Show context-sensitive help (also try --help-long and --help-man).
  	  #指定服务端口
      --web.listen-address=":9091"    Address to listen on for the web interface, API, and telemetry.
      #指定暴露出去的接口
      --web.telemetry-path="/metrics"   	Path under which to expose metrics.
      --web.external-url=        The URL under which the Pushgateway is externally reachable.
      --web.route-prefix=""      Prefix for the internal routes of web endpoints. Defaults to the path of --web.external-url.
      --web.enable-lifecycle     Enable shutdown via HTTP request.
      --web.enable-admin-api     Enable API endpoints for admin control actions.
      #持久化存储的地址,否则重启后采集的指标就没有了
      --persistence.file=""      File to persist metrics. If empty, metrics are only kept in memory.
      #持久化存储的间隔时间
      --persistence.interval=5m  The minimum interval at which to write out the persistence file.
      --log.level=info           Only log messages with the given severity or above. One of: [debug, info, warn, error]
      --log.format=logfmt        Output format of log messages. One of: [logfmt, json]
      --version                  Show application version.
  1. 为了防止 pushgateway 重启或意外挂掉,导致数据丢失,我们可以通过 -persistence.file 和 -persistence.interval 参数将数据持久化下来。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值