MENU

使用Thanos监控Promethues多地域多集群Kubernetes应用指标

• 2021 年 01 月 14 日 • 监控分析,Prometheus

概述

一个完美弥补Prometheus高可用集群化的组件-Thanos-应用总结

Thanos是Prometheus监控系统的上层组件,也可以称为高可用组件。

Prometheus可做集群监控的高可用方案有很多,但是在尝试过联邦,rewirte,influxdb后,我还是选择了Thanos。

Prometheus本是一个很强的开源监控工具,如果只是监控一些主机指标,或者应用指标,在少节点的情况下,单机的Prometheus是可以完全满足你的全部需求的。随着最近几年云原生,容器化,Devops,微服务的迅速发展。我们不再满足于把监控只是单单放在主机的性能指标上,因为我们服务的增加,集群的增加,都需要我们想办法去改进监控这一大关。可以实现监控的组件也很多,Zabbix,netdata等。选择适合自己的就好。

说一下我选择Thanos最关键的两点。

  • 去重
  • 持久化

文章中只提供最基础,最简单,最通俗易懂但却涵盖了Thanos大部分组件的基本使用的部署方案。
至于多地域,可以参考我之前的部署文档
Prometheus多地域联邦集群部署

架构说明

Prometheus监控多地域
所有场景均采用测试环境容器化部署

节点地域Prom节点数硬性配置备注
192.168.1.101海内EU1单节点replica: 0
192.168.1.102海外US1双副本replica: 0 - replica: 1多副本需配置replica字段
192.168.1.104海内ThanosThanos-query,Thanos-store

创建持久化挂载目录及配置文件

mkdir /data/promedata/{prometheus,thanoscf,prometheus-etc}

解释说明

thanoscf目录存放,thanos后续组件所需要的持久化到对象存储的配置文件

cat /data/promedata/prometheus-etc/prometheus.yml

global:
  scrape_interval: 15s
  evaluation_interval: 15s
  external_labels:
    cluster: eu1
    replica: 0
scrape_configs:
  - job_name: '海内'
    static_configs:
      - targets: ['192.168.1.101:9090']
mkdir /data/promedata/{prometheus,prometheus1,thanoscf,prometheus-etc-0,prometheus-etc-0}

解释说明

为什么需要创建两个持久化目录是因为该主机待会儿会用双副本的Prom监控

cat /data/promedata/prometheus-etc-0/prometheus.yml

global:
  scrape_interval: 15s
  evaluation_interval: 15s
  external_labels:
    cluster: eu1
    replica: 0   #副本0
scrape_configs:
  - job_name: '海外'
    static_configs:
      - targets: ['192.168.1.102:9090','192.168.1.102:9091']

cat /data/promedata/prometheus-etc-1/prometheus.yml

global:
  scrape_interval: 15s
  evaluation_interval: 15s
  external_labels:
    cluster: eu1
    replica: 0   #副本1
scrape_configs:
  - job_name: '海外'
    static_configs:
      - targets: ['192.168.1.102:9090','192.168.1.102:9091']
mkdir /data/promedata/{thanoscf,thanosdata}

解释说明

thanosdata目录存放thanos-store组件所产生的缓存文件

部署Prometheus

docker run -d -p 9090:9090 \
--name prometheus \
-v /etc/hosts:/etc/hosts \
-v /data/promedata/prometheus:/prometheus \
-v /data/promedata/prometheus-etc/:/etc/prometheus/ \
prom/prometheus \
--web.read-timeout=5m \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus \
--web.max-connections=512 \
--storage.tsdb.retention=30d \
--query.timeout=2m \
--web.enable-lifecycle \
--log.level=info \
--storage.tsdb.max-block-duration=2h \
--storage.tsdb.min-block-duration=2h \
--storage.tsdb.wal-compression \
--storage.tsdb.retention.time=2h \
--web.enable-admin-api
docker run -d -p 9090:9090 \
--name prometheus \
-v /etc/hosts:/etc/hosts \
-v /data/promedata/prometheus:/prometheus \
-v /data/promedata/prometheus-etc-1/:/etc/prometheus/ \
prom/prometheus \
--web.read-timeout=5m \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus \
--web.max-connections=512 \
--storage.tsdb.retention=30d \
--query.timeout=2m \
--web.enable-lifecycle \
--log.level=info \
--storage.tsdb.max-block-duration=2h \
--storage.tsdb.min-block-duration=2h \
--storage.tsdb.wal-compression \
--storage.tsdb.retention.time=2h \
--web.enable-admin-api
docker run -d -p 9091:9090 \
--name prometheus1 \
-v /etc/hosts:/etc/hosts \
-v /data/promedata/prometheus1:/prometheus \
-v /data/promedata/prometheus-etc-1/:/etc/prometheus/ \
prom/prometheus \
--web.read-timeout=5m \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus \
--web.max-connections=512 \
--storage.tsdb.retention=30d \
--query.timeout=2m \
--web.enable-lifecycle \
--log.level=info \
--storage.tsdb.max-block-duration=2h \
--storage.tsdb.min-block-duration=2h \
--storage.tsdb.wal-compression \
--storage.tsdb.retention.time=2h \
--web.enable-admin-api


部署Thanos

Thanos部署起来非常容易,只有一个二进制文件,不过这次我们使用容器化部署,只需一条命令即可,它自己集合了全部所需的功能,所以不需要下载其他额外的东西,十分好用

Sidecar

sidecar无疑是边车的含义。这边将会把他跟Prometheus服务形成sidecar,不懂的自行百度。也就是说你启动几个Prometheus就需要启动几个sidecar,他们的关系是互相依存的。如果Prometheus挂掉了,那么跟他依存的sidecar将没有任何意义。

它的主要作用是为Prometheus的存储数据做持久化,他会两个小时去检测一下Prometheus数据目录,将新生成的数据块文件,导入到您所配置的对象存储中。以便后续持久化操作。

在这里我们同样使用Docker去部署

docker run -d -p10901:10901 \
--name thanos-sidecar-0 \
-v /data/promedata/prometheus:/home \
-v /data/promedata/thanoscf/:/var/www/ \
thanosio/thanos:master-2021-01-12-855c5088 sidecar \
--tsdb.path=/home \
--prometheus.url=http://192.168.1.101:9090 \
--objstore.config-file /var/www/thanos-tengxun.yaml \
--shipper.upload-compacted
docker run -d -p10901:10901 \
--name thanos-sidecar-0 \
-v /data/promedata/prometheus:/home \
-v /data/promedata/thanoscf/:/var/www/ \
thanosio/thanos:master-2021-01-12-855c5088 sidecar \
--tsdb.path=/home \
--prometheus.url=http://192.168.1.102:9090 \
--objstore.config-file /var/www/thanos-tengxun.yaml \
--shipper.upload-compacted
docker run -d -p10901:10901 \
--name thanos-sidecar-0 \
-v /data/promedata/prometheus1:/home \
-v /data/promedata/thanoscf/:/var/www/ \
thanosio/thanos:master-2021-01-12-855c5088 sidecar \
--tsdb.path=/home \
--prometheus.url=http://192.168.1.102:9090 \
--objstore.config-file /var/www/thanos-tengxun.yaml \
--shipper.upload-compacted

query

query组件在Thanos中充当一个收集数据,去重数据,WebUI的角色,它可以汇聚sidecar采集到Prometheus个各节点的数据进行去重,展示在WebUI端。

docker run -d -p8090:8090 \
--name thanos-query \
thanosio/thanos:master-2021-01-12-855c5088  query \
--query.replica-label replica \
--http-address=0.0.0.0:8090 \
--store=192.168.1.102:10902 \
--store=192.168.1.102:10901 \
--store=192.168.1.101:10901 \
--store=192.168.1.104:19090 

Store

这是一个持久化组件,用于将已经存储在对象存储的Prometheus块数据进行提取输送展示。

docker run -d -p19090:19090 \
--name thanos-store \
-v  /data/promedata/thanoscf/:/var/www/ \
-v /data/promedata/thanosdata/:/home/ \
96d4125d7f5e store \
--data-dir /home/ \
--objstore.config-file /var/www/thanos-tengxun.yaml \
--http-address 0.0.0.0:19191 \
--grpc-address 0.0.0.0:19090

文档可能多处有遗漏,欢迎你的留言,我会认真修改并且进步。

参考

返回文章列表 文章二维码 打赏
本页链接的二维码
打赏二维码