一、架构

学习地址

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

thanos官网:thanos.io

prometheus官网:https://prometheus.io/

二、Thanos介绍

Thanos(灭霸):开源监控解决方案,实现与prometheus的配合。

Prometheus:开源监控解决方案。

2.1 特点

1:兼容现有Prometheus API 接口(对prometheus版本有要求,建议直接使用最新版);

2:支持多种对象存储(兼容S3);

3:支持数据压缩和降准采样;

......

2.2  Thanos组件介绍

thanos sidecar:与prometheus集成,获取prometheus数据供query查询其本地数据,同时每两小时会同步数据到对象存储(必须与prometheus安装在同一主机或者同一pod中);

thanos store gateway:提供历史数据查询功能,其方式为:thanos query调用store gateway接口,store通过对象存储查询数据并返回query需要的形式,需要一个临时空间;

thanos query:查询入口,即可不再需要prometheus提供的查询入口;

thanos compactor:数据处理,将监控数据降准采样和压缩。

三、安装

thanos下载:

wget https://github.com/thanos-io/thanos/releases/download/v0.32.5/thanos-0.32.5.linux-amd64.tar.gz

prometheus下载:

wget https://github.com/prometheus/prometheus/releases/download/v2.47.2/prometheus-2.47.2.linux-amd64.tar.gz

下载好后请解压到自己需要的位置

 3.1 prometheus安装

参照我前面文章:【精选】Prometheus完整搭建+主机、进程监控(邮件)告警(学习笔记)_prometheus groupname=~-CSDN博客

包括prometheus、node_exporter、grafana

需要注意的是:

1、对于具有多个prometheus节点,为保证thanos的高可用性,需要对prometheus进行标签区分,这样thanos才能进行去重操作(即prometheus.yml需要进行特别修改);

2、thanos为每两小时上传同步一次,故其TSDB的块范围必须也为两小时,否则sidecar启动会报错,同时compactor压缩时也会出错(即prometheus启动参数需要特别修改)。

1、在prometheus.yml配置文件全局范围处添加如下配置:

global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).
  external_labels:
    env: demo
    replica: 0
###即以replica进行区分
2、/etc/systemd/system/prometheus.service文件可参照如下配置:
[Unit]
Description=Prometheus Server
After=network-online.target

[Service]
Restart=on-failure
Type=simple
User=app      #按实际更改
Group=app     #按实际更改
ExecStart=/data/prometheus/prometheus --config.file=/data/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus/data --storage.tsdb.min-block-duration=2h --storage.tsdb.max-block-duration=2h --storage.tsdb.retention=1d --storage.tsdb.wal-compression --web.enable-lifecycle --log.level=info --web.listen-address=0.0.0.0:9090

[Install]
WantedBy=multi-user.target

部分参数说明:

--storage.tsdb.min-block-duration=2h

--storage.tsdb.max-block-duration=2h

改为两小时,实际上关闭了prometheus本地压缩,避免thanos compactor压缩失败;

--storage.tsdb.retention是指prometheus在本地存储多长时间的数据,也可改为2h;

--log.level=info参数,还未搭建完成时建议改为info,全部搭建成功持续观察无问题后可改为error

 3.2 thanos sidecar

先给下我的安装方案

注:grpc为thanos组件间的通信

主机1prometheus、thanos-sidecar、thanos-store、thanos-query
主机2thanos-compactor

 注:除sidecar外,其他thanos组件可部署其他主机上(尤其在虚拟机上性能不足且集群数量过大时)。

组件\端口gRPCHTTP
Sidecar1090119091
store gateway1090219092
query109009090
compactor1090319093

编辑thanos sidecar的service文件:

vim /etc/systemd/system/thanos-sidecar.service

[Unit]
Description=Thanos Sidecar
After=network.target

[Service]
User=your-user         #*#
Group=your-user-group  #*#
ExecStart=/data/thanos/thanos sidecar --prometheus.url=http://192.168.100.100:9090 --tsdb.path=/data/prometheus/data --objstore.config-file=/data/thanos/object-store.yml --grpc-address=192.168.100.100:10901 --http-address=192.168.100.100:19091 --log.level=info

Restart=on-failure
[Install]
WantedBy=multi-user.target

systemctl enable thanos-sidecar --now

--prometheus.url:prometheus服务的http地址

--tsdb.path:prometheus的数据本地存储地址

--objstore.config-file:对象存储的配置文件地址

vim /data/thanos/obejct-store.yml
type: S3                          #我这里使用的S3协议,如果其他协议的对象存储请区分
config: 
  bucket: prometheus              #对象存储桶名称
  endpoint: s3*******.com         #对象存储的URL
  access_key: ********            #对象存储的ak 
  secret_key: ********            #对象存储的Sk
  insecure: true                  #当你访问url采用的是http时选择ture,反之false

 3.3 thanos store

编辑thanos store的service文件:

vim /etc/systemd/system/thanos-store.service

[Unit]
Description=Thanos Store
After=network.target

[Service]
User=your-user            #*#
Group=your-user-group     #*#
ExecStart=/data/thanos/thanos store --data-dir=/data/thanos/store --objstore.config-file=/data/thanos/object-store.yml --grpc-address=192.168.100.100:10902 --http-address=192.168.100.100:19092  --log.level=info

Restart=on-failure
[Install]
WantedBy=multi-user.target

systemctl enable thanos-store --now

--data-dir:store需要一个目录。

验证:打开http://192.168.100.100:19092 

注:我这是已经跑了几天的图,如果你的prometheus本地已经有数据了(prometheus启动至少超过两小时,那么服务启动后web页面就会有显示,即开始上传) 。

3.4 thanos query

 编辑thanos query的service文件:

vim /etc/systemd/system/thanos-query.service

[Unit]
Description=Thanos Query
After=network.target

[Service]
User=your-user          #*#
Group=your-user-group   #*#
ExecStart=/data/thanos/thanos query --http-address=192.168.100.100:19090 --grpc-address=192.168.100.100:10900  --store=192.168.100.100:10902  --store=192.168.100.100:10901 --query.replica-label "replica" --query.replica-label "region" --log.level=info

Restart=on-failure
[Install]
WantedBy=multi-user.target

systemctl enable thanos-query --now

--store:需要填写sidecar和store组件的grpc地址

--query.replica-label:指定重复数据删除的标记label(query查询是会根据标签去判定重复数据)。

验证:打开http://192.168.100.100:19090

UI和用法类似prometheus界面

Use Deduplication:允许根据标签去重

Use Partial Response:允许部分相应,即当有多个store时,允许其中一个故障时仍能进行查询。 

 3.5 thanos compactor

 编辑thanos query的service文件:

vim /etc/systemd/system/thanos-compactor.service

Unit]
Description=Thanos Compactor
After=network-online.target

[Service]
User=app
Group=app
ExecStart=/data/thanos/thanos compact --http-address=192.168.100.100:19093 --data-dir=/data/thanos/compactor --objstore.config-file=/data/thanos/ostore.yml --wait --log.level=info

Restart=on-failure
[Install]
WantedBy=multi-user.target

--data-dir:compactor需要一个目录临时存储文件。

验证:打开http://192.168.100.100:19093

此处已经运行了一段时间

四、grafana 接入

 直接将grafana数据源中的prometheus地址改为thanos query的地址即可。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐