prometheus高可用之thanos

1. 前言

当前prometheus存在需求

  • 长期数据存储: >= 1个月的数据存储,存储空间大
  • 多 prometheus server :公有云/私有云/国内/国外
  • 多Grafan:多数据源

解决思路

  • 联邦:优点是可从prometheus抓取指标,缺点是抓取多个指标时会存在问题
  • prometheus remotewrite:用于可写远程存储配置
  • thanos:开源的一套监控解决方案,包含多个功能组件,使用无侵入的方式与 Prometheus 配合部署,从而实现全局查询 、跨集群存储等能力,能够较好地的提升Prometheus的高可用性与扩展性

基于以上考虑,这里选择了 thanos 做为 prometheus 高可用存储的解决方案,同时整合多个grafana,解决多数据源的查询问题,不影响原有的prometheus配置、告警等。

2. thanos介绍

2.1 简介

thanos是根据 prometheus 改编的。使用thanos可以根据部署模型使用或多或少的 prometheus 功能,但 prometheus 始终是使用本地数据收集指标和警报不可或缺的基础。

  • 要求Prometheus v2.2.1+
  • 实现跨集群的全局查询功能
  • 去重、合并,数据压缩等功能,提升查询速度
  • 支持对象存储,包括s3、Azure、阿里云、腾讯云等云存储

2.2 服务端口

Component

Interface

Port

Sidecar

gRPC

10901

Sidecar

HTTP

10902

Query

gRPC

10903

Query

HTTP

10904

Store

gRPC

10905

Store

HTTP

10906

Receive

gRPC (store API)

10907

Receive

HTTP (remote write API)

10908

Receive

HTTP

10909

Rule

gRPC

10910

Rule

HTTP

10911

Compact

HTTP

10912

Query Frontend

HTTP

10913

2.3 组件

  • Sidecar: 与Prometheus集成,读取 prometheus 数据进行查询,并将数据上传到云存储
  • Store Gateway: 读取对象存储中的数据提供给Querier进行历史数据查询
  • Compactor: 对数据进行压缩、缩减采样和应用保留
  • Receiver: 从 prometheus remote-write 接收数据,并上传到云存储
  • Rule: 告警规则
  • Querier:与Prometheus兼容的API并支持Prom语法,与其他组件(Sidecar或Store Gateway)一起协同工作,用于查询Prometheus的数据指标和做为Grafana的监控展示数据源。

注:Thanos有两种运行模式,分别为Sidecar和Receiver,Sidercar主动获取Prometheus数据,Receiver被动接收remote-write传送的数据。这里以sidecar为例

2.4 部署环境说明

主机

用途

说明

172.16.1.100

thanos querier、store、compactor

thanos queriery、store及compactor单独主机部署,每个prometheus 服务上运行sidecar 用于上传对应的数据

172.16.1.39

prometheus server、thanos sidecar

172.17.1.160

prometheus server、thanos sidecar

2.5 部署架构

3. thanos部署

thanos各个组件,都是使用同一个二进制文件进行部署,通过不同的命令参数启用不同的功能。当前版本:v0.30.0

# 下载 
wget https://github.com/thanos-io/thanos/releases/download/v0.30.0/thanos-0.30.0.linux-amd64.tar.gz 

tar -xvf thanos-0.30.0.linux-amd64.tar.gz 

mv thanos-0.0.30.0.linux-amd64 /usr/local/thanos-0.0.30.0

3.1 Prometheus配置

3.1.1 external labels

各个Prometheus实例需要配置外部标签识别,Thanos 的目标是跨所有实例聚合数据,因此提供一组一致的外部标签变得至关重要,每个 Prometheus 实例都必须具有一组全局唯一的标识标签,Thanos的数据去重功能依赖external_labels。参考配置如下:

# vim prometheus.yml
global:
# 增加external labels配置,参考如下,标签根据实际定义
external_labels:
    region: eu-west
    monitor: infrastructure
    replica: A

3.1.2 修改启动命令

配置减少本地数据保存时长,减少本地存储需求,如本地设置只保存2小时数据,历史数据可通过store gateway查询。

参数说明:

  • --storage.tsdb.min-block-duration和--storage.tsdb.min-block-duration 参数必须添加且配置相同的值,用于关闭本地压缩功能
  • --storage.tsdb.retention.time 配置本地保留数据时长
  • --web.enable-admin-api 支持 sidecar 从 Prometheus 获取元数据
/usr/local/prometheus/prometheus \
--storage.tsdb.retention.time=2h \
--storage.tsdb.max-block-duration=2h \
--storage.tsdb.min-block-duration=2h \
--web.enable-lifecycle \
--web.enable-admin-api \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus 

3.2 Sidecar部署

  • Sidecar 进程与 prometheus 集成,与 prometheus 服务部署在同一台机器或同一 Pod 中运行。
  • Sidecar 的目的是将 Prometheus 数据备份到对象存储桶中,并允许其他 Thanos 组件通过 gRPC API 访问 Prometheus 指标。

配置s3配置,以aliyun oss为例,配置如下

# bucket.yml
type: ALIYUNOSS
config:
  bucket: "thanos-oss"
  endpoint: "oss-cn-shenzhen.aliyuncs.com"
  access_key_id: "*** ***" 
  access_key_secret: "*** ***"
prefix: ""

启动命令

./thanos sidecar --grpc-address=0.0.0.0:10901 \
--http-address=0.0.0.0:10902 \
--tsdb.path=/var/lib/prometheus \
--objstore.config-file=bucket.yml \
--prometheus.url=http://localhost:9090
# 说明
--tsdb.path Prometheus 数据存储路径
--prometheus.url Prometheus地址
--objstore.config-file 设置对象存储配置
--http-address http服务端口
--grpc-address grpc服务端口

3.3 Store Gateway部署

  • 配置本地缓存目录
  • 配置对象存储信息(与sidecar的配置一致)
  • 可通过http访问查看对象存储中已存储的块信息
./thanos store --data-dir /usr/local/thanos-0.30.0/store \
--objstore.config-file /usr/local/thanos-0.30.0/bucket.yml  \
--http-address 0.0.0.0:10905 \
--grpc-address 0.0.0.0:10906 
# 说明  
--data-dir 配置缓存
--objstore.config-file 设置对象存储配置
--http-address http服务端口
--grpc-address grpc服务端口

服务启动后可通过http://ip:10905查看块信息

3.4 Compactor部署

部署thanos,需关闭prometheus压缩功能,因此通过compactor对存储中的数据进行压缩,加快查询,Compactor通过扩展标签集来区分不同的Prometheus实例,因此 ,Prometheus labels 必须是唯一。

./thanos compact --data-dir /usr/local/thanos-0.30.0/compactor \
--objstore.config-file /usr/local/thanos-0.30.0/bucket.yml    \
--http-address 0.0.0.0:10912  \
--retention.resolution-raw=0d \
--retention.resolution-5m=0d \ 
--retention.resolution-1h=0d 

# 说明
--data-dir 指定用于数据处理的临时工作空间
--objstore.config-file 设置对象存储配置
--http-address http服务端口
--retention.resolution-raw 原始数据的保留时间
--retention.resolution-5m 5m分钟降采样数据的保留时间
--retention.resolution-1h 1小时降采样数据保留时间,其中--retention.resolution-raw 不能小于其他两个时间段

3.5 Querier部署

Querier组件提供全局查询界面,兼容prometheus查询语法,提供与Prometheus官方一致的HTTP API,可以当成与prometheus:9090功能一致

启动命令如下

/usr/local/thanos-0.30.0/thanos query --query.replica-label prometheus_server --http-address 0.0.0.0:10903 --grpc-address 0.0.0.0:10904  --store 127.0.0.1:10906 --store 172.16.1.39:10901
# 说明
--query.replica-label 指定数据去重的标记label
--store 指定Sidercar或Store Gateway的连接地址
--http-address http服务端口

服务启动后可通过http://ip:10903查询指标,界面如下

Querier兼容Prometheus的API接口,因此Grafana可直接将其做为Prometheus数据源

到此,就完成了prometheus高可用存储,及统一查询的实现,觉得好用的点个收藏 吧~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值