Loading...
ElasticsearchIT

おうちロガーで学ぶ統合ログ管理ソフトElasticsearch [Elasticsearchのインストール]【v7.3.0版】

みなさんこんにちは、szkです。

elasticsearchのVersionがv7になってから久しいですが、少しelasticsearch.ymlの描き方も変わったのでメモがてらリライトしておきます。

この記事の続きもかかないとな・・・

この記事の元となった記事

この記事はv6.6.1の時に記載した記事のアップデート記事となります。

前段等は元記事をご覧ください。

おうちロガーで学ぶ統合ログ管理ソフトElasticsearch [Elasticsearchのインストール]【v6.6.1版】

Elasticsearchとは

Elasticという会社が開発を進めているオープンソースの検索エンジンです。
Javaベースの検索エンジン「ApacheLucene」をベースに、API化と並列処理に特化したものがElasticsearchとなります。
また、Elastic社の開発するElasticsearchを含むソフトウェア軍を「ElastiStack」といい、WebGUI上で可視化するソフトウェアが「Kibana」です。
他にも複数のソフトウェアがありますが、今回は主にElasticsearchとKibanaを使用していきます。

使用する環境

今回の環境の紹介です。

■MasterNode

OS:CentOS7.6(ESX6.5上のVM)
CPU:1vCPU
Mem:2GB(JVM1GB)

■DataNode

OS:CentOS7.6(ESX6.5上のVM)
CPU:1vCPU
Mem:4GB(JVM2GB)

Elasticsearch:Version7.3.0(2019/08/01)
Kibana:Version7.3.0(2019/08/01)

※HDDはOSは60GBもあれば十分ですがElasticsearchのインデックスデータを補完する領域は多めにとっておくと多くのデータを保管できます。

インストール

インストールはメーカーサイトを参照するのがおすすめです。
なぜならElasticsearchはバージョンアップが激しく、インストールやコンフィグレーションが1年経つと陳腐化します。
この記事も来年の今頃には古くなっているはず。。。ですが、といってもそれではあまりにも投げやりですし
メーカーのインストールガイドも少し見づらかったりするので参考程度に記載しておきます。

※前提条件

インストールに前提条件があります。気を付けてください。
前提条件はリリースノートに記載があります。

いくつか条件がありますが、「とりあえずインストールできる+動く」ということであれば
Version7.3.1の場合「Java OpenJDK V1.8.0_131」以降がインストールされていることが条件となります。
ただ、v7からはOpenJDKがバンドルされているので意識する必要はなくなりました。

・Linux環境でのElasticsearchインストール

RPM版を使うとインストールが楽です。

cd /tmp

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.3.0-x86_64.rpm

rpm -ivh elasticsearch-7.3.0-x86_64.rpm

インストールだけであれば上記コマンドで完了します。

・Linux環境でのKibanaインストール

Kibanaも同様に

cd /tmp

wget https://artifacts.elastic.co/downloads/kibana/kibana-7.3.0-x86_64.rpm

rpm -ivh kibana-7.3.0-x86_64.rpm

 

これでインストールは完了です。

コンフィグレーション(Elasticsearch)

Elasticsearchのコンフィグレーションを行います。
設定の前に設計が必要ですが、主に気を付ける点は以下の通り

・クラスタ構成かどうか

前回の記事ではシングルで行きましたが、今回はクラスター構成で行きます。

→Elasticsearchはデータを「index」という単位で保持しますが、クラスタ構成の場合は複数のノードで管理を行います。
もちろん、シングルノードの場合は1つが停止した時点でサービスがダウンするので注意が必要です。
また、クラスタ構成の場合は以下の役割が必要となります。

1.MasterNode
→クラスターを制御するノードです。マスターの機能を持ったノードが1台もいなくなるとサービス停止となります。
推奨は3台です。このノードはデータを保管しません。
今回Masterの冗長性は無しで1台用意します。

2.DataNode
→データを保管し、検索の際に問合せに応答するノードです。このノードが2台以上あることでデータの冗長性が取られます。
今回はデータの冗長は取りたいため3台用意します。

3.CoodinatingNode
→クラスタ構成の場合、データを投入及び検索する際にロードバランサとして機能するノードです。
推奨台数はありませんが、1台で構成するとこのノードが消失したときにサービスが停止します。
このノードはマスターノードとしてもデータノードとしても機能しません。
今回はKibanaの問い合わせのリーディングノードとして1台用意します。

また、v6.6.1時点で存在していた「マスターとデータノードを兼用するとメモリスワップを引き起こして停止するバグ」はv7でも健在です。
クラスタ構成で冗長性を考慮して真面目に組むと「マスター3台、データ2台」で最低5台のマシンが必要になります。

今回はバグを引かないためにマスターノードとデータノードは分けて構築します。

構成まとめ

Master x1
Data x3
Coodinate(Kibana兼用) x1

・データディレクトリの設定

データディレクトリの指定は結構大事です。
あらかじめデータ容量の多いドライブをマウントしておきましょう。
ちなみに帯域が確保できるならCifsなどをマウントしても動作します。

・ネットワーク設定

デフォルトのネットワーク設定では127.0.0.1以外のアクセスはシャットアウトされています。
メーカー推奨値というわけではありませんが、利便性的には0.0.0.0(全て許可)にしたうえでFirewalldで制御したほうがスマートでしょう。

・クラスタ設定

ここがメインの設定になると思います。

# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["192.168.1.40"]←クラスターのシードホストとなるノード(今回はMasterを1台指定)
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["ktk-es-lin01.ktk.szkhaven.com"]←イニシャルマスターノード(基本マスターになるノードを指定)
#
# For more information, consult the discovery and cluster formation module documentation.

v7からここの記載が変わってます。
v6まではマスターノードの一覧を記載し、MasterNodeの最低稼働数を記載する設定でしたが、v7からは最低稼働数は自動的に設定され
クラスターに参加するタイミングで自動設定してくれるシードホストなるノードを指定します。

イニシャルマスターノードは今まで基本的にマスターでActiveになるノードが選べませんでしたが指定できるようになりました。
ただ、注意はHostsで登録されているフルドメイン記載であること。
IPアドレスでも指定可能とマニュアルには書いてますが動作しませんでした。(v7.1時点)

さらに、クラスタのロールの設定をelasticsearch.ymlの最下部に記載します。

Master

node.master: true
node.data: false
node.ingest: false
cluster.remote.connect: false
xpack.ml.enabled: false
node.ml: false

Data

node.master: false
node.data: true
node.ingest: false
cluster.remote.connect: false
xpack.ml.enabled: false
node.ml: false

Coodinate

node.master: false
node.data: false
node.ingest: true
cluster.remote.connect: false
xpack.ml.enabled: false
node.ml: false

補足

IngestはElasticsearch内でデータのパーシングを行うときに使う機能。
NodeMonitoring機能を使いたい場合はこれがONになってるノードが必要。今回はCoodinateに役割を渡しました。

xpack.ml.enableは機械学習機能をON/OFFを切り替える。
プラチナライセンス以上で無いと使えないので未指定でもよいっちゃよい。

まとめ

こんな感じ

# ======================== 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: ktk-cluster ←★クラスタ名
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: ${HOSTNAME} ←★このノード名、自由記載ですが左の表記でホストネームを自動的に記載できます。
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /var/lib/elasticsearch ←★データノードの場合はこのディレクトリにindexデータが置かれます。容量に注意。
#
# Path to log files:
#
path.logs: /var/log/elasticsearch
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# 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: 0.0.0.0 ←★アクセスを許可するIPアドレス。左の表記は「すべてOK」。個人的にはFirewalldで制御するほうが好き
#
# 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 this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["192.168.1.40"] ←★SeedHostの設定
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["ktk-es-lin01.ktk.szkhaven.com"] ←優先的にMasterとなるノード、フルドメイン
#
# For more information, consult the discovery and cluster formation 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 
node.master: true ←★Masterの場合True
node.data: false ←★Dataの場合True
node.ingest: false ←★Ingestの場合True
cluster.remote.connect: false ←★Monitoringの動作に必要。クラスター内で1台TrueでOK
xpack.ml.enabled: false ←★機械学習機能。デフォルトTrueなので使わない場合はFalse
node.ml: false ←★機械学習専用ノードとして動かすか。FalseでOK

・起動

CentOS7系ならばrpmインストールした段階でsystemctlが使えるはずです。

systemctl enable elasticsearch
systemctl start elasticsearch

これで起動します。

ステータスはこのようになればOKです。
ちなみに起動に少し時間(1~3分ほど)がかかります。

● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2019-03-03 20:39:22 JST; 3h 22min ago
     Docs: http://www.elastic.co
 Main PID: 80632 (java)
   CGroup: /system.slice/elasticsearch.service
           tq80632 /bin/java -Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:CMSIni...
           mq80687 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-...

 3月 03 20:39:22 hostname systemd[1]: Started Elasticsearch.
 3月 03 20:39:22 hostname elasticsearch[80632]: OpenJDK 64-Bit Server VM...
Hint: Some lines were ellipsized, use -l to show in full.

・起動しないぞ?というときは

1.JvmOptionでメモリ容量を拡張する

Memory leakうんたらとエラーが起きる場合はJVMでSwapしてる場合があります。
vi /etc/elasticsearch/jvm.optionsにてメモリの要領を拡張してあげましょう。

vi /etc/elasticsearch/jvm.options

#22行目あたり
 # Xms represents the initial size of total heap space
 # Xmx represents the maximum size of total heap space

-Xms1g →2gに
-Xmx1g →2gに

※ちなみにv5ではデフォルトが512MBとなっており高確率でこれが発生します。

v7では1Gになってます。データノードは増やしてもいいかも。

2.設定ファイルが誤っている

SystemctrlでJavaに関するエラーが起こってる場合は大体elasticsearch.ymlの設定ミスです。

コンフィグレーション(Kibana)

Kibanaも同様に/etc/kibana/kibana.ymlにて設定を変更します。
Kibanaは主にアクセスを許可するIPと参照するElasticsearchのアドレスのみです。

# Kibana is served by a back end server. This setting specifies the port to use.
server.port: 5601

# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
# The default is 'localhost', which usually means remote machines will not be able to connect.
# To allow connections from remote users, set this parameter to a non-loopback address.
server.host: "0.0.0.0" ←アクセスを許可するIP

# Enables you to specify a path to mount Kibana at if you are running behind a proxy.
# Use the `server.rewriteBasePath` setting to tell Kibana if it should remove the basePath
# from requests it receives, and to prevent a deprecation warning at startup.
# This setting cannot end in a slash.
#server.basePath: ""

# Specifies whether Kibana should rewrite requests that are prefixed with
# `server.basePath` or require that they are rewritten by your reverse proxy.
# This setting was effectively always `false` before Kibana 6.3 and will
# default to `true` starting in Kibana 7.0.
#server.rewriteBasePath: false

# The maximum payload size in bytes for incoming server requests.
#server.maxPayloadBytes: 1048576

# The Kibana server's name.  This is used for display purposes.
#server.name: "your-hostname"

# The URLs of the Elasticsearch instances to use for all your queries.
elasticsearch.hosts: "http://localhost:9200" ←Kibanaが参照するelasticsearchのIPアドレス

# When this setting's value is true Kibana uses the hostname specified in the server.host
# setting. When the value of this setting is false, Kibana uses the hostname of the host
# that connects to this Kibana instance.
#elasticsearch.preserveHost: true

# Kibana uses an index in Elasticsearch to store saved searches, visualizations and
# dashboards. Kibana creates a new index if the index doesn't already exist.
#kibana.index: ".kibana"

# The default application to load.
#kibana.defaultAppId: "home"

# If your Elasticsearch is protected with basic authentication, these settings provide
# the username and password that the Kibana server uses to perform maintenance on the Kibana
# index at startup. Your Kibana users still need to authenticate with Elasticsearch, which
# is proxied through the Kibana server.
elasticsearch.username: "    " ←elasticsearchのユーザー認証がある場合
elasticsearch.password: "    " ←elasticsearchのユーザー認証がある場合

# Enables SSL and paths to the PEM-format SSL certificate and SSL key files, respectively.
# These settings enable SSL for outgoing requests from the Kibana server to the browser.
#server.ssl.enabled: false
#server.ssl.certificate: /path/to/your/server.crt
#server.ssl.key: /path/to/your/server.key

# Optional settings that provide the paths to the PEM-format SSL certificate and key files.
# These files validate that your Elasticsearch backend uses the same key files.
#elasticsearch.ssl.certificate: /path/to/your/client.crt
#elasticsearch.ssl.key: /path/to/your/client.key

# Optional setting that enables you to specify a path to the PEM file for the certificate
# authority for your Elasticsearch instance.
#elasticsearch.ssl.certificateAuthorities: [ "/path/to/your/CA.pem" ]

# To disregard the validity of SSL certificates, change this setting's value to 'none'.
#elasticsearch.ssl.verificationMode: full

# Time in milliseconds to wait for Elasticsearch to respond to pings. Defaults to the value of
# the elasticsearch.requestTimeout setting.
#elasticsearch.pingTimeout: 1500

# Time in milliseconds to wait for responses from the back end or Elasticsearch. This value
# must be a positive integer.
#elasticsearch.requestTimeout: 30000

# List of Kibana client-side headers to send to Elasticsearch. To send *no* client-side
# headers, set this value to [] (an empty list).
#elasticsearch.requestHeadersWhitelist: [ authorization ]

# Header names and values that are sent to Elasticsearch. Any custom headers cannot be overwritten
# by client-side headers, regardless of the elasticsearch.requestHeadersWhitelist configuration.
#elasticsearch.customHeaders: {}

# Time in milliseconds for Elasticsearch to wait for responses from shards. Set to 0 to disable.
#elasticsearch.shardTimeout: 30000

# Time in milliseconds to wait for Elasticsearch at Kibana startup before retrying.
#elasticsearch.startupTimeout: 5000

# Logs queries sent to Elasticsearch. Requires logging.verbose set to true.
#elasticsearch.logQueries: false

# Specifies the path where Kibana creates the process ID file.
#pid.file: /var/run/kibana.pid

# Enables you specify a file where Kibana stores log output.
#logging.dest: stdout

# Set the value of this setting to true to suppress all logging output.
#logging.silent: false

# Set the value of this setting to true to suppress all logging output other than error messages.
#logging.quiet: false

# Set the value of this setting to true to log all events, including system usage information
# and all requests.
#logging.verbose: false

# Set the interval in milliseconds to sample system and process performance
# metrics. Minimum is 100ms. Defaults to 5000.
#ops.interval: 5000

# Specifies locale to be used for all localizable strings, dates and number formats.
#i18n.locale: "en"

・起動

systemctl enable kibana
systemctl start kibana

これで起動できます。こいつもまた起動に時間がかかります。

・Kibana server is not ready yetと表示される

elasticsearchクラスタが動いてない可能性が高いです。

以下をチェックしましょう。

・curl http://localhost:9200/_cat/nodes?vでノード一覧を取得。動いてないクラスタが無いか確認。
・オールインワン構成の場合はJVMのメモリ容量が足りてない場合が多い、var/log/messagesを確認

また、v6からアップデートした場合は以下の不具合を踏んでいる可能性があります。

Elastic Kibana v7.3.0でSystemctrlを用いた起動ができず再起動を繰り返す。

それとKibanaはElasticsearchの起動後に起動してください。

アクセスするときはポート番号5601を忘れずに。ブラウザでKibanaにアクセスしてみましょう。

この画面が出てくればOKです!

 

とりあえず今日はここまで。

次回の記事:elasticsearchで始めるおうちロガー:インデックスの作成とデータの投入の基本