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

相关推荐
AI木马人2 天前
8.人工智能实战:大模型服务“看起来正常却突然变慢”?Prometheus + Grafana + GPU 指标构建全链路监控体系
人工智能·grafana·prometheus
萧行之3 天前
Docker部署Loki+Grafana+Vector实现全服务器日志监控(含N8N/SSH/Fail2ban监控)
服务器·docker·grafana
invicinble3 天前
关于搭建运维监控系统(Prometheus+Grafana)
运维·grafana·prometheus
Elastic 中国社区官方博客4 天前
使用 Observability Migration Platform 将 Datadog 和 Grafana 的仪表板与告警迁移到 Kibana
大数据·elasticsearch·搜索引擎·信息可视化·全文检索·grafana·datalog
sitellla4 天前
Grafana Loki 入门:高效日志聚合系统
其他·grafana
日取其半万世不竭4 天前
用 Netdata 实时监控服务器,比 Prometheus + Grafana 轻量得多
linux·服务器·网络·系统架构·负载均衡·zabbix·grafana
梵得儿SHI6 天前
(第二篇)Spring AI 架构设计与优化:可观察性体系,打造全链路可视化的 AI 运维方案
人工智能·微服务·grafana·prometheus·监控·可观察性·spring ai
何中应8 天前
Grafana如何给列表设置别名
运维·grafana·监控
xmlhcxr10 天前
基于 HAProxy+Keepalived 构建高可用 ZrLog 博客系统及监控平台实现(Prometheus + Grafana)
架构·grafana·prometheus
川石课堂软件测试11 天前
软件测试:典型面试题库
数据库·python·功能测试·mysql·单元测试·grafana·prometheus