26
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DockerでELK(Elasticsearch Logstash Kibana) を構築してみました

Posted at

はじめに

Docker上に、Logstashを使って、アプリケーション(NGINX)のログをElasticsearchに転送し、Kibanaで表示するまでやってみました。メモとして手順を残しておきます。

環境

-- バージョン
OS CentOS Linux release 7.5.1804 (Core)
Docker 18.06.1-ce
Elasticsearch 6.4.3
Logstash 6.4.3
Kibana 6.4.3
nginx 1.10

手順

準備

#Dockerに共有するフォルダを作成
sudo mkdir -p /data/elk
sudo chown user:user /data/elk
#elasticsearch用共有フォルダ作成
mkdir /data/elk/elasticsearch
#logstash用共有フォルダ作成
mkdir -p /data/elk/logstash/pipeline
#nginx用共有フォルダ作成
mkdir -p /data/elk/log/nginx

nginx起動

docker run -d --restart always -v /data/elk/log/nginx:/var/log/nginx -p 80:80 nginx:1.10

起動後、http://${hostip}にアクセスして確認します。

elasticsearch起動

docker run -d --restart always -v /data/elk/elasticsearch:/usr/share/elasticsearch/data -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node"  --name elasticsearch docker.elastic.co/elasticsearch/elasticsearch-oss:6.4.3

起動後、http://${hostip}:9200にアクセスして確認します。

logstash設定ファイル配置

/data/elk/logstash/pipeline/logstash.conf
input {
  file {
    path => "/var/log/nginx/access.log"
    start_position => beginning
  }
}

filter {
  grok {
    match => { "message" => ["%{IPORHOST:[nginx][access][remote_ip]} - %{DATA:[nginx][access][user_name]} \[%{HTTPDATE:[nginx][access][time]}\] \"%{WORD:[nginx][access][method]} %{DATA:[nginx][access][url]} HTTP/%{NUMBER:[nginx][access][http_version]}\" %{NUMBER:[nginx][access][response_code]} %{NUMBER:[nginx][access][body_sent][bytes]} \"%{DATA:[nginx][access][referrer]}\" \"%{DATA:[nginx][access][agent]}\""] }
    remove_field => "message"
  }
  mutate {
    add_field => { "read_timestamp" => "%{@timestamp}" }
  }
  date {
    match => [ "[nginx][access][time]", "dd/MMM/YYYY:H:m:s Z" ]
    remove_field => "[nginx][access][time]"
  }
  useragent {
    source => "[nginx][access][agent]"
    target => "[nginx][access][user_agent]"
    remove_field => "[nginx][access][agent]"
  }
  geoip {
    source => "[nginx][access][remote_ip]"
    target => "[nginx][access][geoip]"
  }
}

output {
  elasticsearch {
    hosts => [ 'elasticsearch' ]
    index => "access_log1"
  }
}

logstash起動

docker run -d --restart always -v /data/elk/logstash/pipeline:/usr/share/logstash/pipeline -v /data/elk/log:/var/log --link elasticsearch:elasticsearch  --name logstash docker.elastic.co/logstash/logstash-oss:6.4.3

起動後、http://{hostip}:9200/_cat/count/access_log1をアクセスして、ログ転送を開始したことを確認します。

kibana起動

docker run -d --restart always -p 5601:5601 --link elasticsearch:elasticsearch -e ELASTICSEARCH_URL=http://elasticsearch:9200 --name kibana docker.elastic.co/kibana/kibana-oss:6.4.3

起動後、http://{hostip}:5601にアクセスすると、kibana画面が表示されます。
左側の「Dev Tools」を開いて、「Console」に「GET /access_log1/_search?pretty=true」を入力、緑色の「▶」をクリックすると、右側にnginxのアクセスログが表示されます。

参考資料

https://qiita.com/va034600/items/b4b96faa36949b04d103
https://blog.manabusakai.com/2018/05/analyze-elb-logs-with-elk-stack/
https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

26
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?