从亚马逊云上的Elasticsearch获取快照并保存到S3的方法
目的 : 着眼点
“在AWS S3存储库插件中,可以使用AWS S3作为存储库执行快照和恢复操作。本文将介绍如何在EC2上的Elasticsearch中对索引信息进行快照的方法(截至2020年1月的信息)。通过索引生命周期管理,可以通过Kibana的图形用户界面管理索引的生命周期,但是目前尚不支持通过图形界面操作快照,仍然需要使用curator(基于Python的索引操作工具)。虽然可以使用Elasticsearch的标准API进行快照,但是如果对操作不熟悉,实施门槛可能较高。因此,本次我们将使用curator来实现将快照保存到S3的操作。”
操作步骤
第一步:使用pip命令安装Elasticsearch-Curator。
根据curator installation中所述,curator最简单的安装方式是使用pip命令进行安装。如果没有pip,则可以使用以下命令进行安装。
curl -O https://bootstrap.pypa.io/get-pip.py
python get-pip.py
echo export PATH='~/.local/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile
pip install elasticsearch-curator --ignore-installed
请注意:如果出现以下错误信息,您需要更新 AWS 命令行界面(AWS CLI)。
ERROR: awscli 1.16.102 has requirement botocore==1.12.92, but you'll have botocore 1.15.11 which is incompatible.
ERROR: awscli 1.16.102 has requirement s3transfer<0.3.0,>=0.2.0, but you'll have s3transfer 0.3.3 which is incompatible.
在这种情况下,您需要使用以下命令来更新AWS CLI。
pip install awscli --upgrade
第二步。进行curator的各种设置。
将curator的执行日志配置为写入/var/log/curator/目录。
mkdir -p /var/log/curator
touch /var/log/curator/curator.log
ll /var/log/curator/
total 0
-rw-r--r-- 1 root root 0 Jan 20 07:07 curator.log
馆长可以在配置文件和动作文件中(均为yml文件格式)指定执行内容。
配置文件包含客户端连接和日志设置。配置文件的默认位置是 ~/.curator/curator.yml,但可以使用命令行上的 –config 标志指定另一个位置。
对于我的情况来说,我创建了一个配置文件(urator.yml)并将其放在/root/.curator目录下,还创建了一个动作文件(aindex_backup_ation.yml)并将其放在/root/.action目录下。在执行curator命令时,我决定使用–config选项指定这两个文件的位置。
mkdir -p /root/.curator/
mkdir -p /root/.action/
curator.yml的内容如下。
# Remember, leave a key empty if there is no value. None will be a string,
# not a Python "NoneType"
client:
hosts:
- localhost
port: 9200
url_prefix:
use_ssl: False
certificate:
client_cert:
client_key:
ssl_no_validate: False
timeout: 30
master_only: True
logging:
loglevel: INFO
logfile: /var/log/curator/curator.log
logformat: default
blacklist: ['elasticsearch', 'urllib3']
index_backup_ation.yml文件的内容如下所示(为了进行简单的操作确认,过滤条件被设为none)。
actions:
1:
action: snapshot
description: >-
options:
repository: elasticsearch-repo01
name: 'snapshot-%Y.%m.%d'
include_global_state: True
wait_for_completion: True
max_wait: 3600
wait_interval: 10
filters:
- filtertype: none
第三步:安装S3存储库插件。
使用以下命令在构成Elasticsearch集群的所有节点上安装s3插件,并重新启动Elasticsearch以启用插件。
usr/share/elasticsearch/bin/elasticsearch-plugin install repository-s3
systemctl restart elasticsearch.service
第四步:在Elasticsearch中创建一个适用于S3的存储库。
通过Kibana的用户界面,创建存储库(elasticsearch-repo01)。
GET /_snapshot/elasticsearch-repo01
{
"elasticsearch-repo01" : {
"type" : "s3",
"settings" : {
"bucket" : "elastic-test-bucket",
"storage_class" : "standard"
}
}
}
第五步:创建一个Amazon S3存储桶。
我创建了名为elastic-test-bucket的存储桶,并应用了以下策略。
{
"Version": "2012-10-17",
"Id": "Policy1579086116255",
"Statement": [
{
"Sid": "Stmt1579086112517",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::elastic-test-bucket"
}
]
}
另外,我們已經根據以下設定公共訪問(此次僅作功能確認,並未考慮安全事項)。

第6步:创建IAM策略并将其分配给IAM角色。
我将在创建策略时创建以下新策略。

{
"Statement": [
{
"Action": [
"s3:ListBucket",
"s3:GetBucketLocation",
"s3:ListBucketMultipartUploads",
"s3:ListBucketVersions"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::Bucket_A"
]
},
{
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:AbortMultipartUpload",
"s3:ListMultipartUploadParts"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::Bucket_A/*"
]
}
],
"Version": "2012-10-17"
}
将IAM策略分配给IAM角色。

将IAM角色分配给已安装Elasticsearch的EC2实例。

第六步:运行curator命令将快照获取到S3中。
使用–config命令指定已创建的配置文件和操作文件,以此来获取快照。
curator --config /root/.curator/curator.yml /root/.action/index_backup_ation.yml
步骤7. 通过执行日志进行实施确认.
查看curator的执行日志,确认是否没有任何问题。根据显示的”job completed”,可以确认curator的执行已经顺利完成。
tail -n 100 /var/log/curator/curator.log
2020-01-20 08:22:44,074 INFO Preparing Action ID: 1, "snapshot"
2020-01-20 08:22:44,074 INFO Creating client object and testing connection
2020-01-20 08:22:44,076 INFO Instantiating client object
2020-01-20 08:22:44,077 INFO Testing client connectivity
2020-01-20 08:22:44,080 INFO Successfully created Elasticsearch client object with provided settings
2020-01-20 08:22:44,081 INFO Connecting only to local master node...
2020-01-20 08:22:44,085 INFO Trying Action ID: 1, "snapshot":
2020-01-20 08:22:44,656 INFO Creating snapshot "snapshot-2020.01.20" from indices: [u'customer', u'.kibana_1', u'.kibana_task_manager_1', u'kibana_sample_data_ecommerce', u'kibana_sample_data_flights', u'.apm-agent-configuration', u'kibana_sample_data_logs']
2020-01-20 08:22:44,754 INFO Snapshot snapshot-2020.01.20 still in progress.
2020-01-20 08:22:55,023 INFO Snapshot snapshot-2020.01.20 successfully completed.
2020-01-20 08:22:55,085 INFO Snapshot snapshot-2020.01.20 successfully completed.
2020-01-20 08:22:55,085 INFO Action ID: 1, "snapshot" completed.
2020-01-20 08:22:55,085 INFO Job completed.
第八步:执行确认(从S3存储桶)
确认AWS S3中没有问题,并且已经存储了索引信息。
