您的当前位置:首页 >系统运维 >Thanos监控系统实战部署 正文

Thanos监控系统实战部署

时间:2025-11-05 05:13:42 来源:网络整理编辑:系统运维

核心提示

Thanos 是一个基于 Prometheus 实现的监控方案,其主要设计目的是解决原生 Prometheus 上的痛点,并且做进一步的提升,主要的特性有:全局查询,高可用,动态拓展,长期存储。下图是

Thanos 是监控一个基于 Prometheus 实现的监控方案,其主要设计目的系统是解决原生 Prometheus 上的痛点,并且做进一步的实战提升,主要的部署特性有:全局查询,高可用,监控动态拓展,系统长期存储。实战

下图是部署 Thanos 官方的架构图:

图片

Thanos组件

Thanos是一组组件,可以组合成一个具有无限存储容量的高可用指标系统,Thanos 主要由如下几个特定功能的组件组成:

边车组件(Sidecar):连接 Prometheus,并把 Prometheus 暴露给查询网关(Querier/Query),监控以供实时查询,系统并且可以上传 Prometheus 数据给云存储,实战以供长期保存查询网关(Querier/Query):实现了 Prometheus API,部署与汇集底层组件(如边车组件 Sidecar,监控或是系统存储网关 Store Gateway)的数据存储网关(Store Gateway):将云存储中的数据内容暴露出来压缩器(Compactor):将云存储中的数据进行压缩和下采样接收器(Receiver):从 Prometheus 的 remote-write WAL(Prometheus 远程预写式日志)获取数据,暴露出去或者上传到云存储规则组件(Ruler):针对监控数据进行评估和报警Bucket:主要用于展示对象存储中历史数据的实战存储情况,查看每个指标源中数据块的源码库压缩级别,解析度,存储时段和时间长度等信息。

读取指标的流程

首先客户端通过 query API 向 query 发起查询,query 将请求转换成 StoreAPI 发送到其他的 query、sidecar、rule 和 store 上。sidecar 接收到来自于 query 发起的查询请求后将其转换成 query API 请求,发送给其绑定的 Prometheus,由Prometheus 从本地读取数据并响应,返回短期的本地采集和评估数据。rule 接收到来自于 query 发起的查询请求后直接从本地读取数据并响应,返回短期的本地评估数据。store 接收到来自于 query 发起的查询请求后首先从对象存储桶中遍历数据块的 meta.json,根据其中记录的时间范围和标签先进行一次过滤。接下来从对象存储桶中读取数据块的 index 和 chunks 进行查询,部分查询频率较高的index 会被缓存下来,下次查询使用到时可以直接读取。最终返回长期的历史采集和评估指标。

对于发送报警的香港云服务器流程如下所示:

Prometheus 根据自身配置的 alerting 规则定期地对自身采集的指标进行评估,当告警条件满足的情况下发起告警到 Alertmanager 上。rule 根据自身配置的 alerting 规则定期的向 query 发起查询请求获取评估所需的指标,当告警条件满足的情况下发起告警到 Alertmanager 上。Alertmanager 接收到来自于 Prometheus 和 rule 的告警消息后进行分组合并后发出告警通知。

特性(优势)

Thanos 相比起原生的 Prometheus 具有以下的一些优势:

统一查询入口——以 Query 作为统一的查询入口,其自身实现了 Prometheus 的查询接口和StoreAPI,可为其他的 Query 提供查询服务,在查询时会从每个 Prometheus 实例的 Sidecar 和 Store Gateway 获取到指标数据。查询去重——每个数据块都会带有特定的集群标签, Query 在做查询时会去除集群标签,将指标名称和标签一致的序列根据时间排序合并。虽然指标数据来自不同的采集源,但是只会响应一份结果而不是多份重复的IT技术网结果。高空间利用率——每个 Prometheus 本身不存储长时间的数据,Sidecar 会将 Prometheus 已经持久化的数据块上传到对象存储中。Compactor 会定时将远端对象存储中的长期数据进行压缩,并且根据采样时长做清理,节约存储空间。高可用——Query 是无状态服务,天生支持水平拓展和高可用。Store、Rule 和 Sidecar 是有状态服务,在多副本部署的情况下也支持高可用,不过会产生数据冗余,需要牺牲存储空间。存储长期数据——Prometheus 实例的 Sidecar 会将本地数据上传到远端对象存储中作为长期数据横向拓展——当 Prometheus 的指标采集压力过大时,可以创建新的 Prometheus 实例,将scrape job 拆分给多个 Prometheus,Querier 从多个 Prometheus 查询汇聚结果,降低单个 Prometheus 的压力跨集群查询——需要合并多个集群的查询结果时,仅需要在每个集群的 Querier 之上再添加一层 Querier 即可,这样的层层嵌套,可以使得集群规模无限制拓展。

对象存储

一般来说, 我们将存储分为文件存储, 块存储和对象存储.

文件存储: 一般都是POSIX协议的,比如我们的操作系统上的XFS,EXT4块存储: 一般都是有虚拟化层实现的,有可能是kernel自带的模块,如AWS的EBS对象存储: 对象存储通常向外提供API接口,系统通过网络向对象存储的接口传输数据.公有云的代表,AWS的s3,私有云的就是MinIO,案例中我也将用MinIO来作为存储.

部署案例

在了解了Thanos的架构和组件服务之后,下面将进行实战配置安装。准备4台虚拟机,配置如下:

部署Promethues

复制node3, node4 执行 useradd -s /sbin/nologin prometheus mkdir -p /app/src cd /app/src wget https://github.com/prometheus/prometheus/releases/download/v2.36.1/prometheus-2.36.1.linux-amd64.tar.gz tar -xvf prometheus-2.36.1.linux-amd64.tar.gz cd prometheus-2.36.1.linux-amd64 mv prometheus promtool /usr/local/sbin mkdir /var/lib/prometheus mv consoles console_libraries /var/lib/prometheus/ mkdir /etc/prometheus mv prometheus.yml /etc/prometheus/ chown -R prometheus:prometheus /usr/local/sbin/prometheus /usr/local/sbin/promtool /etc/prometheus/ /app/prometheus/ /var/lib/prometheus1.2.3.4.5.6.7.8.9.10.11.12.13. 修改配置文件 复制vim /etc/prometheus/prometheus.yml # my global config 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: replica: A # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global evaluation_interval. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here its Prometheus itself. scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: "prometheus" # metrics_path defaults to /metrics # scheme defaults to http. static_configs: - targets: ["localhost:9090"] - job_name: "node_exporter" static_configs: - targets: ["192.168.100.30:9100","192.168.100.40:9100","192.168.100.50:9100","192.168.100.60:9100"]1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42. system文件 复制vim /etc/systemd/system/prometheus.service [Unit] Descriptinotallow=prometheus Documentatinotallow=https://prometheus.io/ After=network.target [Service] Type=simple User=prometheus ExecStartPre=/usr/local/sbin/promtool check config /etc/prometheus/prometheus.yml ExecStart=/usr/local/sbin/prometheus \ --config.file=/etc/prometheus/prometheus.yml \ --web.listen-address=0.0.0.0:9090 \ --web.enable-lifecycle \ --web.enable-admin-api \ --web.console.templates=/var/lib/prometheus/console \ --web.console.libraries=/var/lib/prometheus/console_libraries \ --storage.tsdb.path=/app/prometheus/ \ --storage.tsdb.min-block-duratinotallow=5m \ --storage.tsdb.max-block-duratinotallow=5m \ --storage.tsdb.retention.time=30d \ --log.level=info ExecReload=/bin/curl -X POST http://127.0.0.1:9090/-/reload TimeoutStopSec=20s Restart=always LimitNOFILE=20480000 [Install] WantedBy=multi-user.target1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.

--storage.tsdb.min-block-duratinotallow=5m--storage.tsdb.max-block-duratinotallow=5m 默认为2h, 修改为5分钟, sidecar向store写入数据,方便查看效果.

Node_exporter 安装

复制node1, node2, node3, node4 执行 cd /app/src wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz tar -xvf node_exporter-1.3.1.linux-amd64.tar.gz cd node_exporter-1.3.1.linux-amd64 mv node_exporter /usr/local/sbin/1.2.3.4.5.6. 创建system文件

 vim /usr/lib/systemd/system/node_exporter.service

复制[Unit] Descriptinotallow=node_exporter Documentatinotallow=https://prometheus.io/ After=network.target [Service] Type=simple ExecStart=/usr/local/sbin/node_exporter \ --collector.systemd ExecReload=/bin/kill -HUP TimeoutStopSec=20s Restart=always [Install] WantedBy=multi-user.target1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.

部署 Thanos

Thanos 只需要两个组件就可以简单形成一个集群,query和sidecar用来抽象数据层,query 来查询抽象出来的数据层,提供查询的接口,

根据Thanos架构图,不考虑高可用的情况下除了sidecar组件外,query,store,Compactor组件只需部署一份

复制node1, node3,node4 ,执行 cd /app/src/ wget https://github.com/thanos-io/thanos/releases/download/v0.26.0/thanos-0.26.0.linux-amd64.tar.gz tar -xvf thanos-0.26.0.linux-amd64.tar.gz cd thanos-0.26.0.linux-amd64 mv thanos /usr/local/sbin mkdir /app/thanos mkdir /app/thanos/compact mkdir /app/thanos/store mkdir /app/thanos/ruler mkdir /etc/thanos1.2.3.4.5.6.7.8.9.10.11.12.13.14.15. Thanos sidecar 复制node3,node4执行1. systemd 文件 复制# vim /etc/systemd/system/thanos-sidecar.service [Unit] Descriptinotallow=thanos-sidecar Documentatinotallow=https://thanos.io/ After=network.target [Service] Type=simple ExecStart=/usr/local/sbin/thanos sidecar \ --tsdb.path=/app/prometheus \ --prometheus.url=http://localhost:9090 \ --http-address=0.0.0.0:10901 \ --grpc-address=0.0.0.0:10902 ExecReload=/bin/kill -HUP TimeoutStopSec=20s Restart=always [Install] WantedBy=multi-user.target1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17. Thanos query

node1执行

systemd文件 复制# vim /etc/systemd/system/thanos-query.service [Unit] Descriptinotallow=thanos-query Documentatinotallow=https://thanos.io/ After=network.target [Service] Type=simple ExecStart=/usr/local/sbin/thanos query \ --http-address=0.0.0.0:10903 \ --grpc-address=0.0.0.0:10904 \ --store=192.168.100.50:10902 \ --store=192.168.100.60:10902 \ --query.timeout=10m \ --query.max-cnotallow=200 \ --query.max-concurrent-select=40 \ --query.replica-label=replica ExecReload=/bin/kill -HUP TimeoutStopSec=20s Restart=always LimitNOFILE=20480000 [Install] WantedBy=multi-user.targe1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.

部署MinIO

复制node1,node2,node3,node4执行1. Minio存储架构单主机,单硬盘模式: Minio只在一台服务器上搭建服务,且数据都存在单块磁盘上,该模式存在单点风险单主机,多硬盘模式:  Minio在一台服务器上搭建服务,但数据分散在多块(大于4块)磁盘上,提供了数据上的安全保障 (类似于容器模式)多主机,多硬盘模式(分布式): 该模式是Minio服务最常用的架构,通过共享一个access_key和secret_key,在多台(2-32)服务器上搭建服务,且数据分散在多块(大于4块,无上限)磁盘上,提供了较为强大的数据冗余机制准备工作

这里我们采用分布式存储,在四台服务器上进行安装.

注意: data目录不可以和root目录在同一磁盘,需要另外添加磁盘。错误信息 :ispart of root disk, will not be used (*errors.errorString)

复制wget http://dl.minio.org.cn/server/minio/release/linux-amd64/minio mv minio /usr/local/sbin chmod +x /usr/local/sbin/minio mkdir -p /app/minio/data mkdir /etc/minio mkdir /app/minio/run1.2.3.4.5.6. MinIO配置文件用户信息 vim /etc/minio/minio.pw 复制MINIO_ROOT_USER=root MINIO_ROOT_PASSWORD=Password1.2.

这里指定了4台minio的地址,通过统一的minio.pw和启动文件,可以让4台minio做到数据互通。minio会依次启动,顺序为参数的先后顺序

systemd文件vim /etc/systemd/system/minio.service 复制[Unit] Descriptinotallow=Minio service Documentatinotallow=https://docs.minio.io/ [Service] WorkingDirectory=/app/minio/run/ Envirnotallow=/etc/minio/minio.pw ExecStart=/usr/local/sbin/minio server \ --config-dir /etc/minio \ --address :9000 \ --console-address :9001 \ http://192.168.100.30:9000/app/minio/data \ http://192.168.100.40:9000/app/minio/data \ http://192.168.100.50:9000/app/minio/data \ http://192.168.100.60:9000/app/minio/data Restart=on-failure RestartSec=5 LimitNOFILE=20480000 [Install] WantedBy=multi-user.target1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21. 负载均衡

在node1 配置nginx vim /etc/nginx/conf.d/minio.conf

复制server { listen 9900; server_name 192.168.100.30; location / { proxy_pass http://minio; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } upstream minio { server 192.168.100.30:9000; server 192.168.100.40:9000; server 192.168.100.50:9000; server 192.168.100.60:9000; }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.

Thanos Store

复制node1执行 mkdir -p /app/thanos/store1.2. systemd文件vim /etc/systemd/system/thanos-store.service 复制[Unit] Descriptinotallow=thanos-store Documentatinotallow=https://thanos.io/ After=network.target [Service] Type=simple ExecStart=/usr/local/sbin/thanos store \ --data-dir=/app/thanos/store \ --objstore.config-file=/etc/thanos/thanos-minio.yml \ --http-address=0.0.0.0:10905 \ --grpc-address=0.0.0.0:10906 \ --chunk-pool-size=8GB \ --max-time=30d ExecReload=/bin/kill -HUP TimeoutStopSec=20s Restart=always LimitNOFILE=20480000 [Install] WantedBy=multi-user.targe1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19. 配置对象存储配置文件vim /etc/thanos/thanos-minio.yml 复制type: S3 config: bucket: "thanos" endpoint: "192.168.100.30:9000" access_key: "root" secret_key: "Password" insecure: true signature_version2: false http_config: idle_conn_timeout: 5m response_header_timeout: 10m insecure_skip_verify: true systemctl start thanos-store1.2.3.4.5.6.7.8.9.10.11.12.13.

在 node3, node4 ,sidecar的system文件添加

复制--objstore.config-file=/etc/thanos/thanos-minio.yml \ # cat /etc/systemd/system/thanos-sidecar.service [Unit] Descriptinotallow=thanos-sidecar Documentatinotallow=https://thanos.io/ After=network.target [Service] Type=simple ExecStart=/usr/local/sbin/thanos sidecar \ --tsdb.path=/app/prometheus \ --prometheus.url=http://localhost:9090 \ --objstore.config-file=/etc/thanos/thanos-minio.yml \ --http-address=0.0.0.0:10901 \ --grpc-address=0.0.0.0:10902 ExecReload=/bin/kill -HUP TimeoutStopSec=20s Restart=always [Install] WantedBy=multi-user.target1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.

在 node1  query 的system文件添加store的grpc地址

复制--store=192.168.100.30:10906 \ [root@node1 ~]# cat /etc/systemd/system/thanos-query.service [Unit] Descriptinotallow=thanos-query Documentatinotallow=https://thanos.io/ After=network.target [Service] Type=simple ExecStart=/usr/local/sbin/thanos query \ --http-address=0.0.0.0:10903 \ --grpc-address=0.0.0.0:10904 \ --query.timeout=10m \ --query.max-cnotallow=200 \ --query.max-concurrent-select=40 \ --store=192.168.100.30:10906 \ --query.replica-label=replica ExecReload=/bin/kill -HUP TimeoutStopSec=20s Restart=always LimitNOFILE=20480000 [Install] WantedBy=multi-user.targe1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.

为了展示对象存储的效果,我们把node3和node4,  sidecar的地址删除,只查询store的地址,这样我们就可以在grafana看到下图, 可以看到提供的信息并不是实时的,而是store写入对象存储的数据, 这只是为了测试store的可用性,实际环境中,数据的写入默认为2h一次,不符合监控实时性的要求.

Thanos compact

node1执行:

compact的作用是定期把历史数据存入对象存储,其实他就像是一个cronjob, 如果发现满足了条件,就会对对象存储中的数据进行整理

初始化 复制mkdir /app/thanos/compact1. systemd文件vim /etc/systemd/system/thanos-compact.service 复制[Unit] Descriptinotallow=Thanos-compact Documentatinotallow=https://thanos.io/ After=network.target [Service] Type=simple ExecStart=/usr/local/sbin/thanos compact \ --data-dir=/app/thanos/compact \ --objstore.config-file=/etc/thanos/thanos-minio.yml \ --http-address=0.0.0.0:10923 \ --wait-interval=5m \ --block-sync-cnotallow=30 \ --compact.cnotallow=6 ExecReload=/bin/kill -HUP TimeoutStopSec=20s Restart=on-failure [Install] WantedBy=multi-user.target1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.

Prometheus自动注册

部署Consul node2

    创建配置文件 vim /etc/consul/server.json

复制{ "data_dir": "/app/consul/data", "log_file": "/app/consul/log/consul.log", "log_level": "INFO", "log_rotate_duration": "24h", "node_name": "node2", "server": true, "bootstrap_expect": 1, "client_addr": "0.0.0.0", "advertise_addr": "192.168.100.40", "acl": { "enabled": true, "default_policy": "deny", "down_policy": "extend-cache", "enable_token_persistence": true, "tokens":{ "master": "727d2766-ac98-26c5-0f30-47b4f6a5632d" } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.

创建守护进程 vim /etc/systemd/system/consul-server.service

复制[Unit] Descriptinotallow=Consul service Documentatinotallow=https://www.consul.io/docs/ [Service] ExecStart=/usr/local/bin/consul agent -ui -config-dir /etc/consul KillSignal=SIGINT Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target1.2.3.4.5.6.7.8.9.10.11.12.13.14. 启动consul并测试 复制systemctl start consul-server1.

浏览器访问 8500端口,提示需要登录

使用 consul acl bootstrap 记录SecretID 作为token

复制[root@node2 ~]# consul acl bootstrap AccessorID: 6036d229-b123-5a0f-ef9f-df2b7efcd410 SecretID: 727d2766-ac98-26c5-0f30-47b4f6a5632d Description: Bootstrap Token (Global Management) Local: false Create Time: 2022-09-19 05:21:26.374769398 +0800 CST Policies: 00000000-0000-0000-0000-000000000001 - global-management1.2.3.4.5.6.7.8.

把token添加到配置文件

vim /etc/consul/server.json

复制{ "data_dir": "/app/consul/data", "log_file": "/app/consul/log/consul.log", "log_level": "INFO", "log_rotate_duration": "24h", "node_name": "node2", "server": true, "bootstrap_expect": 1, "client_addr": "0.0.0.0", "advertise_addr": "192.168.100.40", "acl": { "enabled": true, "default_policy": "deny", "down_policy": "extend-cache", "enable_token_persistence": true, "tokens":{ "master": "727d2766-ac98-26c5-0f30-47b4f6a5632d" } } }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20. 重启consul

github 地址: https://github.com/starsliao/ConsulManager

准备工作

添加镜像仓库

yum-``config``-manager--add-repo**https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

使用docker-compose来部署ConsulManager

下载:wget https://starsl.cn/static/img/docker-compose.yml(仓库根目录下docker-compose.yml)编辑:docker-compose.yml,修改3个环境变量:

consul_token:consul的登录token(上文获取的,SecretID)

consul_url:consul的URL(http开头,/v1要保留)

admin_passwd:登录ConsulManager Web的admin密码

启动:docker-compose pull && docker-compose up -d

访问:http://{IP}:1026,使用配置的ConsulManager admin密码登录

添加主机监控

安装完成后,在平台新增监控主机的信息

图片

添加完成后,查看consul;

图片

配置prometheus读取consul信息;

图片

将之前配置好的内容删除,添加生成的配置信息;

图片

查看query, grafana,显示注册完成;

图片

图片

在本文中,我们详细探讨了Thanos监控系统的部署过程,包括系统架构介绍、各个组件的配置和完整的部署案例。Thanos为Prometheus提供了强大的监控解决方案,具备全局查询、高可用性、动态扩展和长期存储等特性。借助Thanos,我们能够高效管理大规模监控数据,并通过丰富的组件和集成功能,构建一个强大而可靠的监控生态系统。我们希望本文能为那些寻求提升监控系统性能和扩展性的用户提供有价值的指导。随着技术的不断进步,Thanos将持续发展,我们期待它在未来带来更多创新与可能性。