技术分享 | 灭霸与普罗米修斯之无限存储的高可用方案

こ雲淡風輕ζ 提交于 2019-12-28 06:28:01

作者:王继顺
宝尊电商 DBA,主要负责数据库监控告警以及自动化平台的设计开发工作,擅长数据库性能调优、故障诊断。

背景

随着公司各个环境的服务器数量增加,部署有多套 Prometheus(包括生产、测试、Tidb、Kubernetes 等)集群,在一定集群规模下,普通 Prometheus 集群的承载能力会出现短板(查询速度慢、OOM、以及存储空间不够等问题)。加上公司需要保存双十一、双十二等活动期间的监控数据,来统计活动期间同比环比的监控报表。

SO!我们需要有一个统一的查看各个 Prometheus 监控数据的入口,而且具有不受限制的历史数据存储能力的解决方案,经过多个方案的 POC,最终选用 Thanos 的解决方案并和大家分享一下。

Thanos 介绍

image

Thanos 的主要特性

  1. 全局视图:与现有 Prometheus 设置无缝集成,能够跨集群联合,跨所有连接的 Prometheus 服务器的全局查询视图,很好的对 HA 中的 Prometheus 进行容错路由查询。

  2. 不受限的保留数据:支持各种对象存储。

  3. 压缩和降准采样:对历史数据进行自定义的降准采样以大幅提高查询速度。

  4. 实现包括 Prometheus 在内的各个组件高可用。

  5. 能够记录规则,实现告警。

Thanos 架构介绍

image

Thanos 只有一个二进制启动文件,但是根据启动时候的变量不同分为多个组件,结合架构图,我们来介绍一下各个组件所实现的功能。

Sidecar

Sidecar 必须与 Prometheus 一起部署,实现将 Prometheus 监控数据上传到对象存储,并允许 Querier 查询器高效的查询 Prometheus 数据。

Bucket

Bucket 是用来检测对象存储(Object Storage)的一组工具,以及提供了一个 web 界面,来查看对象存储中的块(Blocks)。对象存储可以选择 GCS(Google Cloud Storage),AWS/S3,Azure Storage Account,OpenStack Swift,Tencent COS,AliYun OSS 等,本文部署实践使用的 S3 作为对象存储。

Store

Store 组件在对象存储上实现 Store API,充当了对象存储的网关,使其与对象存储同步,本地只保留对象存储中所有块的少量的源数据信息。

Querier/Query

Querier 组件实现了 Prometheus http v1 API,完全兼容 Promql 查询,他可以连接 Store 组件和 Sidecar 组件实现从对象存储和 Prometheus 中查询所需数据,并可以从任何实现 Store API 的对象中查询数据。

Querier 组件是完全无状态的查询器,可以水平扩展实现高可用。

Compact

Compact 组件为 Thanos 的压缩器。负责压缩在对象存储中的数据,还负责数据的降准采样。

例:超过 30 天的数据创建 5m 的降准采样(降准采样并不是为了减少存储,而是为了在进行长时间范围查询的时候更快的返回结果)

Rule/Ruler

Rule 组件来定义 Thanos 的告警规则,能够通过 Query 组件进行多套 Promethus 监控值的查询,达到单个 Prometheus 告警阈值的效果。当然还是会有限制,因为 Rule 相比 Prometheus 在本地查询来说,Ruler 分布式的读取远程的 Store API 对象更容易发生查询失败。所以官方还是建议将告警规则保留在 Promethus 中。

本文的部署章节中未涉及 Rule 组件部署。

更多具体信息请参阅:

https://thanos.io/components/rule.md/

Check

Check 组件来检查 Rule 组件使用的规则是否可用,类似于 Promtool check rules

配置

二进制安装包下载
https://github.com/thanos-io/thanos/releases
./thanos --help
usage: thanos [<flags>] <command> [<args> ...]

A block storage based long-term storage for Prometheus

Flags:
  -h, --help               Show context-sensitive help (also try --help-long and --help-man).
      --version            Show application version.
      --log.level=info     Log filtering level.
      --log.format=logfmt  Log format to use.
      --tracing.config-file=<file-path>  
                           Path to YAML file with tracing configuration. See format details:
                           https://thanos.io/tracing.md/#configuration
      --tracing.config=<content>  
                           Alternative to 'tracing.config-file' flag (lower priority). Content of YAML file with tracing
                           configuration. See format details: https://thanos.io/tracing.md/#configuration

Commands:
  help [<command>...]
    Show help.

  sidecar [<flags>]
    sidecar for Prometheus server

  store [<flags>]
    store node giving access to blocks in a bucket provider. Now supported GCS, S3, Azure, Swift and Tencent COS.

  query [<flags>]
    query node exposing PromQL enabled Query API with data retrieved from multiple store nodes

  rule [<flags>]
    ruler evaluating Prometheus rules against given Query nodes, exposing Store API and storing old blocks in bucket

  compact [<flags>]
    continuously compacts blocks in an object store bucket

  bucket verify [<flags>]
    Verify all blocks in the bucket against specified issues

  bucket ls [<flags>]
    List all blocks in the bucket

  bucket inspect [<flags>]
    Inspect all blocks in the bucket in detailed, table-like way

  bucket web [<flags>]
    Web interface for remote storage bucket

  downsample [<flags>]
    continuously downsamples blocks in an object store bucket

  receive [<flags>]
    Accept Prometheus remote write API requests and write to local tsdb (EXPERIMENTAL, this may change drastically without
    notice)

  check rules <rule-files>...
    Check if the rule files are valid or not.

部署

Sidecar

配置 Prometheus
  • 更改配置文件 prometheus.yml 中的 external_labels并reload,目的是使用此 labels 在 Thanos 中区分各个不同 Prometheus 集群。
  external_labels:
    cluster: 'test-cluster'
    monitor: "prometheus"
    replica: "A"
启动 Promethus
  • 本地 Prometheus 保留 30 天,同时需要添加 --storage.tsdb.min-block-duration=2h --storage.tsdb.max-block-duration=2h 这两个参数(Prometheus 服务器 IP 为 :1.1.1.1.1)
./prometheus \
--config.file=/data1/deploy/conf/prometheus.yml \
--web.listen-address=:9090 \
--web.external-url=http://0.0.0.0:9090/ \
--web.enable-admin-api \
--log.level=info \
--storage.tsdb.path=/data1/deploy/prometheus2.0.0.data.metrics \
--storage.tsdb.min-block-duration=2h \
--storage.tsdb.max-block-duration=2h \
--storage.tsdb.retention=30d
启动 Sidecar
./thanos sidecar \
--tsdb.path /data1/deploy/prometheus2.0.0.data.metrics \
--prometheus.url http://localhost:9090 \
--objstore.config-file bucket_config.yaml \
--shipper.upload-compacted
Bucket 配置文件
cat bucket_config.yaml 
type: S3
config:
  bucket: "bucket的名字"
  endpoint: 's3的链接地址'
  access_key: "s3的access_key"
  insecure: true  #是否使用安全协议http或https
  signature_version2: false
  encrypt_sse: false
  secret_key: "s3的scret_key"
  put_user_metadata: {}
  http_config:
    idle_conn_timeout: 90s
    response_header_timeout: 2m
    insecure_skip_verify: false
  trace:
    enable: false
  part_size: 134217728 
  • 启动成功后 Sidecar 组件就会向 S3 发送所有的本地数据以及新在 Prometheus 中落盘的数据

安装Store,Query,Compact,Bucket

  • 本文使用单台服务器部署上边四个组件,实现高可用可以使用多台服务器部署(服务器 IP 为 1.2.3.4)
启动 Store
./thanos store \
--data-dir /service/thanos-0.9.0.linux-amd64/store \
--objstore.config-file bucket_config.yaml \
--http-address 0.0.0.0:19191 \
--grpc-address 0.0.0.0:19090
启动 Query
./thanos query \
--http-address 0.0.0.0:19193 \
--grpc-address 0.0.0.0:19091 \
--store 1.2.3.4:19090  \ #接入Store
--store 1.1.1.1:10901    #接入Sidecar
启动 Compact
./thanos compact  \
--data-dir  /service/thanos-0.9.0.linux-amd64/compact   \
--http-address  0.0.0.0:19192  \
--objstore.config-file bucket_config.yaml

至此 Thanos 已经搭建完成!

安装成功后可以访问:

http://1.2.3.4:19193/graph

image

  • 可以看到界面与 Prometheus 非常相似,完全兼容 Promql,通过 thanos web ui 可以查看所有历史监控数据。

  • 在 store 中可以看到 thanos 集群中接入的 Store 和 Sidecar,并且可以看到目前在 thanos 集群中保留监控数据的最大和最小时间等信息。

image

启动 Bucket web

Bucket web 是用于检查对象存储块信息的交互式的 web UI

./thanos bucket web  \
--http-address=0.0.0.0:19194 \
--objstore.config-file bucket_config.yaml

安装成功后可以访问

http://1.2.3.4:19194/

image

image

更多信息可以使用 ./thanos --help 来获取

使用

  • 搭建完成后将 Thanos query http 接入 Grafana,实现统一的访问入口,对跨 Prometheus 集群的聚合等操作。

image

相关链接

官网:https://thanos.io

Github:https://github.com/thanos-io/thanos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!