Try   HackMD
tags: Elastic Stack

Elastic stack(安裝篇)

小弟最近工作需要用 Elastic Stack,整理了一下安裝的重點筆記來跟大家分享。
(註:寫完這篇之後不久就更新到 6.6.0了,但原則上安裝方法是一致的)

一、前情提要(簡介)

首先,什麼是 Elastic Stack?

Elastic stack 就是大家很常聽到的 ELK Stack,最近被正名為 Elastic stack,
而它主要由三個部份所組成,分別是 Elasticsearch、Logstash、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 →

其中最為核心的部份就是 Elasticsearch,它底層應用 Apache Lucene 做全文檢索,
且形式上是將實際資料以 JSON 格式儲存的 NoSQL 資料庫。

Logstas h則是做 Extract, Load, Transfer(ETL)
將資料從輸入(input)透過篩選機制(Filter)最後輸出(output)到elasticsearch。

Kibana 則是透過 UI 介面管理 Elasticsearch 的資料,
並進一步以視覺化的方式將數據作呈現供業務單位作分析。

除了 Elasticsearch, Logstash, Kibana 之外,
還有一個可以在架構圖中看到的就是 Beats,
它主要是作資料傳輸(Data shipping),依其傳輸型態區分 Metricbeat、Filebeat、WinLogBeat、Heartbeat… 等,
在目前需求的實際應用上我們選擇了 Filebeats。

除了以上主要的功能之外,這間公司也提供了很多的 Solution 來整合、強化這些資料的應用,
如資訊安全、商業分析、機器學習、以及最近似乎也很紅的 APM(Application Performance Monitoring)。

當然,有一些功能是要付費才能使用的。

二、主菜(實際安裝)

這個部份主要會簡單介紹如何建立起整個 Elastic stack 的循環,
我們的機器配置如下:

  • OS : CentOS 7 64-bit
  • RAM: 8G
  • CPU: 2-Core
  • HDD: 64GB

1.安裝JDK

由於Elastic stack幾乎所有的組成都是以Java開發的,所以要先安裝JDK才能正常執行。
版本上我們是選擇JDK 8(註: 官方在Elastic stack 6.5 版已支援到Java 11)
執行的指令 -> sudo yum install java-1.8.0-openjdk-devel

安裝完之後記得執行兩個指令確認有正常安裝完成:
a.java -version
b.javac -version

如下圖,都沒有顯示錯誤就代表OK囉!

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 →

2.安裝elasticsearch

Elasticsearch 這一系列的產品都有提供三種安裝方式,yum, tar, rpm,
在此我們選擇用 rpm 的方式作安裝。

a.首先先下載rpm檔

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

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 →

(註: 如果出現wget command not found,記得先用yum install wget安裝)

b.接著下載checksum

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1.rpm.sha512

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 →

c.檢查下載的檔案無誤

sha512sum -c elasticsearch-6.5.1.rpm.sha512

d.開始安裝:

sudo rpm –install elasticsearch-6.5.1.rpm

安裝完成之後也先別急著關機,還有幾個步驟要做,
確保安裝好的elasticsearch有如預期正常運作。

e.至/etc/elasticsearch/elasticsearch.yml調整host與port

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 →

f.將elasticsearch打開

sudo systemctl start elasticsearch.service

g.接著利用curl測試是否有通 -> curl -X GET “您的機器的IP: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 →

3.安裝Kibana

Kibana 的安裝跟 elasticsearch 一樣簡單,跟著步驟做就對了!

a.下載rpm

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

b.下載checksum

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

c.檢查下載的檔案無誤

sha512sum -c kibana-6.5.1-x86_64.rpm.sha512

d.安裝開始

sudo rpm –install kibana-6.5.1-x86_64.rpm
(**註:**安裝時看起來似乎卡住了,其實不然,千萬別按 ctrl+c)

一樣,安裝完別急著下班…
來驗證一下是否安裝成功吧!

e.至/etc/kibana/kibana.yml 設定 host 與 port

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 →

f.啟動kibana

sudo systemctl start kibana.service

g.打開瀏覽器,看5601 port是否有顯示畫面如下

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 →

4.安裝 Filebeat

Filebeat是資來傳輸的專家,負責幫忙監視某個資料夾內的log,
如果有任何的變化就會將內容送到指定的目的(預設是logstash)。

a.下載 rpm 檔

wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.1-x86_64.rpm

b.安裝 filebeat

sudo rpm -vi filebeat-6.5.1-x86_64.rpm

c.調整設定檔(filebeat.yml)如下

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 →

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 →

<strong style="color:red;">[注意]</strong>安裝完先別急著啟動 filebeat,它的輸出目的地 logstash 還沒安裝呢!

5.安裝Logstash:

最後要安裝的是轉換資料來源的 logstash,
logstash 的設定會有一點多,
還請各位多留意。

a.下載tar檔

wget https://artifacts.elastic.co/downloads/logstash/logstash-6.5.1.tar.gz

b.解壓縮

tar xzvf logstash-6.5.1.tar.gz

c.將解壓縮後的logstash搬到喜歡的路徑,

本人是統一和 elasticsearch, kibana 一樣放在 /etc底下。

指令: mv logstash-6.5.1 /etc/logstash
解壓縮完的目錄結構應該會是如下圖:

d.接著要設定一下 logstash 的 user/group,

打開 config/startup.options,如下修改:

因為我的電腦只有一個 root 使用者故這樣設定,
或許上 production 環境的時候該賦予 logstash 適當的 user/group。

e.開始安裝:

先切到 /etc/logstash/bin目錄下,
接著執行 -> ./system-install,
安裝完成如下圖:

f.裝完之後要針對input, filter, output做一些設定

首先先在/etc/logstash/bin下建立一個 logstash-simple.conf
並接著填入以下的內容:

如上圖,留意設定「來源」及「輸出」的部份,
至於 filte r由於比較複雜,先照著圖設定吧,
之後有機會會再詳細說明,
或是可以參考 Logstash設定

g.接著要設定啟動logstash的時候讀上面步驟設定的內容

cd /etc/systemd/system vi logstash.service

將ExecStart的部份調整如下圖:

6.全部串起來!

在裝完所有的東西之後,
就要來試試是不是真的能如想像中的流程運作了。

大致上會是如下流程:
1.Filebeat 從 log 讀取資料傳給 logstash。
2.Logstash 依據 filter 處理資料後丟給 elasticsearch。
3.Elasticsearch 會依 Logstash 傳入的資訊建立 Index。
4.透過 Kibana 以 Index Pattern 匯總 Elasticsearch的Index。
5.建立基本的視覺化。

那麼,就讓我們開始吧!

首先,先啟動Elasticsearch。

sudo systemctl start elasticsearch.service

接著,啟動Kibana。

sudo systemctl start kibana.service

再來先放一個檔案到/var/log目錄底下

此範例內容大致上就是一些json型態的log,
需要的可參考以下載

放完log,該啟動filebeat了

sudo systemctl start filebeat.service

最後再啟動logstash

sudo systemctl start logstash.service

如果一切正常,可以在 Management -> Elasticsearch 看到如下畫面:

這代表 Logstash 將 log 檔裡的內容解析,
並透過 AP I在 elasticsearch 建立了 Index。

接著我們再利用 Kibana 建立 Index Pattern 如下圖:

其中下方的 index 為 logstash 透過 API 在 elasticsearc h建立的 index,
透過設定 index pattern 為「filebeat*」,
就可以把所有 filebeat 開頭的「Elasticsearch」index 匯總,
在 Kibana 以一個 Pattern 建立符合 Pattern 的所有資料。
(註:step 2/2設定Time filter field為@timestamp即可)

如果好奇收了什麼資料,可以去「Discover」瞧瞧,如下圖:

最後可以透過「Visualization」針對資料圖型化,
如下圖是以圓餅圖呈現常用系統使用的port:

三、結語:

以上,是整個 Elastic stack的安裝篇,
往後有機會再分享實戰篇講解各個部件的詳細說明,
畢竟整個 stack 相當的大而廣,本人也尚在學習當中,
希望以上的經驗可以讓各位更順利導入 Elastic stack,謝謝。