prometheus安装使用教程

目录

一、安装prometheus

prometheus简介

核心特性:

组件和架构:

下载与安装

pushgateway安装

下载安装

python客户端

安装prometheus-python客户端

推送数据到网关


一、prometheus安装

Prometheus 是一套开源的监控和警报工具包。它以给定的时间间隔从配置的目标收集指标,评估规则表达式,显示结果,并可以在观察到指定条件时触发警报。

prometheus简介

核心特性:

  1. 多维数据模型:Prometheus的数据模型以时间序列为中心,通过度量名称和键值对标签来定义。

  2. 灵活的查询语言:Prometheus有自己的查询语言PromQL(Prometheus Query Language),可以用来检索和处理时间序列数据。

  3. 不依赖分布式存储:Prometheus的存储完全独立,本地存储时间序列数据,并且运作方式期望无需依赖远程存储。

  4. 独立、单服务器节点作业:Prometheus不是设计成分布式系统,尽管有组件可支持联邦或集群模式,大部分设置中Prometheus服务器是单个节点。

  5. 时间序列收集通过拉(pull)模型:Prometheus通过HTTP协议拉取收集目标上的监控信息。不过,也支持通过一个中间网关推(push)数据。

  6. 目标发现:Prometheus可以通过配置文件、服务发现或其他方法自动发现目标。在云环境或者使用容器编排平台时,这个特性尤其有用。

  7. 内置警报:Alertmanager 与 Prometheus紧密集成,能够处理Alerts警报通知和路由。

组件和架构:

  • 服务器:存储时间序列数据,并运行PromQL查询。
  • 警报管理器(Alertmanager):处理警报,包括静默、抑制、路由及发送通知。
  • 客户端库:多种编程语言的库,用于创建应用或服务的自定义指标。
  • 推送网关(Pushgateway):用于短期作业,这种场合不适合由Prometheus服务器定期拉取。
  • 导出器(Exporters):特殊服务,用于将现有的第三方服务的度量转化为Prometheus可以理解的格式。
  • 服务发现:支持多种机制和平台,自动发现监控目标。

Prometheus在许多组织和环境中用于监控操作系统和硬件指标、应用程序性能、微服务健康状况等,并在系统达到预设阈值时触发警报。由于其简洁性和单服务器的模型,Prometheus对于初学者来说相对容易上手,且在容器和微服务架构中表现出强大的监控能力。

下载与安装

  1. 从 Prometheus 官网下载适用于 Mac 系统的二进制文件。下载链接为:https://prometheus.io/download/,您可以选择适合您操作系统的版本,如果是mac系统,需要下载darwin版本

  2. 解压缩 Prometheus 压缩文件: 将 Prometheus 压缩文件中的内容全部解压缩到您选择的安装目录中。例如,可以将 Prometheus 解压到 /usr/local/bin 目录中。

  3. 运行 Prometheus: 运行以下命令以启动 Prometheus:

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

其中,prometheus.yml 是 Prometheus 的配置文件,您可以在该文件中配置 Prometheus 的行为。如果没有指定 --config.file 标志,则 Prometheus 将尝试加载位于与其二进制文件相同的目录中的 prometheus.yml 文件。

如果配置文件异常,可以使用检测配置文件命令

./promtool check config prometheus.yml

  1. 访问 Prometheus: 运行成功后,在浏览器中打开 http://localhost:9090,您将看到 Prometheus 的 Web UI。

二、pushgateway安装

Prometheus Pushgateway 的存在是为了允许临时作业和批处理作业将其指标公开给 Prometheus。由于此类工作可能存在的时间不够长而无法被删除,因此他们可以将其指标推送到 Pushgateway。然后 Pushgateway 将这些指标公开给 Prometheus。

下载安装

1、从官网地址下载合适的版本,Download | Prometheus,mac系统选择darwin版本

2、解压文件夹

3、启动Pushgateway

./pushgateway

访问Pushgateway: 运行成功后,在浏览器中打开 http://localhost:9091,您将看到Pushgateway 的 Web UI。

python客户端

官方文档:https://github.sheincorp.cn/prometheus/pushgateway

安装prometheus-python客户端

pip3 install prometheus-client

推送数据到网关

# -*- coding = utf-8 -*-
from prometheus_client import CollectorRegistry, push_to_gateway, Gauge, REGISTRY
import random
import time

pushgateway_url = 'http://127.0.0.1:9091'  # 替换为实际的 Pushgateway URL
job_name = 'test'  # 替换为实际的任务名称

# 创建指标
my_metric = Gauge('my_metric', 'Description of my metric', registry=REGISTRY)
def test():
    while True:
        # 生成随机数据,并更新指标的值
        value = random.randint(0, 100)
        my_metric.set(value)
        print(f'Setting my_metric to: {value}')
        # 将指标数据推送到 Pushgateway
        push_to_gateway(pushgateway_url, job=job_name, registry=REGISTRY)
        # yield my_metric
        # 休眠一段时间
        time.sleep(10)
# 迭代器方式调用
# next(test())
# 函数方式调用
test()
  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值