【问题标题】:How to setup customized logging with Fluentbit in EKS Fargate?如何在 EKS Fargate 中使用 Fluentbit 设置自定义日志记录?
【发布时间】:2021-09-24 03:52:22
【问题描述】:

我们正在使用 fargate 类型的 EKS pod,我们想设置自定义的 fluent bit logging,它将从不同服务容器中的不同指定路径捕获日志并将它们输出到 cloudwatch。

例如,

服务 1 将日志保存在以下路径:-

/var/log/nginx/*
/usr01/app/product/logs/*

服务 2 将日志保存在以下路径:-

/jboss/product/logs/*
/birt/1.0.0/deploy/birt.war/logs"
/birt/1.0.0/logs/*

等等……

因此,我们想定制fluent bit configmap来捕获不同路径产生的不同服务的日志,并且应该以服务名称为前缀。请帮助我们实现这一目标。

【问题讨论】:

    标签: kubernetes logging amazon-eks aws-fargate fluent-bit


    【解决方案1】:

    如 Fargate 日志记录文档中所述,您无法为 fluent-bit 配置定义输入块。

    https://docs.aws.amazon.com/eks/latest/userguide/fargate-logging.html

    在典型的 Fluent Conf 中,包括的主要部分是服务, 输入、过滤器和输出。然而,Fargate 日志路由器仅 接受:

    过滤器和输出部分并管理服务和输入 部分本身。

    解析器部分。

    如果您提供过滤器、输出和解析器以外的任何其他部分,则 部分被拒绝。

    您可以针对您的用例尝试使用 sidecar 容器方法(稍微贵一点):

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      namespace: example
      labels:
        app: nginx
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.2
            ports:
              - containerPort: 80
            volumeMounts:
            - name: varlog
              mountPath: /var/log/nginx
          - name: fluent-bit
            image: amazon/aws-for-fluent-bit:2.14.0
            env:
              - name: HOST_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: spec.nodeName
              - name: POD_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.name
              - name: POD_NAMESPACE
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.namespace
            resources:
              limits:
                memory: 200Mi
              requests:
                cpu: 200m
                memory: 100Mi
            volumeMounts:
            - name: fluent-bit-config
              mountPath: /fluent-bit/etc/
            - name: varlog
              mountPath: /var/log/nginx
              readOnly: true
          volumes:
            - name: fluent-bit-config
              configMap:
                name: fluent-bit-config
            - name: varlog
              emptyDir: {}
          terminationGracePeriodSeconds: 10
    

    在这种方法中,您使用 fluentbit sidecar 容器并为输入块指定自定义设置。您可以使用这个简单的 fluent-bit configmap 作为示例进行测试:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: fluent-bit-config
      namespace: example
      labels:
        k8s-app: fluent-bit
    data:
      fluent-bit.conf: |
        [SERVICE]
            Flush                     5
            Log_Level                 info
            Daemon                    off
            Parsers_File              parsers.conf
            HTTP_Server               On
            HTTP_Listen               0.0.0.0
            HTTP_Port                 2020
            
        @INCLUDE application-log.conf
      
      application-log.conf: |
        [INPUT]
            Name                tail
            Path                /var/log/nginx/*.log
            
        [OUTPUT]
            Name                stdout
            Match               *
            
      parsers.conf: |
        [PARSER]
            Name                docker
            Format              json
            Time_Key            time
            Time_Format         %Y-%m-%dT%H:%M:%S.%LZ
    

    您可以通过运行上面的示例并向 nginx pod 发送 http 请求来测试这种方法。之后,您应该会在 fluent-bit sidecar 日志中看到类似以下内容:

    AWS for Fluent Bit Container Image Version 2.14.0
    Fluent Bit v1.7.4
    * Copyright (C) 2019-2021 The Fluent Bit Authors
    * Copyright (C) 2015-2018 Treasure Data
    * Fluent Bit is a CNCF sub-project under the umbrella of Fluentd
    * https://fluentbit.io
    
    [2021/09/27 19:06:06] [ info] [engine] started (pid=1)
    [2021/09/27 19:06:06] [ info] [storage] version=1.1.1, initializing...
    [2021/09/27 19:06:06] [ info] [storage] in-memory
    [2021/09/27 19:06:06] [ info] [storage] normal synchronization mode, checksum disabled, max_chunks_up=128
    [2021/09/27 19:06:06] [ info] [http_server] listen iface=0.0.0.0 tcp_port=2020
    [2021/09/27 19:06:06] [ info] [sp] stream processor started
    [2021/09/27 19:06:06] [ info] [input:tail:tail.0] inotify_fs_add(): inode=1592225 watch_fd=1 name=/var/log/nginx/access.log
    [2021/09/27 19:06:06] [ info] [input:tail:tail.0] inotify_fs_add(): inode=1592223 watch_fd=2 name=/var/log/nginx/error.log
    [0] tail.0: [1632769609.617430221, {"log"=>"10.42.0.139 - - [27/Sep/2021:19:06:49 +0000] "GET / HTTP/1.1" 200 612 "-" "Wget" "-""}]
    [0] tail.0: [1632769612.241034110, {"log"=>"10.42.0.139 - - [27/Sep/2021:19:06:52 +0000] "GET / HTTP/1.1" 200 612 "-" "Wget" "-""}]
    

    【讨论】:

    • 嘿 Emidio,是的,您当然是对的,知道我们无法为 Fargate EKS 配置输入路径真的很遗憾。我们还有其他解决方案吗?因为 daemonset 无法在 Fargate EKS 中运行 :(
    • 嗨@NitinGarg:我建议您使用sidecar 容器方法。我借此机会用一个你可以测试的例子来改变我的答案:)
    • 是的。您可以定义以逗号分隔的多个路径或使用多个输入块并应用Tag 来区分每个输入块。您还可以为 Cloudwatch、Datadog 等配置输出。
    • 嘿 Emidio,我遇到了 emptyDir{} varlog mount 权限的问题。我什至现在尝试使用 root 用户为 pod 分配权限,但权限分配如下 drwxrwsrwx 4 root 1337 4096 Sep 30 10:25 logs # Fluent-bit container error read error, check permissions: /logs/access/*.log错误扫描路径:/logs/access/*.log 你在Dockerfile中有专门的安排来应对这个吗?
    • 另外,您介意对其进行测试以将日志发送到 cloudwatch 并进行验证吗?因为虽然我的容器中没有这样的错误,但它没有创建日志组。
    猜你喜欢
    • 2022-11-11
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2021-08-28
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多