loki + alloy/promtail + grafana 日志系统

日志系统

目前企业级日志系统有几种
1、ELK
elasticSearch + logStash + Kibana

2、LAG
Loki + alloy/promtail + grafana

3、大数据
4、云平台

LAG日志系统

Loki介绍

Loki是 Grafana Labs 团队最新的开源项目,是一个水平可扩展,高可用性,多租户的日志聚合系统。它的设计非常经济高效且易于操作,因为它不会为日志内容编制索引,而是为每个日志流编制一组标签。项目受 Prometheus 启发,官方的介绍就是: Like Prometheus, but for logs ,类似于 Prometheus 的日志系统。

与ELK对比,loki是用go语言编写的,而elasticsearch是以Java语言编写,所有loki更为轻量。

  1. Loki快速上手
    Loki 作为日志系统的后起之秀,设计上可以说非常优秀,设计的理念就是为了让日志聚合更简单,它被设计为非常经济高效且易于操作。它不索引日志的内容,而是为每个日志流设置一组标签。它主要由三部分组成。
    与其他日志聚合系统相比, Loki 具有下面的一些特性:
  • 不对日志进行全文索引。通过存储压缩非结构化日志和仅索引元数据, Loki 操作起来会更简单,更省成本。
  • 通过使用与 Prometheus 相同的标签记录流对日志进行索引和分组,这使得日志的扩展和操作效率更高。
  • 特别适合储存 Kubernetes Pod 日志 ; 诸如 Pod 标签之类的元数据会被自动删除和编入索引

优点

  1. Loki 的架构非常简单,使用了和 Prometheus 一样的标签来作为索引,通过这些标签既可以查询日志的内容也可以查询到监控的数据,不但减少了两种查询之间的切换成本,也极大地降低了日志索引的存储。
  2. 与 ELK 相比,消耗的成本更低,具有成本效益。
  3. 在日志的收集以及可视化上可以连用 Grafana,实现在日志上的筛选以及查看上下行的功能。

缺点

  1. 技术比较新颖,相对应的论坛不是非常活跃。
  2. 功能单一,只针对日志的查看,筛选有好的表现,对于数据的处理以及清洗没有 ELK 强大,同时与 ELK 相比,对于后期,ELK 可以连用各种技术进行日志的大数据处理,但是 loki 不行。

LAG日志系统架构

Pasted image 20260310154639

Loki:Grafana Labs 开源的日志聚合系统,借鉴 Prometheus 设计理念,仅索引日志元数据(标签)而非全文,大幅降低存储和计算开销。
Alloy:代理或客户端,Grafana 官方推荐的新一代数据采集器Alloy(替代 Promtail),支持日志、指标、追踪多信号采集,配置灵活且资源占用低。
Grafana:强大的可视化平台,原生支持 Loki 数据源,提供丰富的日志查询、过滤和仪表盘功能。

Pasted image 20260310155458

Pasted image 20260310155203

核心组件

  • flog:用于生成日志行。flog 是一个支持常见日志格式的日志生成器。

  • Grafana Alloy:它从 flog 中刮下日志线,并通过传送门将它们推给 Loki。

  • 网关(nginx)接收请求,并根据请求的 URL 将请求重定向到相应的容器。

  • Loki 读取组件:运行查询前端和查询器。

  • Loki 写入组件:运行 Distributor 和 Ingester。

  • Loki 后端组件:运行 Index Gateway、Compactor、Ruler、Bloom Planner(实验性)、Bloom Builder(实验性)和 Bloom Gateway(实验性)。

  • Minio:Loki 用来存储其索引和数据块。

  • Grafana:用于可视化 Loki 中捕获的日志行。

写流程

  1. distributor 接收带有流和日志行的HTTP POST请求。
  2. distributor 会 hash 计算请求中包含的每个流,决定发给 一致性 hash 环 中的哪个 ingester
  3. distributor 把每个流 发给合适处理它的 ingester 和其副本
  4. ingester 接收带有日志行的流,并为流的数据创建一个块或附加到现有块。每个租户和每个标签集,块都是唯一的
  5. ingester 回复写操作结果
  6. distributor 等待大多数 ingester 确认写入完成。
  7. distributor 在收到至少法定数量的确认写入时响应成功(2xx状态码)。或者在写入操作失败时响应错误(4xx或5xx状态码)。

读流程

  1. 查询前端(query frontend) 接受到 携带 LogQL 的 HTTP GET 请求
  2. 查询前端 将查询拆分为子查询并将它们传递给查询调度程序(query scheduler)。
  3. querier (查询器)从调度程序(scheduler)中提取子查询。
  4. querier 将查询传递给 所有保存数据的 ingester。
  5. ingester 返回与查询匹配的 记忆数据(如果有)。
  6. 如果 ingester 没有返回或返回的数据不足,querier 会延迟从后备存储加载数据并对其运行查询。
  7. querier 遍历所有接收到的数据并进行重复数据删除,将子查询的结果返回到查询前端。
  8. 查询前端 等待 查询的所有子查询完成 并由 querier 返回。
  9. 查询前端将两个结果合并为最终结果并将其返回给客户端。

LAG部署

本文收集器使用promtail,以展示LAG日志系统,因为LAG日志系统需要用到存储,这里使用的是minio做s3存储。 不展示minio的部署

1、部署loki

前期准备
1、k8s集群
2、helm 3
3、kubectl

创建namespace

kubectl create ns logging

添加grafana Helm仓库

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

查看chart
helm search repo loki

考虑到大多数内网环境无法直接使用helm仓库进行部署,这里使用离线下载的方式

在一台能联网且有Helm的机器
helm pull  grafana/loki --version 6.53.0

将所下载的.tgz文件上传到k8s集群中,并进行解压

tar -zxvf loki-6.53.0.tgz

修改 values.yaml

默认loki需要object storage

global:
  imageRegistry: <私网镜像仓库>

loki:
  auth_enabled: false


  storage:
    bucketName:
      chunks: loki-log
      ruler: loki-log
      admin: loki-log
    type: s3
    s3:
      endpoint: <minio:9000>
      secretAccessKey: <minio登录的用户名>
      accessKeyId: <minio登录的密码>
      s3ForcePathStyle: true
      insecure: true

  schemaConfig:
    configs:
      - from: 2026-01-01
        store: tsdb
        object_store: s3
        schema: v13
        index:
          prefix: loki_index_
          period: 24h

helm部署loki

helm install loki ./loki -n logging -f ./loki/values.yaml

成功部署

root@inblk8s-m01:~/monitor/loki# helm install loki ./loki -n logging -f ./loki/values.yaml
NAME: loki
LAST DEPLOYED: Tue Mar 10 07:37:16 2026
NAMESPACE: logging
STATUS: deployed
REVISION: 1
NOTES:
***********************************************************************
 Welcome to Grafana Loki
 Chart version: 6.53.0
 Chart Name: loki
 Loki version: 3.6.5
***********************************************************************

** Please be patient while the chart is being deployed **

Tip:

  Watch the deployment status using the command: kubectl get pods -w --namespace logging

If pods are taking too long to schedule make sure pod affinity can be fulfilled in the current cluster.

***********************************************************************
Installed components:
***********************************************************************
* gateway
* read
* write
* backend


***********************************************************************
Sending logs to Loki
***********************************************************************

Loki has been configured with a gateway (nginx) to support reads and writes from a single component.

You can send logs from inside the cluster using the cluster DNS:

http://loki-gateway.logging.svc.cluster.local/loki/api/v1/push

You can test to send data from outside the cluster by port-forwarding the gateway to your local machine:

  kubectl port-forward --namespace logging svc/loki-gateway 3100:80 &

And then using http://127.0.0.1:3100/loki/api/v1/push URL as shown below:

curl -H "Content-Type: application/json" -XPOST -s "http://127.0.0.1:3100/loki/api/v1/push"
--data-raw "{"streams": [{"stream": {"job": "test"}, "values": [["$(date +%s)000000000", "fizzbuzz"]]}]}"


Then verify that Loki did receive the data using the following command:

curl "http://127.0.0.1:3100/loki/api/v1/query_range" --data-urlencode 'query={job="test"}' | jq .data.result


***********************************************************************
Connecting Grafana to Loki
***********************************************************************

If Grafana operates within the cluster, you'll set up a new Loki datasource by utilizing the following URL:

http://loki-gateway.logging.svc.cluster.local/

部署promtail

以daemonset形式部署到每个主机中

helm install promtail grafana/promtail \
-n logging

成功部署


NAME: promtail
LAST DEPLOYED: Tue Mar 10 08:16:58 2026
NAMESPACE: logging
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
***********************************************************************
 Welcome to Grafana Promtail
 Chart version: 6.17.1
 Promtail version: 3.5.1
***********************************************************************

Verify the application is working by running these commands:
* kubectl --namespace logging port-forward daemonset/promtail 3101
* curl http://127.0.0.1:3101/metrics
posted @ 2026-03-10 16:07  Zncoro  阅读(284)  评论(0)    收藏  举报