VictoriaMetrics 监控存储系统部署与运维实战

一、概述

VictoriaMetrics 是一款高性能、开源的时序数据库(TSDB),专为监控和遥测数据设计。它完全兼容 Prometheus 的查询语言(PromQL)和数据格式,但提供了更好的压缩率、查询性能和可扩展性。

1.1 核心优势

特性 VictoriaMetrics Prometheus
压缩率 7:1 以上 3:1 左右
查询性能 快 2-10 倍 基准
存储成本 低 70%+ 基准
水平扩展 支持集群模式 有限支持
长期存储 原生支持 需 Thanos/Cortex

1.2 适用场景

  • 大规模 Prometheus 数据长期存储
  • 多租户监控数据隔离
  • 高基数指标存储
  • 需要复杂查询分析的场景

二、单机版部署

2.1 系统要求

  • CPU: 4 核以上
  • 内存:8GB 以上(推荐 16GB+)
  • 磁盘:SSD 优先,容量根据保留周期计算
  • 操作系统:Linux(推荐 Ubuntu 22.04+ / CentOS 8+)

2.2 安装部署

方式一:二进制安装

1
2
3
4
5
6
7
8
9
# 下载最新版本
wget https://github.com/VictoriaMetrics/VictoriaMetrics/releases/download/v1.100.0/victoria-metrics-linux-amd64-v1.100.0.tar.gz

# 解压
tar xzf victoria-metrics-linux-amd64-v1.100.0.tar.gz
cd victoria-metrics-linux-amd64-v1.100.0

# 移动到系统目录
sudo mv victoria-metrics /usr/local/bin/

方式二:Docker 部署

1
2
3
4
5
docker run -d \
--name victoriametrics \
-p 8428:8428 \
-v /data/victoriametrics:/victoria-metrics-data \
victoriametrics/victoria-metrics:v1.100.0

2.3 配置 systemd 服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# /etc/systemd/system/victoriametrics.service
[Unit]
Description=VictoriaMetrics Server
After=network.target

[Service]
Type=simple
User=victoriametrics
Group=victoriametrics
ExecStart=/usr/local/bin/victoria-metrics \
-storageDataPath=/data/victoriametrics \
-retentionPeriod=12 \
-httpListenAddr=:8428 \
-loggerLevel=INFO
Restart=on-failure
RestartSec=10
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
# 创建用户和数据目录
sudo useradd -r -s /bin/false victoriametrics
sudo mkdir -p /data/victoriametrics
sudo chown -R victoriametrics:victoriametrics /data/victoriametrics

# 启动服务
sudo systemctl daemon-reload
sudo systemctl enable victoriametrics
sudo systemctl start victoriametrics

2.4 核心参数说明

参数 说明 推荐值
-storageDataPath 数据存储路径 /data/victoriametrics
-retentionPeriod 数据保留时间(月) 12-24
-httpListenAddr HTTP 监听地址 :8428
-memory.allowedPercent 内存使用百分比 60
-maxConcurrentInserts 最大并发插入数 4-8
-dedup.minScrapeInterval 去重间隔 1ms

三、集群模式部署

3.1 架构组件

VictoriaMetrics 集群包含以下组件:

  • vmselect:查询前端,负责接收 PromQL 查询
  • vminsert:写入前端,负责接收远程写入数据
  • vmstorage:存储节点,负责实际数据存储
  • vmagent:可选,用于数据收集和路由
1
2
3
4
5
6
7
8
9
10
                    ┌─────────────┐
│ vmselect │ :8481
│ (查询) │
└──────┬──────┘

┌──────────┐ ┌─────────┴─────────┐ ┌──────────┐
│ vminsert │────▶│ vmstorage 集群 │◀────│ vminsert │
│ (写入) │ │ (数据存储) │ │ (写入) │
│ :8480 │ │ :8482 │ │ :8480 │
└──────────┘ └───────────────────┘ └──────────┘

3.2 部署 vmstorage

1
2
3
4
5
6
7
# vmstorage 启动命令
victoria-metrics-prod \
-storageNode \
-storageDataPath=/data/vmstorage \
-vminsertAddr=:8400 \
-vmselectAddr=:8401 \
-httpListenAddr=:8482

3.3 部署 vminsert

1
2
3
4
5
# vminsert 启动命令(多 vmstorage)
victoria-metrics-prod \
-insertNode \
-storageNode=vmstorage1:8400,vmstorage2:8400,vmstorage3:8400 \
-httpListenAddr=:8480

3.4 部署 vmselect

1
2
3
4
5
# vmselect 启动命令(多 vmstorage)
victoria-metrics-prod \
-selectNode \
-storageNode=vmstorage1:8401,vmstorage2:8401,vmstorage3:8401 \
-httpListenAddr=:8481

3.5 Kubernetes 部署示例

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
# victoria-metrics-cluster.yaml
apiVersion: operator.victoriametrics.com/v1beta1
kind: VMCluster
metadata:
name: prod-cluster
namespace: monitoring
spec:
retentionPeriod: "12"
replicationFactor: 2
vminsert:
replicaCount: 2
resources:
requests:
cpu: "1"
memory: "2Gi"
vmselect:
replicaCount: 2
resources:
requests:
cpu: "1"
memory: "2Gi"
vmstorage:
replicaCount: 3
storage:
volumeClaimTemplate:
spec:
storageClassName: ssd
accessModes: [ReadWriteOnce]
resources:
requests:
storage: 500Gi

四、与 Prometheus 集成

4.1 配置远程写入

在 Prometheus 配置中添加:

1
2
3
4
5
6
7
8
9
# prometheus.yml
remote_write:
- url: http://victoriametrics:8428/api/v1/write
queue_config:
capacity: 10000
max_shards: 50
max_samples_per_send: 5000
metadata_config:
send: true

4.2 Grafana 数据源配置

  1. 添加 Prometheus 类型数据源
  2. URL 设置为:http://victoriametrics:8428
  3. 启用 Prometheus type 查询

4.3 vmagent 数据收集

1
2
3
4
5
# vmagent 启动配置
vmagent \
-remoteWrite.url=http://victoriametrics:8428/api/v1/write \
-promscrape.config=/etc/vmagent/scrape.yml \
-memory.allowedPercent=50

五、运维管理

5.1 健康检查

1
2
3
4
5
6
7
8
# 检查服务状态
curl http://localhost:8428/health

# 检查集群状态
curl http://vmselect:8481/health

# 查看统计信息
curl http://localhost:8428/api/v1/status

5.2 性能监控指标

关键监控指标:

1
2
3
4
5
6
7
8
9
10
11
# 插入速率
sum(rate(vm_rows_inserted_total[5m]))

# 查询延迟
histogram_quantile(0.95, rate(vmselect_request_duration_seconds_bucket[5m]))

# 存储使用率
vmstorage_disk_space_used_bytes / vmstorage_disk_space_total_bytes

# 内存使用
vm_concurrent_select_current

5.3 数据备份

1
2
3
4
5
6
7
8
9
10
# 创建快照
curl -XPOST http://localhost:8428/api/v1/snapshot/create

# 备份快照
rsync -av /data/victoriametrics/snapshots/ backup-server:/backups/vm/

# 恢复数据
# 1. 停止 VictoriaMetrics
# 2. 恢复快照目录
# 3. 启动服务

5.4 数据迁移

使用 vmbackup/vmrestore 工具:

1
2
3
4
5
6
7
8
9
10
# 备份到 S3
vmbackup \
-storageDataPath=/data/victoriametrics \
-dst=s3://bucket/backups/vm \
-configFile=/etc/vmbackup/config.yml

# 从 S3 恢复
vmrestore \
-storageDataPath=/data/victoriametrics \
-src=s3://bucket/backups/vm

六、性能优化

6.1 写入优化

1
2
3
4
5
6
# 批量写入参数
-maxConcurrentInserts=8
-insertQueueSize=1000000

# 压缩优化
-dedup.minScrapeInterval=1ms

6.2 查询优化

1
2
3
4
5
6
# 缓存配置
-search.cacheTimestampOffset=5m
-search.maxQueryDuration=30s

# 并发控制
-search.maxConcurrentRequests=16

6.3 存储优化

1
2
3
4
5
# 压缩级别
-storageConfig.maxHourlySeries=100000

# 分区策略
-storageConfig.maxDailySeries=1000000

6.4 内存管理

1
2
3
4
5
6
# 内存限制
-memory.allowedPercent=60
-memory.allowedBytes=8GB

# 缓存大小
-search.cacheSize=2GB

七、故障排查

7.1 常见问题

问题 1:写入延迟高

1
2
3
4
5
6
7
# 检查插入队列
curl http://localhost:8428/api/v1/status | jq '.insertQueueSize'

# 解决方案
# 1. 增加 -maxConcurrentInserts
# 2. 检查磁盘 IO 性能
# 3. 减少指标基数

问题 2:查询超时

1
2
3
4
5
6
7
# 检查并发查询数
curl http://localhost:8428/api/v1/status | jq '.concurrentSelects'

# 解决方案
# 1. 增加 -search.maxConcurrentRequests
# 2. 优化 PromQL 查询
# 3. 增加 vmselect 副本

问题 3:磁盘空间不足

1
2
3
4
5
6
7
# 查看数据分布
curl http://localhost:8428/api/v1/status/tsdb

# 解决方案
# 1. 缩短 retentionPeriod
# 2. 清理高基数指标
# 3. 扩容 vmstorage 节点

7.2 日志分析

1
2
3
4
5
6
7
# 查看错误日志
journalctl -u victoriametrics -f --grep="ERROR"

# 常见错误
# "cannot open database" - 数据目录权限问题
# "out of memory" - 内存不足
# "too many open files" - 文件描述符不足

7.3 性能诊断

1
2
3
4
5
6
7
8
# 查看慢查询
curl http://localhost:8428/api/v1/status/top_queries

# 查看热点指标
curl http://localhost:8428/api/v1/status/hot_series

# 分析存储分布
curl http://localhost:8428/api/v1/status/storage

八、最佳实践

8.1 容量规划

指标数量 采样间隔 保留时间 推荐配置
<100K 15s 3 个月 单机 8C16G
100K-1M 15s 6 个月 集群 3 节点
>1M 15s 12 个月 集群 5+ 节点

8.2 高可用配置

  • vmstorage 至少 3 节点,replicationFactor=2
  • vminsert/vmselect 至少 2 副本
  • 使用负载均衡分发查询
  • 跨可用区部署

8.3 安全配置

1
2
3
4
5
6
7
8
9
10
11
# 启用认证
-httpAuth.username=admin
-httpAuth.password=secure_password

# 启用 TLS
-httpsCertFile=/etc/ssl/vm.crt
-httpsKeyFile=/etc/ssl/vm.key

# 网络隔离
-允许 Prometheus/Grafana 访问
-禁止公网直接访问

8.4 指标治理

  • 定期清理无用指标
  • 限制 label 基数
  • 使用 relabeling 过滤噪声数据
  • 设置合理的 scrape 间隔

九、总结

VictoriaMetrics 作为 Prometheus 生态的高性能替代方案,在大规模监控场景下表现出色。通过合理的架构设计和运维管理,可以构建稳定、高效、可扩展的监控存储系统。

关键要点:

  1. 单机版适合中小规模,集群版适合大规模部署
  2. 与 Prometheus 完全兼容,迁移成本低
  3. 重点关注存储容量规划和性能调优
  4. 定期备份和监控是运维的关键

参考文档: