6- Elasticsearch的安装和配置,修改配置文件(yml)并启动Elasticsearch
我曾经在即刻博客上发表过的东西。
首先 –
我們將進行Elasticsearch的配置。
作为结构的一部分,我打算按照上图的方式创建IP地址、内部通信端口(93XX)和API端口(92XX),就像上一次解释的那样。
由于我是SIer出身,所以尽量统一设置,考虑到可拓展性。
考虑到许多客户也关注端口,我正在考虑固定并根据节点类型进行分配。

创建各种目录
在Elasticsearch的配置中需要以下两个目录。
需要对RHEL#1~#5这五台服务器分别进行设置。
创建用于存储数据的目录。
创建存储Elasticsearch数据的目录。
在配置文件中,被定义为path.data。
可以创建一个或多个目录,Elasticsearch会自动分配数据。
由于涉及到相当大的磁盘I/O,最好为每个磁盘(RAID)创建一个目录。
创建目录
# mkdir -p /data
(2) 所有者和权限的变更
# chown -R elasticsearch:elasticsearch /data
# chmod -R o-rx /data
修改yml文件的配置
每个RHEL #1~#5服务器都需要设置。
虽然可以将所有定义都写在配置文件中,但是考虑到本次假设在一个实例内启动多个节点,所以将每个节点的公共设置写在配置文件中。
※也可以将所有定义写在启动命令中的方法也可以。
(1) 配置的备份
# cp -p /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.org
(2) 确认备份和原始文件没有差异
# diff /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.org
(3)对配置文件进行修改
# vi /etc/elasticsearch/elasticsearch.yml
【解说】: 记述了我的观点。
【新增】: 添加了在原始配置中没有的定义。
【修改】: 记述了从默认值进行的更改。
# ======================== 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配置后,我们将通过以下命令启动每个节点。
作为命令中的设置内容,它包括无法通过配置文件共享的端口和节点名称等。
(有些人可能会通过命令来设置所有内容)。
接下来,我将在以后的部分中介绍有关启动命令systemctl的内容,这样最终会更加轻松。
使用「elasticsearch」命令启动节点。
※为了方便阅读已换行,请在执行时将其修正为单行命令。
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
(1) 红帽企业 Linux#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 第二版
・主节点
# 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)红帽企业 Linux #3
・主节点 (zhǔ jié
# 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) 红帽企业 Linux #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 -> Red Hat企业版Linux第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
如果与使用上述命令启动的节点数相匹配,那就没问题。