深入浅出--从零开始建设k8s监控之thanos(六)

前言

书接上文,目前环境已经做好了水平拆分,是这个样子的

本文使用thanos对这些prometheus进行数据汇聚,并且详细讨论一下thanos

环境准备

组件 版本
操作系统 Ubuntu 22.04.4 LTS
docker 24.0.7
thanos 0.36.1

thanos概述

thanos主要有4个组件

  • receive:独立部署,提供了数据写入的api,prometheus通过这个api把数据推送到receive的对象存储
  • sidecar:与prometheus部署在一起,成为prometheus的sidecar,负责把prometheus本地的数据上传至对象存储当中
  • query:独立部署,是一个兼容了prometheus的查询组件,汇总了来自不同来源的查询结果,并且可以从Sidecar和Store中读取数据
  • store:独立部署,提供了对象数据存储功能,并且提供相关的api,query通过该api查询历史数据

thanos有两种工作模式,receive与sidecar

receive模式

Receive 是一个可以接收来自 Prometheus 远程写入的数据的组件,主要用于高可用的集群环境中。它能够处理来自多个 Prometheus 实例的写入请求

1. 部署thanos receive

复制代码
▶ mkdir -p data && chmod 777 data
▶ docker run -d --net=host \
  --name thanos-receive \
  -v "./data:/data" \
  registry.cn-beijing.aliyuncs.com/wilsonchai/thanos:0.36.1 \
  receive \
    --grpc-address 0.0.0.0:10907 \
    --http-address 0.0.0.0:10909 \
    --receive.replication-factor 1 \
    --label "receive_cluster=\"wilson-test\"" \
    --remote-write.address 0.0.0.0:10908

2. 改造prometheus

分别对3个prometheus进行改造,分别修改其configmap

  • 负责采集k8s监控数据

    ...
    scrape_configs:
    ...
    remote_write:
    - url: "http://10.22.11.156:10908/api/v1/receive"
    write_relabel_configs:
    - action: replace
    source_labels: []
    target_label: from
    replacement: "prometheus-k8s"

  • 负责采集node监控数据

    ...
    scrape_configs:
    ...
    remote_write:
    - url: "http://10.22.11.156:10908/api/v1/receive"
    write_relabel_configs:
    - action: replace
    source_labels: []
    target_label: from
    replacement: "prometheus-node"

  • 负责采集pushgateway监控数据

    ...
    scrape_configs:
    ...
    remote_write:
    - url: "http://10.22.11.156:10908/api/v1/receive"
    write_relabel_configs:
    - action: replace
    source_labels: []
    target_label: from
    replacement: "prometheus-pushgateway"

3. 部署thanos query

复制代码
docker run -d --net=host \
  --name thanos-query \
  registry.cn-beijing.aliyuncs.com/wilsonchai/thanos:0.36.1 \
  query \
    --http-address "0.0.0.0:39090" \
    --grpc-address "0.0.0.0:39091" \
    --store "127.0.0.1:10907"

登录thanos-query提供的web界面http://127.0.0.1:39090/,可以看到3个prometheus节点的数据都已汇聚,并且已经标注了来源,from标签

4. 部署对象存储 minio

目前我们的数据还是存储在receive的本地磁盘上的,为了长期保留数据,将其推送至自建的对象存储当中

创建minio

复制代码
docker run -d --name minio --net host \
  -e "MINIO_ACCESS_KEY=minioadmin" \
  -e "MINIO_SECRET_KEY=minioadmin" \
  -v ./data:/data \
  -v ./config:/root/.minio \
  registry.cn-beijing.aliyuncs.com/wilsonchai/minio:RELEASE.2024-10-13T13-34-11Z \
  server /data --console-address ":9000" -address ":9090"

通过用户名密码登录web页面http://127.0.0.1:9000/,然后创建bucket,再创建access key

回到thanos,创建bucket.yml文件

复制代码
type: S3
config:
  bucket: "wilson-test"
  endpoint: "127.0.0.1:9090"
  access_key: "zzUrkBzyqcCDXySsMLlS"
  secret_key: "nWCcztESnxnUZIKSKsELGEFdg6l6fjzhtqkARJB8"
  insecure: true

最后重建thanos receive

复制代码
docker run -d --net=host \
  --name thanos-receive \
  -v ./bucket.yml:/etc/thanos/bucket.yml \
  -v "./data:/data" \
  registry.cn-beijing.aliyuncs.com/wilsonchai/thanos:0.36.1 \
  receive \
    --grpc-address 0.0.0.0:10907 \
    --http-address 0.0.0.0:10909 \
    --objstore.config-file=/etc/thanos/bucket.yml \
    --tsdb.retention=2d \
    --label "receive_cluster=\"wilson-test\"" \
    --remote-write.address 0.0.0.0:10908

默认情况下每个2小时,thanos就会往对象存储当中推送数据,这个时间可以调整,只需要调整以下参数即可

复制代码
    --tsdb.min-block-duration=10m \
    --tsdb.max-block-duration=10m \

检查minio的控制台,检查是否成功上传

5. 部署thanos-store,直接读取对象存储

store gateway就是简单的去读取对象存储当中的数据,通过 api暴露给thanos-query,thanos-query调用store api然后获取数据,这样就可以直接读取对象存储的数据了

部署store

复制代码
docker run -d --net=host \
  --name thanos-store \
  -v "./data-store:/data" \
  -v ./bucket.yml:/etc/thanos/bucket.yml \
  registry.cn-beijing.aliyuncs.com/wilsonchai/thanos:0.36.1 \
  store \
    --objstore.config-file "/etc/thanos/bucket.yml"

这里的bucket.yaml,还是沿用了上面的bucket.yaml,指向同样的对象存储

调整thanos-query的配置,新增thanos-store的地址

复制代码
docker run -d --net=host \
  --name thanos-query \
  registry.cn-beijing.aliyuncs.com/wilsonchai/thanos:0.36.1 \
  query \
    --http-address "0.0.0.0:39090" \
    --grpc-address "0.0.0.0:39091" \
    --store "127.0.0.1:10907" \
    --store "127.0.0.1:10901"

127.0.0.1:10901就是新增的store的grpc地址,添加完毕后,检查thanos-query的web页面

thanos-query的数据源来自两个地方,一个是receive,一个是store,并且thanos-query有自动去重的功能,真牛皮!

6. 小结

  • 以上就是receive模式的基本使用方法,在receive模式中,使用了receive、query、store等组件,并且展示了它们之间如何协调合作
  • 上述演示的对象存储,是自建的minio,在实际工作中,可以使用云厂商的对象存储相互配合,易于管理

联系我

  • 联系我,做深入的交流

至此,本文结束

在下才疏学浅,有撒汤漏水的,请各位不吝赐教...