升级 Elasticsearch之前的准备工作

总结

由于在开发环境中使用的Elasticsearch版本过旧,因此决定进行升级到最新版本的操作。在查看了官方指南后,发现了一个名为Elasticsearch迁移插件的工具,可以帮助检查迁移时所需的操作等。

在这里,我们将检查从1.x升级到2.x的版本是否可行。指南在这里。
顺便提一下,如果是从2.x升级到5.x的情况,请参考这里。

工作环境是AWS的EC2实例。

引入 Elasticsearch 迁移插件。

# elasticsearchのあるディレクトリに移動
$ cd /usr/local/share/elasticsearch

# インストール実行
$ ./bin/plugin -i migration -u https://github.com/elastic/elasticsearch-migration/releases/download/v1.19/elasticsearch-migration-1.19.zip
-> Installing migration...

# エラー(ディレクトリのパーミッションの問題)
Failed to install migration, reason: plugin directory /usr/local/share/elasticsearch/plugins is read only

# rootで実行
$ sudo ./bin/plugin -i migration -u https://github.com/elastic/elasticsearch-migration/releases/download/v1.19/elasticsearch-migration-1.19.zip
-> Installing migration...
Trying https://github.com/elastic/elasticsearch-migration/releases/download/v1.19/elasticsearch-migration-1.19.zip...
Failed: IOException[Can't get https://github.com/elastic/elasticsearch-migration/releases/download/v1.19/elasticsearch-migration-1.19.zip to /usr/local/share/elasticsearch/plugins/migration.zip]; nested: FileNotFoundException[https://github.com/elastic/elasticsearch-migration/releases/download/v1.19/elasticsearch-migration-1.19.zip]; nested: FileNotFoundException[https://github.com/elastic/elasticsearch-migration/releases/download/v1.19/elasticsearch-migration-1.19.zip];
Trying https://github.com/null/migration/archive/master.zip...
Failed to install migration, reason: failed to download out of all possible locations..., use --verbose to get detailed information

被告知这个版本不存在,他生气了。

我查看了发布版本,发现有v1.18,所以先试试把它安装上去。

# インストール
$ sudo ./bin/plugin -i migration -u https://github.com/elastic/elasticsearch-migration/releases/download/v1.18/elasticsearch-migration-1.18.zip
-> Installing migration...
Trying https://github.com/elastic/elasticsearch-migration/releases/download/v1.18/elasticsearch-migration-1.18.zip...
Downloading ...............................................................DONE
Installed migration into /usr/local/share/elasticsearch/plugins/migration

请从浏览器进行确认。

capture_elastic_migration.png
Kobito.5pvKaA.png

索引的备份

听说在迁移时,可以使用这里的快照和恢复功能进行操作。

在elasticsearch.yml文件中新增以下内容。

path.repo: ["/usr/local/share/elasticsearch_snapshot"]
# バックアップ先のディレクトリ作成
$ sudo mkdir -p /usr/local/share/elasticsearch_snapshot
$ sudo chmod 777 /usr/local/share/elasticsearch_snapshot

# リポジトリを登録
$ curl -XPUT 'http://localhost:9200/_snapshot/snapshot1' -d '{
    "type": "fs",
    "settings": {
        "location": "/usr/local/share/elasticsearch_snapshot/snapshot1",
        "compress": true
    }
}'
{"acknowledged":true}

# 確認
$ ls -l /usr/local/share/elasticsearch_snapshot
合計 4
drwxr-xr-x 2 root root 4096  1月 15 22:08 snapshot1

# バックアップ(スナップショットの取得)
# ここでは、「areas」インデックスのスナップショットを、「areas-2017.01.15」に取得する
$ curl -XPUT 'http://localhost:9200/_snapshot/snapshot1/areas-2017.01.15?wait_for_completion=true' -d '{
    "indices": "areas",
    "ignore_unavailable": true,
    "include_global_state": false
}'
{"snapshot":{"snapshot":"areas-2017.01.15","indices":["areas"],"state":"SUCCESS","start_time":"2017-01-15T13:13:55.213Z","start_time_in_millis":1484486035213,"end_time":"2017-01-15T13:13:55.353Z","end_time_in_millis":1484486035353,"duration_in_millis":140,"failures":[],"shards":{"total":5,"failed":0,"successful":5}}}

# ディレクトリ確認
$ ls -l /usr/local/share/elasticsearch_snapshot/snapshot1
合計 16
-rw-r--r-- 1 root root   39  1月 15 22:13 index
drwxr-xr-x 3 root root 4096  1月 15 22:13 indices
-rw-r--r-- 1 root root   61  1月 15 22:13 metadata-areas-2017.01.15
-rw-r--r-- 1 root root  183  1月 15 22:13 snapshot-areas-2017.01.15

# スナップショット確認
$ curl -XGET 'http://localhost:9200/_snapshot/snapshot1/areas-2017.01.15'
{"snapshots":[{"snapshot":"areas-2017.01.15","indices":["areas"],"state":"SUCCESS","start_time":"2017-01-15T13:13:55.213Z","start_time_in_millis":1484486035213,"end_time":"2017-01-15T13:13:55.353Z","end_time_in_millis":1484486035353,"duration_in_millis":140,"failures":[],"shards":{"total":5,"failed":0,"successful":5}}]}

使用备份来尝试恢复。

$ curl -XPOST 'http://localhost:9200/_snapshot/snapshot1/areas-2017.01.15/_restore' -d '{
    "indices": "areas",
    "include_global_state": false
}'
{"error":"SnapshotRestoreException[[snapshot1:areas-2017.01.15] cannot restore index [areas] because it's open]","status":500}

由于索引已登记,因此无法进行覆写,所以先删除。

$ curl -XDELETE http://localhost:9200/areas
{"acknowledged":true}

进行再次恢复。

$ curl -XPOST 'http://localhost:9200/_snapshot/snapshot1/areas-2017.01.15/_restore' -d '{
    "indices": "areas",
    "include_global_state": false
}'
{"accepted":true}
bannerAds