Programming/python

es curator

유주원 2024. 3. 8. 14:47

es에서 index가 기하급수적으로 늘어나면서 디스크 용량이 부족해지기 시작했다.

 

index 정리를 하자...

 

그래서 찾아본게 curator!!

 

쉽게 말해서 index를 날짜 패턴별로 삭제를 쉽게 도와주는 프로그램이다.

 

이제 curator를 설치하고 실행하는 걸 해 보자.

아래와 같이 curator를 설치한다.

 

$> pip install elasticsearch-curator

 

설치 시에 중요한 점이 있는데 curator 버전과 es의 버전을 major 정도는 맞춰주어야 동작이 원할하게 된다.

 

만약에 버전이 안 맞는 경우 curator 실행 시에 아래와 같은 에러가 발생 할 수 있다.

 

  File "/hanmail/.pyenv/versions/3.9.4/lib/python3.9/base64.py", line 45, in _bytes_from_decode_data
    raise TypeError("argument should be a bytes-like object or ASCII "
TypeError: argument should be a bytes-like object or ASCII string, not 'NoneType'

 

내가 쓰고자 하는 es는 6.x 였기 때문에 curator도 6점대 버전으로 지정해서 설치해 주었다.

 

$> pip install elasticsearch-curator==v6.0.0

 

curator가 잘 설치가 되었는지 확인하자.

 

curator --version이라고 실행하면 아래와 같은 결과가 출력된다.

 

curator, version 6.0.0

 

이제 curator 실행을 위한 config 파일과 action 파일을 생성하자.

 

config.yml

client:
  hosts:
    - es 주소
  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']

 

특이한 옵션 중 master_only과 blacklist가 있는데 각각에 대해 살펴보자.

master_only : True로 설정하면, master로 선출된 노드에 대해서만 curator가 설치가 된다. 만약 master_only이 True이고 hosts의 값이 2개 이상일 경우에는 에러가 발생한다.

blacklist : 선택된 항목에 대해서는 logging을 남기지 않는다.

 

action.yml

actions:
    1:
        action: delete_indices
        options:
            ignore_empty_list: True
            continue_if_exception: False
            disable_action: False
        filters:
            - filtertype: pattern
              kind: prefix
              value: test1-
            - filtertype: age
              source: name
              direction: older
              timestring: '%Y.%m'
              unit: days
              unit_count: 365
    2:
        action: delete_indices
        options:
            ignore_empty_list: True
            continue_if_exception: False
            disable_action: False
        filters:
            - filtertype: pattern
              kind: prefix
              value: test2-
            - filtertype: age
              source: name
              direction: older
              timestring: '%Y'
              unit: months
              unit_count: 12

 

위와 같이 해당 패턴으로 삭제하고자 하는 index를 등록함으로써 curator에서 삭제를 하도록 설정할 수가 있다.

unit의 경우 seconds, minutes, hours, days, weeks, months, years 의 설정이 가능하다.

또한 해당 action을 잠시 중지시키고 싶을 때는 disable_action 값을 True로 선언하면 된다.

 

이렇게 두 개의 파일 생성이 끝났다면 이제 curator를 실행해보자.

 

$> curator --config config.yml action.yml

 

아래와 같이 index가 잘 삭제되는 걸 확인할 수가 있다.

 

 

참고로 아래와 같이 dry-run 모드로 실행하면 실제 index 삭제는 이뤄지지 않는다. test 경우에만 dry-run 모드로 실행하도록 하자.

 

$> curator --config config.yml --dry-run action.yml