elk+filebeat


elk +filebeat+kafka

一: ELK企業級日志分析系統

1.1 ELK 簡介

ELK平台是一套完整的日志集中處理解決方案,將 ElasticSearch、Logstash 和 Kiabana 三個開源工具配合使用, 完成更強大的用戶對日志的查詢、排序、統計需求。

  • ElasticSearch
    • ElasticSearch:是基於Lucene(一個全文檢索引擎的架構)開發的分布式存儲檢索引擎,用來存儲各類日志。
    • Elasticsearch 是用 Java 開發的,可通過 RESTful Web 接口,讓用戶可以通過瀏覽器與 Elasticsearch 通信。
    • Elasticsearch 是個分布式搜索和分析引擎,優點是能對大容量的數據進行接近實時的存儲、搜索和分析操作。

  • Logstash:
    • Logstash:作為數據收集引擎。它支持動態的從各種數據源搜集數據,並對數據進行過濾、分析、豐富、統一格式等操作,然后存儲到用戶指定的位置,一般會發送給 Elasticsearch。
    • Logstash 由JRuby 語言編寫,運行在 Java 虛擬機(JVM)上,是一款強大的數據處理工具, 可以實現數據傳輸、格式處理、格式化輸出。Logstash 具有強大的插件功能,常用於日志處理。

  • Kiabana:
    • Kiabana:是基於 Node.js 開發的展示工具,可以為 Logstash 和 ElasticSearch 提供圖形化的日志分析 Web 界面展示,可以匯總、分析和搜索重要數據日志。

  • Filebeat:
    • Filebeat:輕量級的開源日志文件數據搜集器。通常在需要采集數據的客戶端安裝 Filebeat,並指定目錄與日志格式,Filebeat 就能快速收集數據,並發送給 logstash 進行解析,或是直接發給 Elasticsearch 存儲,性能上相比運行於 JVM 上的 logstash 優勢明顯,是對它的替代。



1.2 為什么使用ELK

  • 日志主要包括系統日志、應用程序日志和安全日志。系統運維和開發人員可以通過日志了解服務器軟硬件信息、檢查配置過程中的錯誤及錯誤發生的原因。經常分析日志可以了解服務器的負荷,性能安全性,從而及時采取措施糾正錯誤。

  • 往往單台機器的日志我們使用grep、awk等工具就能基本實現簡單分析,但是當日志被分散的儲存不同的設備上。如果你管理數十上百台服務器,你還在使用依次登錄每台機器的傳統方法查閱日志。這樣是不是感覺很繁瑣和效率低下。當務之急我們使用集中化的日志管理,例如:開源的syslog,將所有服務器上的日志收集匯總。集中化管理日志后,日志的統計和檢索又成為一件比較麻煩的事情,一般我們使用grep、awk和wc等Linux命令能實現檢索和統計,但是對於要求更高的查詢、排序和統計等要求和龐大的機器數量依然使用這樣的方法難免有點力不從心。

  • 一般大型系統是一個分布式部署的架構,不同的服務模塊部署在不同的服務器上,問題出現時,大部分情況需要根據問題暴露的關鍵信息,定位到具體的服務器和服務模塊,構建一套集中式日志系統,可以提高定位問題的效率。



1.3 完整日志系統基本特征

  • 收集:能夠采集多種來源的日志數據
  • 傳輸:能夠穩定的把日志數據解析過濾並傳輸到存儲系統
  • 存儲:存儲日志數據
  • 分析:支持 UI 分析
  • 警告:能夠提供錯誤報告,監控機制

1.4 ELK的工作原理

  1. 在所有需要收集日志的服務器上部署Logstash;或者先將日志進行集中化管理在日志服務器上,在日志服務器上部署 Logstash。
  2. Logstash 收集日志,將日志格式化並輸出到 Elasticsearch 群集中。
  3. Elasticsearch 對格式化后的數據進行索引和存儲。
  4. Kibana 從 ES 群集中查詢數據生成圖表,並進行前端數據的展示。





二:elk集群之部署elasticsearch集群部署

主機 角色
Node1節點(2c/4G) :node1:192.168.23.11 Elasticsearch ; Kibana
Node2 節點(2c/4G):node2:192.168.23.12 Elasticsearch
Apache節點: apache:192.168.23.13 Logstash ; Apache

2.1 環境准備

更改主機名,配置域名解析,查看java哦環境

[root@node1 opt]# systemctl stop firewalld
[root@node1 opt]# setenforce 0

---------------------------
#修改主機名
Node1 節點: hostnamectl  set-hostname node1
Node2 節點: hostnamectl  set-hostname node2

#node1,node2配置主機名映射
 echo "192.168.23.11 node1" >> /etc/hosts
 echo "192.168.23.12 node2" >> /etc/hosts
 
 ----------------
 node1,node2查看java環境
 [root@node1 ~]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)



2.2 部署elasticsearch 軟件

#安裝 elasticsearch-rpm 包
#上傳elasticsearch-5.5.0.rpm 到/opt目錄下
cd /opt
rpm -ivh elasticsearch-5.5.0.rpm


#加載系統服務
systemctl  daemon-reload
systemctl enable eelasticsearch.service



2.2.1 修改elasticsearch 主配置文件

[root@node1 opt]#cp /etc/elasticsearch/elasticsearch.yml  /etc/elasticsrch.yml.bak

[root@node1 opt]# vim /etc/elasticsearch/elasticsearch.yml

[root@node1 opt]# grep -nv "^#" /etc/elasticsearch/elasticsearch.yml
#17行,取消注釋,指定集群名
17:cluster.name: my-elk-cluster   

#23行:取消注釋,指定節點名。node1節點為node1,node2節點為node2
23:node.name: node1

#33行:取消注釋,指定數據存放路徑(/data/elk_data目錄需要自己創建)
33:path.data: /data/elk_data

#37行:取消注釋,指定日志存放路徑
37:path.logs: /var/log/elasticsearch/

#43: 取消注釋,改為在啟動時不鎖定內存
#內存鎖:在啟動時預估需要多少內存,然后將這些內存鎖定,其他服務就無法使用這些內存.
43:bootstrap.memory_lock: false

#55: 取消注釋,設置監聽地址,0.0.0.0 代表所有地址
55:network.host: 0.0.0.0

#59: 取消注釋,ES服務的默認監聽端口為 9200
59:http.port: 9200

#68: 取消注釋,集群發現通過單播時間,指定要發現的節點node1,node2
68:discovery.zen.ping.unicast.hosts: ["node1","node2"]

image-20210927084935159



2.2.2 創建數據存放路徑並授權,啟動elasticsearch

[root@node1 ~]# mkdir -p /data/elk_data
[root@node1 ~]# chown elasticsearch:elasticsearch  /data/elk_data/
[root@node1 ~]# systemctl start elasticsearch.service 
[root@node1 ~]# netstat -natp | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      43371/java         

image-20210927135610306



2.3 查看節點信息

#查看節點node1,node2的信息
[root@node1 ~]# firefox http://192.168.23.11:9200
[root@node2 ~]# firefox http://192.168.23.12:9200

#查看集群健康狀態,status值為"green",則表示健康
[root@node1 ~]# firefox http://192.168.23.11:9200/_cluster/health?pretty
[root@node1 ~]# firefox http://192.168.23.12:9200/_cluster/health?pretty

#查看集群狀態信息
[root@node1 ~]# firefox http://192.168.23.11:9200/_cluster/state?pretty

image-20210927140118946

image-20210927140809860

image-20210927141133295



2.4 安裝elasticsearch-hand

Elasticsearch 在 5.0 版本后,Elasticsearch-head 插件需要作為獨立服務進行安裝,需要使用npm工具(NodeJS的包管理工具)安裝。

安裝 Elasticsearch-head 需要提前安裝好依賴軟件 node 和 phantomjs

node:是一個基於 Chrome V8 引擎的 JavaScript 運行環境。

phantomjs:是一個基於 webkit 的JavaScriptAPI,可以理解為一個隱形的瀏覽器,任何基於 webkit 瀏覽器做的事情,它都可以做到。


2.4.1 編譯安裝node

#上傳軟件包上傳軟件包 node-v8.2.1.tar.gz 到/opt
[root@node1 opt]# yum -y install gcc gcc-c++ make
[root@node1 ~]# cd /opt/
[root@node1 opt]# tar xf node-v8.2.1.tar.gz
[root@node1 node-v8.2.1]# ./configure  && make -j 2 && make install 

2.4.2 安裝phantomjs

[root@node1 opt]# cd /opt/
[root@node1 opt]# tar xf phantomjs-2.1.1-linux-x86_64.tar.bz2  -C /usr/local/src/
[root@node1 opt]# cd /usr/local/src/phantomjs-2.1.1-linux-x86_64/bin/
[root@node1 bin]# cp phantomjs /usr/local/bin/

2.4.3 安裝elasticsearch-hand 數據可視化工具

[root@node1 opt]# cd /opt/
[root@node1 opt]# tar -xf elasticsearch-head.tar.gz -C /usr/local/src/
[root@node1 opt]# cd /usr/local/src/elasticsearch-head/
[root@node1 elasticsearch-head]# npm install

image-20210927214553346


2.4.4 修改elasticsearch 主配置文件

#修改yml 文件,要注意yml的格式要求
[root@node1 ~]# vim /etc/elasticsearch/elasticsearch.yml
.......
#在配置文件的最后添加(這是yml文件,冒號后面要有空格)
 http.cors.enabled: true           #開啟跨域訪問支持,默認是false
 http.cors.allow-origin: "*"      #指定跨域訪問允許的域名地址為所有

[root@node1 ~]# systemctl  restart  elasticsearch.servic

image-20210927221637465


2.4.5 啟動elasticsearch-head 服務

必須在解壓后的elasticsearch-head 目錄下啟動服務,進程會讀取該目錄下的gruntfile.js文件,否則可能啟動失敗。

[root@node1 ~]# cd /usr/local/src/elasticsearch-head/
[root@node1 elasticsearch-head]# npm run start &
#elasticsearch-head 監聽的端口是9100
[root@node1 elasticsearch-head]# netstat -natp | grep 910

image-20210927215450781

image-20210927215607425


2.4.6 訪問測試

[root@node1 ~]# firefox http://192.168.23.11:9100

image-20210927222038899


2.4.7 插入索引

#通過命令插入一個測試索引,索引為 index-demo,類型為 test。

curl -X PUT '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" : 2,
"failed" : 0
},
"created" : true
}



#瀏覽器訪問9100端口,查看索引信息
[root@node1 ~]# firefox http://192.168.23.11:9100

image-20210927222254312

image-20210927222426977

image-20210927222542759





三elK  Logstash 部署(Apache節點上操作)

Logstash 一般部署在需要監控其日志的服務器。在本案例中,Logstash 部署在 Apache 服務器上,用於收集 Apache 服務器的日志信息並發送到 Elasticsearch。

#修改主機名
[root@host13 ~]# hostnamectl  set-hostname apache
#安裝Apache服務
[root@apache ~]# yum -y install httpd
[root@apache ~]# systemctl start httpd
#安裝java環境
[root@apache ~]# yum  -y install java
[root@apache ~]# java -version


3.1  安裝 logstash

#上傳軟件包logstash-5.5.1.rpm 到/opt 目錄下
[root@apache opt]# cd /opt/
[root@apache opt]# rpm -ivh logstash-5.5.1.rpm 
[root@apache opt]# systemctl  start logstash.service 
[root@apache opt]# systemctl enable logstash.service 
[root@apache opt]# ln -s /usr/share/logstash/bin/logstash  /usr/local/bin/

3.2 測試logstash

Logstash 命令常用選項

-f:通過這個選項可以指定 Logstash 的配置文件,根據配置文件配置 Logstash 的輸入和輸出流。

-e:從命令行中獲取,輸入、輸出后面跟着字符串,該字符串可以被當作 Logstash 的配置(如果是空,則默認使用 stdin 作為輸入,stdout 作為輸出)。

-t:測試配置文件是否正確,然后退出。

3.2.2 定義輸入輸出流

輸入采用標准輸入,輸出采用標准輸出(類似管道)

[root@apache opt]# logstash -e 'input { stdin{} } output { stdout{} }'
。。。。。。
www.baidu.com    #輸入
2021-09-28T06:10:21.738Z apache www.baidu.com   #輸出
www.sina.com.cn   #輸入  
2021-09-28T06:10:28.310Z apache www.sina.com.cn  #輸出

// 執行ctrl +c  退出

image-20210928141833596


3.2.3 使用rubydebug 輸出

使用 rubydebug 輸出詳細格式顯示,codec 為一種編解碼器

[root@apache opt]# logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug } }'
www.baidu.com   #輸入
#輸出
{
    "@timestamp" => 2021-09-28T06:32:07.951Z,
      "@version" => "1",
          "host" => "apache",
       "message" => "www.baidu.com"
}

image-20210928143349747


3.2.4 使用elasticsearch 進行輸入(不是輸出在屏幕上)

[root@apache opt]# logstash -e 'input { stdin{} } output { elasticsearch{ hosts=>["192.168.23.11:9200"]} }' 
www.baidu.com
www.sina.com.cn

image-20210928144807869

image-20210928145233836



3.3 定義logstash 的配置文件

Logstash 配置文件基本由三部分組成:input,output,以及 filter(可選,根據需要選擇使用)

注意:input,filter,output 后面有空格

格式如下:

input {......}

filter {.....}

output {.....}

在每個部分中,也可以指定多個訪問方式。例如,若要指定兩個日志來源文件,則格式如下:

input {

​ file { path =>"/var/log/messages" type =>"syslog"}

​ file { path =>"/var/log/httpd/access.log" type =>"apache"}

}

#修改 Logstash 配置文件,讓其收集系統日志/var/log/messages,並將其輸出到 elasticsearch 中。
#修改/var/log/messages權限,讓 Logstash 可以讀取日志
[root@apache opt]# chmod +r /var/log/messages

#注意,是yml文件,注意格式
[root@apache opt]# vim /etc/logstash/conf.d/system.conf
#輸入
input {
  file{ 
     path =>"/var/log/messages"     #指定要收集的日志位置
     type =>"system"                #自定義日志類型標識
     start_position =>"beginning"   #表示從快開始處收集
 }
}

#輸出
output {
   #輸出到elasticsearch
   elasticsearch {   
       #指定elasticsearch服務器的地址和端口,多個ES服務器使用 “逗號空格” 隔開
      hosts =>["192.168.23.11:9200", "192.168.23.12:9200"]
      #指定輸出到elasticsearch 的索引格式
      index =>"system-%{+YYYY.MM.dd}"
 }
}

image-20210928162835922



3.4 訪問測試

#重啟logstash 服務
[root@apache opt]# systemctl restart  logstash.service
#訪問node 主機的 9100端口
[root@apache opt]# firefox http://192.168.23.11:9100/

image-20210928151822940





四:elk之kiabana部署(在node1或者node2節點上操作)

4.1 安裝kibana

上傳軟件包 kibana-5.5.1-x86_64.rpm 到 /opt 目錄

[root@node1 opt]# cd /opt/
[root@node1 opt]# rpm -ivh kibana-5.5.1-x86_64.rpm 


4.2 設置kibana的主配置文件

[root@node1 opt]# vim /etc/kibana/kibana.yml 
#第2行:取消注釋,kiabana 默認監聽端口為5601 
2// server.port: 5601
#第7行,取消注釋,設置kiabana的監聽地址。0.0.0.0表示所有地址
7// server.host: "0.0.0.0"
#21行,取消注釋,設置和elasticsearch 建立連接的地址和端口
21// elasticsearch.url: "http://192.168.23.11:9200"
#第30 行,取消注釋,設置在elasticsearch 中添加.kiabana索引
30// kibana.index: ".kibana"



4.3 啟動kibana服務

[root@node1 opt]# systemctl start kibana.service
[root@node1 opt]# systemctl enable kibana.service
[root@node1 opt]# netstat  -natp | grep 5601

image-20210928155638704



4.4 驗證kibana

瀏覽器訪問http://192.168.23.11:5601

[root@node1 opt]# firefox http://192.168.23.11:5601

瀏覽器訪問 http://192.168.23.11:5601 登錄 Kibana,單擊“Create Index Pattern”按鈕添加索引, 在索引名中輸入之前配置的 Output 前綴 apache_access-,並單擊“Create”按鈕。在用相同的方法添加 apache_error-索引。

選擇“Discover”選項卡,在中間下拉列表中選擇剛添加的 apache_access-* 、apache_error-* 索引, 可以查看相應的圖表及日志信息。

如果無法顯示圖表信息,試試看關閉瀏覽器,在重新打開

image-20210928160114279

image-20210928160239415

image-20210928160732967



4.5 將Apache服務器日志添加到elasticsearch

 在 logstash節點, 將Apache服務器日志(訪問的,錯誤的)添加到Elasticsearch 並通過kibana顯示

[root@apache opt]# vim /etc/logstash/conf.d/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.23.11:9200", "192.168.23.12:9200"]
           index => "apache_access-%{+YYYY.MM.dd}"
     }
 }
  if [type] == "error" {
     elasticsearch {
          hosts => ["192.168.23.11:9200", "192.168.23.12:9200"]
          index => "apache_error-%{+YYYY.MM.dd}"
    }
 }
}

image-20210928163849781



4.6 訪問測試

瀏覽器訪問 http://192.168.23.11:9100 查看索引是否創建

[root@apache opt]# cd /etc/logstash/conf.d/
#測試配置文件
[root@apache conf.d]# logstash  -tf  apache_log.conf

#讀取apache_log.conf配置文件(這個是前台啟動,會將內容讀取出來
[root@apache conf.d]# /usr/share/logstash/bin/logstash -f apache_log.conf 

[root@node1 ~]# firefox  http://192.168.23.11:9100

image-20210928184229252


瀏覽器訪問 http://192.168.23.11:5601 登錄 Kibana,單擊“Create Index Pattern”按鈕添加索引, 在索引名中輸入之前配置的 Output 前綴 apache_access-,並單擊“Create”按鈕。在用相同的方法添加 apache_error-索引。

選擇“Discover”選項卡,在中間下拉列表中選擇剛添加的 apache_access-* 、apache_error-* 索引, 可以查看相應的圖表及日志信息。

[root@node1 ~]# firefox  http://192.168.23.11:5601

image-20210928185009697

image-20210928185457072

image-20210928190335517

image-20210928190445946

image-20210928192851310



如果沒有搜索到日志信息,可能是時間不對,選擇的時間內沒有訪問日志。點擊time picker 時間選擇按鈕,選擇時間

img

img



五:Filebeat+ELK部署

節點 角色
Node1節點(2C/4G):node1/192.168.23.11 Elasticsearch , Kibana
Node2節點(2c/4G) :node2/192.168.23.12 Elasticsearch
Apache 節點:apache/192.168.23.13 Logstash, Apache
Filebeat 節點: filebeat/192.168.23.104 Filebeat

阿帕奇基金會軟件包。但是filebeat軟件需要和elk軟件版本對應

https://archive.apache.org/dist/


5.1 環境部署,安裝filebeat

[root@host104 ~]# systemctl  stop firewalld
[root@host104 ~]# setenforce  0

#將軟件包 filebeat-6.2.4-linux-x86_64.tar.gz 上傳到/opt 目錄下
[root@host104 opt]# cd /opt/ 
[root@host104 opt]# tar xf filebeat-6.2.4-linux-x86_64.tar.gz 
[root@host104 opt]# mv filebeat-6.2.4-linux-x86_64  /usr/local/filebeat



5.2  設置filebeat 的主配置文件,啟動filebeat

[root@host104 opt]# cd /usr/local/filebeat/
[root@host104 filebeat]# vim filebeat.yml 
#yml 文件注意格式,縮進和空格
#=========================== Filebeat prospectors ============
15// filebeat.prospectors:
21// - type: log           #指定log類型,從日志文件中讀取消息
24//   enabled: true       #開啟收集
27//   paths:              #指定收集數據的路徑
28//     - /var/log/messages   #從/var/log/messages中收集
29//     - /var/log/*.log      #從/var/log/目錄中所以以 .log結尾的文件中shoji 
31//   fields:              #指定字段,fields配置選項設置的一些參數字段可以添加到output中,值自定義
32//     service_name: filebeat
33//     log_type: log
34//     service_id: 192.168.23.11

#默認是輸出草ES中,可以將Elasticsearch output 部分的都注釋
#-------Elasticsearch output----------
152// #output.elasticsearch:
154// #  hosts: ["localhost:9200"]

#----------------------------- Logstash output ---
#指定輸出到logstash中
157// output.logstash:
159//   hosts: ["192.168.23.13:5044"]   #指定logstash的ip和端口



[root@host104 filebeat]# ./filebeat  -e -c filebeat.yml

#新開窗口,查看5044端口,這是filebeat的連接端口,連接logstash的5044端口。
#在logstash主機上,也可以查看到 開放了5044端口
[root@host104 ~]# netstat  -natp | grep 5044
tcp        0      0 192.168.23.104:43538    192.168.23.13:5044      ESTABLISHED 46666/./filebeat

image-20210929143001168



5.3在logstash 組件的節點上新建一個logstash配置文件 

[root@apache ~]# cd /etc/logstash/conf.d/
[root@apache conf.d]# vim  logstash.conf
input {
    beats {
        port => "5044"
    }
}
output {
    elasticsearch {
        hosts => ["192.168.23.11:9200"]
 # %{[fields][server_name]} ,引入filebeat配置文件的filds字段配置的service_name字段的值作為索引
        index => "%{[fields][service_name]}-%{+YYYY.MM.dd}"
    }
    stdout {
        codec => rubydebug
    }
}

#啟動logstash
[root@apache conf.d]# logstash -f logstash.conf 



5.4 訪問測試

瀏覽器訪問 http://192.168.23.11:5601 登錄 Kibana,單擊“Create Index Pattern”按鈕添加索引“filebeat-*”,單擊 “create” 按鈕創建,單擊 “Discover” 按鈕可查看圖表信息及日志信息。

[root@host104 ~]# firefox  http://192.168.23.11:5601

image-20210929150155544

image-20210929150336648


端口

服務 端口
elasticsearch 9200
elasticsearch-head 9100
logstash 9600,9601
kibana 5601


免責聲明!

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



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