前提条件
CentOS 7、JDK 8の環境が必要です。
ELKスタックとは?
ELKスタックは、Elasticsearch、Logstash、Kibanaの3つのオープンソースツールを組み合わせたログ解析プラットフォームです。
- Elasticsearch: 分散型検索エンジンで、ログデータの格納と高速検索を提供します。
- Logstash: ログ収集・変換ツールで、さまざまなソースからデータを取得し、処理してElasticsearchに送信します。
- Kibana: Elasticsearchに保存されたデータを可視化するためのWebインターフェースです。
Elasticsearchのインストール手順
1. パッケージのダウンロード
公式サイトよりパッケージを取得します:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.8.0-linux-x86_64.tar.gz
2. 展開とディレクトリ構成
# インストールディレクトリ作成
mkdir /opt/elasticsearch
# パッケージ展開
tar -xzf elasticsearch-7.8.0-linux-x86_64.tar.gz -C /opt/elasticsearch/
3. 設定ファイルの編集
cd /opt/elasticsearch/elasticsearch-7.8.0/
vim config/elasticsearch.yml
以下の設定を追加または変更:
cluster.name: my-cluster
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]
4. セキュリティ設定
rootユーザーでは起動できないため、専用ユーザーを作成:
# 新規ユーザー作成
useradd elastic-user
# パスワード設定
passwd elastic-user
# ディレクトリ権限変更
chown -R elastic-user:elastic-user /opt/elasticsearch/
5. 起動プロセス
su - elastic-user
/opt/elasticsearch/elasticsearch-7.8.0/bin/elasticsearch -d
6. システム制限の調整
rootユーザーで以下の設定を実施:
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 131072" >> /etc/security/limits.conf
echo "* soft nproc 4096" >> /etc/security/limits.conf
echo "* hard nproc 4096" >> /etc/security/limits.conf
カーネルパラメータの調整:
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p
7. ポート開放
firewall-cmd --permanent --add-port=9200/tcp
firewall-cmd --reload
Kibanaのセットアップ
1. パッケージ取得
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.8.0-linux-x86_64.tar.gz
2. インストールと構成
tar -xzf kibana-7.8.0-linux-x86_64.tar.gz -C /opt/
mv /opt/kibana-7.8.0-linux-x86_64 /opt/kibana-7.8.0
3. 設定ファイル変更
vim /opt/kibana-7.8.0/config/kibana.yml
内容:
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
i18n.locale: "ja-JP"
4. 起動コマンド
chown -R elastic-user:elastic-user /opt/kibana-7.8.0
su - elastic-user
/opt/kibana-7.8.0/bin/kibana &
Logstashの構成
1. インストール
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.8.0.tar.gz
tar -xzf logstash-7.8.0.tar.gz -C /opt/
mv /opt/logstash-7.8.0 /opt/logstash
2. 設定ファイル作成
vim /opt/logstash/config/app-logging.conf
設定内容:
input {
tcp {
port => 9800
mode => "server"
type => "service-a"
codec => json_lines
}
tcp {
port => 9801
mode => "server"
type => "service-b"
codec => json_lines
}
}
filter {
if [message] =~ /^\s*$/ {
drop {}
}
}
output {
if [type] == "service-a" {
elasticsearch {
hosts => ["localhost:9200"]
index => "app-service-a-%{+YYYY.MM.dd}"
document_type => "_doc"
}
}
if [type] == "service-b" {
elasticsearch {
hosts => ["localhost:9200"]
index => "app-service-b-%{+YYYY.MM.dd}"
document_type => "_doc"
}
}
}
3. プラグインインストール
/opt/logstash/bin/logstash-plugin install logstash-codec-json_lines
4. 実行コマンド
/opt/logstash/bin/logstash -f /opt/logstash/config/app-logging.conf &
Spring Bootアプリケーションとの連携
1. 依存関係追加
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
2. ロガー設定
src/main/resources/logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STASH_CONNECTOR" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>192.168.1.100:9800</destination>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp/>
<logLevel/>
<loggerName/>
<message/>
<mdc/>
</providers>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STASH_CONNECTOR"/>
</root>
</configuration>
3. アプリケーションコード例
@SpringBootApplication
public class LoggingApplication implements CommandLineRunner {
private static final org.slf4j.Logger appLogger =
org.slf4j.LoggerFactory.getLogger(LoggingApplication.class);
public static void main(String[] args) {
SpringApplication.run(LoggingApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// ログ出力テスト
appLogger.info("アプリケーション開始");
for (int counter = 0; counter < 3; counter++) {
appLogger.error("エラー発生: 処理ID={}", counter);
}
}
}