Thanos 是监控一个基于 Prometheus 实现的监控方案 ,其主要设计目的系统是解决原生 Prometheus 上的痛点,并且做进一步的实战提升 ,主要的部署特性有 :全局查询 ,高可用 ,监控动态拓展,系统长期存储 。实战
下图是部署 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 :主要用于展示对象存储中历史数据的实战存储情况 ,云计算查看每个指标源中数据块的压缩级别,解析度,存储时段和时间长度等信息 。对于发送报警的流程如下所示:
Prometheus 根据自身配置的 alerting 规则定期地对自身采集的指标进行评估,当告警条件满足的情况下发起告警到 Alertmanager 上。rule 根据自身配置的 alerting 规则定期的向 query 发起查询请求获取评估所需的指标,当告警条件满足的情况下发起告警到 Alertmanager 上 。Alertmanager 接收到来自于 Prometheus 和 rule 的告警消息后进行分组合并后发出告警通知 。Thanos 相比起原生的高防服务器 Prometheus 具有以下的一些优势 :
统一查询入口——以 Query 作为统一的查询入口,其自身实现了 Prometheus 的查询接口和StoreAPI,可为其他的 Query 提供查询服务 ,在查询时会从每个 Prometheus 实例的 Sidecar 和 Store Gateway 获取到指标数据。查询去重——每个数据块都会带有特定的集群标签 , Query 在做查询时会去除集群标签 ,将指标名称和标签一致的序列根据时间排序合并。虽然指标数据来自不同的采集源 ,源码下载但是只会响应一份结果而不是多份重复的结果 。高空间利用率——每个 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台虚拟机,配置如下:

--storage.tsdb.min-block-duratinotallow=5m--storage.tsdb.max-block-duratinotallow=5m 默认为2h, 修改为5分钟, sidecar向store写入数据,方便查看效果.
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 只需要两个组件就可以简单形成一个集群,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 querynode1执行
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.这里我们采用分布式存储,在四台服务器上进行安装.
注意: 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.在 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一次,不符合监控实时性的要求.

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.创建配置文件 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. 重启consulgithub 地址: 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将持续发展 ,我们期待它在未来带来更多创新与可能性。