VictoriaMetrics(简称 VM)是兼容 Prometheus 生态的时序数据库,资源占用低于 Prometheus,支持单机版与集群版。单机版可直接替换 Prometheus 存储;集群版通过 vmstorage / vminsert / vmselect 拆分读写与存储,水平扩展能力更强。
1. VictoriaMetrics 单节点
单机版(victoria-metrics)集采集写入、存储、查询于一体,适合中小规模监控、作为 Prometheus remote_write 远端存储。
1.1 配置环境变量
1
2
3
4
5
6
7
| export CONTAINER_CLI=nerdctl
export VM_TAG=v1.147.0
export VM_IMAGE=victoriametrics/victoria-metrics:$VM_TAG
export VM_INSTANCE_NAME=victoriametrics
export VM_DATA=/data/ops/victoriametrics/$VM_INSTANCE_NAME
export VM_HTTP_PORT=8428
export VM_RETENTION=15d
|
1.2 启动服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| mkdir -p $VM_DATA/data
$CONTAINER_CLI run -d \
--name $VM_INSTANCE_NAME \
--restart always \
--security-opt apparmor=unconfined \
--security-opt seccomp=unconfined \
--network host \
--ulimit nofile=1048576:1048576 \
--memory-swappiness=0 \
-v $VM_DATA/data:/victoria-metrics-data \
$VM_IMAGE \
-storageDataPath=/victoria-metrics-data \
-retentionPeriod=$VM_RETENTION \
-httpListenAddr=:$VM_HTTP_PORT
|
1.3 验证
1
2
3
4
5
6
7
8
| # 健康检查
curl -s http://127.0.0.1:$VM_HTTP_PORT/health
# 查看自身指标
curl -s http://127.0.0.1:$VM_HTTP_PORT/metrics | head
# PromQL 查询(兼容 Prometheus API)
curl -s 'http://127.0.0.1:8428/api/v1/query?query=up'
|
写入测试(Prometheus remote_write 格式):
1
2
3
| curl -s -X POST http://127.0.0.1:$VM_HTTP_PORT/api/v1/import/prometheus -d '
vm_test_metric{env="dev"} 1
'
|
1.4 打印交付结果
1
2
3
4
5
6
7
8
9
10
| cat <<EOF
VictoriaMetrics (single):
Instance Name: $VM_INSTANCE_NAME
IP: $(hostname -I | awk '{print $1}')
HTTP Port: $VM_HTTP_PORT
Retention: $VM_RETENTION
Write URL: http://$(hostname -I | awk '{print $1}'):$VM_HTTP_PORT/api/v1/write
Query URL: http://$(hostname -I | awk '{print $1}'):$VM_HTTP_PORT/api/v1/query
Metrics URL: http://$(hostname -I | awk '{print $1}'):$VM_HTTP_PORT/metrics
EOF
|
2. VictoriaMetrics 集群
下面以 3 节点 vmstorage + 每台各跑 vminsert / vmselect 为例。这是集群版的最小生产拓扑:vmstorage 存数据,vminsert 负责写入路由,vmselect 负责查询聚合。
| 节点 | IP(示例) | 角色 |
|---|
| node1 | 10.0.0.1 | vmstorage + vminsert + vmselect |
| node2 | 10.0.0.2 | vmstorage + vminsert + vmselect |
| node3 | 10.0.0.3 | vmstorage + vminsert + vmselect |
三台机器上都要能互相访问以下端口:
| 端口 | 组件 | 用途 |
|---|
| 8400 | vmstorage | 接收 vminsert 写入(TCP) |
| 8401 | vmstorage | 接收 vmselect 查询(TCP) |
| 8480 | vminsert | HTTP 写入 / remote_write |
| 8481 | vmselect | HTTP 查询 / Grafana 数据源 |
| 8482 | vmstorage | HTTP 监控 /metrics |
2.1 在每个节点配置环境变量
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
| export CONTAINER_CLI=nerdctl
export VM_TAG=v1.147.0-cluster
export VM_CLUSTER_NAME=vm_cluster_3n
export VM_STORAGE_IMAGE=victoriametrics/vmstorage:$VM_TAG
export VM_INSERT_IMAGE=victoriametrics/vminsert:$VM_TAG
export VM_SELECT_IMAGE=victoriametrics/vmselect:$VM_TAG
export VM_NODE1_IP=10.0.0.1
export VM_NODE2_IP=10.0.0.2
export VM_NODE3_IP=10.0.0.3
export NODE_ID=1 # node2 改为 2,node3 改为 3
export VM_NODE_IP=$VM_NODE1_IP # 本机 IP:node1/2/3 分别填对应变量
export VM_STORAGE_PORT=8400
export VM_SELECT_PORT=8401
export VM_INSERT_HTTP_PORT=8480
export VM_SELECT_HTTP_PORT=8481
export VM_STORAGE_HTTP_PORT=8482
export VM_RETENTION=15d
export VM_REPLICATION_FACTOR=2 # 副本因子,不超过 vmstorage 节点数
export VM_DATA=/data/ops/victoriametrics/$VM_CLUSTER_NAME-node$NODE_ID
export VM_STORAGE_NODES="$VM_NODE1_IP:$VM_STORAGE_PORT,$VM_NODE2_IP:$VM_STORAGE_PORT,$VM_NODE3_IP:$VM_STORAGE_PORT"
export VM_SELECT_NODES="$VM_NODE1_IP:$VM_SELECT_PORT,$VM_NODE2_IP:$VM_SELECT_PORT,$VM_NODE3_IP:$VM_SELECT_PORT"
|
检查端口是否被占用:
1
| lsof -i :$VM_STORAGE_PORT -i :$VM_SELECT_PORT -i :$VM_INSERT_HTTP_PORT -i :$VM_SELECT_HTTP_PORT -i :$VM_STORAGE_HTTP_PORT
|
2.2 启动 vmstorage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| mkdir -p $VM_DATA/vmstorage
$CONTAINER_CLI run -d \
--name vmstorage-$VM_CLUSTER_NAME-$NODE_ID \
--restart always \
--security-opt apparmor=unconfined \
--security-opt seccomp=unconfined \
--network host \
--ulimit nofile=1048576:1048576 \
--memory-swappiness=0 \
-v $VM_DATA/vmstorage:/storage \
$VM_STORAGE_IMAGE \
-storageDataPath=/storage \
-retentionPeriod=$VM_RETENTION \
-vminsertAddr=:$VM_STORAGE_PORT \
-vmselectAddr=:$VM_SELECT_PORT \
-httpListenAddr=:$VM_STORAGE_HTTP_PORT
|
2.3 启动 vminsert
1
2
3
4
5
6
7
8
9
10
11
12
| $CONTAINER_CLI run -d \
--name vminsert-$VM_CLUSTER_NAME-$NODE_ID \
--restart always \
--security-opt apparmor=unconfined \
--security-opt seccomp=unconfined \
--network host \
--ulimit nofile=1048576:1048576 \
--memory-swappiness=0 \
$VM_INSERT_IMAGE \
-storageNode=$VM_STORAGE_NODES \
-replicationFactor=$VM_REPLICATION_FACTOR \
-httpListenAddr=:$VM_INSERT_HTTP_PORT
|
2.4 启动 vmselect
1
2
3
4
5
6
7
8
9
10
11
| $CONTAINER_CLI run -d \
--name vmselect-$VM_CLUSTER_NAME-$NODE_ID \
--restart always \
--security-opt apparmor=unconfined \
--security-opt seccomp=unconfined \
--network host \
--ulimit nofile=1048576:1048576 \
--memory-swappiness=0 \
$VM_SELECT_IMAGE \
-storageNode=$VM_SELECT_NODES \
-httpListenAddr=:$VM_SELECT_HTTP_PORT
|
2.5 验证集群
1
2
3
4
5
6
7
8
9
10
| # vmstorage 指标
curl -s http://$VM_NODE_IP:$VM_STORAGE_HTTP_PORT/metrics | grep vm_rows | head
# 经 vminsert 写入
curl -s -X POST "http://$VM_NODE_IP:$VM_INSERT_HTTP_PORT/insert/0/prometheus/api/v1/import/prometheus" -d '
vm_cluster_test{node="'"$NODE_ID"'"} 1
'
# 经 vmselect 查询
curl -s "http://$VM_NODE_IP:$VM_SELECT_HTTP_PORT/select/0/prometheus/api/v1/query?query=vm_cluster_test"
|
查看各 vmstorage 是否都被 vminsert / vmselect 识别:
1
2
| curl -s http://$VM_NODE_IP:$VM_INSERT_HTTP_PORT/metrics | grep vm_rpc_vmstorage_is_reachable
curl -s http://$VM_NODE_IP:$VM_SELECT_HTTP_PORT/metrics | grep vm_rpc_vmstorage_is_reachable
|
vm_rpc_vmstorage_is_reachable 为 1 表示连通正常。
2.6 打印交付结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| cat <<EOF
VictoriaMetrics Cluster:
Cluster Name: $VM_CLUSTER_NAME
Node ID: $NODE_ID
Node IPs:
node1: $VM_NODE1_IP
node2: $VM_NODE2_IP
node3: $VM_NODE3_IP
Retention: $VM_RETENTION
Replication Factor: $VM_REPLICATION_FACTOR
Write URLs (任一 vminsert):
http://$VM_NODE1_IP:$VM_INSERT_HTTP_PORT/insert/0/prometheus/api/v1/write
http://$VM_NODE2_IP:$VM_INSERT_HTTP_PORT/insert/0/prometheus/api/v1/write
http://$VM_NODE3_IP:$VM_INSERT_HTTP_PORT/insert/0/prometheus/api/v1/write
Query URLs (任一 vmselect):
http://$VM_NODE1_IP:$VM_SELECT_HTTP_PORT/select/0/prometheus/
http://$VM_NODE2_IP:$VM_SELECT_HTTP_PORT/select/0/prometheus/
http://$VM_NODE3_IP:$VM_SELECT_HTTP_PORT/select/0/prometheus/
EOF
|
3. vmagent 采集配置
vmagent 负责抓取 Exporter 指标并 remote_write 到 VM,可替代 Prometheus 采集侧。单机版写 vmsingle;集群版写任意 vminsert。
3.1 配置环境变量
1
2
3
4
5
6
7
8
9
10
11
12
| export CONTAINER_CLI=nerdctl
export VM_TAG=v1.147.0
export VM_AGENT_IMAGE=victoriametrics/vmagent:$VM_TAG
export VM_AGENT_NAME=vmagent
export VM_AGENT_DATA=/data/ops/victoriametrics/$VM_AGENT_NAME
export VM_AGENT_HTTP_PORT=8429
# 单机版
export VM_REMOTE_WRITE_URL=http://127.0.0.1:8428/api/v1/write
# 集群版(改用 vminsert 地址)
export VM_REMOTE_WRITE_URL=http://10.0.0.1:8480/insert/0/prometheus/api/v1/write
|
3.2 生成抓取配置
1
2
3
4
5
6
7
8
9
10
11
12
| mkdir -p $VM_AGENT_DATA
cat > $VM_AGENT_DATA/scrape.yml <<'EOF'
global:
scrape_interval: 15s
scrape_configs:
- job_name: node-exporter
static_configs:
- targets:
- 10.0.0.1:9100
EOF
|
3.3 启动 vmagent
1
2
3
4
5
6
7
8
9
10
11
12
13
| $CONTAINER_CLI run -d \
--name $VM_AGENT_NAME \
--restart always \
--security-opt apparmor=unconfined \
--security-opt seccomp=unconfined \
--network host \
--ulimit nofile=1048576:1048576 \
--memory-swappiness=0 \
-v $VM_AGENT_DATA/scrape.yml:/etc/vmagent/scrape.yml \
$VM_AGENT_IMAGE \
-promscrape.config=/etc/vmagent/scrape.yml \
-remoteWrite.url=$VM_REMOTE_WRITE_URL \
-httpListenAddr=:$VM_AGENT_HTTP_PORT
|
3.4 验证
1
2
| curl -s http://127.0.0.1:$VM_AGENT_HTTP_PORT/metrics | grep vmagent_remotewrite_rows_sent_total
curl -s http://127.0.0.1:$VM_AGENT_HTTP_PORT/targets
|
4. 自监控
VM 各组件均暴露 /metrics,建议由 vmagent 统一抓取并写入集群。
4.1 抓取配置
由 vmagent 统一抓取 VM 组件与 node-exporter,经 remote_write 写入集群。覆盖第 3.2 节配置,三台节点组件均纳入监控:
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
43
44
45
46
47
48
49
50
51
52
53
| export VM_AGENT_DATA=/data/ops/victoriametrics/vmagent
mkdir -p $VM_AGENT_DATA
cat > $VM_AGENT_DATA/scrape.yml <<'EOF'
global:
scrape_interval: 15s
scrape_configs:
- job_name: vmstorage
static_configs:
- targets:
- 10.0.0.1:8482
- 10.0.0.2:8482
- 10.0.0.3:8482
labels:
cluster: vm_cluster_3n
component: vmstorage
- job_name: vminsert
static_configs:
- targets:
- 10.0.0.1:8480
- 10.0.0.2:8480
- 10.0.0.3:8480
labels:
cluster: vm_cluster_3n
component: vminsert
- job_name: vmselect
static_configs:
- targets:
- 10.0.0.1:8481
- 10.0.0.2:8481
- 10.0.0.3:8481
labels:
cluster: vm_cluster_3n
component: vmselect
- job_name: vmagent
static_configs:
- targets:
- 127.0.0.1:8429
labels:
cluster: vm_cluster_3n
component: vmagent
- job_name: node-exporter
static_configs:
- targets:
- 10.0.0.1:9100
- 10.0.0.2:9100
- 10.0.0.3:9100
EOF
|
单机版将 scrape_configs 改为 vmsingle job(127.0.0.1:8428),去掉 vmstorage / vminsert / vmselect。
修改后重启 vmagent:
1
| $CONTAINER_CLI restart $VM_AGENT_NAME
|
4.2 验证抓取
1
2
3
4
5
6
7
8
9
10
| # vmagent 目标状态(State 应为 up)
curl -s http://127.0.0.1:8429/targets | grep -E 'vmstorage|vminsert|vmselect|vmagent'
# 各组件指标抽样
curl -s http://10.0.0.1:8482/metrics | grep -E '^vm_rows|^vm_active_merges' | head
curl -s http://10.0.0.1:8480/metrics | grep vm_rpc_vmstorage_is_reachable
curl -s http://10.0.0.1:8481/metrics | grep vm_cache_requests_total | head
# 经 vmselect 查自监控指标是否入库
curl -s 'http://10.0.0.1:8481/select/0/prometheus/api/v1/query?query=up{component="vmstorage"}'
|
关注指标:
| 指标 | 含义 |
|---|
vm_rows | vmstorage 当前时序行数 |
vm_rpc_vmstorage_is_reachable | vminsert/vmselect 到 vmstorage 是否可达(1=正常) |
vmagent_remotewrite_rows_sent_total | vmagent 成功写入行数 |
vm_http_requests_total | 各组件 HTTP 请求量 |
process_resident_memory_bytes | 进程内存占用 |
4.3 导入 Grafana 面板
数据源使用第 5.1 节配置的 Prometheus 类型数据源(指向 vmselect)。
Grafana 左侧 Dashboards → New → Import,输入面板 ID 导入:
导入步骤:
- 输入 ID(如
11176)→ Load - VictoriaMetrics 数据源选第 5.1 节创建的项(集群填 vmselect URL)
- Import
集群版建议同时导入 11176(组件)和 12683(vmagent);单机版导入 10229。
面板中按 instance 或 component 标签过滤,可分别查看三台节点的 vmstorage 状态。
5. 对接 Grafana / Prometheus
5.1 Grafana 数据源
| 部署模式 | URL |
|---|
| 单机版 | http://<vm-ip>:8428 |
| 集群版 | http://<vmselect-ip>:8481/select/0/prometheus/ |
类型选 Prometheus,Save & Test 通过即可。
5.2 remote_write 写入
单机版:
1
2
| remote_write:
- url: http://10.0.0.1:8428/api/v1/write
|
集群版:
1
2
| remote_write:
- url: http://10.0.0.1:8480/insert/0/prometheus/api/v1/write
|