系统学习 演练学习路径:35 个 lab 的难度排序、4 周基础计划与复盘方法

从 ELK 迁移到 Loki

适用场景

日志量大、成本敏感、已有 Grafana/Prometheus,且主要按标签+时间检索日志时,从 Elasticsearch/Logstash/Kibana 迁移到 Loki。

前置条件

步骤

  1. 盘点现有查询和看板:
curl -s 'http://es:9200/_cat/indices?v'
# 导出 Kibana saved objects
curl -s 'http://kibana:5601/api/saved_objects/_export' -X POST
  1. 部署 Loki 和采集器:
# Loki 配置要点
auth_enabled: false
server:
  http_listen_port: 3100
storage_config:
  boltdb_shipper:
    active_index_directory: /loki/index
    cache_location: /loki/cache
  filesystem:
    directory: /loki/chunks
  1. 设计标签:
{namespace="prod", app="api", level="error"}

不要使用请求 ID、用户 ID 等高基数字段作为标签。

  1. 改写查询:
{app="api"} |= "error"
{app="api"} | json | status >= 500
sum by (level) (count_over_time({app="api"} | json [5m]))
  1. 并行运行:
  1. 切换并保留回滚:

验证

回滚

常见坑

相关命令

参考链接

读完自测

合上页面,先回答,再展开对照。

  1. 为什么请求 ID、用户 ID 不能作为 Loki 标签?
  2. Loki 的全文检索与 Elasticsearch 有什么差异?
  3. Logstash 中的解析和 enrichment 迁移时应该怎么处理?
答案
  1. 这些字段基数过高,会拖垮 Loki。
  2. Loki 只索引标签,全文检索是扫描式,复杂搜索可能比 ES 慢。
  3. 在采集器或 Loki 查询层重做。
微信小程序码:扫码打开命令速查小程序

小程序版

微信扫码,随时查命令