← 文章 / 云原生与基础设施
signoz 12天前 · 2026-10-23 15:04:43 · 13 阅读

将 Fluent Bit 日志从虚拟机和 Kubernetes 发送到 SigNoz

本指南介绍如何把 Fluent Bit 采集的日志发送到 SigNoz。Fluent Bit 通过 forward 输出插件把日志发给 OpenTelemetry Collector,Collector 用 fluentforward receiver 接收后导出到 SigNoz。在 Kubernetes 上,Collector 还会把 Fluent Bit 附加的 pod 元数据解析为独立的 k8s.* 属性。

使用自托管 SigNoz?

步骤完全相同。只需把 Collector 指向你的 SigNoz 实例,并去掉 ingestion key 请求头,详见 Cloud → Self-Hosted。

前提条件

  • Fluent Bit。本指南使用 Fluent Bit v5.1.5。
  • 一个 SigNoz 实例(Cloud 或 Self-Hosted 均可)。
  • 部署在 VM 上的 OpenTelemetry Collector,或 Kubernetes 上的 k8s-infra chart。

把 Fluent Bit 日志发送到 SigNoz

Linux VMKubernetes

第 1 步:在 Collector 中添加 fluentforward receiver

在 Collector 配置文件(/etc/otelcol-contrib/config.yaml)的 receivers 部分添加 fluentforward receiver,然后把它加入 logs pipeline:

config.yaml
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318
  # On Collector v0.151.0 and newer, use "fluent_forward" to avoid a deprecation warning.
  fluentforward:
    endpoint: 0.0.0.0:24224
service:
  pipelines:
    logs:
      receivers: [otlp, fluentforward]
      processors: [batch, resourcedetection]
      exporters: [otlphttp]

该 receiver 在 24224 端口监听 Fluent Forward 协议。配置文件中的其他部分保持安装指南中的内容不变。

重启 Collector:

Copy
sudo systemctl restart otelcol-contrib

该路径和命令适用于 DEB 和 RPM 软件包。对于 macOS 和 Windows 用户,请参考安装指南中提供的配置路径和重启步骤。

步骤 2:为 Fluent Bit 添加 forward 输出

在 Fluent Bit 配置(fluent-bit.yaml)中添加一个 forward 输出:

fluent-bit.yaml
pipeline:
  outputs:
    - name: forward
      match: '*'
      host: <collector-host>
      port: 24224
      retain_metadata_in_forward_mode: false

请确认以下数值:

  • <collector-host>:Collector 运行所在的宿主机地址。若 Fluent Bit 运行在同一宿主机上,请使用 127.0.0.1。

将 retain_metadata_in_forward_mode 设置为 false。Fluent Bit v5.0.4 及更高版本默认将其设为 true,而 fluentforward 接收器无法读取该格式。接收器随后会丢弃所有日志,但 Fluent Bit 仍会报告刷新成功。

重启 Fluent Bit 以加载新的输出配置。

Fluent Bit 的 Helm chart 以 DaemonSet 形式运行 Fluent Bit。Fluent Bit 读取每个节点上的容器日志文件,其 kubernetes 过滤器会将 Pod 元数据作为嵌套的 kubernetes 映射添加至每条日志。此步骤将这些日志发送至 k8s-infra OpenTelemetry Collector 代理,该代理会将此映射拆分为 k8s.* 资源属性。

步骤 1:在 k8s-infra 中添加接收器及映射

在你的 k8s-infra 发布版本的 override-values.yaml 文件中添加以下配置:

override-values.yaml
presets:
  logsCollection:
    enabled: false
otelAgent:
  ports:
    fluentforward:
      enabled: true
      containerPort: 24224
      servicePort: 24224
      protocol: TCP
  config:
    receivers:
      fluentforward:
        endpoint: 0.0.0.0:24224
    processors:
      transform/fluentbit_k8s:
        error_mode: ignore
        log_statements:
          - set(log.attributes["k8s.pod.name"], log.attributes["kubernetes"]["pod_name"]) where log.attributes["kubernetes"] != nil
          - set(log.attributes["k8s.namespace.name"], log.attributes["kubernetes"]["namespace_name"]) where log.attributes["kubernetes"] != nil
          - set(log.attributes["k8s.pod.uid"], log.attributes["kubernetes"]["pod_id"]) where log.attributes["kubernetes"] != nil
          - set(log.attributes["k8s.container.name"], log.attributes["kubernetes"]["container_name"]) where log.attributes["kubernetes"] != nil
          - set(log.attributes["k8s.node.name"], log.attributes["kubernetes"]["host"]) where log.attributes["kubernetes"] != nil
          - delete_key(log.attributes, "kubernetes")
      groupbyattrs/fluentbit_k8s:
        keys: [k8s.pod.name, k8s.namespace.name, k8s.pod.uid, k8s.container.name, k8s.node.name]
    service:
      pipelines:
        logs:
          receivers: [otlp, fluentforward]
          processors: [transform/fluentbit_k8s, groupbyattrs/fluentbit_k8s, batch]

这些配置项的作用如下:

  • presets.logsCollection.enabled: false 会阻止 k8s-infra agent 直接读取容器日志文件。由于 Fluent Bit 负责读取日志,如果没有这行配置,每条日志会被接收两次。
  • otelAgent.ports.fluentforward 会在 agent 的 Pod 和 Service 上开放 24224 端口。
  • transform/fluentbit_k8s 处理器会将 kubernetes 映射中的字段复制到 k8s.* 属性中,随后删除该映射。
  • groupbyattrs/fluentbit_k8s 处理器会把这些属性移至资源层。这样 SigNoz 就能将日志正确归类到对应的 Pod、命名空间和容器下。

该 Chart 会在这些处理器之前添加其自身的处理器,因此你只需列出新增的部分。

应用上述配置值:

Copy
helm upgrade  signoz/k8s-infra -n  -f override-values.yaml

验证这些配置值:

  • <release-name>:k8s-infra Release 的名称。
  • <namespace>:k8s-infra Release 所在的命名空间。

步骤 2. 安装带 forward 输出的 Fluent Bit

将以下内容保存为 fluent-bit-values.yaml:

fluent-bit-values.yaml
config:
  inputs: |
    [INPUT]
        Name tail
        Path /var/log/containers/*.log
        Exclude_Path /var/log/containers/*-otel-agent-*_<namespace>_*.log
        multiline.parser docker, cri
        Tag kube.*
        Mem_Buf_Limit 5MB
        Skip_Long_Lines On
 
  outputs: |
    [OUTPUT]
        Name forward
        Match kube.*
        Host <release-name>-k8s-infra-otel-agent.<namespace>.svc.cluster.local
        Port 24224
        Retain_Metadata_In_Forward_Mode false

请核实这些配置值:

  • <release-name>:k8s-infra Release 的名称。如果 Release 名称已包含 k8s-infra,则 Service 名称为 <release-name>-otel-agent。运行 kubectl get svc -n <namespace> 查看名称。
  • <namespace>:k8s-infra Release 所在的命名空间。Exclude_Path 行可阻止 Fluent Bit 读取 Agent Pod 的日志,但同一命名空间内的其他 Pod 仍会发送日志。

该 Chart 保留默认的 kubernetes 过滤器,用于添加 Pod 元数据。将 Retain_Metadata_In_Forward_Mode 设置为 false。Fluent Bit v5.0.4 及更高版本默认将其设为 true,而 fluentforward 接收器会丢弃该格式下的日志。

安装该 Chart:

Copy
helm repo add fluent https://fluent.github.io/helm-charts
helm repo update
helm install fluent-bit fluent/fluent-bit -n logging --create-namespace -f fluent-bit-values.yaml

如果你已经部署了 Fluent Bit Chart,请改为在现有的 values 文件中添加 forward 输出和 Exclude_Path 配置行,然后升级该 Release:

Copy
helm upgrade <fluent-bit-release> fluent/fluent-bit -n <fluent-bit-namespace> -f <your-values-file>

将日志关联到 Trace(可选)

如果日志中包含 `trace_id` 和 `span_id` 字段,Collector 会将其作为普通属性存储。请将它们移至日志的 trace context 中,以便 SigNoz 将每条日志关联到对应的 trace。 在 Collector 配置的 `processors` 部分添加一个 `transform` 处理器: config.yaml
processors:
  transform/trace_context:
    error_mode: ignore
    log_statements:
      - set(log.trace_id.string, log.attributes["trace_id"]) where log.attributes["trace_id"] != nil
      - set(log.span_id.string, log.attributes["span_id"]) where log.attributes["span_id"] != nil
      - delete_key(log.attributes, "trace_id")
      - delete_key(log.attributes, "span_id")
将该处理器添加到 `logs` 管道的开头: config.yaml
service:
  pipelines:
    logs:
      receivers: [otlp, fluentforward]
      processors: [transform/trace_context, batch, resourcedetection]
      exporters: [otlphttp]
在 Kubernetes 上,需要在 `override-values.yaml` 中将相同的处理器添加到 `otelAgent.config.processors` 以及 `logs` 管道中。

绕过 Collector 发送日志(可选)

Fluent Bit 可以使用其 `opentelemetry` 输出,通过 OTLP 将日志发送到 SigNoz Cloud。当你不运行 Collector 时,请使用此路径。上述 Kubernetes 映射依赖于 Collector,因此此路径适用于 VM 或 Docker 主机。此路径将日志发送到 SigNoz Cloud。对于自托管的 SigNoz,请使用 Collector 路径。 fluent-bit.yaml
pipeline:
  inputs:
    - name: tail
      path: <log-file-path>
      processors:
        logs:
          - name: opentelemetry_envelope
          - name: content_modifier
            context: otel_resource_attributes
            action: upsert
            key: service.name
            value: <service-name>
  outputs:
    - name: opentelemetry
      match: '*'
      host: ingest.<region>.signoz.cloud
      port: 443
      tls: on
      tls.verify: on
      logs_uri: /v1/logs
      header: signoz-ingestion-key <your-ingestion-key>

验证这些值:

  • <log-file-path>:Fluent Bit 读取的日志文件,例如 /var/log/app/*.log。
  • <service-name>:SigNoz 中显示的日志服务名称。opentelemetry_envelope 和 content_modifier 处理器会将其设置为 service.name 资源属性。
  • <region>:你的 SigNoz Cloud region。
  • <your-ingestion-key>:你的 SigNoz ingestion key。

验证

  1. 在 SigNoz 中,打开 Logs > Logs Explorer。
  2. 按 fluent.tag EXISTS 过滤。fluentforward 接收器会将每条日志的 Fluent Bit tag 作为 fluent.tag 属性添加进去。如果你发送的日志 w
原始来源: signoz

评论 (0)