
在开始之前,我们先来回顾下prometheus的存储机制。
prometeus的数据以 2 小时为一个 block 存储,每个block有索引支持快速查询,的默认保留时间为15天,虽然可以通过参数调整存储的时间,但是对于大量增长的数据量,如果要查询较长时间的数据查询时需要扫描更大范围的数据block,这会导致查询效率的下降。
Prometheus 重启时需要扫描本地 TSDB block 进行 WAL(写前日志)恢复,数据量越大,恢复时间越长,可能从几分钟变成几十分钟甚至更久。
如果 Prometheus 长时间存储数据(数月甚至数年),会出现:
因此在生产上,更建议采用远程存储,如利用thanos、victoriaMetrics等实现。
今天介绍的就是利用thanos实现数据存储到对象存储,那么为什么是对象存储呢?因为对象存储 = 无限容量 + 低成本
关于thanos的sidecar和querier,介绍可以参考前面的文章。
为了展示效果,我们需要先用thanosbench工具生成一年的prometheus数据
mkdir -p /root/prom-eu1 && docker run -i quay.io/thanos/thanosbench:v0.2.0-rc.1 block plan -p continuous-365d-tiny --labels 'cluster="eu1"' --max-time=6h | docker run -v /root/prom-eu1:/prom-eu1 -i quay.io/thanos/thanosbench:v0.2.0-rc.1 block gen --output.dir prom-eu1
prom的配置文件 prometheus0_eu1.yml
global:
scrape_interval: 5s
external_labels:
cluster: eu1
replica: 0
tenant: team-eu # Not needed, but a good practice if you want to grow this to multi-tenant system some day.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['127.0.0.1:9090']
- job_name: 'sidecar'
static_configs:
- targets: ['127.0.0.1:19090']
- job_name: 'minio'
metrics_path: /minio/prometheus/metrics
static_configs:
- targets: ['127.0.0.1:9000']
- job_name: 'querier'
static_configs:
- targets: ['127.0.0.1:9091']
- job_name: 'store_gateway'
static_configs:
- targets: ['127.0.0.1:19091']启动prometheus调整保留时间为1000d
docker run -d --net=host --rm -v $(pwd)/prometheus0_eu1.yml:/etc/prometheus/prometheus.yml -v $(pwd)/prom-eu1:/prometheus -u root --name prometheus-0-eu1 quay.io/prometheus/prometheus:v2.38.0 --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.retention.time=1000d --storage.tsdb.path=/prometheus --storage.tsdb.max-block-duration=2h --storage.tsdb.min-block-duration=2h --web.listen-address=:9090 --web.enable-lifecycle --web.enable-admin-api根据前面介绍的内容,我们同样需要启动thanod的sidecar和querier
docker run -d --net=host --rm --name prometheus-0-eu1-sidecar -u root quay.io/thanos/thanos:v0.28.0 sidecar --http-address 0.0.0.0:19090 --grpc-address 0.0.0.0:19190 --prometheus.url http://172.17.0.1:9090docker run -d --net=host --rm --name querier quay.io/thanos/thanos:v0.28.0 query --http-address 0.0.0.0:9091 --query.replica-label replica --store 172.17.0.1:19190mkdir /root/minio && docker run -d --rm --name minio -v /root/minio:/data -p 9000:9000 -e "MINIO_ACCESS_KEY=minio" -e "MINIO_SECRET_KEY=melovethanos" minio/minio:RELEASE.2019-01-31T00-31-19Z server /data新建一个目录用于存储thanos的数据
mkdir /root/minio/thanos通过启动文件中的key及secret来访问minio ui

然后我们新增一个thanos访问minio的配置文件bucket_storage.yaml
type: S3
config:
bucket: "thanos"
endpoint: "172.17.0.1:9000"
insecure: true
signature_version2: true
access_key: "minio"
secret_key: "melovethanos"停掉sidecar
docker stop prometheus-0-eu1-sidecar重新启动sidecar,需要新增一些配置
docker run -d --net=host --rm -v $(pwd)/bucket_storage.yaml:/etc/thanos/minio-bucket.yaml -v $(pwd)/prom-eu1:/prometheus --name prometheus-0-eu1-sidecar -u root quay.io/thanos/thanos:v0.28.0 sidecar --tsdb.path /prometheus --objstore.config-file /etc/thanos/minio-bucket.yaml --shipper.upload-compacted --http-address 0.0.0.0:19090 --grpc-address 0.0.0.0:19190 --prometheus.url http://172.17.0.1:9090现在去看minio,可以看到数据已经进去了

thanos store gateway
docker run -d --net=host --rm -v /root/editor/bucket_storage.yaml:/etc/thanos/minio-bucket.yaml --name store-gateway quay.io/thanos/thanos:v0.28.0 store --objstore.config-file /etc/thanos/minio-bucket.yaml --http-address 0.0.0.0:19091 --grpc-address 0.0.0.0:19191然后我们需要变更查询器来通过gateway去查询数据
docker stop querier && docker run -d --net=host --rm --name querier quay.io/thanos/thanos:v0.28.0 query --http-address 0.0.0.0:9091 --query.replica-label replica --store 172.17.0.1:19190 --store 172.17.0.1:19191可以看到已经历史一年的数据

这篇文章介绍到这里,后面我们介绍thanos的降采样组件
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。