7.6. Elasticsearch 数据策展

Elasticsearch Curator 工具在全局范围和/或以项目为基础执行调度的维护操作。Curator 根据其配置执行操作。

Cluster Logging Operator 将安装 Curator 及其配置。您可以使用集群日志记录自定义资源配置 Curator cron 计划,其他配置选项则可在 openshift-logging 项目中的 Curator ConfigMap curator(融合了 Curator 配置文件 curator5.yaml 以及 OpenShift Container Platform 自定义配置文件 config.yaml)中找到。

OpenShift Container Platform 在内部使用 config.yaml 来生成 Curator action 文件

(可选)您可以直接使用此 action 文件。通过编辑此文件,您可以使用 Curator 提供的可定期运行的任何操作。但是,仅建议高级用户使用此功能,因为修改文件可能会对集群造成破坏,并可能导致从 Elasticsearch 中删除必要的索引/设置。大多数用户仅需修改 Curator 配置映射,无需编辑 action 文件。

7.6.1. 配置 Curator 计划

您可以使用由集群日志记录安装创建的集群日志记录自定义资源来指定 Curator 的调度。

先决条件

  • 必须安装 Cluster Logging 和 Elasticsearch。

流程

配置 Curator 调度:

  1. 编辑 openshift-logging 项目中的集群日志记录自定义资源:

    $ oc edit clusterlogging instance
    apiVersion: "logging.openshift.io/v1"
    kind: "ClusterLogging"
    metadata:
      name: "instance"
    
    ...
    
      curation:
        curator:
          schedule: 30 3 * * * 1
        type: curator
    1
    cron 格式指定 Curator 的调度。
    注意

    时区是根据 Curator Pod 运行所在的主机节点设置的。

7.6.2. 配置 Curator 索引删除

您可以配置 Curator,以根据保留设置删除 Elasticsearch 数据。您可以配置在特定项目范围内的设置,也可以配置全局范围的设置。全局设置应用到任何未指定的项目。特定项目范围内的设置会覆盖全局设置。

先决条件

  • 必须安装集群日志记录。

流程

删除索引:

  1. 编辑 OpenShift Container Platform 自定义 Curator 配置文件:

    $ oc edit configmap/curator
  2. 根据需要设置以下参数:

    config.yaml: |
      project_name:
        action
          unit:value

    可用的参数如下:

    表 7.1. 项目选项

    变量名称描述

    project_name

    项目的实际名称,例如 myapp-devel。对于 OpenShift Container Platform operations 日志,请使用 .operations 作为项目名称。

    action

    当前只支持 delete

    unit

    用于删除的期限,可以是 daysweeksmonths

    value

    单位数。

    表 7.2. 过滤选项

    变量名称描述

    .defaults

    使用 .defaults 作为 project_name,可为尚未指定的项目设置默认值。

    .regex

    与项目名称匹配的正则表达式列表。

    pattern

    有效且正确转义的正则表达式,用单引号括起。

例如,要将 Curator 配置为:

  • 删除 myapp-dev 项目中存在时间超过 1 天的索引
  • 删除 myapp-qe 项目中存在时间超过 1 个星期的索引
  • 删除存在时间超过 8 个星期operations日志
  • 删除所有其他项目中存在时间超过 31 天的索引
  • 删除与 ^project\..+\-dev.*$ 正则表达式匹配且存在时间超过 1 天的索引
  • 删除与 ^project\..+\-test.*$ 正则表达式匹配且存在时间超过 2 天的索引

使用:

  config.yaml: |
    .defaults:
      delete:
        days: 31

    .operations:
      delete:
        weeks: 8

    myapp-dev:
      delete:
        days: 1

    myapp-qe:
      delete:
        weeks: 1

    .regex:
      - pattern: '^project\..+\-dev\..*$'
        delete:
          days: 1
      - pattern: '^project\..+\-test\..*$'
        delete:
          days: 2
重要

当您将 months 用作操作的 $UNIT 时,Curator 会从当月的第一天开始计算,而不是当月的当天。例如,如果今天是 4 月 15 日,并且您想要删除目前存在时间已达 2 个月的索引 (delete: months: 2),Curator 不会删除日期在 2 月 15 日前的索引,而是会删除日期在 2 月 1 日前的索引。也就是说,它会退回到当前月份的第一天,然后从该日期起返回两个整月。如果您想使 Curator 准确一些,则最好使用 days(例如 delete: days: 30)。

7.6.3. Curator 故障排除

您可以参照本节中的信息来调试 Curator。例如,如果 Curator 处于失败状态,但日志消息未提供原因,您可以提高日志级别并触发新任务,而不必等待另一次调度运行 cron 任务。

先决条件

必须安装 Cluster Logging 和 Elasticsearch。

流程

启用 Curator 调试日志并手动触发下一次 Curator 操作

  1. 启用 Curator 的调试日志:

    $ oc set env cronjob/curator CURATOR_LOG_LEVEL=DEBUG CURATOR_SCRIPT_LOG_LEVEL=DEBUG

    指定日志级别:

    • CRITICAL:Curator 仅显示严重消息。
    • ERROR:Curator 仅显示错误和严重消息。
    • WARNING:Curator 仅显示错误、警告和严重消息。
    • INFO:Curator 仅显示参考、错误、警告和严重消息。
    • DEBUG:除上述所有消息外,Curator 仅显示调试消息。

      默认值为 INFO。

      注意

      集群日志记录在 OpenShift Container Platform 打包程序脚本(run.shconvert.py)中使用 OpenShift Container Platform 自定义环境变量 CURATOR_SCRIPT_LOG_LEVEL。根据需要,环境变量采用与 CURATOR_LOG_LEVEL 相同的值进行脚本调试。

  2. 触发下一次 Curator 迭代:

    $ oc create job --from=cronjob/curator <job_name>
  3. 使用以下命令来控制 CronJob:

    • 暂停 CronJob:

      $ oc patch cronjob curator -p '{"spec":{"suspend":true}}'
    • 恢复 CronJob:

      $ oc patch cronjob curator -p '{"spec":{"suspend":false}}'
    • 更改 CronJob 调度:

      $ oc patch cronjob curator -p '{"spec":{"schedule":"0 0 * * *"}}' 1
      1
      schedule 选项接受 cron 格式的调度。

7.6.4. 在脚本化部署中配置 Curator

如果必须在脚本化部署中配置 Curator,请使用本节中的信息。

先决条件

  • 必须安装 Cluster Logging 和 Elasticsearch。
  • 将集群日志记录设置为非受管状态。

流程

在脚本中使用以下代码片段配置 Curator:

  • 如果是脚本化部署

    1. 创建并修改配置:

      1. 从 Curator 配置映射中复制 Curator 配置文件和 OpenShift Container Platform 自定义配置文件,并为它们分别创建单独的文件:

        $ oc extract configmap/curator --keys=curator5.yaml,config.yaml --to=/my/config
      2. 编辑 /my/config/curator5.yaml/my/config/config.yaml 文件。
    2. 删除现有的 Curator 配置映射,并将编辑后的 YAML 文件添加到新 Curator 配置映射中。

      $ oc delete configmap curator ; sleep 1
      $ oc create configmap curator \
          --from-file=curator5.yaml=/my/config/curator5.yaml \
          --from-file=config.yaml=/my/config/config.yaml \
          ; sleep 1

      下一次操作将使用此配置。

  • 如果使用 action 文件:

    1. 创建并修改配置:

      1. 从 Curator 配置映射中复制 Curator 配置文件和 action 文件,并为它们分别创建单独的文件:

        $ oc extract configmap/curator --keys=curator5.yaml,actions.yaml --to=/my/config
      2. 编辑 /my/config/curator5.yaml/my/config/actions.yaml 文件。
    2. 删除现有的 Curator 配置映射,并将编辑后的 YAML 文件添加到新 Curator 配置映射中。

      $ oc delete configmap curator ; sleep 1
      $ oc create configmap curator \
          --from-file=curator5.yaml=/my/config/curator5.yaml \
          --from-file=actions.yaml=/my/config/actions.yaml \
          ; sleep 1

      下一次操作将使用此配置。

7.6.5. 使用 Curator Action 文件

openshift-logging 项目中的 Curator ConfigMap 包含一个 Curator action 文件,您可以在其中配置任何要定期运行的 Curator 操作。

不过,使用 action 文件时,OpenShift Container Platform 会忽略 curator ConfigMap 中的 config.yaml 部分,它配置为用于确保不误删重要的内部索引。若要使用 action 文件,您应在配置中添加排除规则来保留这些索引。您还必须按照本主题中的步骤,手动添加所有其他模式。

重要

actionconfig.yaml 是互斥的配置文件。一旦存在 action 文件,OpenShift Container Platform 就会忽略 config.yaml 文件。仅建议高级用户使用 action 文件,因为使用该文件可能会对集群造成破坏,并可能导致从 Elasticsearch 中删除必要的索引/设置。

先决条件

  • 必须安装 Cluster Logging 和 Elasticsearch。
  • 将集群日志记录设置为非受管状态。处于非受管状态的 Operator 不被正式支持,集群管理员需要完全掌控各个组件的配置和升级。

流程

配置 Curator 以删除索引:

  1. 编辑 Curator ConfigMap:

    oc edit cm/curator -n openshift-logging
  2. action 文件进行以下更改:

    actions:
    1:
          action: delete_indices 1
          description: >-
            Delete .operations indices older than 30 days.
            Ignore the error if the filter does not
            result in an actionable list of indices (ignore_empty_list).
            See https://www.elastic.co/guide/en/elasticsearch/client/curator/5.2/ex_delete_indices.html
          options:
            # Swallow curator.exception.NoIndices exception
            ignore_empty_list: True
            # In seconds, default is 300
            timeout_override: ${CURATOR_TIMEOUT}
            # Don't swallow any other exceptions
            continue_if_exception: False
            # Optionally disable action, useful for debugging
            disable_action: False
          # All filters are bound by logical AND
          filters:            2
          - filtertype: pattern
            kind: regex
            value: '^\.operations\..*$'
            exclude: False    3
          - filtertype: age
            # Parse timestamp from index name
            source: name
            direction: older
            timestring: '%Y.%m.%d'
            unit: days
            unit_count: 30
            exclude: False
    1
    指定 delete_indices 以删除指定的索引。
    2
    使用 filers 参数以指定要删除的索引。如需这些参数的信息,请参阅 Elasticsearch Curator 文档
    3
    指定 false 以允许删除索引。