Linux日志分析ELK環境搭建


場景:ELK作為一個日志收集和檢索系統,感覺功能還是相當的強大的。 

ELK是啥, 其實是是三個組件的縮寫, 分別是elasticsearch, logstash, kibana. ELK平台可以用於實現日志收集、日志搜索和日志分析. 當然, 如果你有花不完的money, 完全可以使用商業版的Splunk, Orz...

ELK分別是什么

1). ELK現在都是屬於elastic公司的產品, 產品主頁.

1.elasticsearch(es): 它是個開源分布式搜索引擎,它的特點有:分布式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。
2.logstash: 簡單說是一個日志收集工具, 可以定義從哪里獲取數據, 並且可以簡單處理數據, 最后可以定義將數據輸出到哪里, 一般輸出到es. 
3.kibana: 其實就是一個數據展示界面, 並且可以分析數據, 例如從es中讀取數據進行展示分析. es自己也帶有簡單的展示dashboard: http://127.0.0.1:9200/_plugin/head/, 當然 前提是你安裝了head插件, 這個下面再說.

2). 簡單流程

下面圖簡單的展示了三組件之間的協作,

1

總的來說分成四大部分, 第一, 應用產生數據, 第二, logstash從應用收集數據, 第三, 將數據放入es, 第四, 使用kibana進行數據展示以及分析.

1 准備

1.1 環境檢測

本文介紹安裝 logstash 2.2.0 和 elasticsearch 2.2.0,操作系統環境版本是

安裝 JDK 是必須的,一般操作系統都會有,只是版本的問題,這里在安裝的過程中發現ELK大部分都要求1.8以上的JDK,所以果斷換1.8版本的JDK省事些。

 

而 Kibana 只是一個用純 JavaScript 寫的前端 UI。一定要注意 Kibana 的版本,它會要求 ES 的版本。下表展示了kibana和Elasticsearch之間的對應關系

 

1.2 軟件下載

注意:這三個軟件之間的版本如果選擇不對,中間會出現很多想不到的問題。我這里的總結是elasticsearch和logstash最好保持版本一直,kibana和es的版本如上表所示的對應。

 在網站中搜索下面三個軟件的壓縮包。https://www.elastic.co/

這里我都是選擇Linux 64位的壓縮包,然后將這三個文件通過FTP工具上傳到自己建立的目錄下面,這里以Etest為例。

ps:ELK三個軟件的安裝基本就是解壓,然后執行就可以運行了,所以安裝的目錄完全可以自己選擇。

logstash all plugins 2.2.0

elasticsearch 2.2.0

kibana 4.4.1

 

1.3 解壓縮軟件

#將軟件解壓到/usr/local目錄
##tar -xzvf elasticsearch-5.0.1.tar.gz -C /usr/local

#首先切換到軟件下載目錄,然后將三個軟件直接解壓到Etset目錄
tar -xzvf elasticsearch-2.2.0.tar.gz

tar -xzvf logstash-all-plugins-2.2.0.tar.gz

tar -xzvf kibana-4.4.1-linux-x64.tar.gz

解壓后文件如下圖所示:

2 elasticsearch

2.1 啟動elasticsearch

ps:啟動時候不能以root用戶啟動,否則會報錯。

cd /home/ipsp/Etest/elasticsearch-2.2.0 #啟動命令
bin/elasticsearch

正常啟動情況的狀態如下圖所示:

注意:為了能夠通過瀏覽器訪問elasticsearch,我們需要配置elasticsearch的配置文件,主要是修改network.host和http.port

vim config/elasticsearch.yml

#修改配置文件內容如下:
network.host: 0.0.0.0

 http.port: 9200

之所以修改端口,是因為ES 每次啟動時,端口可能會變(端口被占用,ES 自己會改端口)~

如果還不能正常啟動 ES,並提示端口被占用,就查看一下什么程序占用 9200 端口,kill 掉,重啟 ES 就行。

我遇到的,Java 把 9200 端口占用了。

 通過上述配置在瀏覽器就能夠訪問ElasticSearch了,這里我使用的Chrome瀏覽器,用360瀏覽器每次打開都是下載文件,瀏覽器的問題。 

 

2.2 檢測elasticsearch狀態

 重新打開一個終端,用非root 用戶登錄, 輸入指令來檢測Elasticsearch的連通性:

curl -X GET http://localhost:9200

#需要先修改elasticsearch的配置文件,參考2.1,這樣也能夠檢測
curl -X GET http://168.33.130.224:9200

狀態如圖所示:

 

2.3 插件安裝

先查看你的 ES 都有哪些插件,這里我已經安裝了Head插件,所以直接能看到。

ipsp@ibps:~/Etest/elasticsearch-2.2.0> bin/plugin list
Installed plugins in /home/ipsp/Etest/elasticsearch-2.2.0/plugins:
- head
ipsp@ibps:~/Etest/elasticsearch-2.2.0>

1、安裝Head插件

ES 支持在線和本地安裝 Head。本地安裝時,從 Github 上下載 Head 插件,然后上傳到你的 ES 服務器,比如,/home/ipsp/Etest/elasticsearch-2.2.0 目錄。

ps:在線安裝總是安裝失敗,這里就講解下本地安裝的過程。

1、本地安裝

  • 下載插件elasticsearch-head-master
  • 執行命令安裝
     bin/plugin install -v file:plugins/elasticsearch-head-master.zip
    [root@vcyber elasticsearch]# bin/plugin install -v file:plugins/elasticsearch-head-master.zip
    -> Installing from file:plugins/elasticsearch-head-master.zip...
    Trying file:plugins/elasticsearch-head-master.zip ...
    Downloading .........DONE
    Verifying file:plugins/elasticsearch-head-master.zip checksums if available ...
    NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
    - Plugin information:
    Name: head
    Description: head - A web front end for an elastic search cluster
    Site: true
    Version: master
    JVM: false
     
    Installed head into /usr/local/elasticsearch/elasticsearch/plugins/head    //安裝成功的標志

可以通過命令來查看插件是否安裝完成:

 bin/plugin list

注意:install 選項,ES 的 2.* 版本,都不帶“-”橫線選項,即“-install”。

注意:若是本地安裝,而且,你把 Head 壓縮包放到了 Elasticsearch/plugins 目錄下,安裝后,一定要將 Elasticsearch-head zip 壓縮包刪掉,否則啟動 ES 時會報“不能初始化插件”錯誤。

  • 瀏覽器訪問 http://your ip adress:9200/_plugin/head你就會看到如下界面:

關於head插件會在后面的部分進行介紹,未完待續

2、在線安裝(只是參考)

[root@vcyber elasticsearch]# bin/plugin install mobz/elasticsearch-head
-> Installing mobz/elasticsearch-head...
Trying https://github.com/mobz/elasticsearch-head/archive/master.zip ...
Downloading ...................................................................................................................................................................................................................................................................................................................................................................................................................................DONE
Verifying https://github.com/mobz/elasticsearch-head/archive/master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /usr/local/elasticsearch/elasticsearch/plugins/head

2.4 問題匯總

下面有些問題是從別的地方摘抄的,可以借鑒,有的是自己遇到的。

1、java版本不對——更換JDK版本

[root@vcyber elasticsearch]# bin/elasticsearch
 
Exception in thread "main" java.lang.RuntimeException: Java version: Oracle Cooration 1.7.0_51 [Java HotSpot(TM) 64-Bit Server VM 24.51-b03] suffers from crical bug https://bugs.openjdk.java.net/browse/JDK-8024830 which can cause dataorruption.
 
Please upgrade the JVM, see http://www.elastic.co/guide/en/elasticsearch/referce/current/_installation.html for current recommendations.
 
If you absolutely cannot upgrade, please add -XX:-UseSuperWord to the JAVA_OPT environment variable.
 
Upgrading is preferred, this workaround will result in degraded performance.
 
        at org.elasticsearch.bootstrap.JVMCheck.check(JVMCheck.java:123)
 
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:283)
 
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:3
 
Refer to the log for complete error details.

 

大意是:Java 運行時異常,本機版本 JDK 有 bug……讓升級 JVM。如果實在不能升級,就向 JAVA_OPT 環境變量添加 -XX:-UseSuperWord 選項。

 

2、elasticsearch 不能用 root 用戶運行——切換到非root用戶運行

在root用戶解壓完elasticsearch后出現如下錯誤:

[root@localhost cs408]# /usr/local/elasticsearch-5.0.1/bin/elasticsearch
[2016-11-30T09:24:56,235][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:116) ~[elasticsearch-5.0.1.jar:5.0.1] at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:103) ~[elasticsearch-5.0.1.jar:5.0.1] at org.elasticsearch.cli.SettingCommand.execute(SettingCommand.java:54) ~[elasticsearch-5.0.1.jar:5.0.1] at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:96) ~[elasticsearch-5.0.1.jar:5.0.1] at org.elasticsearch.cli.Command.main(Command.java:62) ~[elasticsearch-5.0.1.jar:5.0.1] at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:80) ~[elasticsearch-5.0.1.jar:5.0.1] at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:73) ~[elasticsearch-5.0.1.jar:5.0.1] Caused by: java.lang.RuntimeException: can not run elasticsearch as root at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:96) ~[elasticsearch-5.0.1.jar:5.0.1] at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:155) ~[elasticsearch-5.0.1.jar:5.0.1] at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:286) ~[elasticsearch-5.0.1.jar:5.0.1] at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:112) ~[elasticsearch-5.0.1.jar:5.0.1] ... 6 more [root@localhost cs408]# vim /usr/local/elasticsearch-5.0.1/config/elasticsearch.yml

解決方案:

因為安全問題elasticsearch 不讓用root用戶直接運行,所以要創建新用戶,這里以已有用戶cs408為例:

第一步:liunx創建新用戶  adduser  XXX  然后給創建的用戶加密碼 passwd XXX    輸入兩次密碼。(已有可以忽略)

第二步:切換剛才創建的用戶 su XXX  然后執行elasticsearch  會顯示AccessDeniedException 權限不足。

第三步:給新建的XXX賦權限,chmod 777 *  這個不行,因為這個用戶本身就沒有權限,肯定自己不能給自己付權限。所以要用root用戶登錄賦予權限。

第四步:root給XXX賦權限,chown -R XXX /你的elasticsearch安裝目錄。

chown -R cs408 /usr/local/elasticsearch-5.0.1 

再次執行就能看到成功輸出信息。

bin/elasticsearch

3 logstash

logstash 提供各種安裝包,包括 tar.gz,ZIP,DEB 和 RPM。另外,又提供了一個包含所有插件的壓縮包——logstash-all-plugins-2.2.0.tar.gz 。本文以它為例。解壓后,配置 logstash,運行即可。

3.1 創建配置文件

logstash運行時需要使用配置文件,主要是用於定義內容的輸入、輸出、中間處理等。

創建配置文件所在目錄,並編輯配置文件

cd /home/ipsp/Etest/logstash-2.2.0
vim
logstash_agent.conf

在配置文件logstash_agent.conf添加如下內容:

input { stdin{} } output { elasticsearch { action => "index" # 在ES上操作index hosts => "168.33.130.224:9200" # ES地址 index => "logstash--%{+YYYY-MM}" # 索引名 }
}

上面的配置主要是在logstash命令段輸入日志信息,然后將日志輸出到elasticsearch中。

ps:注意這里的index選項,我命名的是logstash--%{+YYYY-MM},這個在后面的head插件和kibana中都會有所體現的。

3.2 啟動

bin/logstash -f logstash_agent.conf 

看到如下的內容,便表示成功了

ipsp@ibps:~/Etest/logstash-2.2.0> bin/logstash -f logstash_agent.conf
Settings: Default pipeline workers: 40
Logstash startup completed

3.3 測試

 通過上面的配置,我們已經可以在logstash命令端輸入信息,然后通過head插件進行查看了:

前提要保證elasticsearch和logstash都正常啟動(需要先啟動elasticsearch,再啟動logstash,不然elasticsearch會報錯),head插件也已經正確安裝!

我們輸入Hello world!

在head插件中的,我們可以看到如下信息:

在數據庫瀏覽中,我們可以看到索引項——logstash--%{+YYYY-MM},這個是我們在配置logstash輸出到elasticsearch的配置文件中定義的(logstash_agent.conf)

我在logstash命令端輸入的字符串Hello world!在head的圖形化界面能夠查看到,其中還包含了其他一些默認的字段。

這里每輸出一條信息,在head插件中都能夠實時的看到,感覺很強大。

到此,elasticsearch和logstash之間的連通已經搭建成功。

3.4 插件

你可以查看 logstash 都安裝了哪些插件

bin/plugin list

 

ipsp@ibps:~/Etest/logstash-2.2.0> bin/plugin list
logstash-codec-avro
logstash-codec-cef
logstash-codec-cloudfront
logstash-codec-cloudtrail
logstash-codec-collectd
logstash-codec-compress_spooler
logstash-codec-dots
logstash-codec-edn
logstash-codec-edn_lines
logstash-codec-es_bulk
logstash-codec-fluent
logstash-codec-graphite
logstash-codec-gzip_lines
logstash-codec-json
logstash-codec-json_lines
logstash-codec-line
logstash-codec-msgpack
logstash-codec-multiline
logstash-codec-netflow
logstash-codec-nmap
logstash-codec-oldlogstashjson
logstash-codec-plain
logstash-codec-rubydebug
logstash-codec-s3plain
logstash-codec-spool
logstash-filter-aggregate
logstash-filter-alter
logstash-filter-anonymize
logstash-filter-checksum
logstash-filter-cidr
logstash-filter-cipher
logstash-filter-clone
logstash-filter-collate
logstash-filter-csv
logstash-filter-date
logstash-filter-de_dot
logstash-filter-dns
.......................
.......................

 

4 kibana

4.1 啟動

ipsp@ibps:~/Etest> cd kibana-4.4.1-linux-x64/
ipsp@ibps:~/Etest/kibana-4.4.1-linux-x64> bin/kibana

如果看到類似如下輸出,就代表成功了:

ipsp@ibps:~/Etest/kibana-4.4.1-linux-x64> bin/kibana
log [16:59:39.891] [info][status][plugin:kibana] Status changed from uninitialized to green - Ready
log [16:59:39.942] [info][status][plugin:elasticsearch] Status changed from uninitialized to yellow - Waiting for Elasticsearch
log [16:59:39.961] [info][status][plugin:kbn_vislib_vis_types] Status changed from uninitialized to green - Ready
log [16:59:39.974] [info][status][plugin:markdown_vis] Status changed from uninitialized to green - Ready
log [16:59:39.983] [info][status][plugin:metric_vis] Status changed from uninitialized to green - Ready
log [16:59:39.987] [info][status][plugin:spyModes] Status changed from uninitialized to green - Ready
log [16:59:39.991] [info][status][plugin:statusPage] Status changed from uninitialized to green - Ready
log [16:59:39.995] [info][status][plugin:table_vis] Status changed from uninitialized to green - Ready
log [16:59:40.026] [info][listening] Server running at http://168.33.130.224:5601
log [16:59:40.040] [info][status][plugin:elasticsearch] Status changed from yellow to green - Kibana index ready

通過瀏覽器訪問http://168.33.130.224:5601/就可以看到kibana界面了。

配置ES索引:

kibana第一次使用時,會要求創建index,只要按照默認值即可。

 

首次會提示沒有索引。。。。

首先需要加上我們剛剛建立的索引index => "logstash--%{+YYYY-MM}" (這里請參考3.1中的logstash_agent.conf的配置), 點擊setting->indices, 在這里我們可以Configure an index pattern, 也就是說可以配置 正則匹配的index,

可以看到默認的index是"logstash-*", 默認是從logstash導出的數據, 因為我們在logstash中配置的索引就是logstash開頭的,所以這里我們保持默認不變.

下面還有一個Time-field name, 一般來說time都是一個必要的字段, 並且這個字段的類型是date類型! 不是string!!! 如果沒有時間字段, 那么將上面的" Index contains time-based events" 取消就OK.

創建完索引是這樣的:

點擊菜單欄: Discover, 如果沒有看到數據, 那么點擊右邊的時間, 改成Today, 現在你應該能看到數據了吧...反正我能看到:

ps:這里的時間是以Linux服務器中的時間為主。我這里點today看不到,點的This Week才看到:

在上面可以直接搜索,很方便。其他功能待開發。

4.2 問題

1、出現Configure an index pattern create是灰色的的問題

這個問題是配置文件的問題,修改logstash的配置文件,使其輸出到elasticsearch,其中的index選項配置為kibana上的index,比如我是index => "logstash--%{+YYYY-MM}" ,這個時候重啟logstash,kibana就保持默認的logstash-*索引既可,應該能夠看到Create按鈕了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM