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: {}

相关推荐
DylanlZhao3 天前
我想看看本地 LLM 给我省了多少钱,于是我监控了它
llm·grafana
天天喝旺仔5 天前
Prometheus + Grafana 监控告警体系搭建实战:从 Exporter 指标采集、PromQL 查询到 Alertmanager 告警落地
容器·kubernetes·grafana·prometheus·时序数据库
qq_4523962312 天前
第十二篇:《数据采集:Grafana Alloy、Fluent Bit、Vector 的选型与配置》
java·贪心算法·grafana
2601_9620973612 天前
可观测性实战:Prometheus+Grafana+OTel构建监控
grafana·prometheus·微服务架构·可观测性·opentelemetry
Joker-Full-stack12 天前
higress Grafana生产事故复盘:Loki 2.9 WAL 竞态 Bug 导致 5 天审计日志消失
bug·grafana·higress
IT界的老黄牛12 天前
Jenkins 监控一条龙:接入、看板 9964、11 条告警规则直接抄走
jenkins·grafana·prometheus·监控·ci-cd·告警规则
heimeiyingwang13 天前
【Prometheus·可视化篇】Grafana 集成:数据源配置与 Dashboard 设计原则
grafana·prometheus
2601_9620973614 天前
GraphQL,Grafana和Dash
python·grafana·数据可视化·graphql·dash
happymade16 天前
7000 台网络设备批量纳管 & 自动拓扑生成实战——MSRM3 完整操作流程与关键要点复盘
运维·服务器·网络·zabbix·grafana·msrm3
AAA@峥16 天前
从零搭建 Prometheus 完整监控告警体系|Linux 部署 + node_exporter+Grafana 可视化
云原生·grafana·prometheus