构建 Log 集合流水线
你将构建什么
log 集合流水线 using Fluent 位到 collect 容器 logs 从 Kubernetes and 发送它们到 Loki for storage and querying.
学习目标
- 理解如何 log 集合工作 in Kubernetes
- 部署 Fluent 位 as DaemonSet 到 collect logs
- 部署 Loki for log storage
- 查询 logs using Grafana
前置要求
- Kubernetes cluster 使用 Helm
- Grafana already deployed (or 部署它 as 部分的 这个项目)
- 基础理解的 logging 概念
步骤
- 1
添加 Grafana Loki Helm 仓库
添加 Grafana Helm 仓库哪个包含 charts for Loki and Fluent 位.
代码helm repo add grafana https://grafana.github.io/helm-charts helm repo update预期结果
Grafana Helm 仓库是 添加.
- 2
安装 Loki
部署 Loki in monitoring 命名空间 for log storage.
代码helm install loki grafana/loki-stack -n monitoring --set fluent-bit.enabled=true,promtail.enabled=false预期结果
Loki and Fluent 位是 deployed. Fluent 位开始 collecting 容器 logs.
- 3
添加 Loki as Grafana 数据源
In Grafana, go 到配置 > 数据源 > 添加数据源 > Loki. 设置 URL 到 http://loki:3100 and 保存.
预期结果
Loki 是配置 as 数据源 in Grafana.
- 4
查询 logs in Grafana
Go 到 Explore in Grafana, 选择 Loki 数据源, and 运行 log 查询如 {命名空间="default"} 到 see logs 从默认命名空间.
代码{namespace="default"}预期结果
你 see 容器 logs 从默认命名空间 in 真实-时间.
- 5
添加健康检查端点
为应用添加 /health 端点返回 200 OK,供编排系统(Kubernetes、负载均衡器)探测存活。健康检查是生产部署的基础设施。
代码# app.py (Flask example) from flask import Flask, jsonify app = Flask(__name__) @app.route("/health") def health(): return jsonify({"status": "ok"}), 200 @app.route("/ready") def ready(): # Check DB connection etc. return jsonify({"ready": True}), 200预期结果
curl localhost:5000/health 返回 {"status":"ok"} 和 200 状态码。
自己动手试试
你学到了什么
你构建 log 集合流水线那个 gathers 所有容器 logs and 制作它们 searchable in Grafana. Centralized logging 是关键 for 调试 in containerized 环境.
相关资源
相关术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。