技術(tech)

EC2+ELK環境構築

やること

  1. EC2を使ってサーバを建てる
  2. 建てたサーバ上にELKをインストールする
  3. Kibanaからブラウザを確認できるように、ファイアウォールの設定をする
  4. ブラウザからKibanaの動作確認をする(とりあえずここまで実施する)
  5. Logstashからログを読み込み、Elasticsearchへ登録する

動作環境

  • Elasticsearch 6.5
  • Kibana 6.5
  • Logstash 6.5

それではまずEC2でサーバを建てます

  • Launch Instanceを選択しますスクリーンショット 2018-12-11 23.56.04.png
  • とりあえず今回はRed Hat EnterPrise Linux7.6を選択しますスクリーンショット 2018-12-11 23.56.52.png
  • 無料枠を使用するのでt2 microを選択してReview and Launchを選択してサーバを建てますスクリーンショット 2018-12-11 23.57.09.png

建てたサーバ上にELKをインストールします

1で建てたサーバへsshでログインします

  • 対象サーバのインスタンスを右クリックして「Connect」を選択します
    スクリーンショット 2018-12-12 0.10.36.png
  • Exsample欄にあるssh以下をコピペして、Terminalに貼り付けてやります
    ※別途公開鍵の設定が必要になるかも・・・
    スクリーンショット 2018-12-12 0.11.39.png
  • EC2で建てたサーバにssh接続できました
  • JAVAインストール
sudo yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64

[ec2-user@ip-172-31-35-102 ~]$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
  • ELKをそれぞれインストールします
yum install elasticsearch
yum install kibana
yum install logstash

無料枠のメモリが小さすぎてデフォルトの設定のまま起動すると、サービスが起動しないので、メモリの割当を変更します

  • logstash: 1G → 128MB
  • Elasticsearch: 1G → 256MB
## JVM configuration

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms128m
-Xmx128m
# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms256m
-Xmx256m

外部からKibanaへアクセスできるように

# 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: "localhost"
server.host: "0.0.0.0"
  • ELKを起動します
sudo systemctl start elasticsearch
sudo systemctl start kibana

sudo systemctl start logstash
Failed to start logstash.service: Unit not found.

logstashだけ起動しない・・・
→systemctlから起動するためには、以下のコマンドを実行する必要があったみたいです・・・

/usr/share/logstash/bin/system-install

再度実施

/usr/share/logstash/bin/system-install

[ec2-user@ip-172-31-35-102 ~]$ sudo systemctl start logstash
[ec2-user@ip-172-31-35-102 ~]$ sudo systemctl status logstash
● logstash.service - logstash
   Loaded: loaded (/etc/systemd/system/logstash.service; disabled; vendor preset: disabled)
   Active: active (running) since 水 2018-12-12 10:34:14 UTC; 2min 40s ago
 Main PID: 9856 (java)
   CGroup: /system.slice/logstash.service
           └─9856 /bin/java -Xms256M -Xmx256M -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFrac...

12月 12 10:34:14 ip-172-31-35-102.ap-northeast-1.compute.internal systemd: logstash.service holdoff time over, ...t.
12月 12 10:34:14 ip-172-31-35-102.ap-northeast-1.compute.internal systemd: Stopped logstash.
12月 12 10:34:14 ip-172-31-35-102.ap-northeast-1.compute.internal systemd: Started logstash.
12月 12 10:34:14 ip-172-31-35-102.ap-northeast-1.compute.internal logstash[9856]: OpenJDK 64-Bit Server VM warning:...N
Hint: Some lines were ellipsized, use -l to show in full.

無事に起動しました。

  • Elasticsearchの動作確認
[ec2-user@ip-172-31-35-102 ~]$ curl http://localhost:9200
{
  "name" : "unxoSpU",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "uqXWS77mQH2FsxeXk63yUQ",
  "version" : {
    "number" : "6.5.2",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "9434bed",
    "build_date" : "2018-11-29T23:58:20.891072Z",
    "build_snapshot" : false,
    "lucene_version" : "7.5.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}
  • kibanaの動作確認
[ec2-user@ip-172-31-35-102 ~]$ curl http://localhost:5601
<script>var hashRoute = '/app/kibana';
var defaultRoute = '/app/kibana';

var hash = window.location.hash;
if (hash.length) {
  window.location = hashRoute + hash;
} else {
  window.location = defaultRoute;
}</script>
  • logstashの動作確認
[ec2-user@ip-172-31-35-102 ~]$ sudo /usr/share/logstash/bin/logstash -e 'input { stdin { } } output { stdout {} }'

hello
[INFO ] 2018-12-13 09:33:37.114 [Converge PipelineAction::Create<main>] pipeline - Starting pipeline {:pipeline_id=>"main", "pipeline.workers"=>1, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>50}
[INFO ] 2018-12-13 09:33:37.407 [Converge PipelineAction::Create<main>] pipeline - Pipeline started successfully {:pipeline_id=>"main", :thread=>"#<Thread:0x37addd8f run>"}
[INFO ] 2018-12-13 09:33:37.503 [Ruby-0-Thread-1: /usr/share/logstash/lib/bootstrap/environment.rb:6] agent - Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
The stdin plugin is now waiting for input:
{
      "@version" => "1",
    "@timestamp" => 2018-12-13T09:33:37.614Z,
       "message" => "hello",
          "host" => "ip-172-31-35-102.ap-northeast-1.compute.internal"
}
[INFO ] 2018-12-13 09:33:38.507 [Api Webserver] agent - Successfully started Logstash API endpoint {:port=>9600}
hhh
{
      "@version" => "1",
    "@timestamp" => 2018-12-13T09:33:45.914Z,
       "message" => "hhh",
          "host" => "ip-172-31-35-102.ap-northeast-1.compute.internal"
}
^C[WARN ] 2018-12-13 09:33:51.238 [SIGINT handler] runner - SIGINT received. Shutting down.
[INFO ] 2018-12-13 09:33:51.594 [[main]>worker0] pipeline - Pipeline has terminated {:pipeline_id=>"main", :thread=>"#<Thread:0x37addd8f run>"}
  • 外のIPアドレスからアクセスしようとすると・・・
    ポートを開けていないため、閲覧できません。
    次の手順を実施すると、外のIPアドレスから閲覧できます。

Kibanaからブラウザを確認できるように、ファイアウォールの設定をします

  • Security Groupsを選択して、ポートの設定をします
    スクリーンショット 2018-12-12 0.54.10.png
  • 「edit」を選択します
    スクリーンショット 2018-12-12 0.54.32.png
  • とりあえずTCPの20番ポートと5601番ポート(Kibana用)を空けます
    動作確認のために、すべてのIPアドレスからのアクセスを許可していますが、セキュリティガバガバなので、実際に使うときには気をつけてください
    スクリーンショット 2018-12-12 0.54.53.png

ここまで実施すると、サーバを建てた際に設定されたグローバルIPアドレスからアクセスすることで、Kibanaへ接続できると思います。

手元のブラウザからKibanaの動作確認をします

設定されたグローバルIPアドレスと5601番ポートを指定して、Kibanaにアクセスします。

http://[IPアドレス]:5601

  • 以下の画面が表示されれば問題ありません
    スクリーンショット 2018-12-12 1.09.04.png
  • Elasticsearch、Logstash、Kibanaを同時起動しようとすると、Elasticsearchだけメモリが足りずに落ちる・・・

⇩リベンジしてみました
https://qiita.com/gogogonkun/items/c6536038e840c9838dbf

  • 結局、メモリ1GBのサーバだけでELKを同時起動するのは無理でした
  • 次は、サーバ2台使ってやってみよ