Curator: eliminando índices de Elasticsearch

por | enero 7, 2019

La herramienta Curator nos permite trabajar de forma sencilla con nuestros índices de Elasticsearch. Una de sus funcionalidades es la de eliminar índices con cierta antigüedad, ideal para realizar limpiezas periódicas y evitar que aumente la ocupación en disco de forma indefinida.

En estos momentos Curator tiene su propio repositorio independiente del principal de elastic. En mi caso en Debian 9 creo el fichero /etc/apt/sources.list.d/curator.list

deb [arch=amd64] https://packages.elastic.co/curator/5/debian9 stable main

Después con apt instalamos:

apt update && apt-get install elasticsearch-curator

También se puede instalar con pip independientemente del sistema:

pip install elasticsearch-curator

El paquete elasticsearch-curator nos proporciona dos comandos: curator_cli y curator. El primero permite realizar filtros sobre índices pasando un JSON como argumento, por lo que a priori es más directo y de fácil uso.

Una vez instalado podemos hacer un pequeño test mostrando los índices de nuestro Elastic con curator_cli:

curator_cli --host 192.168.1.2 show_indices --verbose --header

Obteniendo:

Comando curator_cli y filtros JSON

Echando mano de curator_cli, por ejemplo para eliminar todos los índices con prefijo filebeat con antigüedad superior a 3 días lanzaríamos lo siguiente:

curator_cli --host 192.168.1.2 \
--dry-run delete_indices \
--filter_list '[{"filtertype":"age","source":"creation_date","direction":"older","unit":"days","unit_count":10},{"filtertype":"pattern","kind":"prefix","value":"filebeat"}]'

En el ejemplo incluyo la opción <code>dry-run</code> para realizar un test y ver qué índices borraría:

En mi caso utilizo curator_cli en un script como el siguiente que lanzo desde una crontab, recorriendo con un bucle el array de prefijos de índices que quiero borrar:

#!/bin/bash                          
#
# Description   :Clean elasticsearch indices based on date
# Author        :Julio Sanz
#

retention_days="10"
elastic_host="192.168.1.2"
indices_patterns=(filebeat metricbeat auditbeat packetbeat)

for pattern in ;do
    curator_cli --host 192.168.1.2 delete_indices \
    --filter_list '[{"filtertype":"age","source":"creation_date","direction":"older","unit":"days","unit_count":"'"$retention_days"'"},{"filtertype":"pattern","kind":"prefix","value":"'"$pattern"'"}]'
done

Curator con ficheros YAML

Por su parte el comando curator utiliza ficheros YAML en los que se parametrizan configuraciones y operaciones a realizar. Si conoces Ansible, la sintaxis te resultará familiar. Para el caso de ejemplo podríamos crear un fichero curator.yml con el siguiente contenido:

---
client:
  hosts:
    - 192.168.1.2
  port: 9200
  timeout: 30

Y otro llamado delete_indices.yml con esto:

actions:
  1:
    action: delete_indices
    description: >-
      Delete indices older than 10 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: False
    filters:
    - filtertype: pattern
      kind: prefix
      value: filebeat-
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: days
      unit_count: 10

Después lanzaríamos curator con:

curator ./delete_indices.yml --config ./curator.yml --dry-run

Obteniendo:

En el fichero delete_indices.yml sólo he incluído el prefijo de filebeat, podemos crear otro bloque de acciones que incluya por ejemplo el prefijo de metricbeat:

actions:            
  1:
    action: delete_indices
    description: >-
      Delete indices older than 10 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: False
    filters:
    - filtertype: pattern
      kind: prefix
      value: filebeat-
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: days
      unit_count: 10
  2:
    action: delete_indices
    description: >-
      Delete indices older than 10 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: False
    filters:
    - filtertype: pattern
      kind: prefix
      value: metricbeat-
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y.%m.%d'
      unit: days
      unit_count: 10

Para el comando curator tenéis varias plantillas para todo tipo de acciones en la web oficial de Elastic.

Recordad también que tenéis el proyecto en GitHub.