远程存储 #

一、远程存储概述 #

1.1 为什么需要远程存储 #

text
本地存储限制:

┌─────────────────────────────────────────────┐
│ 1. 数据保留时间有限                         │
├─────────────────────────────────────────────┤
│ • 默认15天                                  │
│ • 延长保留时间增加存储成本                  │
│ • 单机存储容量有限                          │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ 2. 无法全局查询                             │
├─────────────────────────────────────────────┤
│ • 每个Prometheus独立                        │
│ • 无法跨实例查询历史数据                    │
│ • 需要联邦或聚合方案                        │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ 3. 高可用挑战                               │
├─────────────────────────────────────────────┤
│ • 单点故障风险                              │
│ • 数据冗余困难                              │
│ • 故障恢复复杂                              │
└─────────────────────────────────────────────┘

1.2 远程存储解决方案 #

text
常用远程存储方案:

┌─────────────────────────────────────────────┐
│ Thanos                                      │
├─────────────────────────────────────────────┤
│ • 支持长期存储                              │
│ • 全局查询视图                              │
│ • 高可用架构                                │
│ • 对象存储后端                              │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Cortex                                      │
├─────────────────────────────────────────────┤
│ • 多租户支持                                │
│ • 长期存储                                  │
│ • 水平扩展                                  │
│ • 兼容PromQL                                │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ VictoriaMetrics                             │
├─────────────────────────────────────────────┤
│ • 高性能                                    │
│ • 低资源占用                                │
│ • 兼容PromQL                                │
│ • 单节点部署简单                            │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Mimir                                       │
├─────────────────────────────────────────────┤
│ • Grafana Labs出品                          │
│ • 多租户支持                                │
│ • 高性能                                    │
│ • 兼容PromQL                                │
└─────────────────────────────────────────────┘

二、远程存储配置 #

2.1 远程写入配置 #

yaml
# prometheus.yml

global:
  external_labels:
    cluster: 'production'
    region: 'cn-east'

remote_write:
  - url: 'http://remote-storage:9090/api/v1/write'
    name: 'remote-storage'
    
    # 远程写入标签
    write_relabel_configs:
      - source_labels: [__name__]
        regex: 'go_.*'
        action: drop
    
    # 队列配置
    queue_config:
      capacity: 10000
      max_shards: 200
      max_samples_per_send: 500
      batch_send_deadline: 5s
      min_shards: 1
      min_backoff: 30ms
      max_backoff: 100ms
    
    # 元数据配置
    metadata_config:
      send: true
      send_interval: 1m
      max_samples_per_send: 500

2.2 远程读取配置 #

yaml
# prometheus.yml

remote_read:
  - url: 'http://remote-storage:9090/api/v1/read'
    name: 'remote-storage'
    
    # 是否读取最近的数据
    read_recent: true
    
    # 是否必须匹配
    required_matchers: []
    
    # 超时配置
    remote_timeout: 1m

2.3 完整配置示例 #

yaml
# prometheus.yml

global:
  scrape_interval: 15s
  evaluation_interval: 15s
  external_labels:
    cluster: 'production'
    region: 'cn-east'

remote_write:
  - url: 'http://thanos-receive:19291/api/v1/receive'
    queue_config:
      max_samples_per_send: 1000
      max_shards: 200

remote_read:
  - url: 'http://thanos-query:19192/api/v1/read'
    read_recent: true

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

三、Thanos #

3.1 Thanos架构 #

text
Thanos组件:

┌─────────────────────────────────────────────┐
│ Thanos Sidecar                              │
├─────────────────────────────────────────────┤
│ • 与Prometheus一起部署                      │
│ • 上传数据到对象存储                        │
│ • 提供查询接口                              │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Thanos Query                                │
├─────────────────────────────────────────────┤
│ • 统一查询入口                              │
│ • 聚合多个StoreAPI                          │
│ • 支持PromQL                                │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Thanos Store                                │
├─────────────────────────────────────────────┤
│ • 查询对象存储数据                          │
│ • 提供StoreAPI                              │
│ • 支持历史数据查询                          │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Thanos Compact                              │
├─────────────────────────────────────────────┤
│ • 压缩对象存储数据                          │
│ • 下采样数据                                │
│ • 清理过期数据                              │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Thanos Receive                              │
├─────────────────────────────────────────────┤
│ • 接收远程写入                              │
│ • 多租户支持                                │
│ • 写入对象存储                              │
└─────────────────────────────────────────────┘

3.2 Thanos部署 #

yaml
# docker-compose.yml

version: '3.8'

services:
  prometheus:
    image: prom/prometheus:v2.48.0
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--storage.tsdb.retention.time=2h'
      - '--web.enable-lifecycle'
      - '--storage.tsdb.max-block-duration=2h'
      - '--storage.tsdb.min-block-duration=2h'
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_data:/prometheus
    ports:
      - "9090:9090"

  thanos-sidecar:
    image: thanosio/thanos:v0.32.0
    command:
      - 'sidecar'
      - '--tsdb.path=/prometheus'
      - '--prometheus.url=http://prometheus:9090'
      - '--objstore.config-file=/etc/thanos/bucket.yml'
    volumes:
      - prometheus_data:/prometheus
      - ./bucket.yml:/etc/thanos/bucket.yml
    depends_on:
      - prometheus

  thanos-query:
    image: thanosio/thanos:v0.32.0
    command:
      - 'query'
      - '--store=thanos-sidecar:10901'
      - '--store=thanos-store:10901'
    ports:
      - "19192:10902"

  thanos-store:
    image: thanosio/thanos:v0.32.0
    command:
      - 'store'
      - '--data-dir=/data'
      - '--objstore.config-file=/etc/thanos/bucket.yml'
    volumes:
      - thanos_store_data:/data
      - ./bucket.yml:/etc/thanos/bucket.yml

  thanos-compact:
    image: thanosio/thanos:v0.32.0
    command:
      - 'compact'
      - '--data-dir=/data'
      - '--objstore.config-file=/etc/thanos/bucket.yml'
      - '--wait'
    volumes:
      - thanos_compact_data:/data
      - ./bucket.yml:/etc/thanos/bucket.yml

volumes:
  prometheus_data:
  thanos_store_data:
  thanos_compact_data:

3.3 对象存储配置 #

yaml
# bucket.yml

type: S3
config:
  bucket: "thanos-data"
  endpoint: "s3.amazonaws.com"
  region: "us-east-1"
  access_key: "xxx"
  secret_key: "xxx"
  insecure: false
  signature_version2: false

四、VictoriaMetrics #

4.1 VictoriaMetrics部署 #

yaml
# docker-compose.yml

version: '3.8'

services:
  victoriametrics:
    image: victoriametrics/victoria-metrics:latest
    command:
      - '--storageDataPath=/storage'
      - '--httpListenAddr=:8428'
      - '--retentionPeriod=12'
    volumes:
      - victoriametrics_data:/storage
    ports:
      - "8428:8428"

  prometheus:
    image: prom/prometheus:v2.48.0
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    depends_on:
      - victoriametrics

volumes:
  victoriametrics_data:

4.2 Prometheus配置 #

yaml
# prometheus.yml

global:
  external_labels:
    cluster: 'production'

remote_write:
  - url: 'http://victoriametrics:8428/api/v1/write'

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

五、总结 #

远程存储方案对比:

方案 优势 适用场景
Thanos 全局视图、高可用 大规模部署
Cortex 多租户、水平扩展 多租户环境
VictoriaMetrics 高性能、简单 中小规模

配置要点:

配置项 说明
remote_write 远程写入配置
remote_read 远程读取配置
queue_config 队列配置

下一步,让我们学习高可用架构!

最后更新:2026-03-27