【问题标题】:How to configure long term retention of logs for EFK stack using S3?如何使用 S3 为 EFK 堆栈配置日志的长期保留?
【发布时间】:2020-10-30 12:17:39
【问题描述】:

为安装了 ElasticSearch、FluentD 和 Kibana 的 Kubernetes 集群配置 S3 中日志长期保留的最佳方法是什么?

【问题讨论】:

    标签: elasticsearch amazon-s3 kubernetes fluent efk


    【解决方案1】:

    如果您还没有安装 efk 堆栈,您可以这样做:

    helm repo add cryptexlabs https://helm.cryptexlabs.com
    helm install my-efk-stack cryptexlabs/efk
    

    或添加到您的 Chart.yaml 依赖项

      - name: efk
        version: 7.8.0
        repository: https://helm.cryptexlabs.com
        condition: efk.enabled
    

    接下来创建一个配置图,其中也包含您的 AWS 机密

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: fluentd-extra-config
    data:
      s3.conf: |-
        <match **>
          @type copy
          copy_mode deep
          <store>
            @type s3
            aws_key_id xxx
            aws_sec_key xxx
            s3_bucket "#{ENV['AWS_S3_BUCKET']}"
            s3_region "#{ENV['AWS_REGION']}"
            path "#{ENV['S3_LOGS_BUCKET_PREFIX']}"
            buffer_path /var/log/fluent/s3
            s3_object_key_format %{path}%{time_slice}/cluster-log-%{index}.%{file_extension}
            time_slice_format %Y%m%d-%H
            time_slice_wait 10m
            flush_interval 60s
            buffer_chunk_limit 256m
          </store>
        </match>
    

    可以选择使用您的 AWS 访问密钥和 ID 创建一个秘密,请参阅下文了解更多信息。不要忘记不透明的秘密必须是 base64 编码的

    apiVersion: v1
    kind: Secret
    metadata:
      name: s3-log-archive-secret
    type: Opaque
    data:
      AWS_ACCESS_KEY_ID: xxx
      AWS_SECRET_ACCESS_KEY: xxx
    

    如果您想知道为什么我没有为 aws 访问密钥和 ID 使用环境变量,那是因为它不起作用:https://github.com/fluent/fluent-plugin-s3/issues/340。如果您使用的是 kube-2-iam 或 kiam,那么这无关紧要。请参阅 fluentd s3 插件的文档以将其配置为承担角色而不是使用凭据。

    这些值将允许您使用配置映射运行 s3 插件。需要注意的一些重要事项:

    • 我使用“软”的 antiAffinity,因为我运行的是单实例金属集群。
    • S3_LOGS_BUCKET_PREFIX 为空,因为我为每个环境使用单独的存储桶,但您可以为环境共享一个存储桶并将前缀设置为环境名称
    • 您需要一个扩展 fluent/fluentd-kubernetes-daemonset:v1-debian-elasticsearch 镜像并安装了 s3 插件的 docker 镜像。
    • 如果您跳过了为访问密钥和 ID 创建密钥的步骤,则可以删除将密钥作为环境变量导入的 envFrom
    efk:
      enabled: true
      elasticsearch:
        antiAffinity: "soft"
      fluentd:
        env:
          - name: FLUENT_ELASTICSEARCH_HOST
            value: "elasticsearch-master"
          - name: FLUENT_ELASTICSEARCH_PORT
            value: "9200"
          - name: AWS_REGION
            value: us-east-1
          - name: AWS_S3_BUCKET
            value: your_buck_name_goes_here
          - name: S3_LOGS_BUCKET_PREFIX
            value: ""
        envFrom:
          - secretRef:
              name: s3-log-archive-secret
        extraVolumeMounts:
          - name: extra-config
            mountPath: /fluentd/etc/conf.d
        extraVolumes:
          - name: extra-config
            configMap:
              name: fluentd-extra-config
              items:
                - key: s3.conf
                  path: s3.conf
        image:
          repository: docker.io/cryptexlabs/fluentd
          tag: k8s-daemonset-elasticsearch-s3
    

    如果你想制作自己的 docker 镜像,你可以这样做:

    FROM fluent/fluentd-kubernetes-daemonset:v1-debian-elasticsearch
    
    RUN fluent-gem install \
     fluent-plugin-s3
    

    接下来您可能想要为 s3 数据设置保留期。您可以在一段时间后将其删除或将其移至 Glacier,具体取决于您的要求。

    最后,由于我们在 S3 中保留了较长时间的日志,因此我们可以安全地为使用 ElasticSearch Curator 发送到 elasticsearch 的数据设置较短的保留期,例如 30 天。

    你可以像这样安装currator:

    helm repo add stable https://kubernetes-charts.storage.googleapis.com
    helm install curator stable/elasticsearch-curator
    

    或添加到您的 Chart.yaml 依赖项:

      - name: elasticsearch-curator
        version: 2.1.5
        repository: https://kubernetes-charts.storage.googleapis.com
    

    values.yaml:

    elasticsearch-curator:
      configMaps:
        action_file_yml: |-
          1: &delete
            action: delete_indices
            description: "Delete selected indices"
            options:
              ignore_empty_list: True
              continue_if_exception: True
              timeout_override: 300
            filters:
            - filtertype: pattern
              kind: prefix
              value: 'logstash-'
            - filtertype: age
              source: name
              direction: older
              timestring: '%Y-%m-%d'
              unit: days
              unit_count: 30
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 2021-04-13
      • 2015-03-24
      • 1970-01-01
      相关资源
      最近更新 更多