あきろぐ

いろいろめもするよ🐈🐈🐈

Elasticsearchが立ち上がらないときの対処法

どんなエラーが発生したのか?

Elasticsearchを起動させようとすると失敗する。

$ sudo systemctl start elasticsearch
● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sun 2019-06-09 06:41:29 UTC; 6min ago
     Docs: http://www.elastic.co
  Process: 5538 ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet -Edefault.path.logs=${LOG_DIR} -Edefault.path.data=${DATA_DIR} -Edefault.path.conf=${CONF_DIR} (code=exited, status=1/FAILURE)
  Process: 5537 ExecStartPre=/usr/share/elasticsearch/bin/elasticsearch-systemd-pre-exec (code=exited, status=0/SUCCESS)
 Main PID: 5538 (code=exited, status=1/FAILURE)

環境

  • Elasticsearch5.6
  • vagrant2.2.3
  • CentOS7.6

対処法

Elasticsearchのステータスの詳細を表示

上記の情報だけではよく分からないので、どこで失敗しているか詳細を見てみます。

$sudo systemctl status elasticsearch -l
[中略] 
Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: #
Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: # There is insufficient memory for the Java Runtime Environment to continue.
Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: # Native memory allocation (mmap) failed to map 2060255232 bytes for committing reserved memory.
Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: # An error report file with more information is saved as:
Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: # /tmp/hs_err_pid5538.log
Jun 09 06:41:29 localhost.localdomain systemd[1]: elasticsearch.service: main process exited, code=exited, status=1/FAILURE
Jun 09 06:41:29 localhost.localdomain systemd[1]: Unit elasticsearch.service entered failed state.
Jun 09 06:41:29 localhost.localdomain systemd[1]: elasticsearch.service failed.

Javaを起動するために必要なメモリが不十分であることが判明。

Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: #
Jun 09 06:41:28 localhost.localdomain elasticsearch[5538]: # There is insufficient memory for the Java Runtime Environment to continue.

今回使用しているVMの搭載メモリは4GBでElasticsearchには2GB割り当てるように設定していたのですが、そんなに空きがなかったことが起動に失敗する原因でした。

設定ファイルの修正

Elasticsearchの割り当てメモリの変更(ヒープサイズ)は、"/etc/elasticesearch/jvm.options"で行います。
XmsとXmxの値を2GBから1GBに書き換えました。

$sudo vi /etc/elasticesearch/jvm.options
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms1g
-Xmx1g

公式ドキュメントにも記載がありますが、Xms(最小ヒープサイズ)とXmx(最大ヒープサイズ)は等しい値にする必要があります。詳しくは以下を参照してください。
www.elastic.co

Elasticsearchを再起動させる

設定ができたら、Elasticsearchを再起動させてステータスを確認します。ステータスがactiveになっていたらOKです。

$sudo systemctl restart elasticsearch
$systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-06-09 06:53:47 UTC; 13s ago
     Docs: http://www.elastic.co
  Process: 6279 ExecStartPre=/usr/share/elasticsearch/bin/elasticsearch-systemd-pre-exec (code=exited, status=0/SUCCESS)
 Main PID: 6280 (java)
   CGroup: /system.slice/elasticsearch.service
           mq6280 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInit...

Jun 09 06:53:47 localhost.localdomain systemd[1]: Starting Elasticsearch...
Jun 09 06:53:47 localhost.localdomain systemd[1]: Started Elasticsearch.
Jun 09 06:53:47 localhost.localdomain elasticsearch[6280]: OpenJDK 64-Bit Ser...
Hint: Some lines were ellipsized, use -l to show in full. 

動作確認

これでElasticsearchは正常に使えるはずなので、curlコマンドを使ってElasticsearchからレスポンス返ってくるか確認します。

$ curl http://0.0.0.0:9200
{
  "name" : "WTDjnDZ",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "f_ZGcQzRQGyhlqOA8XLVOA",
  "version" : {
    "number" : "5.6.16",
    "build_hash" : "3a740d1",
    "build_date" : "2019-03-13T15:33:36.565Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"

今回は、elasticsearchのymlファイルにて"network.host"を"0.0.0.0"に設定しているので、curl先は0.0.0.0にしています。ローカルホストを指定している場合は、"curl localhost:9200"でレスポンス返ってくるはずです。

curlを実行して以下のようなコネクションエラーが発生した場合は、"/etc/elasticsearch/elasticsearch.yml"ファイルのnetwork.hostを確認してください。

curl: (7) Failed connect to localhost:9200; Connection refused

以上です。