Try   HackMD
tags: Elastic Stack

Elastic stack(安裝篇 via Docker)

一、前情提要

前一篇 有透過rpm的方式分別安裝Elasticsearch, Logstash, Kibana, 以及Filebeats,
但官方其實也有提供Docker image讓各位可以直接在Docker Engine上建立Container。

因此在這篇就會利用Docker的方式來建立起整個Elastic stack。
再次幫大家回憶一下Elastic stack的架構:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

二、安裝篇

透過Docker就簡單多啦,不過要特別留意,
由於Elastic stack的所有Component都要互通,
要先設定一個network讓所有的Container吃同一個網路如下:

docker create network elastic_stack

安裝Elasticsearch

docker pull

docker pull docker.elastic.co/elasticsearch/elasticsearch:7.4.2

docker run

docker run -d name elasticsearch net elastic_stack -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.4.2

測試是否成功運行

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

安裝Kibana

docker pull

docker pull docker.elastic.co/kibana/kibana:7.4.2

docker run

docker run -d name kibana net elastic_stack -p 5601:5601 docker.elastic.co/kibana/kibana:7.4.2

測試是否成功運行

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

安裝Filebeat

接下來安裝Filebeat與Logstash的部份會稍微有點麻煩,
有一些必備的設定檔需要建立與處理,請各位耐心跟上。
(應該說前面Elasticsearch跟Kibana在預設的設定就可以符合大半的需求)

首先是Filebeat的部份:

docker pull

docker pull docker.elastic.co/beats/filebeat:7.4.2

接著要在kibana建立index pattern及一些基本的圖型

docker run net=elastic_stack docker.elastic.co/beats/filebeat:7.4.2 setup -E setup.kibana.host=kibana:5601 -E output.elasticsearch.hosts=["elasticsearch:9200"]

成功的話會顯示如下的畫面:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

如上圖,這個部份其實是在將Filebeat相關的一些Index Pattern及Visualization建到Kibana,
之後資料進到Elasticsearch的時候就可以直接匯整。

再來要做的是設定Filebeat讀取的Log路徑:

  1. 在你喜歡的路徑下建立filebeat/filebeat.yml檔案,內容如下:
filebeat.config:
  modules:
    path: ${path.config}/modules.d/*.yml
    reload.enabled: false

filebeat.inputs:
  - type: log
    paths:
      - /var/log/elk-sample.log

output.logstash:
  hosts: ["logstash:5044"]

  1. 啟動docker時設定volume mount:

docker run -d net=elastic_stack name=filebeat user=root volume="$(pwd)/filebeat.yml:/usr/share/filebeat/filebeat.yml:ro" volume="/var/lib/docker/containers:/var/lib/docker/containers:ro" volume="/var/run/docker.sock:/var/run/docker.sock:ro" volume="/var/log:/var/log" docker.elastic.co/beats/filebeat:7.4.2 filebeat -e -strict.perms=false

安裝Logstash

docker pull

docker pull docker.elastic.co/logstash/logstash:7.4.2

再來要做的是設定Logstash的Input/Filter/Output:

  1. 在你喜歡的路徑下建立logstash/pipline/logstash.conf檔案,內容如下:
input {
  beats {
    port => 5044
  }
}

filter {
  grok {
    match => {
      "message" => "%{GREEDYDATA:result}"
    }
  }
  json {
    source => "result"
  }
  mutate {
    remove_tag => ["_jsonparsefailure"]
  }
}

output {
  elasticsearch {
    hosts => "elasticsearch:9200"
    index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
  }
}

  1. 接著就是啟動logstash,一樣要設定volume mount:

docker run -d -p 5044:5044 -p 9600:9600 name logstash net elastic_stack -v $(pwd)/pipeline/:/usr/share/logstash/pipeline/ docker.elastic.co/logstash/logstash:7.4.2

以上的步驟都完成後就大功告成了,
接著只要將這個檔案放到/var/log/底下就可以去Kibana看結果:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

三、結語

以上就是透過Docker建立起Elastic stack的部份,
如果對Docker不熟的人可以先去稍微研究一下docker run的部份喔!