有个管理ES index的需求,感觉使用curl调用HTTP接口很不方便也不是很科学。在Github上一查,果然有官方提供的工具curator,这个工具不仅仅满足了我简单管理的需要(索引的关闭、打开、段合并、删除等),也为以后的扩展提供了新的解决方案(支持磁盘占用触发),非常不错。以下记录我的调研测试过程。另外补充一句curator的官方文档十分完备,如喜欢直接阅读官方文档可以直接跳到本文底部,那里有官方文档链接。

安装

官方提供了多种安装途径(pip、yum、SourceCode、Docker),支持Unix及Windows平台。由于安装需要python3.x环境且我使用的是AWS的ec2实例,感觉使用源码或yum的方式管理维护不太方便,所以决定使用Docker方式。

  • 本地环境
1
2
# cat /etc/issue
Amazon Linux AMI release 2017.03
  • 安装Docker环境&下载源代码
1
2
3
4
5
6
yum install docker
/etc/init.d/docker start
#Install and start docker service
mkdir -pv /data/ES-curator&&cd  /data/ES-curator
wget https://github.com/elastic/curator/archive/v5.5.4.zip && unzip v5.5.4.zip
#Download curator source code
  • 简单升级DockerFile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
FROM python:3.6-alpine3.6 as builder
RUN apk --no-cache add build-base tar musl-utils openssl-dev
RUN pip3 install setuptools cx_Freeze==6.0b1 requests-aws4auth boto3
COPY . .
RUN ln -s /lib/libc.musl-x86_64.so.1 ldd
RUN ln -s /lib /lib64
RUN pip3 install -r requirements.txt
RUN python3 setup.py build_exe
FROM alpine:3.6
ENV CURATOR_CONFIG_HOME /data/curator-config
RUN apk --no-cache upgrade && apk --no-cache add ca-certificates  && \
    mkdir -p "${CURATOR_CONFIG_HOME}"
COPY --from=builder build/exe.linux-x86_64-3.6 /curator/
VOLUME ["${CURATOR_CONFIG_HOME}"]
USER root:root
ENTRYPOINT ["/curator/curator"]
  • 构建Docker镜像
1
2
cd curator-5.5.4/ && docker build -t curator:v5.5.4 .
#build docker image from source code

使用

  • 配置文件(默认会使用~/.curator/curator.yml,可以手动通过--config参数指定。配置如下,更具体的文档可以查阅【官方】
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
---
# Remember, leave a key empty if there is no value.  None will be a string,
# not a Python "NoneType"
client:
  hosts:
  #填写ES的服务节点,可以只写一个节点,可以写成下面的形式,也可以写成['host1', 'host2]这种形式
    - x.x.x.x
  port: 9200 #ES的端口
  url_prefix: #默认留空即可,使用域名的时候可以根据需求修改
  use_ssl: False
  certificate:
  client_cert:
  client_key:
  ssl_no_validate: False
  http_auth:
  timeout: 30
  master_only: False

logging:
  loglevel: INFO #记录log的级别
  logfile: /data/curator-config/log/curator_%Y%m%d.log #log的输出设置,默认留空
  logformat: default
  blacklist: ['elasticsearch', 'urllib3'] #日志输出屏蔽的列表,按默认即可,这两个相关log输入太影响Debug

使用Docker对Index进行管理

由于直接测试写的文档,也没有什么高深的技术,有兴趣可移步到我的Github。里面有测试使用的例子(包括打开索引、关闭索引、段合并以及删除索引)

自己写了一个简单的启动脚本,由于比较长也没什么营养,感兴趣的可以看【这里】 使用我这个脚本,然后再服务器上进行设置定时任务运行,当需要单独执行某个操作时候这种管理方式可能方便些。

1
2
#crontab -e
0 3 * * * service curator-manage-index cron

使用命令

1
2
3
4
5
6
7
8
docker run -d --rm -v /data/ES-curator/curator-config/:/data/curator-config curator:v5.5.4 --config /data/curator-config/curator.yml /data/curator-config/close_index_rule.yml
#关闭索引
docker run -d --rm -v /data/ES-curator/curator-config/:/data/curator-config curator:v5.5.4 --config /data/curator-config/curator.yml /data/curator-config/delete_index_rule.yml
#删除索引
docker run -d --rm -v /data/ES-curator/curator-config/:/data/curator-config curator:v5.5.4 --config /data/curator-config/curator.yml /data/curator-config/merge_index_rule.yml
#段合并
docker run -d --rm -v /data/ES-curator/curator-config/:/data/curator-config curator:v5.5.4 --config /data/curator-config/curator.yml /data/curator-config/open_index_rule.yml
#打开索引

补充配置说明

上面记录了我使用中需求的具体示例,还有一些配置选项没有进行说明,就简单介绍下curator可以对Index进行哪些操作,以及常见具体操作时的参数选项。


选取Index(Filters)

使用filters进行筛选需要操作的index。虽然过滤器可以链接,但是每个过滤器都通过隐含的逻辑AND操作链接。如果想从几个不同的模式中匹配,就像用逻辑或运算一样,可以用模式过滤器类型使用正则表达式来匹配。

操作筛选操作分为两部分(索引以及镜像),这里只对Index的过滤选项进行介绍,更多可以参照【官方】

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#支持正则进行多个index的匹配
  filters:
  - filtertype: pattern
    kind: regex
    value: '^(alpha-|bravo-|charlie-).*$'
  - filtertype: age #按时间进行筛选index
    source: creation_date
    direction: older
    unit: days
    unit_count: 3

参考链接