やさぐれSEの技術メモ

皆が辛い思いをしないようにハマったポイントなどを書きます

Elasticsearchのインストール及び設定 その4 Config(yml)の修正及びElasticsearchの起動

Elasticsearchの設定をしていきます。

構成としては前回解説したもので、さらにIPアドレスと内部通信用ポート(93XX)、API用ポート(92XX)を下図のような感じで作成しようと思います。

SIer出身なのでなるべく設定は統一し、拡張性を考えた感じとしています。
ポートも気にされるお客様が多いので固定かつノードの種類毎に割り振りを考えています。

f:id:gitya107:20180513160318p:plain

各種ディレクトリの作成

Elasticsearchの設定で以下の2つのディレクトリが必要となります。

RHEL#1~#5それぞれのサーバに設定が必要です。

データ格納用ディレクトリの作成

Elasticsearchのデータが格納されるディレクトリを作成します。
Configではpath.dataという定義で指定されます。
1つでも複数でも良く、データの配分等々はElasticsearchが自動的に実施してくれます。
結構なディスクI/Oがあるようなので、ディスク(RAID)毎にディレクトリを作成した方が良いです。

(1)ディレクトリの作成
# mkdir -p /data

(2)所有者及び権限の変更

# chown -R elasticsearch:elasticsearch /data
#chmod -R o-rx /data

スナップショット用ディレクトリの格納

スナップショットを作成するためにはConfigのpath.repoという定義を設定する必要があります。
スナップショットはNFSサーバの共有ディレクトリに取得する必要があります。
ただし、常時NFSをマウントするより必要時にマウントする方が良いと思われます。
そのため、path.repoにはマウントポイントを設定するのが良いと思われます。

(1)ディレクトリの作成
# mkdir -p /snapshot

(2)所有者及び権限の変更

# chown -R elasticsearch:elasticsearch /snapshot
# chmod -R o-rx /snapshot

Config(yml)の修正

RHEL#1~#5それぞれのサーバに設定が必要です。

全ての定義をConfigに記述しても良いのですが、今回1インスタンス内に複数ノードを起動する想定のため各ノードの共通設定をConfigに記述します。
※全て起動コマンドに記述する方法もあります。

(1)Configのバックアップ
# cp -p /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.org

(2)バックアップとオリジナルの差分がないことを確認 # diff /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.org

(3)Configの修正 # vi /etc/elasticsearch/elasticsearch.yml

【解説】:私の所見を記述しています。
【追加】:オリジナルのConfigに無い定義を追加しています。
【変更】:デフォルト値から変更したものを記述しています。

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# 【解説】クラスタに関する設定項目
# ---------------------------------- Cluster -----------------------------------    
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application

# 【解説】【変更】任意のクラスタ名を設定します。
cluster.name: クラスタ名

# 【解説】【追加】同一インスタンス内にシャードを作成しないための設定
#    複数ノード/インスタンスのインスタンスダウン時に2重障害にならないようにする設定です。
cluster.routing.allocation.awareness.attributes: host

#
# 【解説】ノードに関する設定
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#

# 【解説】【変更】ノードにホスト名を覚え込ませる設定
#    OSのホスト名を取得して設定するようにしています。
node.attr.host: ${HOSTNAME} 

# 【解説】【追加】1インスタンス内にいくつノードを起動できるかの設定
#    こちらを設定しないと1インスタンス内に複数ノードの起動ができません。
#    今回の最大ノード数が「3」なので3を設定します。
node.max_local_storage_nodes: 3

# 【解説】各種パスに関する設定
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /var/lib/elasticsearch

#【解説】【変更】データを配置するディレクトリ
#    複数ディレクトリ指定したい場合はカンマ( , )で区切って設定します。
path.data: /data

#
# Path to log files:
#

# 【解説】elasticsearchに関するログを保存するディレクトリ
#    困ったことがあればこのログを参照すると手がかりがあります。
path.logs: /var/log/elasticsearch

#
# Path to Snapshot:
#

# 【解説】【追加】スナップショットのデータを保存するディレクトリ
#    他のディレクトリと記述の仕方が異なるので注意が必要です。
#    APIでスナップショットを作成する場合も必要となります。
path.repo: ["/snapshot"]


#
# 【解説】メモリに関する設定
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true

# 【解説】【変更】物理メモリのみ利用する設定
#     起動スクリプト側(Systemctl)でも対応が必要なので注意
#     この時点では`false`としておきます。
bootstrap.memory_lock: false

#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# 【解説】ネットワークに関する設定
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1

# 【解説】【変更】バインドIPアドレス
#     ネットワークインタフェースが複数ある場合は、どのIPからの接続を許可するか記述する。
#    (全部OKにする場合は 0.0.0.0)
network.host: 各インスタンスのIPアドレス(19.168.0.1~5)

#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#

# 【解説】クラスタを構成するための要素を設定する項目
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]

# 【解説】【変更】マスター適格ノードの指定
#        マスター適格ノード配置するインスタンスのIPアドレスとノードのポート番号を指定します。
#        例では3つのマスター適格ノードを想定しています。
discovery.zen.ping.unicast.hosts: ["192.168.0.1:9390", "192.168.0.2:9390", "192.168.0.3:9390"]

#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes:

# 【解説】【変更】マスター適格ノードの最定数
#     マスター適格ノードの障害に備え、どこを最低数とするか指定をします。
#     「discovery.zen.ping.unicast.hostsに指定したノード数 / 2 +1 」で計算します。
#     この数を下回るとElasticsearchの機能が停止します。
discovery.zen.minimum_master_nodes: 2

#
# For more information, consult the zen discovery module documentation.
#
# 【解説】ゲートウェイに関する設定
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# 【解説】その他設定
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true 

# 【解説】【変更】インデックスを指定しないと削除できなくするオプション
#     x-packを導入すると管理用インデックスが作成されます。(「.monitering」等)
#     インデックス全削除APIなどあるのですが、管理用インデックスを削除してしまう事故が
#     発生しないようにこちらを有効にすることをオススメします。
action.destructive_requires_name: true  

# 【解説】X-Pack設定(通常のConfigには記述されていないので、追記する必要があります)
# ---------------------------------- X-Pack ----------------------------------- 

# 【解説】x-packのセキュリティを有効化
xpack.security.enabled: true

# 【解説】セキュリティ有効化にするとTLS設定を有効化する必要がある
xpack.security.transport.ssl.enabled: true

# 【解説】TLSの認証モードの指定
xpack.security.transport.ssl.verification_mode: certificate

# 【解説】TLSのキー設定 
# 「Elasticsearchのインストール及び設定 その2 TLS設定」で作成した認証キーを指定します。
xpack.security.transport.ssl.keystore.path: /etc/elasticsearch/certificate/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: /etc/elasticsearch/certificate/elastic-certificates.p12

# 【解説】モニタリングの設定
# ※この時点ではfalseにしておきます。
xpack.monitoring.enabled: false

# 【解説】ウォッチャーの設定
# ※この時点ではfalseにしておきます。
xpack.watcher.enabled: false

# 【解説】機械学習の設定
# GOLDライセンスには機械学習が含まれないためfalseとします。
xpack.ml.enabled: false

起動コマンドによる起動

上記のElasticsearchのConfig作成が完了したら、以下のコマンドから各ノードの起動を実施します。
コマンドで設定する内容としてはポートやノード名などConfigで共通定義できないものとしています。
(全部コマンドで設定する人も居るようです)

次回以降で起動コマンドのsystemctlについて記載しますので、最終的にはもっと楽になります。

「elasticsearch」コマンドでノードの起動を実施します。
※長いので改行して見やすくしています。実行時は1行のコマンドに修正してください。

sudo -u elasticsearch ES_JAVA_OPTS="JAVAメモリ設定" /etc/elasticsearch/bin/elasticsearch 
-d -Ehttp.port=API用ポート番号 
-Etransport.tcp.port=内部通信用ポート番号 
-Enode.master=マスターノードかどうか 
-Enode.data=データノードかどうか 
-Enode.name=ノード名

各サーバで実行するコマンドは今回の環境では下記のようになります。
結構なコマンド数になるのでsystemctlに登録した方がいいです。

(1)RHEL#1

・マスターノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms8g -Xmx8g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9290  -Etransport.tcp.port=9390 -Enode.master=true -Enode.data=false -Enode.name=master

・データノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms30g -Xmx30g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9210  -Etransport.tcp.port=9310 -Enode.master=false -Enode.data=true -Enode.name=data

(2)RHEL#2

・マスターノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms8g -Xmx8g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9290  -Etransport.tcp.port=9390 -Enode.master=true -Enode.data=false -Enode.name=master

・データノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms30g -Xmx30g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9210  -Etransport.tcp.port=9310 -Enode.master=false -Enode.data=true -Enode.name=data

(3)RHEL#3

・マスターノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms8g -Xmx8g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9290  -Etransport.tcp.port=9390 -Enode.master=true -Enode.data=false -Enode.name=master

・データノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms30g -Xmx30g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9210  -Etransport.tcp.port=9310 -Enode.master=false -Enode.data=true -Enode.name=data

・クライアントノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms8g -Xmx8g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9200  -Etransport.tcp.port=9300 -Enode.master=false -Enode.data=false -Enode.name=client

(4)RHEL#4

・データノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms30g -Xmx30g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9210  -Etransport.tcp.port=9310 -Enode.master=true -Enode.data=false -Enode.name=master

・クライアントノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms8g -Xmx8g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9200  -Etransport.tcp.port=9300 -Enode.master=false -Enode.data=false -Enode.name=client

(5)RHEL#5

・データノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms30g -Xmx30g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9210  -Etransport.tcp.port=9310 -Enode.master=true -Enode.data=false -Enode.name=master

・クライアントノード

# sudo -u elasticsearch ES_JAVA_OPTS="-Xms8g -Xmx8g" /etc/elasticsearch/bin/elasticsearch -d -Ehttp.port=9200  -Etransport.tcp.port=9300 -Enode.master=false -Enode.data=false -Enode.name=client

起動後の確認

この状態ですと次の「ビルトインアカウントのパスワード変更」をしないとElasticsearchのAPIが利用(認証)できないです。
そのため、起動しているかどうかは「java」のプロセス数で確認してください。

# ps -ef | grep java | grep -v grep | wc -l

上記のコマンドで起動したノード数と一致していれば大丈夫です。