Kubernetes 上的多租户 Loki

发布: (2025年12月18日 GMT+8 00:44)
4 min read
原文: Dev.to

I’m ready to translate the article, but I’ll need the full text you’d like translated. Could you please paste the content of the Dev.to post here? Once I have the article text, I’ll provide a Simplified Chinese translation while preserving the source link, formatting, markdown, and any code blocks.

什么是 Loki?

Loki 是一个受 Prometheus 启发的横向可扩展、高可用、多租户日志聚合系统。

如果您是第一次使用 Loki,请查看官方文档:
Loki Docs

我们需要

  • 一个 Loki 部署(通过 Helm 的 SimpleScalable 模式)
  • Promtail
  • 一个兼容 S3 的桶(用于对象存储)

Loki 设置(SimpleScalable + S3 存储)

部署依赖对象存储,因此您需要一个兼容 S3 的桶及其凭证来存储日志。

Helm Chart 结构

loki-helm-chart/
├── templates/
│   └── loki-secrets.yaml
├── Chart.yaml
└── values.yaml

loki-secrets.yaml – S3 凭证

apiVersion: v1
kind: Secret
metadata:
  name: loki-s3-credentials
  namespace: monitoring
type: Opaque
stringData:
  AWS_ACCESS_KEY_ID: ""
  AWS_SECRET_ACCESS_KEY: ""

values.yaml – Loki 配置

deploymentMode: SimpleScalable

loki:
  auth_enabled: true

  extraEnvFrom:
    - secretRef:
        name: loki-s3-credentials

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

  storage_config:
    tsdb_shipper:
      index_gateway_client:
        server_address: '{{ include "loki.indexGatewayAddress" . }}'

  storage:
    type: s3
    s3:
      endpoint: rook-ceph-rgw-store.rook-ceph.svc.cluster.local:80
      s3ForcePathStyle: true
      insecure: true

    bucketNames:
      chunks: loki-logs
      ruler: loki-ruler
      admin: loki-admin

  commonConfig:
    replication_factor: 3

  ingester:
    chunk_encoding: snappy

  querier:
    multi_tenant_queries_enabled: true
    max_concurrent: 4

  pattern_ingester:
    enabled: true

  limits_config:
    allow_structured_metadata: true
    volume_enabled: true

重要说明

  • auth_enabled: true 告诉 Loki 需要一个租户标识(通过 X‑Scope‑OrgID)以实现多租户。
  • 保留由 Compactor 执行。如果在此未启用保留,即使设置了 retention_period,日志也可能永久保留。

剩余值(本地状态的 PVC)

Loki 组件仍然需要本地磁盘用于 WAL、缓存和工作状态,因此您会在 read/write/backend pod 上看到 PVC。

注意: ceph-block 是我们的可扩展存储类。

backend:
  replicas: 2
  persistence:
    enabled: true
    size: 2Gi
    storageClass: ceph-block
    accessModes:
      - ReadWriteOnce
  extraEnvFrom:
    - secretRef:
        name: loki-s3-credentials

write:
  replicas: 3
  persistence:
    enabled: true
    size: 2Gi
    storageClass: ceph-block
    accessModes:
      - ReadWriteOnce
  extraEnvFrom:
    - secretRef:
        name: loki-s3-credentials

read:
  replicas: 2
  extraEnvFrom:
    - secretRef:
        name: loki-s3-credentials

Gateway configuration (what it does and does not do)

gateway:
  nginx:
    customHeaders:
      - name: X-Scope-OrgID
        value: $http_x_scope_orgid
  • 不强制隔离;它仅转发调用方提供的租户头部。
  • Grafana 文档建议 X‑Scope‑OrgID经过身份验证的反向代理 设置,以防用户伪造其他租户。

在我的设置中,客户端从不直接访问 Loki。只有在集群内部运行的 Promtail 推送日志,我控制租户映射,因此转发此头部是可接受的。

Promtail 设置

Promtail 在每个节点上运行并将容器日志发送到 Loki。

daemonset:
  enabled: true
deployment:
  enabled: false

config:
  clients:
    - url: http://loki-gateway.monitoring.svc.cluster.local:80/loki/api/v1/push
      timeout: 60s
      batchwait: 1s
      batchsize: 1048576

  serverPort: 3101

正确设置租户

使用 Kubernetes 命名空间作为 namespace 标签:

snippets:
  common:
    - action: replace
      source_labels: [__meta_kubernetes_namespace]
      target_label: namespace

然后,在 pipeline 中从该标签设置租户:

snippets:
  pipelineStages:
    - cri: {}
    - tenant:
        label: namespace

就这样——多租户 Loki 环境已经准备就绪!

在 Grafana 中添加 Loki 作为数据源

当 Loki 启动并运行后,将其添加为 Grafana 的数据源,并开始按租户浏览日志。

Grafana 仪表板

租户过滤截图

设置 X‑Scope‑OrgID – 我将值设为 monitoring

Grafana 仪表板 – monitoring 命名空间

现在我们将 看到来自 monitoring 命名空间的日志:

Grafana 仪表板 – 过滤后的日志

Ben Ye 致以 巨大的 感谢!
GitHub 个人资料

希望对你有帮助。我随时乐于快速交流——通过 LinkedInTwitter 与我联系。

Back to Blog

相关文章

阅读更多 »

仓库利用的权威指南

引言 仓库本质上只是一个 3‑D 盒子。利用率只是衡量你实际使用了该盒子多少的指标。虽然物流 c...