使用 fluentbit 收集日志到 loki - 索引自定义标签

Collecting logs with fluentbit to loki - Indexing custom labels

我想和你确认以下内容:

我有这种 YAML 清单,它是一个在我的应用程序上发生事件时触发的 pod:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-11-10T12:13:46Z"
  generateName: job-1bb229b1-aa15-4349-b676-ad9cf840b44a-
  labels:
    app: CalculationPod
    calculationId: "1467"
    controller-uid: 75d707d6-aab6-446d-9726-7ffafde29191
    job-name: job-1bb229b1-aa15-4349-b676-ad9cf840b44a
  name: job-1bb229b1-aa15-4349-b676-ad9cf840b44a-h4pj

我正在使用 fluent-bit(来自 Loki 堆栈)在我的 k8s 集群中收集日志。 实际上,我想索引我在 pod 中的 calculationId: "1467" 标签,使其出现在 grafana-Loki 中,例如应用程序:CalculationPod 现在在图片中:

所以这是我在 fluetbit configmap 端的输出 Loki 插件配置:

我正在尝试获取 calculationId 标签的值,并以这种方式将其作为参数传递给 Labels 参数处的标签集:

[Output] 
        Name grafana-loki
        Match *
        Url http://loki:3100/loki/api/v1/push
        TenantID ""
        BatchWait 1
        BatchSize 1048576
        Labels {job="fluent-bit",calculationId="$calculationId"} # SEE HERE
        RemoveKeys kubernetes,stream
        AutoKubernetesLabels false
        LabelMapPath /fluent-bit/etc/labelmap.json
        LineFormat json
        LogLevel warn

但我看不到我的 Loki 有任何变化 UI。 我不确定这是否是添加要编入索引的新标签的正确方法我正在检查此 https://docs.fluentbit.io/manual/pipeline/outputs/loki#labels

任何帮助将不胜感激。

我设法通过将其添加到 kubernetes 标签中来获取 calculationId 标签及其值 JSON 信息被引用并且 kubernetes 过滤器调用。你可以看到更多关于这个 here

所以整个configmap/loki-fluent-bit-loki配置文件是这样的:

apiVersion: v1
data:
  fluent-bit.conf: |-
    [SERVICE]
        HTTP_Server    On
        HTTP_Listen    0.0.0.0
        HTTP_PORT      2020
        Flush          1
        Daemon         Off
        Log_Level      warn
        Parsers_File   parsers.conf
    [INPUT]
        Name           tail
        Tag            kube.*
        Path           /var/log/containers/*.log
        Parser         docker
        DB             /run/fluent-bit/flb_kube.db
        Mem_Buf_Limit  1000MB
    [FILTER]
        Name           kubernetes
        Match          kube.*
        Kube_URL       https://kubernetes.default.svc:443
        Merge_Log On
        K8S-Logging.Exclude Off
        K8S-Logging.Parser Off
    [Output]
        Name grafana-loki
        Match *
        Url http://loki:3100/loki/api/v1/push
        TenantID ""
        BatchWait 1
        BatchSize 1048576
        Labels {job="fluent-bit"}
        RemoveKeys kubernetes,stream
        AutoKubernetesLabels false
        LabelMapPath /fluent-bit/etc/labelmap.json
        LineFormat json
        LogLevel warn
  labelmap.json: |-
    {
      "kubernetes": {
        "container_name": "container",
        "host": "node",
        "labels": {
          "app": "app",
          "release": "release",
          "calculationId": "calculationId" # IT WAS ADDED HERE
        },
       "namespace_name": "namespace",
       "pod_name": "instance"
      },
      "stream": "stream"
    }
  parsers.conf: |-
    [PARSER]
        Name        docker
        Format      json
        Time_Key    time
        Time_Format %Y-%m-%dT%H:%M:%S.%L 
kind: ConfigMap
metadata:
  annotations:
    meta.helm.sh/release-name: loki
    meta.helm.sh/release-namespace: monitoring
  creationTimestamp: "2021-10-26T10:23:32Z"
  labels:
    app: fluent-bit-loki
    app.kubernetes.io/managed-by: Helm
    chart: fluent-bit-2.3.0
    heritage: Helm
    release: loki
  name: loki-fluent-bit-loki
  namespace: monitoring  

然后我得到了 calculationId 标签索引:

我没试过,但根据 loki fluentbit 输出插件的工作方式,LabelKeys 参数还允许我添加自定义标签,如我的 calculationId 并使用它它获取日志流

grafana loki documentation says:

LabelKeys: Comma separated list of keys to use as stream labels. All other keys will be placed into the log line. LabelKeys is deactivated when using LabelMapPath label mapping configuration.

所以根据这个,LabelKeys 应该也可以工作,然后如果我使用它,我就不需要使用 LabelMapPath 参数来引用 json 所需的标签。


重要

我在这里使用的是 loki 堆栈附带的 fluent-bit 收集器代理,这意味着 this installation approach. Is opportune to highlight this is not the official fluent-bit collector, the official one is this. Right now there is a request to deprecate fluent-bit grafana loki 堆栈有利于官方,因此将适时强调这一点, 考虑用官方的