Prometheus的Pushgateway快速部署及使用

这篇具有很好参考价值的文章主要介绍了Prometheus的Pushgateway快速部署及使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

prometheus-pushgateway安装

一. Pushgateway简介

Pushgateway为Prometheus整体监控方案的功能组件之一,并做于一个独立的工具存在。它主要用于Prometheus无法直接拿到监控指标的场景,如监控源位于防火墙之后,Prometheus无法穿透防火墙;目标服务没有可抓取监控数据的端点等多种情况。

在类似场景中,可通过部署Pushgateway的方式解决问题。当部署该组件后,监控源通过主动发送监控数据到Pushgateway,再由Prometheus定时获取信息,实现资源的状态监控。
Prometheus的Pushgateway快速部署及使用,Prometheus监控,prometheus

简单图
Prometheus的Pushgateway快速部署及使用,Prometheus监控,prometheus

工作流程:

a. 监控源通过Post方式,发送数据到Pushgateway,路径为/metrics。

b. Prometheus服务端设置任务,定时获取Pushgateway上面的监控指标。

c. Prometheus拿到监控指标后,根据配置的告警规则,如果匹配将触发告警到Alertmanager;同时,Grafana可配置数据源调用Prometheus数据,做为数据展示。

d. Alertmanager收到告警后,根据规则转发到对应接收人及接收介质;Grafana方面,用户可登录并根据数据源的监控指标,配置相关的图表展示 。

二. 安装部署

二进制安装

下载安装包

cd /usr/local
wget https://github.com/prometheus/pushgateway/releases/download/v1.4.3/pushgateway-1.4.3.linux-amd64.tar.gz
tar -xf pushgateway-1.4.3.linux-amd64.tar.gz

Prometheus的Pushgateway快速部署及使用,Prometheus监控,prometheus

system管理

启动服务,默认端口为9091,可通过–web.listen-address更改监听端口

root@bj-1:/usr/local# cat /usr/lib/systemd/system/pushgateway.service 
[Unit]
Description=Prometheus pushgateway
Requires=network.target remote-fs.target
After=network.target remote-fs.target
? 
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/pushgateway/pushgateway --persistence.file="/usr/local/pushgateway/data/" --persistence.interval=5m #保存时间5分钟
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=5s
? 
[Install]
WantedBy=multi-user.target

Prometheus的Pushgateway快速部署及使用,Prometheus监控,prometheus

三.prometheus添加配置

新增job pushgateway

vim /usr/local/prometheus/prometheus.yml
  - job_name: 'pushgateway'
    scrape_interval: 30s
    honor_labels: true  #加上此配置exporter节点上传数据中的一些标签将不会被pushgateway节点的相同标签覆盖
    static_configs:
      - targets: ['10.3.1.11:9091']
        labels:
          instance: pushgateway

‘’查看target状态:

Prometheus的Pushgateway快速部署及使用,Prometheus监控,prometheus

四. 数据推送Pushgateway

pushgateway的数据推送支持两种方式,Prometheus Client SDK推送和API推送。

1、Client SDK推送

Prometheus本身提供了支持多种语言的SDK,可通过SDK的方式,生成相关的数据,并推送到pushgateway,这也是官方推荐的方案。目前的SDK覆盖语言有官方的​

Go
Java or Scala
Python
Ruby

也有许多第三方的,详情可参见此链接:https://prometheus.io/docs/instrumenting/clientlibs/

示例:

本示例以python为例,讲解SDK的使用

from prometheus_client import Counter,Gauge,push_to_gateway
from prometheus_client.core import CollectorRegistry
 
registry = CollectorRegistry()
data1 = Gauge('gauge_test_metric','This is a gauge-test-metric',['method','path','instance'],registry=registry) 
data1.labels(method='get',path='/aaa',instance='instance1').inc(3)
 
push_to_gateway('10.12.61.3:9091', job='alex-job',registry=registry)

注解:

第一、二行代码:引入相关的Prometheus SDK;

第五行代码:创建相关的指标,类型为Gauge。其中“gauge_test_metric”为指标名称,'This is a gauge-test-metric’为指标注释,[‘method’,‘path’,‘instance’] 为指标相关的label。

第六行代码:添加相关的label信息和指标value 值。

第六行代码:push数据到pushgateway,'10.12.61.3:9091’为发送地址,job指定该任务名称。

以上代码产生的指标数据等同如下 :

# HELP gauge_test_metric This is a gauge-test-metric
# TYPE gauge_test_metric gauge
gauge_test_metric{instance="instance1",method="get",path="/aaa"} 3.0

2、Post推送Node-expoerter组件数据

安装好node_exporter,此处不多介绍
传送监控数据到pushgateway节点
对于传过去的监控项会添加此处定义的标签 job=test instance=10.2.1.11 hostname=ip-10-2-1-11

curl 127.0.0.1:9100/metrics|curl --data-binary @- http://10.3.1.11:9091/metrics/job/test/instance/10.2.1.11/hostname/ip-10-2-1-11

编写脚本

node_date.sh

#!/bin/bash
job_name="Bj"
hostname=$(hostname)
HOST_IP=$(hostname --all-ip-addresses | awk '{print $1}')

/usr/bin/curl 127.0.0.1:9100/metrics|/usr/bin/curl --data-binary @- http://sanming.f3322.net:9091/metrics/job/$job_name/instance/$HOST_IP/hostname/$hostname


crontab定时任务

#Ansible: node_date
* * * * * /bin/bash /usr/local/node_exporter/node_date.sh

批量给node-exporter添加定时任务

Ansible剧本

root@bj-1:/opt/node_date# cat playbook.yml 
- hosts: all
  remote_user: root
  gather_facts: no
  tasks:
    - name: 推送磁盘脚本
      copy: src=node_date.sh dest=/usr/local/node_exporter mode=u+x
    - name: 设置定时任务
      cron: name="node_date"  job="/bin/bash /usr/local/node_exporter/node_date.sh" state="present"
    - name: 执行脚本
      shell: /bin/bash /usr/local/node_exporter/node_date.sh

删除某个实例的数据:

curl -X DELETE http://10.3.1.11:9091/metrics/job/test/instance/10.2.1.11/hostname/ip-10-2-1-11

3、pushgateway脚本示例

(1)TCP连接

pushgateway本身没有任何抓取监控数据的功能,它只能被动地等待数据被推送过来,故需要用户自行编写数据采集脚本。

例:采集TCP waiting_connection瞬时数量

mkdir -p /app/scripts/pushgateway

cat <<EOF >/app/scripts/pushgateway/tcp_waiting_connection.sh
#!/bin/bash

# 获取hostname,且host不能为localhost
instance_name=`hostname -f | cut -d '.' -f 1`
if [ $instance_name = "localhost" ];then
  echo "Must FQDN hostname"
  exit 1
fi

# For waiting connections
label="count_netstat_wait_connetions"
count_netstat_wait_connetions=`netstat -an | grep -i wait | wc -l`
echo "$label:$count_netstat_wait_connetions"
echo "$label $count_netstat_wait_connetions" | curl --data-binary @- http://localhost:9091/metrics/job/pushgateway/instance/$instance_name

EOF

chmod +x /app/scripts/pushgateway/tcp_waiting_connection.sh

1)netstat -an | grep -i wait | wc -l该自定义监控的取值方法

2)实际上就是将K/V键值对通过POST方式推送给pushgateway,格式如下:

http://localhost:9091/metricspushgateway url
job/pushgateway数据推送过去的第一个label,即exported_job=“pushgateway”(类似prometheus.yml中定义的job)
instance/$instance_name数据推送过去的第一个label,即exported_instance=“deepin-PC”

2.定时执行脚本

crontab -e 

* * * * * /app/scripts/pushgateway/tcp_waiting_connection.sh >/dev/null 2>&1

prometheus默认每15秒从pushgateway获取一次数据,而cron定时任务最小精度是每分钟执行一次,若想没15秒执行一次,则:

方法1:sleep:定义多条定时任务

* * * * * /app/scripts/pushgateway/tcp_waiting_connection.sh >/dev/null 2>&1
* * * * * * sleep 15; /app/scripts/pushgateway/tcp_waiting_connection.sh >/dev/null 2>&1
* * * * * * sleep 30; /app/scripts/pushgateway/tcp_waiting_connection.sh >/dev/null 2>&1
* * * * * * sleep 45; /app/scripts/pushgateway/tcp_waiting_connection.sh >/dev/null 2>&1

方法2:for循环

cat <<EOF >/app/scripts/pushgateway/tcp_waiting_connection.sh
#!/bin/bash
time=15
for (( i=0; i<60; i=i+time )); do
  instance_name=`hostname -f | cut -d '.' -f 1`
  if [ $instance_name = "localhost" ];then
    echo "Must FQDN hostname"
    exit 1
  fi
  label="count_netstat_wait_connetions"
  count_netstat_wait_connetions=`netstat -an | grep -i wait | wc -l`
  echo "$label:$count_netstat_wait_connetions"
  echo "$label $count_netstat_wait_connetions" | curl --data-binary @- http://localhost:9091/metrics/job/pushgateway/instance/$instance_name
  
  sleep $time  
done
exit 0

EOF

此时cron定时任务只需要定义一条:

crontab -e 

* * * * * /app/scripts/pushgateway/tcp_waiting_connection.sh >/dev/null 2>&1

注:若解释器使用#!/bin/bash,则调试时使用全路径或相对路径或者bash /app/scripts/pushgateway/tcp_waiting_connection.sh执行脚本;若解释器使用#!/bin/sh,则调试时使用sh /app/scripts/pushgateway/tcp_waiting_connection.sh执行脚本,否则出现错误:Syntax error: Bad for loop variable

3.promethues查看监控值count_netstat_wait_connetions

4.TCP等待连接数:count_netstat_wait_connetions(通过自定义脚本实现,通过node_exporter也可实现)

处于各种wait状态的TCP连接(close_wait,time_wait等)也是日常排查负载(网络负载,服务器负载,数据库负载等)的一个重要指标:一般wait类型的TCP过大时,一定说明系统网络负载(流量负载)出现了问题;原因多样(网络问题,访问请求量,DDOS流量,数据库,CPU等都有可能)


vi count_netstat_wait_connections.sh
#!/bin/bash
instance_name=`hostname -f | cut -d'.' -f1`  #获取本机名,用于后面的的标签
label="count_netstat_wait_connections"  #定义key名
count_netstat_wait_connections=`netstat -an | grep -i wait | wc -l`  #获取数据的命令
echo "$label: $count_netstat_wait_connections"
echo "$label  $count_netstat_wait_connections" | curl --data-binary @- http://server.com:9091/metrics/job/pushgateway_test/instance/$instance_name  #这里pushgateway_test就是prometheus主配置文件里job的名字,需要保持一致,这样数据就会推送给这个job。后面的instance则是指定机器名,使用的就是脚本里获取的那个变量值

参考文档:

Prometheus分布式监控

prometheus-pushgateway安装

Prometheus监控运维实战十一:Pushgateway文章来源地址https://www.toymoban.com/news/detail-721637.html

到了这里,关于Prometheus的Pushgateway快速部署及使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • prometheus pushgateway 性能差的解决办法

    我们是分区分服的游戏,生产环境会有几百上千个游戏服进程,这些进程都想接入 prometheus 做一些指标监控。优化前的状况是: 全局只部署一个 pushgateway。 每个物理服会部署 50 个左右的游戏服进程,每个进程定时打印指标到各自的指标 log 文件。 每个物理服部署一个定时脚

    2024年02月01日
    浏览(32)
  • 云原生监控平台 Prometheus 从部署到监控

    角色 节点 IP地址 监控端 Prometheus ,Grafana,node_exporter ,Nginx 47.120.35.251 被监控端1 node_exporter 47.113.177.189 被监控端2 mysqld_exporter,node_exporter,Nginx,Nginx Exporter 47.113.146.118 2.1.1 二进制安装脚本安装Nginx 2.1.2 修改Nginx.conf 2.2.1 下载相关软件包 1.2.2 将Prometheus添加至System管理  1.2

    2024年02月11日
    浏览(38)
  • Prometheus + grafana 的监控平台部署

    vim /opt/module/prometheus-2.44.0/prometheus.yml 命令 修改配置文件 命令 分发 /opt/module/node_exporter-1.6.0 目录到需要监控的节点 使用systemctl 管理node_exporter服务 分发到各个节点,并且启动服务 使用systemctl管理 kafka_exporter 服务 命令 使用systemctl 管理grafana 服务 命令 1.7.1 导入 grafana Dashboa

    2024年02月09日
    浏览(33)
  • 在k8s中快速搭建基于Prometheus监控系统

    公众号「架构成长指南」,专注于生产实践、云原生、分布式系统、大数据技术分享 K8s本身不包含内置的监控工具,所以市场上有不少这样监控工具来填补这一空白,但是没有一个监控工具有prometheus全家桶使用率高,因为它由 CNCF维护,已经成为了监控 k8s 集群的事实上的行

    2024年02月04日
    浏览(33)
  • 云原生监控平台 Prometheus 的相关概念及部署

          Prometheus 是一个开源的系统监控和报警系统,在 2012 年由 SoundCloud 公司创建,并于 2015 年正式发布。2016 年,Prometheus 正式加入 CNCF (Cloud Native Computing Foundation),成为继kubernetes之后第二个在CNCF托管的项目, 现已广泛用于在容器和微服务领域中得到了广泛的应用,当然不仅

    2024年02月10日
    浏览(38)
  • 【云原生】prometheus监控告警之安装部署alertmanager实战

    前言 🏠个人主页:我是沐风晓月 🧑个人简介:大家好,我是沐风晓月,阿里云社区博客专家😉😉 💕 座右铭: 先努力成长自己,再帮助更多的人 ,一起加油进步🍺🍺🍺 💕欢迎大家:这里是CSDN,我总结知识的地方,喜欢的话请三连,有问题请私信😘 本文中的是prome

    2023年04月27日
    浏览(43)
  • docker容器监控:Cadvisor +Prometheus+Grafana的安装部署

    目录 Cadvisor +Prometheus+Grafana的安装部署 一、安装docker: 1、安装docker-ce 2、阿里云镜像加速器 3、下载组件镜像 4、创建自定义网络 二、部署Cadvisor 1、被监控主机上部署Cadvisor容器 2、访问cAdvisor页面 三、安装prometheus 1、部署Prometheus  2、先准备配置 3、访问prometheus页面 四、部

    2024年02月14日
    浏览(36)
  • kubernete部署prometheus监控sring-boot程序

    目录 1、kubernete集群环境以及prometheus基础环境 2、kubernetes监控集群内部的spring-boot程序 2.1、application.yml系统配置,endpoints相关设置 2.2、引入监控的相关依赖文件  pom.xml ,主要是spring-boot-starter-actuator和micrometer-registry-prometheus 2.3、Dockerfile构建基础镜像  2.4、k8s环境部署spring

    2024年02月12日
    浏览(73)
  • Kibana+Prometheus+node_exporter 监控告警部署

    下载好三个软件包 一、prometheus安装部署 1、解压  2、修改配置文件的IP地址 3、运行Prometheus 4、打开浏览器根据配置文件的地址和端口访问,如果状态栏看到的跟下图不一样,记得在标签栏中的Status状态选择Targets  二、node_exporter 安装部署 1、解压,运行  2、打开浏览器输入

    2024年02月15日
    浏览(43)
  • 二进制部署Prometheus + Grafana监控集群,及各exporter安装

    Prometheus三大组件: Server 主要负责数据采集和存储,提供PromQL查询语言的支持。 Alertmanager 警告管理器,用来进行报警。 Push Gateway 支持临时性Job主动推送指标的中间网关。 Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB)。Prometheus使用Go语言开发,是Google B

    2024年02月13日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包