Xóa indices tự động với Elasticsearch Curator

Elasticsearch Curator (Curator) ban đầu có tên là clearESindices.py, có chức năng xóa indices. Sau một thời gian nó được đổi tên thành Elasticsearch Curator và repo chứa tại Github sau: https://github.com/elastic/curator

Chức năng của Curator vì thế cũng được bổ sung nhiều hơn, giúp bạn sắp xếp, quản lý indices, snapshot bằng cách:

Hôm nay mình sẽ hướng dẫn các bạn Xóa indices tự động với Elasticsearch Curator

Bước 1: Cài đặt Elastic curator

Hướng dẫn này trên Centos 7:

rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch

Bạn tạo file trong /etc/yum.repo/curator.repo:

[curator-5]
name=CentOS/RHEL 7 repository for Elasticsearch Curator 5.x packages
baseurl=https://packages.elastic.co/curator/5/centos/7
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
yum install elasticsearch-curator -y

Khi cài đặt thành công, file config curator nằm tại /opt/elastic-curator/curator.conf

Bạn mở config ra và thay đổi theo sau:

---
# Remember, leave a key empty if there is no value.  None will be a string,
# not a Python "NoneType"
client:
  hosts:
    - 192.168.1.12
  port: 9200
  url_prefix:
  use_ssl: False
  certificate:
  client_cert:
  client_key:
  aws_key:
  aws_secret_key:
  aws_region:
  ssl_no_validate: False
  timeout: 60
  master_only: False

logging:
  loglevel: DEBUG
  logfile: /var/log/curator.log
  logformat: default
  blacklist: ['elasticsearch', 'urllib3']

Bước 2: Tạo file .yaml để định nghĩa cho curator cần xóa những indices nào

Bạn tạo ra file delete.yml trong path: /opt/elastic-curator/

---
# 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 "index-{1,2}" older than 3 month (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
      timeout_override:
      continue_if_exception: True
      disable_action: False
    filters:
    - filtertype: pattern
      kind: regex
      value: 'new-index-1-|new-index-2-'
      exclude: False
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: months
      unit_count: 3
      exclude: False

  2:
    action: delete_indices
    description: >-
      Delete indices older than 30 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
      timeout_override:
      continue_if_exception: True
      disable_action: False
    filters:
    - filtertype: pattern
      kind: regex
      value: 'new-index-1-|new-index-2-'
      exclude: True
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: days
      unit_count: 30
      exclude: False

Kịch bản trong đoạn yaml này rất đơn giản, những indices nào là “index-1 hoặc index-2” sẽ được giữ lại trong 3 tháng, những indices nào lớn hơn 3 tháng thì sẽ được xóa.

Còn những indices nào không phải là “index-1 hoặc index-2” thì sẽ được xóa khi lớn hơn 30 ngày.

Những option bạn cần chú ý là:

Xong rồi đó, bây giờ bạn có thể test xem file delete.yml đã chạy đúng hay chưa bằng cách sử dụng option “dry-run” mà curator có hỗ trợ, nó sẽ diễn ra y như kịch bản bạn định nghĩa, chỉ có điều nó không xóa những indices đi thôi.

/opt/elasticsearch-curator/curator --config /opt/elasticsearch-curator/curator.yml  /opt/elasticsearch-curator/delete.yml --dry-run

Mở file curator.log ra, bạn sẽ thấy mode dry-run hoạt động ra sao:

2020-03-22 04:07:06,072 INFO               curator.utils           show_dry_run:917  DRY-RUN MODE.  No changes will be made.
2020-03-22 04:07:06,073 INFO               curator.utils           show_dry_run:920  (CLOSED) indices may be shown that may not be acted on by action "delete_indices".
2020-03-22 04:07:06,073 INFO               curator.utils           show_dry_run:927  DRY-RUN: delete_indices: index-1-2020.02.19 with arguments: {}
...
2020-03-22 04:07:06,075 INFO                 curator.cli                    run:196  Action ID: 2, "delete_indices" completed.
2020-03-22 04:07:06,075 INFO                 curator.cli                    run:197  Job completed.

Bước 3: Hoàn tất bằng cách lập lịch cho curator tự động xóa indices

Rất đơn giản, chỉ cần bạn add vào cronjob là xong:

0 0 * * 0 /opt/elasticsearch-curator/curator --config /opt/elasticsearch-curator/curator.yml  /opt/elasticsearch-curator/delete.yml

Mình lập lịch xóa vào chủ nhật hàng tuần, vào lúc 0h00p.

Tổng kết

Như vậy bạn đã được giới thiệu qua về curator, công cụ rất hữu ích để bạn tự động xóa những indices trong elasticesarch, rất hữu ích cho những ELK stack nào cần dọn dẹp log, những log không được dùng hoặc phân tích nữa, dọn dẹp được mớ log là giải phóng được biết bao nhiêu disk rồi nhỉ 😀

Cám ơn các bạn đã theo dõi bài hướng dẫn Xóa indices tự động với Elasticsearch Curator.