1. 程式人生 > >ELK日誌分析系統

ELK日誌分析系統

env apache服務器 吞吐量 輕松 靈活 數據處理 地圖 output host

理論部分

一:ELK平臺介紹
1.1:ELK概述
ELK由ElasticSearch、Logstash和Kiabana三個開源工具組成:
1:Elasticsearch是個開源分布式搜索引擎,它的特點有:分布式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。

2:Logstash是一個完全開源的工具,他可以對你的日誌進行收集、過濾,並將其存儲供以後使用

3:Kibana 也是一個開源和免費的工具,它Kibana可以為 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 界面,可以幫助您匯總、分析和搜索重要數據日誌。

進行日誌處理分析,一般需要經過一下幾步:

(1)將日誌進行集中化管理
(2)將日誌格式化(Logstash)並輸出到Elasticsearch
(3)對格式化後的數據進行索引和存儲(Elasticsearch)
(4)前端數據的展示(Kibana)

1.2:Elasticsearch
1:ElasticSearch概述
Elasticsearch是一個基於Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的。

2:Elasticsearch核心概念
(1)接近實時(NRT)
Elasticsearch是一個接近實時的搜索平臺。

(2)集群(cluster)

一個集群就是由一個或多個節點組織在一起,它們共同持有你整個的數據,並一起提供索引和搜索功能。

(3)節點(node)
一個節點是你集群中的一個服務器,作為集群的一部分,它存儲你的數據,參與集群的索引和搜索功能。

(4)索引(index)
一個索引就是一個擁有幾分相似特征的文檔的集合。在一個集群中,可以定義任意多的索引。

(5)類型(type)
在一個索引中,你可以定義一種或多種類型。一個類型是你的索引的一個邏輯上的分類/分區,其語義完全由你來定。

(6)文檔(document)
一個文檔是一個可被索引的基礎信息單元。

(7)分片和復制(shards & replicas)
一個索引可以存儲超出單個結點硬件限制的大量數據。

分片很重要,主要有兩方面的原因:
1)允許你水平分割/擴展你的內容容量。
2)允許你在分片(潛在地,位於多個節點上)之上進行分布式的、並行的操作,進而提高性能/吞吐量。

1.3:Logstash
1:Logstash介紹
Logstash有JRuby語言編寫,運行在Java虛擬機(JVM)上,是一款強大的數據處理工具,可以實現數據傳輸、格式處理、格式化輸出。Ligstash具有強大的插件功能,常用於日誌處理。
Logstash的設計理念:Logstash只做三件事,數據輸入、數據加工、數據輸出

2:Logstash 工作的三個階段:

(1)input 數據輸入端,可以接收來自任何地方的源數據。

(2)Filter 數據中轉層,主要進行格式處理,數據類型轉換、數據過濾、字段添加,修改等,常用的過濾器如下。

(3)output 是logstash工作的最後一個階段,負責將數據輸出到指定位置,兼容大多數應用,常用的有:

2:Logstash組件

(1)Shipper:日誌收集者,負責監控本地日誌文件的變化,及時收集最新的日誌文件內容
(2)Indexer:日誌存儲者,負責接收日誌並寫入到本地文件
(3)Broker:日誌Hub,負責鏈接多個Shipper和多個Indexer
(4)Search and Storage:允許對時間進行搜索和存儲
(5)Web Interface:基於web的展示界面

1.4:Kibana
1:Kibana介紹
Kibana 是一個設計使用和Elasticsearch配置工作的開源分析和可視化平臺。可以用它進行搜索、查看、集成Elasticsearch中的數據索引。可以利用各種圖表、報表、地圖組件輕松的對數據僅進行可視化分析

2:Kibana主要功能
Elasticsearch無縫集成
整合數據
復雜數據分析
讓更多的團隊成員收益
接口靈活
配置簡單
可視化多數據源
簡單數據導出

實驗部分

技術分享圖片

1.1:環境準備(兩個節點上都要準備)
1:配置域名解析
(1)node1節點的設置
[root@localhost ~]# hostname node1
[root@localhost ~]# bash
[root@node1 ~]# vi /etc/hosts
192.168.8.134 node1
192.168.8.136 node2

(2)node2節點的設置
[root@localhost ~]# hostname node2
[root@localhost ~]# bash
[root@node2 ~]# vi /etc/hosts

2:檢查java環境
(1)node1的java環境
[root@node1 ~]# java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)

(2)node2的java環境
[root@node2 ~]# java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)

1.2:部署Elasticsearch軟件(在兩個node節點上都部署)
1:在node1節點上安裝Elasticsearch軟件
(1)安裝Elasticsearch軟件
[root@node1 ~]# systemctl stop firewalld
[root@node1 ~]# rpm -ivh elasticsearch-5.5.0.rpm

(2)加載系統服務
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl enable elasticsearch.service

(3)更改Elasticsearch主配置文件
[root@node1 ~]# vi /etc/elasticsearch/elasticsearch.yml

cluster.name: my-elk-cluster ##17行
node.name: node1 ##23行
path.data: /data/elk_data ##33行
path.logs: /var/log/elasticsearch/ ##37行
bootstrap.memory_lock: false ##43行
network.host: 0.0.0.0 ##55行
http.port: 9200 59行
discovery.zen.ping.unicast.hosts: ["node1", "node2"] ##68行

(4)創建數據存放路徑並授權
[root@node1 ~]# mkdir -p /data/elk_data
[root@node1 ~]# chown elasticsearch:elasticsearch /data/elk_data/

2:在node2節點上安裝Elasticsearch軟件
(1)安裝Elasticsearch軟件
[root@node2 ~]# systemctl stop firewalld
[root@node2 ~]# rpm -ivh elasticsearch-5.5.0.rpm

(2)加載系統服務
[root@node2 ~]# systemctl daemon-reload
[root@node2 ~]# systemctl enable elasticsearch.service

(3)更改Elasticsearch主配置文件
[root@node2 ~]# vi /etc/elasticsearch/elasticsearch.yml

cluster.name: my-elk-cluster ##17行
node.name: node2 ##23行
path.data: /data/elk_data ##33行
path.logs: /var/log/elasticsearch/ ##37行
bootstrap.memory_lock: false ##43行
network.host: 0.0.0.0 ##55行
http.port: 9200 59行
discovery.zen.ping.unicast.hosts: ["node1", "node2"] ##68行

(4)創建數據存放路徑並授權
[root@node2 ~]# mkdir -p /data/elk_data
[root@node2 ~]# chown elasticsearch:elasticsearch /data/elk_data/

3:啟動Elasticsearch並查看是否開啟成功
(1)在node1上啟動
[root@node1 ~]# systemctl start elasticsearch.service
[root@node1 ~]# netstat -anpt | grep 9200
tcp6 0 0 :::9200 :::* LISTEN 6987/java

(2)在node2上啟動
[root@node2 ~]# systemctl start elasticsearch.service
[root@node2 ~]# netstat -anpt | grep 9200
tcp6 0 0 :::9200 :::* LISTEN 5991/java

4:查看節點信息
(1)查看節點1
打開瀏覽器
http://192.168.8.134:9200

(2)查看節點2
打開瀏覽器
http://192.168.8.136:9200

(3)查看群集健康情況
打開瀏覽器
http://192.168.8.136:9200/_cluster/health?pretty

(4)查看群集的狀態信息
打開瀏覽器
http://192.168.8.136:9200/_cluster/state?pretty

1.3:在node1上安裝Elasticsearch-head插件(只需在node1上安裝)
1:編譯安裝node
[root@node1 ~]# tar zxvf node-v8.2.1.tar.gz
[root@node1 ~]# cd node-v8.2.1/
[root@node1 node-v8.2.1]# yum -y install gcc*
[root@node1 node-v8.2.1]# ./configure && make && make install

2:安裝phantomjs
[root@node1 ~]# tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node1 ~]# cd phantomjs-2.1.1-linux-x86_64/bin/
[root@node1 bin]# cp phantomjs /usr/local/bin/

3:安裝Elasticsearch-head
[root@node1 ~]# tar zxvf elasticsearch-head.tar.gz
[root@node1 ~]# cd elasticsearch-head/
[root@node1 elasticsearch-head]# npm install

4:修改Elasticsearch主配置文件

[root@node1 elasticsearch-head]# vi /etc/elasticsearch/elasticsearch.yml
在末尾添加:
http.cors.enabled: true
http.cors.allow-origin: "*"

5:啟動服務(必須在解壓的elasticsearch-head目錄下啟動)
[root@node1 ~]# cd elasticsearch-head/
[root@node1 elasticsearch-head]# npm run start &
[1] 90716
[root@node1 elasticsearch-head]#

[email protected] start /root/elasticsearch-head
grunt server

Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100

[root@node1 elasticsearch-head]# netstat -anpt | grep 9100
tcp 0 0 0.0.0.0:9100 0.0.0.0: LISTEN 90729/grunt
[root@node1 elasticsearch-head]# netstat -lnupt | grep 9200
tcp6 0 0 :::9200 :::
LISTEN 90600/java

6:通過elasticsearch-head查看elasticsearch信息
瀏覽器中訪問:
http://localhost:9100/

7:插入索引
[root@node1 ~]# curl -XPUT ‘localhost:9200/index-demo/test/1?pretty&pretty‘ -H ‘Content-Type:application/json‘ -d ‘{"user":"zhangsan","mesg":"hello world"}‘
{
"_index" : "index-demo",
"_type" : "test",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}

刷新瀏覽器,會發現添加的索引:

1.4:Logstash安裝及使用方法
1:在node1上安裝Logtash
[root@node1 ~]# rpm -ivh logstash-5.5.1.rpm
[root@node1 ~]# systemctl start logstash.service
[root@node1 ~]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/

2:測試Logtash
(1)輸入采用標準輸入,輸出采用標準輸出
[root@node1 ~]# logstash -e ‘input { stdin{} } output { stdout{} }‘
The stdin plugin is now waiting for input:
10:19:45.831 [Api Webserver] INFO logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com
2018-04-17T02:20:01.564Z localhost.localdomain www.baidu.com
www.sina.com
2018-04-17T02:20:14.612Z localhost.localdomain www.sina.com
^C10:20:21.838 [SIGINT handler] WARN logstash.runner - SIGINT received. Shutting down the agent.
10:20:21.851 [LogStash::Runner] WARN logstash.agent - stopping pipeline {:id=>"main"}

按下ctrl+c退出

(2)使用rubydubug顯示詳細輸出
[root@node1 ~]# logstash -e ‘input { stdin{} } output { stdout{ codec=>rubydebug } }‘
The stdin plugin is now waiting for input:
10:21:43.495 [Api Webserver] INFO logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.baidu.com
{
"@timestamp" => 2018-04-17T02:23:02.842Z,
"@version" => "1",
"host" => "localhost.localdomain",
"message" => "www.baidu.com"
}

按下ctrl+c退出

(3)使用Logtash將信息寫入Eloasticsearch中
註意:如果下列命令執行後提示拒絕連接,可以重啟以下elasticsearch服務

[root@node1 ~]# logstash -e ‘input { stdin{} } output { elasticsearch { hosts=>["192.168.8.134:9200"] } }‘

3:Logstash配置文件
(1)修改Logstash配置文件
[root@node1 ~]# chmod o+r /var/log/messages
[root@node1 ~]# touch /etc/logstash/conf.d/system.conf
[root@node1 ~]# vi /etc/logstash/conf.d/system.conf
input {
file {
path=>"/var/log/messages"
type=>"system"
start_position=>"beginning"
}
}

output {
elasticsearch {
hosts=>["192.168.8.134:9200"]
index=>"system-%{+YYYY.MM.dd}"
}
}

(2)重啟Logstash服務
[root@node1 ~]# systemctl restart logstash

(3)刷新頁面,查看Elasticsearch的信息

1.5:安裝Kibana
1:在node1上安裝Kibana
[root@node1 ~]# rpm -ivh kibana-5.5.1-x86_64.rpm
[root@node1 ~]# systemctl enable kibana.service

2:修改Kibana主配置文件
[root@node1 ~]# vi /etc/kibana/kibana.yml
server.port: 5601 ##2行
server.host: "0.0.0.0" ##7行
elasticsearch.url: "http://192.168.8.134:9200" ##21行
kibana.index: ".kibana" ##30行

3:啟動Kibana服務
[root@node1 ~]# systemctl start kibana

4:驗證Kibana
(1)瀏覽器訪問
http://192.168.8.134:5601

(2)查看索引字段

(3)查看圖標和日誌信息

6:添加apache服務器的日誌
(1)在apache服務器上安裝httpd
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)

[root@localhost ~]# rpm -ivh logstash-5.5.1.rpm
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable logstash
[root@localhost conf.d]# vi /apache_log.conf
input {
file {
path=>"/etc/httpd/logs/access_log"
type=>"access"
start_position=>"beginning"
}
}

    file {
      path=>"/etc/httpd/logs/error_log"
      type=>"error"
      start_position=>"beginning"
         }

output {
if [type]=="access" {
elasticsearch {
hosts=>["192.168.8.134:9200"]
index=>"apache_access-%{+YYYY.MM.dd}"
}
}
if [type]=="error" {
elasticsearch {
hosts=>["192.168.8.134:9200"]
index=>"apache_error-%{+YYYY.MM.dd}"
}
}

  }

[root@localhost ~]# systemctl start httpd
[root@localhost conf.d]# /usr/share/logstash/bin/logstash -f apache_log.conf

(2)瀏覽器訪問
http://192.168.8.134:9100/

http://192.168.8.134:5601

ELK日誌分析系統