grafana/lock-stack 日志 Pipeline 配置

前言

本文使用的是 grafana/loki-stack chart 抓取的 k8s 日志。其他 chart 配置都差不多。

日志问题

docker 容器运行时 pod 内原始日志

bash 复制代码
[cpu-4] Hello, 第 9788 次报时,时间:2025-08-01T06:35:42+0000
{"HOSTNAME":"cpu-4","level":"info","count":9788,"time":"2025-08-01T06:35:42+0000","msg":"Hello from cpu-4"}
{"HOSTNAME":"cpu-4","log":"{"HOSTNAME":"cpu-4","level":"info","count":9788,"time":"2025-08-01T06:35:42+0000","msg":"Hello from cpu-4"}"}
{"log":"Hi, 第    71461 次报时,Node: cpu-4, The current time is: 2025-08-01 14:47:47 +0800"}

docker 容器运行时被 docker 存放的日志,是一个带 json 格式(带 log stream time 字段)

bash 复制代码
{"log":"[cpu-4] Hello, 第 1061 次报时,时间:2025-08-01T08:14:58+0000\n","stream":"stdout","time":"2025-08-01T08:14:58.490561319Z"}
{"log":"{\"HOSTNAME\":\"cpu-4\",\"level\":\"info\",\"count\":1061,\"time\":\"2025-08-01T08:14:58+0000\",\"msg\":\"Hello from cpu-4\"}\n","stream":"stdout","time":"2025-08-01T08:14:58.490600455Z"}
{"log":"{\"HOSTNAME\":\"cpu-4\",\"log\":\"{\"HOSTNAME\":\"cpu-4\",\"level\":\"info\",\"count\":1061,\"time\":\"2025-08-01T08:14:58+0000\",\"msg\":\"Hello from cpu-4\"}\"}\n","stream":"stdout","time":"2025-08-01T08:14:58.490611277Z"}
{"log":"{\"log\":\"Hi, 第    76992 次报时,Node: cpu-4, The current time is: 2025-08-01 16:20:45 +0800\"}\n","stream":"stdout","time":"2025-08-01T08:20:45.91951057Z"}

如果是 containerd 容器运行时,会在原来的日志上加前缀 2025-08-01T11:20:10.111114661+08:00 stdout F 这里不再展示日志对比。

解决

编辑 values.yaml 文件添加如下配置

yaml 复制代码
promtail:
  config:
    snippets:
      pipelineStages:
      # 参考 https://grafana.com/docs/loki/latest/send-data/promtail/configuration/?utm_source=chatgpt.com#docker
      - docker: {}
      # 参考 https://grafana.com/docs/loki/latest/send-data/promtail/configuration/?utm_source=chatgpt.com#cri
      - cri: {}

配置说明:

docker: {} 相当于如下配置:

yaml 复制代码
- json:
    expressions:
      output: log
      stream: stream
      timestamp: time
- labels:
    stream:
- timestamp:
    source: timestamp
    format: RFC3339Nano
- output:
    source: output

cri: {} 相当于如下配置:

yaml 复制代码
- regex:
    expression: "^(?s)(?P<time>\\S+?) (?P<stream>stdout|stderr) (?P<flags>\\S+?) (?P<content>.*)$"
- labels:
    stream:
- timestamp:
    source: time
    format: RFC3339Nano
- output:
    source: content

具体配置参考官网

docker: {}
cri: {}

相关推荐
程序员老邢18 小时前
【技术底稿 12】内网统一日志系统 Loki + Promtail 全流程部署(对接 Grafana,监控日志一体化)
java·运维·程序人生·grafana·devops
zhanghongbin012 天前
Grafana 可视化:开箱即用的监控面板
grafana
Luke~2 天前
阿里云计算巢已上架!3分钟部署 Loki AI 事故分析引擎,SRE 复盘时间直接砍掉 80%
人工智能·阿里云·云计算·loki·devops·aiops·sre
国产化创客3 天前
ESP32 (ESPectre)+Grafana构建专业级CSI监控面板
物联网·grafana·信息与通信·智能硬件
鬼先生_sir3 天前
Spring Cloud 微服务监控实战:SkyWalking + Prometheus+Grafana 全栈解决方案
运维·spring cloud·grafana·prometheus·skywalking
SkyWalking中文站5 天前
使用 TraceQL 查询 SkyWalking 和 Zipkin 链路追踪数据并在 Grafana 中可视化
运维·grafana·监控
安审若无6 天前
运维监控及可视化工具Prometheus和grafana
运维·grafana·prometheus
却话巴山夜雨时i7 天前
Java面试实录:从Spring Boot到Kafka的技术探讨
spring boot·微服务·kafka·grafana·prometheus·java面试
阿杜杜不是阿木木7 天前
authentik开源身份认证与管理平台-与 Grafana 集成(12)
grafana·authentik
人人常欢笑10 天前
Grafana 表格自定义下载样式。
javascript·react.js·grafana