告别 Loki + Promtail:用 Vector + VictoriaLogs 构建更轻量的日志系统

如果你已经在用 Prometheus + Grafana 做监控,日志方案大概率是 Promtail + Loki。但现在有个绕不开的问题:Grafana 官方已经将 Promtail 标记为废弃(deprecated),推荐迁移到 Grafana Alloy。也就是说 Promtail 不会再有新功能,未来安全补丁也不确定能维护多久。

既然 Agent 端要动,不如顺便重新审视整个日志栈。Loki 本身也有不少痛点:内存开销大(单体模式 1~2GB 起步)、大数据量下查询慢、微服务模式部署复杂且依赖对象存储。

Vector + VictoriaLogs 是一个更轻量的替代方案:

  • Vector 替代 Promtail,资源占用低且 pipeline 能力远超 Promtail
  • VictoriaLogs 替代 Loki,内存占用仅为 Loki 的 1/5 ~ 1/10,单二进制部署,不依赖对象存储
  • Grafana 原生支持 VictoriaLogs 数据源,现有仪表盘体系无需推翻
  • 写入性能更高,支持大规模日志摄入

本文记录从 Promtail + Loki 迁移到 Vector + VictoriaLogs 的完整过程。


一、部署 VictoriaLogs

VictoriaLogs 是由 VictoriaMetrics 团队开发的高性能日志系统,兼容 LogQL,资源占用极低。

1️⃣ 创建 docker-compose.yml

services:
victorialogs:
  image: victoriametrics/victoria-logs:v1.46.0
  container_name: victorialogs
  restart: unless-stopped

  ports:
    - "9428:9428"

  volumes:
    - ./data:/victoria-logs-data

  command:
    - -storageDataPath=/victoria-logs-data
    - -retentionPeriod=30d
    - -memory.allowedPercent=60
    - -search.maxQueryDuration=30s

  ulimits:
    nofile:
      soft: 65535
      hard: 65535

  deploy:
    resources:
      limits:
        memory: 4g

2️⃣ 启动服务

docker compose up -d

3️⃣ 验证服务

访问:

http://服务器IP:9428

看到 Web UI 即部署成功。


二、安装 Vector(日志采集 Agent)

Vector 是由 Datadog 开源的高性能日志采集器。

👉 官网: https://vector.dev


1️⃣ 安装

bash -c "$(curl -L https://setup.vector.dev)"

# 使用yum
yum install vector -y

# 使用apt
apt-get install vector -y

更多方式:https://vector.dev/docs/setup/installation/package-managers/


2️⃣ 修改 systemd 运行用户

默认 vector 不是 root,很多日志会无权限读取。

编辑:

vim /usr/lib/systemd/system/vector.service

修改:

User=root
Group=root

然后执行:

systemctl daemon-reload

3️⃣ 配置 vector.yaml

编辑:

vim /etc/vector/vector.yaml

示例(生产可用版本):

sources:
journald:
  type: journald

nginx:
  type: file
  include:
    - /var/log/nginx/*.log
  read_from: beginning

syslog:
  type: file
  include:
    - /var/log/*.log
docker:
  type: file
  include:
    - /var/lib/docker/containers/*/*.log

transforms:
normalize:
  type: remap
  inputs: ["journald", "docker", "nginx", "syslog"]
  source: |
    .host = get_hostname!()
    .env = "prod"

    # 默认兜底
    ._msg = encode_json(.)

    # journald MESSAGE 字段(大写)
    if exists(.MESSAGE) {
      ._msg = string!(.MESSAGE)
    }

    # journald message 字段(小写)
    if exists(.message) {
      ._msg = string!(.message)
    }

    # file source log 字段
    if exists(.log) {
      ._msg = string!(.log)
    }

    if match(._msg, r'(?i)error') {
      .level = "error"
    }

    # docker json
    if exists(.message) {
      parsed = parse_json(.message) ?? null
      if parsed != null {
        ._msg = string!(parsed.log)
        .stream = parsed.stream
        .container_time = parsed.time
      }
    }

sinks:
victorialogs:
  type: http
  inputs: ["normalize"]
  uri: "http://你的VictoriaLogs服务器IP:9428/insert/jsonline"
  encoding:
    codec: json
  framing:
    method: newline_delimited
  batch:
    max_events: 1000
    timeout_secs: 5

4️⃣ 启动 Vector

systemctl enable vector --now

检查状态:

systemctl status vector

三、VictoriaLogs 查询示例

VictoriaLogs 支持类似 LogQL 的语法。


🔎 查询 ssh 登录失败

SYSLOG_IDENTIFIER:"sshd" AND "Invalid user"

🔎 查询 5 分钟内登录失败次数

SYSLOG_IDENTIFIER:"sshd" AND "Invalid user" | stats by (_time:5m) count() as hits

🔎 按 _stream 分组统计

SYSLOG_IDENTIFIER:"vector" | stats by (_stream, _time:5m) count() as hits

🔎 查询 nginx 5xx

service_name:"nginx" AND _msg:~" 5\\d{2} "

四、生产优化建议

✅ 1. retentionPeriod

-retentionPeriod=30d

建议:

  • 日志多:7~15天
  • 中小规模:30天

✅ 2. 限制查询时间

-search.maxQueryDuration=30s

防止大查询拖垮服务器。


✅ 3. Vector 批量优化

batch:
max_events: 1000
timeout_secs: 1
compression: gzip

可以降低网络压力。


五、架构示意


六、资源占用对比

组件内存占用(典型范围)备注
Vector30~100MB取决于 pipeline 复杂度和吞吐量,简单转发可以很低
VictoriaLogs256MB~1GB官方宣称比 Loki/Elasticsearch 低 10x+,实际取决于数据量和查询负载
Loki1~4GB+单体模式 1~2GB 起步,微服务模式更高

👉 对比 Loki,VictoriaLogs 资源节省非常明显。


七、总结

如果你已经有:

  • Prometheus
  • Grafana

那么加入:

  • Vector(轻量采集)
  • VictoriaLogs(高性能存储)

可以构建一个:

  • 🚀 高性能
  • 🪶 低占用
  • 🧱 易扩展
  • 📈 支持 LogQL

的完整日志系统。

给TA打赏
共{{data.count}}人
人已打赏
SRE可观测性监控

基于 Dify + Grafana MCP 的智能巡检与日志分析系统

2026-2-8 20:43:07

SkyWalking云原生

基于Helm部署Skywalking

2025-7-15 6:24:12

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索