【问题标题】:How to delete older logs in ELK to give each application a certain disk quota如何在 ELK 中删除旧日志以给每个应用程序一定的磁盘配额
【发布时间】:2015-02-15 18:38:43
【问题描述】:

我正在尝试在以下场景中使用 ELK (Elasticsearch+Logstash+Kibana) 堆栈:

我有大约十个应用程序通过 Logstash 将它们的日志发送到单个 Elasticsearch 集群。

其中一些应用程序自然会生成比其他应用程序更多的日志,有时,其中一个应用程序可能会因为错误而变得“疯狂”,因此生成的日志条目甚至比正常情况还要多。因此,集群中的可用磁盘空间可能会被单个应用程序的日志不公平地“占用”,从而没有足够的空间留给其他应用程序。

我目前正在通过 Elasticsearch Curator 管理可用磁盘空间。它定期运行,就像在 crontab 中一样,并根据磁盘使用配额删除旧索引。当所有索引使用的磁盘空间超过一定限制时,最旧的索引会被一个一个删除,直到它们使用的磁盘空间总和再次在指定的限制范围内。

这种方法的第一个问题是 Elasticsearch Curator 只能删除整个索引。因此,我必须将 Logstash 配置为每小时创建一个不同的索引,并增加它们的粒度;因此,Curator 一次删除较小的日志块。此外,很难决定 Curator 应该多久运行一次。如果应用程序以更高的速率生成日志,那么即使是一小时的索引也可能不够。其次,无法为每个不同的应用程序指定磁盘使用配额。

理想情况下,Elasticsearch 应该能够在索引达到某个磁盘使用限制时自行删除较旧的日志条目。这将消除定义 Curator 运行频率的问题。但是,我在 Elasticsearch 手册中找不到任何类似的功能。

有人会推荐一种不同的方法来解决这些问题吗?

参考资料: http://www.elasticsearch.org https://github.com/elasticsearch/curator

【问题讨论】:

    标签: elasticsearch logstash kibana


    【解决方案1】:

    尝试使用索引生命周期管理,它在 ELK stack 6.6 更新版本中可用。

    请查看此链接:
    https://www.elastic.co/guide/en/elasticsearch/reference/6.6/getting-started-index-lifecycle-management.html

    这将在大小超过 2GB 或 1d 时创建新索引,并删除 1 天前的数据。

    PUT _ilm/policy/stream_policy
    {
      "policy": {
        "phases": {
          "hot": {
            "actions": {
              "rollover": {
                "max_size": "2GB" ,   
                "max_age": "1d"
              }
            }
          },
          "delete": {
            "min_age": "1d",
            "actions": {
              "delete": {} 
            }
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      这是删除旧日志的方法(本例中为 filebeat 日志)

      curl -XDELETE 'localhost:9200/filebeat-2016*?pretty'
      

      【讨论】:

      • 它帮助了我。谢谢@abhishek-goel
      【解决方案3】:

      您可以使用 curator 来完成此任务。它是来自 elastic 的实用程序。

      文档可在link获取

      安装简单:

      pip install elasticsearch-curator
      

      使用很简单。

      -首先,创建配置文件,例如,包含下一个内容:

      ---
      # Remember, leave a key empty if there is no value.  None will be a string,
      # not a Python "NoneType"
      client:
        hosts:
          - 127.0.0.1
        port: 9200
        url_prefix:
        use_ssl: False
        certificate:
        client_cert:
        client_key:
        ssl_no_validate: False
        http_auth:
        timeout: 30
        master_only: False
      
      logging:
        loglevel: INFO
        logfile:
        logformat: default
        blacklist: ['elasticsearch', 'urllib3']
      

      -下一步,创建具有以下内容的动作文件:

      ---
      # Remember, leave a key empty if there is no value.  None will be a string,
      # not a Python "NoneType"
      #
      # Also remember that all examples have 'disable_action' set to True.  If you
      # want to use this action as a template, be sure to set this to False after
      # copying it.
      actions:
        1:
          action: delete_indices
          description: >-
            Delete indices older than 45 days (based on index name), for logstash-
            prefixed indices. Ignore the error if the filter does not result in an
            actionable list of indices (ignore_empty_list) and exit cleanly.
          options:
            ignore_empty_list: True
            disable_action: True
          filters:
          - filtertype: pattern
            kind: prefix
            value: logstash-
          - filtertype: age
            source: name
            direction: older
            timestring: '%Y.%m.%d'
            unit: days
            unit_count: 45
      

      最后,您可以使用 curator

      运行任务
      curator [--config CONFIG.YML] [--dry-run] ACTION_FILE.YML
      

      【讨论】:

        【解决方案4】:

        如果您有很多索引和分片,elasticsearch 将更难以保持它们打开(您会遇到内存错误)。使用 10 个应用程序,您将拥有大量分片。出于这个原因,我更喜欢更少的索引。

        我认为您可能想要一种在给定应用程序获得过多记录时删除“剩余”记录的方法。

        想象一个小脚本可以运行并按类型和日期计算文档数量,然后删除超出限制的文档。

        按类型和日期计算的方法如下:

        curl -XPOST 'localhost:9200/_search?pretty' -d '
        {
          "size": 0,
          "aggs": {
            "by_type": {
              "terms": {
                "field": "_type"
              },
              "aggs": {
                "by_date": {
                  "date_histogram": {
                    "field": "@timestamp",
                    "interval": "day",
                    "order": {
                      "_key": "desc"
                    }
                  }
                }
              }
            }
          }
        }'
        

        结果如下所示:

          "aggregations" : {
            "by_type" : {
              "buckets" : [ {
                "key" : "type1",
                "doc_count" : 900000,
                "by_date" : {
                  "buckets" : [ {
                    "key_as_string" : "2015-02-13T00:00:00.000Z",
                    "key" : 1423785600000,
                    "doc_count" : 300000
                  }, {
                    "key_as_string" : "2015-02-12T00:00:00.000Z",
                    "key" : 1423699200000,
                    "doc_count" : 200000
                  }, {
                    "key_as_string" : "2015-02-11T00:00:00.000Z",
                    "key" : 1423612800000,
                    "doc_count" : 400000
                  }
                }
              }
            }
          }
        

        现在您可以遍历类型和每个日期的计数,确定您要删除的日期。在上面的示例中,如果您只想要“type1”的 500,000 个条目,那么您应该知道从 2015 年 2 月 11 日开始删除条目。

        您可以使用“delete by query”删除这些文件。

        希望对您有所帮助。

        【讨论】:

          【解决方案5】:

          最简单的修复方法 - logstash throttle 过滤器,根据应用程序名称设置密钥。

          另一种解决方案:在 elasticsearch 输出中设置“index”参数以指定应用程序名称,例如(伪代码)“logstash-%{appname}-%{date_format}”,然后使用“--”运行 curator prefix" 设置为 "logstash-appname" 和 --disk-space 设置为您喜欢的任何内容。不过我自己还没有测试过。

          最后,有很多方法可以(几乎)实时监控磁盘空间,但我通常使用的是http://mmonit.com/monit/documentation/monit.html#SPACE-TESTING

          附:当然有多个“logstash-xxx-”在与 Kibana 一起使用时会出现问题

          【讨论】:

            猜你喜欢
            • 2020-04-03
            • 1970-01-01
            • 2019-04-29
            • 2019-06-09
            • 2020-03-04
            • 2020-03-05
            • 1970-01-01
            • 2019-05-15
            • 1970-01-01
            相关资源
            最近更新 更多