> 文章列表 > thanos prometheus 的高可用、长期存储二进制部署

thanos prometheus 的高可用、长期存储二进制部署

thanos prometheus 的高可用、长期存储二进制部署

1.简介

http://thanos.io/

thanos 是具有长期存储功能的开源、高可用性 Prometheus的集群组件。

  • 全局查询视图
    跨多个 Prometheus 服务器和集群查询指标

  • 无限保留
    使用对象存储扩展系统,不限时间保留指标。

  • Prometheus兼容
    兼容 Prometheus api,用于grafana

  • 降低采样以及块压缩
    在查询大时间范围或配置复杂的保留策略时,对历史数据进行降低采样以提高查询速度。

降低采样如何做:
正常情况下,每分钟一个数据点(指标样本)
为超过40 小时(2d)的块创建 5m 采样
为超过10 天(2w)的块创建 1h 采样

使得我们在大跨度时间内查询时,能够在不获取过多样本的情况下,保持查询的精度。

2. 架构

2.1 以sidecar方式部署

thanos prometheus 的高可用、长期存储二进制部署

sidecar将TSDB块上传到对象存储,因为Prometheus每两小时生成一次。

2.2 以Receive方式部署

thanos prometheus 的高可用、长期存储二进制部署

使用Remote Write会增加Prometheus的内存占用。大多数情况内存使用量会增加约25%,也取决于指标数据的类型及大小,对于WAL中每个时序,远程 写入代码缓存序列ID到标签值的映射,大量序列将显著增加内存使用量。

2.3 基础组件

  • Query: 实现了 Prometheus API,对外提供与 Prometheus 一致的查询接口
  • Sidecar: 用于连接 Prometheus,提供 Query 查询接口、也可以上报数据
  • Store Gateway:访问放在对象存储的指标数据
  • Compact: 压缩采样、清理对象存储中的数据
  • Receive: 接收 Prometheus Remote Write 的数据,将其上传到云存储
  • Ruler:配置和管理告警规则,以进行展示和上传
  • query frontend:实现prometheus的v1 API代理到它到查询,同时缓存响应和可选的天分割

2. 部署

2.1 二进制部署

2.1.1 prometheus 配置

https://prometheus.io/docs/prometheus/latest/getting_started/

下载 prometheus-2.37.0.linux-amd64.tar.gz
上传解压到
/root/thanos/prometheus-2.37.0.linux-amd64

cd /root/thanos/
mv prometheus-2.37.0.linux-amd64 prometheus
cd /root/thanos/prometheus
cat <<EOF >prometheus.yml
global:scrape_interval: 15sexternal_labels:monitor: 'prom01'
scrape_configs:- job_name: 'node-exporter'scrape_interval: 5sstatic_configs:- targets:- 10.55.1.13:9100- 10.55.1.25:9100
EOF
 ./prometheus \\--storage.tsdb.max-block-duration=2h \\--storage.tsdb.min-block-duration=2h \\--web.enable-lifecycle \\--web.enable-admin-api \\--config.file=prometheus.yml \\--web.listen-address="0.0.0.0:9091" &> prometheus.log &
  • –web.enable-admin-api标志以支持 sidecar 从 Prometheus 获取元数据,如external_labels 。
  • –web.enable-lifecycle 则启用该标志,prometheus 允许通过 POST /-/reload 重新加载配置,可以使用sidecar配置重新加载功能(–reload.*标志)
  • –storage.tsdb.min-block-duration和–storage.tsdb.max-block-duration值必须设置相等以禁用本地压缩,推荐使用默认值2h。便于 sidecar上传数据到存储桶。

2.1.2 node-exporter

./node_exporter--collector.textfile.directory=/tmp/node-exporter/--collector.tcpstat--collector.processes--collector.systemd--collector.supervisord

2.1.3 minio

https://docs.min.io/minio/baremetal/quickstart/linux.html#quickstart-for-linux
MinIO 是一种高性能对象存储解决方案,原生支持 Kubernetes 部署。MinIO 提供与 Amazon Web Services S3 兼容的 API,并支持所有核心 S3 功能。
下载当前版本 minio version RELEASE.2022-08-08T18-34-09Z
上传到目录 /root/thanos/minio

chmod +x minio
cd /root/thanos/minio
MINIO_ROOT_USER=minio MINIO_ROOT_PASSWORD=12345678 ./minio server ./data --address :9095 --console-address :9096 &

2.1.4 sidecar

https://thanos.io/tip/components/sidecar.md/
需要与 prometheus 部署到同一台主机上共享数据目录。

下载 当前版本 thanos, version 0.27.0
https://github.com/thanos-io/thanos/tags

cd /root/thanos
tar xf thanos-0.27.0.linux-amd64.tar.gz
mv thanos-0.27.0.linux-amd64 thanos

2.1.5 对象存储配置

https://thanos.io/tip/thanos/storage.md/

cd /root/thanos/thanos
cat <<EOF > bucket.yml
type: S3
config:bucket: "thanos"endpoint: "192.168.171.129:9095"region: ""aws_sdk_auth: falseaccess_key: "thanos"insecure: truesignature_version2: falsesecret_key: "12345678"put_user_metadata: {}
EOF
cd /root/thanos/thanos
./thanos sidecar --grpc-address=0.0.0.0:10901 --http-address=0.0.0.0:10902 --tsdb.path=/root/thanos/prometheus/data/ --objstore.config-file=bucket.yml --prometheus.url=http://localhost:9091/

2.1.6 Store Gateway 存储网关

–data-dir 用于指定本地的缓存目录,缓存索引数据。

cd /root/thanos/thanos
./thanos store \\--data-dir  ./store \\--objstore.config-file bucket.yml \\--grpc-address 0.0.0.0:19093 \\--http-address 0.0.0.0:19094

2.1.7 Querier/Query 查询器

cd /root/thanos/thanos
./thanos query \\--http-address 0.0.0.0:19095 \\--grpc-address 0.0.0.0:19096 \\--store       127.0.0.1:10901 \\--store       127.0.0.1:19093

2.1.8 query-frontend 查询前端

运行查询前端的示例命令:

https://thanos.io/tip/components/query-frontend.md/#query-frontend
该thanos query-frontend命令实现了一个可以放在 Thanos Queriers 前面的服务,以改进读取路径。它基于Cortex 查询前端组件,因此您可以找到一些常见的功能,例如Splitting和Results Caching。

查询前端是完全无状态和水平可扩展的。

thanos query-frontend \\--http-address     "0.0.0.0:9090" \\--query-frontend.downstream-url="<thanos-querier>:<querier-http-port>"

注意:目前只有范围查询(/api/v1/query_range API 调用)实际上是通过查询前端处理的。所有其他 API 调用只是直接转到下游查询器,这意味着只有范围查询被拆分和缓存。但我们也计划支持即时查询。

2.1.9 Compactor 压缩器

compactor 是一个不参与 Thanos 集群的单实例进程。它只指向一个对象存储桶,并不断地将多个较小的块合并为较大的块。显着减少了存储桶中的总存储大小、存储节点上的负载以及从存储桶中获取查询数据所需的请求数。

压缩器还进行额外的批处理,例如降低采样和应用保留策略。

./thanos compact \\--data-dir ./compact \\--objstore.config-file bucket.yml \\--http-address 0.0.0.0:19097 \\--wait

数据保留时间设置

  • –retention.resolution-raw=0d
  • –retention.resolution-5m=0d
  • –retention.resolution-1h=0d

compactor 不在查询或数据备份的关键路径上。它可以作为定期批处理作业运行,也可以保持运行以始终尽快压缩数据。建议提供 100-300GB 的本地磁盘空间用于数据处理。

访问:
http://192.168.171.129:19095

2.1.10 grafana

tar xf grafana-enterprise-8.5.6.linux-amd64.tar.gz  -C /data/
cd /data/
mv grafana-8.5.6/ grafana
cd /data/grafana
./bin/grafana-server web &> grafana.log &

https://starsl.cn/article/8919.html

2.1.11 多个prometheus 进行聚合

cd /root/thanos/
mv prometheus-2.37.0.linux-amd64 prometheus02
cd /root/thanos/prometheus02
cat <<EOF >prometheus.yml
global:scrape_interval: 15sexternal_labels:monitor: 'prom02'
scrape_configs:- job_name: 'node-exporter'scrape_interval: 5sstatic_configs:- targets:- 10.55.1.31:9100- 10.55.1.4:9100
EOF

运行(后台运行,修改了端口)

cd /root/thanos/prometheus02
./prometheus \\--storage.tsdb.max-block-duration=2h \\--storage.tsdb.min-block-duration=2h \\--web.enable-lifecycle \\--web.enable-admin-api \\--config.file=prometheus.yml \\--web.listen-address="0.0.0.0:9092"  &> prometheus.log  &

再启动一个对应的 sidecar,使用相同的bucket.yml

mkdir /root/thanos/sidecar02
cd /root/thanos/sidecar02
cp ../thanos/thanos .
cp ../thanos/bucket.yml  .
./thanos sidecar \\
--grpc-address=0.0.0.0:10908 \\
--http-address=0.0.0.0:10909 \\
--tsdb.path=/root/thanos/prometheus02/data/ \\
--objstore.config-file=bucket.yml \\
--prometheus.url=http://localhost:9092/ &

把新启动的 sidecar 加入 query

cd /root/thanos/thanos
./thanos query \\--http-address 0.0.0.0:19095 \\--grpc-address 0.0.0.0:19096 \\--store       127.0.0.1:10901 \\--store       127.0.0.1:19093 \\--store       127.0.0.1:10908

访问:
http://192.168.171.129:19095

2.1.12 sidecar 高阶配置

  • –shipper.upload-compacted 在启动时同步 Prometheus 本地存储中的所有较旧的现有块。

  • –reloader.rule-dir=DIR_NAME 自动触发prometheus重新加载配置 (需要prometheus启用–web.enable-lifecycle)

cd /root/thanos/sidecar02
./thanos sidecar \\
--grpc-address=0.0.0.0:10908 \\
--http-address=0.0.0.0:10909 \\
--tsdb.path=/root/thanos/prometheus02/data/ \\
--objstore.config-file=bucket.yml \\
--reloader.rule-dir=/root/thanos/prometheus02/rules \\
--reloader.rule-dir=/root/thanos/prometheus02/prometheus.yml \\
--reloader.watch-interval=1m \\
--prometheus.url=http://localhost:9092/ &>./sidecar.log &

–reloader.config-file=CONFIG_FILE ,以 $(VARIABLE) 格式替换其中的环境变量,并在 --reloader.config-envsubst-file=OUT_CONFIG_FILE 文件中输出生成的配置。

生成配置文件
cd /root/thanos/prometheus02/
cp prometheus.yml prometheus_env.yml
修改 prometheus_env.yml 中 monitor: ‘$(ENV)’

cd /root/thanos/sidecar02
ENV=prom02 ./thanos sidecar \\
--grpc-address=0.0.0.0:10908 \\
--http-address=0.0.0.0:10909 \\
--tsdb.path=/root/thanos/prometheus02/data/ \\
--objstore.config-file=bucket.yml \\
--reloader.rule-dir=/root/thanos/prometheus02/rules \\
--reloader.rule-dir=/root/thanos/prometheus02/prometheus.yml \\
--reloader.watch-interval=1m \\
--reloader.config-file=/root/thanos/prometheus02/prometheus_env.yml \\
--reloader.config-envsubst-file=/root/thanos/prometheus02/prometheus.yml \\
--prometheus.url=http://localhost:9092/ &>./sidecar.log &

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IUNLdcS7-1681254793144)(assets/image-20230411181539-woz5whd.png)]