用 Helm 在 K8s 上部署 Loki 日志聚合:从装到能查

863 字
4 分钟
用 Helm 在 K8s 上部署 Loki 日志聚合:从装到能查

日志平台选型绕不开一个对比:ELK 全量索引,查得快但贵;Loki 只索引标签,查得慢一点但存储成本低得多。

对于”出故障时按 Pod 名捞最近几小时日志”这种场景,Loki 够用,而且部署比 ELK 简单太多。

一、准备 Helm 环境#

Terminal window
helm version
helm repo add grafana https://grafana.github.io/helm-charts
helm repo update

拉 chart 到本地,方便改 values 和留档:

Terminal window
helm pull grafana/loki-stack --untar
cd loki-stack

二、values 里必须改的三项#

chart 默认值能跑,但有三处不改会很难受。

2.1 给 Loki 配持久化#

默认是 emptyDir,Pod 重启日志就没了:

loki:
enabled: true
persistence:
enabled: true
size: 5Gi
resources:
requests:
memory: 512Mi
cpu: 200m
limits:
memory: 1Gi
config:
limits_config:
retention_period: 168h # 保留 7 天
table_manager:
retention_deletes_enabled: true
retention_period: 168h

retention_period 两个地方都要写:limits_config 控制写入侧,table_manager 控制清理侧。只写一个的话,数据永远不会被删,磁盘迟早撑爆。

2.2 Grafana 改成 NodePort#

集群没装 Ingress 的话,ClusterIP 根本访问不到:

grafana:
enabled: true
service:
type: NodePort
port: 80
nodePort: 30300
adminPassword: admin123

2.3 Promtail 打上节点标签#

否则日志里没有节点信息,出问题时不知道是哪台机器的 Pod:

promtail:
enabled: true
config:
snippets:
extraScrapeConfigs: |
- job_name: kubernetes-pods
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_node_name]
target_label: node

三、安装#

Terminal window
kubectl create namespace monitoring
helm install loki ./loki-stack -n monitoring -f values.yaml

等 Pod 起来:

Terminal window
kubectl get pods -n monitoring -w

期望状态:

loki-0 1/1 Running
loki-promtail-xxxxx 1/1 Running # 每个节点一个
loki-grafana-xxxxx 1/1 Running

坑一:loki-0 一直 Pending,多半是 PVC 没绑上。kubectl describe pvc -n monitoring 看事件,如果没有默认 StorageClass,需要先装一个(如 local-path-provisioner)或手动建 PV。

四、访问并验证#

浏览器打开 http://节点IP:30300:

Grafana 登录页
Grafana 登录页

默认账号 admin,密码是 values 里设的那个。

登录后进 Explore,数据源选 Loki,先跑一条最简单的查询验证链路:

{namespace="monitoring"}

能出日志就说明 Promtail → Loki → Grafana 全通了。

再按 Pod 精确定位:

{namespace="default", pod=~"web-.*"} |= "error"

|= 是内容过滤,|~ 是正则匹配。Loki 只对标签建索引,内容过滤是扫描式匹配,所以标签一定要选准,不然查询会很慢。

五、日志查不到时的排查顺序#

这是最常遇到的问题,按链路依次确认:

① Pod 真的在产日志吗
kubectl logs <pod> -n <ns>
② Promtail 有没有采到
kubectl logs -n monitoring <promtail-pod> | grep -i "tail"
③ Loki 有没有收到
kubectl port-forward -n monitoring svc/loki 3100:3100
curl http://localhost:3100/loki/api/v1/labels
④ Grafana 数据源配的对不对
Explore 里点 "Save & test"

四步走完基本能定位。经验上,卡在第二步的最多——Promtail 的 __path__ 没匹配到容器日志路径,或者 RBAC 权限不够列不出 Pod。

六、常用运维命令#

Terminal window
# 看 Loki 存储占用
kubectl exec -n monitoring loki-0 -- du -sh /var/loki
# 调整保留期后重载配置
helm upgrade loki ./loki-stack -n monitoring -f values.yaml
# 只重启 Promtail
kubectl rollout restart daemonset/loki-promtail -n monitoring
# 卸载(PVC 默认保留,要手动删)
helm uninstall loki -n monitoring
kubectl delete pvc -n monitoring -l app=loki

七、这套方案的边界#

Loki 适合”按标签捞日志”,不适合:

  • 全文检索:没有倒排索引,大规模关键词搜索会很慢
  • 复杂聚合分析:需要统计分析的场景还是得上 ES 或 ClickHouse
  • 超大规模:单机 Loki 到 TB 级要考虑微服务模式和对象存储后端

如果只是想”出故障时快速看到某个 Pod 最近干了什么”,Loki + Grafana 的组合性价比很高,一个 Helm 命令就能跑起来。

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

用 Helm 在 K8s 上部署 Loki 日志聚合:从装到能查
https://aiyw.top/posts/helm-loki-k8s/
作者
LiPeng
发布于
2025-10-28
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
LiPeng
运维工程师 · 可观测性 / 系统迁移 / 容器编排
公告
记录可观测体系、系统迁移、容器编排的实战复盘;「学习」里是 K8s 四周课程笔记。
分类
标签
站点统计
文章
31
分类
5
标签
58
总字数
59,760
运行时长
0 天
最后活动
0 天前
文章目录