ElasticsearchをAmazon Elasticsearch Serviceへ移行する

2015.11.05

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

藤本です。

先月、Amazon Elasticsearch Serviceがリリースされました。AWSフルマネージドなElasticsearchということで面倒なキャパシティ管理や高可用性管理をAWSに任せることができるとあり、是非とも活用していきたいですよね。ただ既存環境でElasticsearchをご利用中の方もいらっしゃると思います。そんな方のために今回は既に稼働中のElasticsearchからAmazon Elasticsearch Serviceにデータ移行する方法をご紹介します。

概要

ElasticsearchのSnapshot/Restore機能を利用し、今回はElasticsearch on Amazon LinuxからAmazon Elasticsearch Serviceへデータ移行します。

以下の制約があることはご注意ください。

  • 移行元のElasticsearchのバージョンが1.5.2以下である Amazon Elasticsearch Serviceはv1.5.2です。Elasticsearchは古いバージョンへのリストアができないため、Elasticsearch v1.6.0以降をご利用中の方はSnapshotによる移行ができません。

※ 1.5.2より高いバージョンからデータだけを移行したい場合、サードパーティ製ツール等でデータだけをjson形式でエクスポートしてbulkインポートすることは可能です。

移行は以下のような流れとなります。

  • Snapshot
  • S3プラグインのインストール
  • スナップショットリポジトリ登録
  • スナップショット取得
  • Restore
  • Amazon Elasticsearch Serviceドメイン作成
  • IAM Role作成
  • スナップショットリポジトリ登録
  • リストア

環境

  • 移行元
  • AMI : Amazon Linux 2015.09
  • Elasticsearch Version : 1.5.2
  • IAM Role : S3

  • 移行先

  • Amazon Elasticsearch Service(v1.5.2)

  • S3

  • バケット名 : fujimoto-es-backup

Snapshot

S3プラグインインストール

Amazon Elasticsearch ServiceはS3にあるスナップショットデータからリストアすることができます。そのため既存のElasticsearchのスナップショットリポジトリをS3に登録する必要があります。S3との連携にはelastic社製のS3プラグインを利用することで簡単に行えます。プラグインはコマンド一発でインストールできます。インストール後、有効にするためにはElasticsearchを再起動します。

# /usr/share/elasticsearch/bin/plugin install elasticsearch/elasticsearch-cloud-aws/2.7.1
-> Installing elasticsearch/elasticsearch-cloud-aws/2.7.1...
Trying http://download.elasticsearch.org/elasticsearch/elasticsearch-cloud-aws/elasticsearch-cloud-aws-2.7.1.zip...
Downloading ........................................................................................................................................................................................................................................................................................................................................................................................DONE
Installed elasticsearch/elasticsearch-cloud-aws/2.7.1 into /usr/share/elasticsearch/plugins/cloud-aws

# /etc/init.d/elasticsearch restart
Stopping elasticsearch: [ OK ]
Starting elasticsearch: [ OK ]

スナップショットリポジトリ登録

スナップショットを取得する前にスナップショットの保存先となるリポジトリを登録する必要があります。ElasticsearchのREST APIで登録できます。

# curl -XPUT http://localhost:9200/_snapshot/repos -d'
{
"type": "s3",
"settings": {
"bucket": "fujimoto-es-backup",
"region": "ap-northeast-1"
}
}'

{"acknowledged":true}

# curl localhost:9200/_snapshot/repos

{"repos":{"type":"s3","settings":{"region":"ap-northeast-1","bucket":"fujimoto-es-backup"}}}

今回はIAM Roleを割り当てたEC2上で設定しましたが、オンプレミス環境で設定する場合、S3へアクセスするためのIAM(access_key、secret_key)も合わせて設定する必要があります。

スナップショット取得

スナップショットを取得します。

# curl -XPUT localhost:9200/_snapshot/repos/snap-3
{"accepted":true}

# aws s3 ls s3://fujimoto-es-backup
PRE indices/
2015-10-24 09:51:56 50 index
2015-10-24 09:51:49 74 metadata-snap-3
2015-10-24 09:51:56 200 snapshot-snap-3

スナップショットリポジトリとして登録したS3バケットにスナップショットデータが保存されています。

Restore

Amazon Elasticsearch Service作成

Amazon Elasticsearch Serviceのドメイン作成は[新機能]Amazon Elasticsearch Serviceがリリースされました!を参照。

IAM Role作成

Amazon Elasticsearch ServiceはIAM Policyによりアクセス制御を持つことができますが、ドメイン自体は権限を持っていません。そのためスナップショットリポジトリとなるS3へアクセスできません。が、リポジトリの設定の一部としてIAM Roleを設定でき、そのRole権限でS3にアクセスすることができます。

{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:ListBucket"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::fujimoto-es-backup"
]
},
{
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"iam:PassRole"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::fujimoto-es-backup/*"
]
}
]
}

スナップショットリポジトリ登録

移行元と同じようにスナップショットリポジトリを登録します。異なる点として先に作成したIAM RoleのARNを設定として渡します。

# curl search-fujimoto-restore-d2235vmhg6vjb7xtamup5awi3i.ap-northeast-1.es.amazonaws.com/_snapshot/repos -d'
{
"type": "s3",
"settings": {
"bucket": "fujimoto-es-backup",
"region": "ap-northeast-1",
"role_arn": "arn:aws:iam::************:role/es-role"
}
}'

{"acknowledged":true}

# curl search-fujimoto-restore-d2235vmhg6vjb7xtamup5awi3i.ap-northeast-1.es.amazonaws.com/_snapshot/repos

{"repos":{"type":"s3","settings":{"bucket":"fujimoto-es-backup","role_arn":"arn:aws:iam::************:role/es-role","region":"ap-northeast-1"}}}

リストア

リストアAPIを実行します。 と、その前にAmazon Elasticsearch Serviceのドメインに既にインデックスが存在する場合、リストアできません。本来であれば、closeAPIによりクローズステータスとした上でデータをリストアできますが、Amazon Elasticsearch ServiceはcloseAPIをサポートしていません。そのため、既にインデックスが存在する場合、インデックスを削除する必要があります。

# curl "search-fujimoto-restore-d2235vmhg6vjb7xtamup5awi3i.ap-northeast-1.es.amazonaws.com/shakespeare?pretty"
{
"error" : "IndexMissingException[[shakespeare] missing]",
"status" : 404
}

# curl "search-fujimoto-restore-d2235vmhg6vjb7xtamup5awi3i.ap-northeast-1.es.amazonaws.com/_count?pretty"
{
"count" : 0,
"_shards" : {
"total" : 0,
"successful" : 0,
"failed" : 0
}
}

# curl -XPOST "search-fujimoto-restore-d2235vmhg6vjb7xtamup5awi3i.ap-northeast-1.es.amazonaws.com/_snapshot/repos/snap-3/_restore"
{"accepted":true}

# curl "search-fujimoto-restore-d2235vmhg6vjb7xtamup5awi3i.ap-northeast-1.es.amazonaws.com/_count?pretty"
{
"count" : 111396,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
}
}

# curl "search-fujimoto-restore-d2235vmhg6vjb7xtamup5awi3i.ap-northeast-1.es.amazonaws.com/shakespeare?pretty"
{
"shakespeare" : {
"aliases" : { },
"mappings" : {
"_default_" : {
"properties" : {
"line_id" : {
"type" : "integer"
},
"play_name" : {
"type" : "string",
"index" : "not_analyzed"
},
"speaker" : {
"type" : "string",
"index" : "not_analyzed"
},
"speech_number" : {
"type" : "integer"
}
}
},
"line" : {
"properties" : {
"line_id" : {
"type" : "integer"
},
"line_number" : {
"type" : "string"
},
"play_name" : {
"type" : "string",
"index" : "not_analyzed"
},
"speaker" : {
"type" : "string",
"index" : "not_analyzed"
},
"speech_number" : {
"type" : "integer"
},
"text_entry" : {
"type" : "string"
}
}
},
"scene" : {
"properties" : {
"line_id" : {
"type" : "integer"
},
"line_number" : {
"type" : "string"
},
"play_name" : {
"type" : "string",
"index" : "not_analyzed"
},
"speaker" : {
"type" : "string",
"index" : "not_analyzed"
},
"speech_number" : {
"type" : "integer"
},
"text_entry" : {
"type" : "string"
}
}
},
"act" : {
"properties" : {
"line_id" : {
"type" : "integer"
},
"line_number" : {
"type" : "string"
},
"play_name" : {
"type" : "string",
"index" : "not_analyzed"
},
"speaker" : {
"type" : "string",
"index" : "not_analyzed"
},
"speech_number" : {
"type" : "integer"
},
"text_entry" : {
"type" : "string"
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1445682940917",
"number_of_shards" : "5",
"uuid" : "IVGdcsKZSMChdIdFDZOQWA",
"version" : {
"created" : "1050299"
},
"number_of_replicas" : "1"
}
},
"warmers" : { }
}
}

データが復旧されました。ちなみに移行したデータはElastic社が用意しているデータセットです。

まとめ

いかがでしたでしょうか?

AWSは基本的に移行しやすい環境を整えてくれていることが嬉しいです。ただ移行となると現行のバージョン固定というところが課題でしょうか。

脚注

Making Manual Index Backups