ELK、ELFK企業級日志分析系統
一、ELK日志分析系統
1. ELK簡介
ELK平台是一套完整的日志集中處理解決方案,將ElasticSearch、Logstash和Kibana三個開源工具配合使用,完成更強大的用戶對日志的查詢、排序、統計需求。
1.2 ElasticSearch
ES是基於Lucene(一個全文檢索引擎的架構)開發的分布式存儲檢索引擎,用來存儲各類日志。
ES是用JAVA開發的,可通過RESTful Web接口,讓用戶可以通過瀏覽器與ES通信。
ES是個分布式搜索和分析引擎,優點是能對大容量的數據進行接近實時的存儲、搜索和分析操作。
1.3 Logstash
Logstash作為數據收集引擎。它支持動態的從各種數據源搜索數據,並對數據進行過濾、分析、豐富、統一格式等操作,然后存儲到用戶指定的位置,一般會發送給ES。
Logstash由JRuby語言編寫,運行在JAVA虛擬機(JVM)上,是一款強大的數據處理工具,可以實現數據傳輸、格式處理、格式化輸出。Logstash具有強大的插件功能,常用於日志處理。
1.4 Kibana
Kibana是基於Node.js開發的展示工具,可以為Logstash和ES提供圖形化的日志分析Web界面展示,可以匯總、分析和搜索重要數據日志。
1.5 Filebeat
Filebeat是一款輕量級的開源日志文件數據搜索器。通常在需要采集數據的客戶端安裝Filebeat,並指定目錄與日志格式,Filebeat就能快速收集數據,並發送給logstash進行解析,或是直接發給ES存儲,性能上相比運行於JVM上的logstash優勢明顯,是對它的替代。
2. ELK的使用原因
日志主要包括日志、應用程序日志和安全日志。系統運維和開發人員可以通過日志了解服務器軟硬件信息、檢查配置過程中的錯誤及錯誤及錯誤發生的原因。經常分析日志可以了解服務器的負荷,性能安全性,從而及時采取措施糾正錯誤。
往往單台機器的日志我們使用grep、awk等工具就能基本實現簡單分析,但是當日志被分散的儲存不同的設備上。如果你管理數十上百台服務器,你還在使用依次登錄每台機器的傳統方法查閱匯總。集中化管理日志后,日志的統計和檢索又成為一件比較麻煩的事情,一般我們使用grep、awk和wc等linux命令能實現檢索和統計,但是對於要求更高的查詢、排序和統計等要求和龐大的機器數量依然使用這樣的方法難免有點力不從心。
一般大型系統是一個分布式部署的架構,不同的服務模塊部署在不同的服務器上,問題出現時,大部分情況需要根據問題暴露的關鍵信息,定位到具體的服務器和服務模塊,構建一套集中式日志系統,可以提高定位問題的效率。
3. 完整日志系統的基本特征
● 收集:能夠采集多種來源的日志數據
● 傳輸:能夠穩定的把日志數據解析過濾並傳輸到存儲系統
● 存儲:存儲日志數據
● 分析:支持UI分析
● 警告:能夠提供錯誤報告,監控機制
4. ELK的工作原理
(1)在所有需要收集日志的服務器上部署Logstash;或者先將日志進行集中化管理在日志服務器上,在日志服務器上部署Logstash。
(2)Logstash收集日志,將日志格式化並輸出到es群集中。
(3)ES對格式化后的數據進行索引和存儲。
(4)Kibana從ES群集中查詢數據生成圖表,並進行前端數據的顯示。
Node.js Mirror (npmmirror.com) nodev8.2.1下載地址
下載 | PhantomJS (kuryun.com) phantomjs-2.1.1下載地址
Past Releases of Elastic Stack Software | Elastic filebeat下載地址
Release 5.0.0 · mobz/elasticsearch-head · GitHub
最終准備這些軟件
二、部署ELK日志分析系統
1. 服務器配置
服務器 |
配置 |
主機名 |
ip地址 |
主要軟件部署 |
node1節點 |
2C/4G |
node1 |
192.168.10.2 |
ElasticSearch、Kibana |
node2節點 |
2C/4G |
node2 |
192.168.10.3 |
ElasticSearch |
apache節點 |
- |
apache |
192.168.10.4 |
Logstash、Apache |
2. 關閉防火牆
systemctl stop firewalld
setenforce 0
setenforce: SELinux is disabled
3. ELK ElasticSearch集群部署(在node1、node2節點上操作)
3.1 環境准備(更改主機名、配置域名解析、查看Java環境)在所有節點上做
以node1為例,
[root@node1 ~]# echo "192.168.10.2 node1" >> /etc/hosts
[root@node1 ~]# echo "192.168.10.3 node2" >> /etc/hosts
[root@node1 ~]# java -version
-bash: java: 未找到命令
[root@node1 ~]# yum -y install -y java &>/dev/null
[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)
3.2 部署ElasticSearch軟件 在所有節點上做
3.2.1 安裝elasticsearch-rpm包
以node1為例
[root@node1 ~]# cd /opt/
[root@node1 opt]# ls
elasticsearch-5.5.0.rpm redis-5.0.7 redis-5.0.7.tar.gz
[root@node1 opt]# rpm -ivh elasticsearch-5.5.0.rpm
3.2.2 加載系統服務
以node1為例
[root@node1 opt]# systemctl daemon-reload
[root@node1 opt]# systemctl enable elasticsearch.service
Created symlink from /etc/systemd/system/multi-user.target.wants/elasticsearch.service to /usr/lib/systemd/system/elasticsearch.service.
3.2.3 修改elasticsearch主配置文件
以node1為例
[root@node1 opt]# cd /etc/elasticsearch/
[root@node1 elasticsearch]# cp elasticsearch.yml elasticsearch.yml.bak
[root@node1 elasticsearch]# vim elasticsearch.yml
##17行,取消注釋,指定群集名稱
cluster.name: my-elk-cluster
##23行,取消注釋,指定節點名稱(node1節點為node1,node2節點為node2)
node.name: node1
##33行,取消注釋,指定數據存放路徑
path.data: /data/elk_data
##37行,取消注釋,指定日志存放路徑
path.logs: /var/log/elasticsearch/
##43行,取消注釋,改為在啟動的時候不鎖定內存,開啟為true
bootstrap.memory_lock: false
##55行,取消注釋,設置監聽地址,0.0.0.0代表所有地址
network.host: 0.0.0.0
##59行,取消注釋,ES服務的默認監聽端口為9200
http.port: 9200
##68行,取消注釋,集群發現通過單播實現,指定要發現的節點node1、node2
discovery.zen.ping.unicast.hosts: ["node1", "node2"]
[root@node1 elasticsearch]# grep -v "^#" /etc/elasticsearch/elasticsearch.yml
cluster.name: my-elk-cluster
node.name: node1
path.data: /data/elk_data
path.logs: /var/log/elasticsearch/
bootstrap.memory_lock: false
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["host1", "host2"]
這里就不截圖了
3.2.4 創建數據存放路徑並授權
以node1為例
[root@node1 ~]# mkdir -p /data/elk_data
[root@node1 ~]# chown elasticsearch:elasticsearch /data/elk_data/
3.2.5 啟動elasticsearch
以node1為例
[root@node1 ~]# systemctl start elasticsearch
[root@node1 ~]# netstat -natp | grep 9200
tcp6 0 0 :::9200 :::* LISTEN 3128/java
3.2.6 查看節點信息
瀏覽器訪問http://192.168.10.2:9200、http://192.168.10.3:9200查看節點node1、node2的信息
瀏覽器訪問http://192.168.10.2:9200/_cluster/health?pretty
、http://192.168.10.3:9200/_cluster/health?pretty
查看群集的健康情況,可以看到status值為green(綠色),表示節點健康運行
瀏覽器訪問http://192.168.10.2:9200/_cluster/state?pretty
、http://192.168.10.3:9200/_cluster/state?pretty檢查群集狀態信息
使用上述方式查看群集的狀態對用戶並不友好,可以通過安裝Elasticsearch-head插件,更方便的對群集進行管理。
3.3 安裝Elasticsearch-head插件 所有節點都做
ES在5.0版本后,插件需要作為獨立服務進行安裝,需要使用npm工具(NodeJS的包管理工具)安裝。安裝Elasticsarch-head需要提前安裝好依賴軟件node和phantomjs。
● node
是一個基於Chrome V8引擎的JavaScript運行環境
● phantomjs
是一個基於webkit的JavaScriptAPI,可以理解為一個隱形的瀏覽器,任何基於webkit瀏覽器做的事情,它都可以做到。
3.3.1 編譯安裝node
以node1為例
[root@node1 ~]# cd /opt/
[root@node1 opt]# ls
elasticsearch-5.5.0.rpm node-v8.2.1.tar.gz elasticsearch-head-5.0.0.tar.gz phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node1 opt]# yum -y install gcc gcc-c++ make &>/dev/null
[root@node1 opt]# tar xf node-v8.2.1.tar.gz
[root@node1 opt]# cd node-v8.2.1
[root@node1 node-v8.2.1]# ./configure
[root@node1 node-v8.2.1]# make -j 2 && make install
3.3.2 安裝phantomjs
以node1為例
yum -y install bzip2
cd /opt
tar xf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C /usr/local/src
cd /usr/local/src/phantomjs-2.1.1-linux-x86_64/bin
cp phantomjs /usr/local/bin
3.3.3 安裝Elasticsearch-head數據可視化工具
以node1為例
cd /opt
tar xf elasticsearch-head-5.0.0.tar.gz -C /usr/local/src/
cd /usr/local/src/elasticsearch-head-5.0.0/
npm install
3.3.4 修改Elasticsearch主配置文件
以node1為例
vim /etc/elasticsearch/elasticsearch.yml
末行添加
http.cors.enabled: true ##開啟跨域訪問支持,默認為false
http.cors.allow-origin: "*" ##指定跨域訪問允許的域名地址為所有
systemctl restart elasticsearch
3.3.5 啟動elasticsearch-head服務
以node1為例
cd /usr/local/src/elasticsearch-head-5.0.0
npm run start &
[1] 46748
[root@node1 elasticsearch-head-5.0.0]#
> elasticsearch-head@0.0.0 start /usr/local/src/elasticsearch-head-5.0.0
> grunt server
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
^C
netstat -natp | grep 9100
tcp 0 0 0.0.0.0:9100 0.0.0.0:* LISTEN 46758/grunt
注:必須在解壓后的elasticsearch-head-5.0.0目錄下啟動服務,進程會讀取該目錄下的gruntfile.js文件,否則可能啟動失敗。
3.3.6 通過Elasticsearch-head查看ES信息
通過瀏覽器訪問http://192.168.10.2:9100地址並連接群集。如果看到群集健康值為green,代表群集很健康。
3.3.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
}
3.3.8 瀏覽器查看索引信息
瀏覽器訪問http://192.168.10.2:9100查看索引信息,可以看見索引默認被分片為5個,並且有一個副本。
點擊"數據瀏覽",會發現在node1上創建的索引為index-demo,類型為test的相關信息。
4. ELK-Logstash部署(在Apache節點上操作)
Logstash一般部署在需要監控其日志的服務器。在本案例中,Logstash部署在Apache服務器上,用於收集Apache的日志信息並發送到Elasticsearch。
4.1 更改主機名
4.2 安裝Apache服務(httpd)
[root@apache ~]# yum -y install httpd &>/dev/null
[root@apache ~]# systemctl start httpd
[root@apache ~]# netstat -apnt |grep httpd
tcp6 0 0 :::80 :::* LISTEN 1930/httpd
4.3 安裝Java環境
[root@apache ~]# yum -y install java &>/dev/null
[root@apache ~]# 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)
4.4 安裝logstash
[root@apache ~]# cd /opt/
[root@apache opt]# ls
logstash-5.5.1.rpm
[root@apache opt]# rpm -ivh logstash-5.5.1.rpm
警告:logstash-5.5.1.rpm: 頭V4 RSA/SHA512 Signature, 密鑰 ID d88e42b4: NOKEY
准備中... ################################# [100%]
正在升級/安裝...
1:logstash-1:5.5.1-1 ################################# [100%]
Using provided startup.options file: /etc/logstash/startup.options
Successfully created system startup script for Logstash
[root@apache opt]# systemctl start logstash
[root@apache opt]# systemctl enable logstash
Created symlink from /etc/systemd/system/multi-user.target.wants/logstash.service to /etc/systemd/system/logstash.service.
[root@apache opt]# ln -s /usr/share/logstash/bin/logstash /usr/local/bin/
ln -s /etc/logstash/log* /usr/share/logstash/config/
chown -R logstash:logstash /usr/share/logstash/
4.5 測試Logstash
4.5.1 Logstash命令常用選項
Logstash命令常用選項 |
說明 |
-f |
通過這個選項可以指定Logstash的配置文件,根據配置文件配置Logstash的輸入和輸出流。 |
-e |
從命令行中獲取,輸入、輸出后面跟着字符串,該字符串可以被當做Logstash的配置(如果是空,則默認使用stdin作為輸入,stdout作為輸出)。 |
-t |
測試配置文件食肉正確,然后退出。 |
4.5.2 定義輸入和輸出流
4.5.2.1 標准輸入、輸出
輸入采用標准輸入,輸出采用標准輸出(類似管道)
[root@apache opt]# logstash -e 'input { stdin{} } output { stdout{} }'
……
The stdin plugin is now waiting for input:
20:26:17.422 [Api Webserver] INFO logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.test.com
#鍵入內容(標准輸入)
2021-10-02T12:27:03.538Z apache www.test.com
#輸入結果(標准輸出)
4.5.2.2 rubydebug輸出
使用rubydebug輸出詳細格式顯示,codec為一種編解碼器
[root@apache opt]# logstash -e 'input { stdin{} } output { stdout{ codec=>rubydebug } }'
The stdin plugin is now waiting for input:
20:37:23.433 [Api Webserver] INFO logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.test.com
#鍵入內容(標准輸入)
{
#輸出結果(處理后的結果)
"@timestamp" => 2021-10-02T12:39:01.630Z,
"@version" => "1",
"host" => "apache",
"message" => "www.test.com"
}
4.5.2.3 輸出到ES
使用logstash將嘻嘻寫入到ES中
logstash -e 'input { stdin{} } output { elasticsearch { hosts=>["192.168.10.2:9200"] } }'
……
The stdin plugin is now waiting for input:
20:44:06.190 [Api Webserver] INFO logstash.agent - Successfully started Logstash API endpoint {:port=>9600}
www.test.com
#鍵入內容(標准輸出
結果不在標准輸出顯示,而是發送至ES,可瀏覽器訪問http://192.168.10.2:9100查看索引和數據
4.6 定義logstash配置文件
logstash配置文件基本由三部分組成:input輸入、output輸出以及filter過濾(可選,根據需要選擇使用)。
格式如下:
input {...}
output {...}
filter {...}
在每個部分中,也可以指定多個訪問方式。例如,若要指定兩個日志來源文件,則格式如下:
input {
file { path =>"/var/log/messages" type =>"syslog"}
file { path =>"/var/log/httpd/access.log" type =>"apache"}
}
修改logstash配置文件,讓其收集系統日志/var/log/messages,並將其輸出到ES中。
chmod +r /var/log/messages
[root@apache conf.d]# pwd
/etc/logstash/conf.d
[root@apache conf.d]# vim system.conf
##該文件需自行創建,文件名可自定義
input {
file{
path =>"/var/log/messages"
##指定要收集的日志的位置
type =>"system"
##自定義日志類型標識
start_position =>"beginning"
##表示從開始處收集
}
}
output {
elasticsearch{
##輸出到ES
hosts =>["192.168.122.10:9200", "192.168.122.11:9200"]
##指定ES服務器的地址和端口,為避免單機故障,建議寫全
index =>"system-%{+YYYY.MM.dd}"
##指定輸出到ES的索引格式
}
}
systemctl restart logstash
4.7 訪問測試
瀏覽器訪問http://192.168.10.2:9100查看索引信息
5. ELK-Kibana部署(在node1節點上操作)
5.1 安裝Kibana
[root@node1 ~]# cd /opt/
[root@node1 opt]# ls
elasticsearch-5.5.0.rpm kibana-5.5.1-x86_64.rpm node-v8.2.1.tar.gz
elasticsearch-head-5.0.0.tar.gz node-v8.2.1 phantomjs-2.1.1-linux-x86_64.tar.bz2
[root@node1 opt]# rpm -ivh kibana-5.5.1-x86_64.rpm
5.2 設置Kibana的主配置文件
[root@node1 opt]# cp /etc/kibana/kibana.yml /etc/kibana/kibana.yml.bak
[root@node1 opt]# vim /etc/kibana/kibana.yml
##2行,取消注釋,kibana服務的默認監聽端口為5601
server.port: 5601
##7行,取消注釋,設置kibana的監聽地址,0.0.0.0代表所有地址
server.host: "0.0.0.0"
##21行,取消注釋,設置和ES建立連接的地址和端口
elasticsearch.url: "http://192.168.10.2:9200"
##30行,取消注釋,設置在ES中添加.kibana索引
kibana.index: ".kibana"
5.3 啟動kibana服務
[root@node1 opt]# systemctl start kibana
[root@node1 opt]# systemctl enable kibana
Created symlink from /etc/systemd/system/multi-user.target.wants/kibana.service to /etc/systemd/system/kibana.service.
[root@node1 opt]# netstat -anpt |grep 5601
tcp 0 0 0.0.0.0:5601 0.0.0.0:* LISTEN 57074/node
5.4 驗證kibana
第一次登錄需要添加一個ES索引
輸入system-*
索引添加完成后,點擊"Discover"按鈕可查看圖表信息及日志信息
數據展示可以分類顯示,例如:在"Available Fileds"中的"host"
5.5 將Apache服務器的日志(訪問的、錯誤的)添加到ES並通過kibana顯示
apache服務器
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.10.2:9200", "192.168.10.3:9200"]
index => "apache_access-%{+YYYY.MM.dd}"
}
}
if [type] == "error" {
elasticsearch {
hosts => ["192.168.10.2:9200", "192.168.10.3:9200"]
index => "apache_error-%{+YYYY.MM.dd}"
}
}
/usr/share/logstash/bin/logstash -f apache_log.conf
5.6 瀏覽器訪問
瀏覽器訪問http://192.168.10.2:9100查看索引是否創建
瀏覽器訪問http://192.168.10.2:5601登錄kibana,添加
"apache_access-、*"和"apache_error-*"索引,查看日志信息。
切換和創建索引都在這
三、ELFK(Filebeat+ELK)
1. Filebeat的作用
由於logstash會大量占用系統的內存資源,一般我們會使用filebeat替換logstash收集日志的功能,組成ELFK架構。
或用fluentd替代logstash組成EFK(elasticsearch/fluentd/kibana),由於fluentd是由Go語言開發的,一般在K8S環境中使用較多。
2. ELFK工作流程
(1)filebeat將日志收集后交由logstash處理
(2)logstash進行過濾、格式化等操作,滿足過濾條件的數據將發送給ES
(3)ES對數據進行分片存儲,並提供索引功能
(4)kibana對數據進行圖形化的web展示,並提供索引接口
3. ELFK的部署
3.1 服務器配置
服務器 |
配置 |
主機名 |
ip地址 |
主要軟件部署 |
node1節點 |
2C/4G |
node1 |
192.168.10.2 |
ElasticSearch、Kibana |
node2節點 |
2C/4G |
node2 |
192.168.10.3 |
ElasticSearch |
apache節點 |
- |
apache |
192.168.10.4 |
Logstash、Apache |
filebeat節點 |
- |
filebeat |
192.168.10.5 |
Filebeat |
在ELK的基礎上,增加一台filebeat服務器,因此只需再前述ELK部署的前提下進一步操作。 |
3.2 服務器環境
filebeat節點
[root@localhost ~]# hostnamectl set-hostname filebeat
[root@localhost ~]# su
[root@filebeat ~]# systemctl stop firewalld
[root@filebeat ~]# systemctl disable firewalld
[root@filebeat ~]# setenforce 0
3.3 安裝filebeat
filebeat節點
[root@filebeat opt]# ls
filebeat-6.2.4-linux-x86_64.tar.gz
[root@filebeat opt]# tar xf filebeat-6.2.4-linux-x86_64.tar.gz
[root@filebeat opt]# mv filebeat-6.2.4-linux-x86_64 /usr/local/filebeat
3.4 修改filebeat主配置文件
filebeat節點
[root@filebeat opt]# cd /usr/local/filebeat/
[root@filebeat filebeat]# cp filebeat.yml filebeat.yml.bak
[root@filebeat filebeat]# vim filebeat.yml
filebeat.prospectors:
##21行,指定log類型,從日志文件中讀取消息
- type: log
##24行,開啟日志收集功能,默認為false
enabled: true
##28行,指定監控的日志文件
- /var/log/*.log
##29行,添加收集/var/log/messages
- /var/log/messages
##31行,添加以下內容,注意格式
fields:
service_name: filebeat
log_type: log
service_id: 192.168.10.5
#-------------------------- Elasticsearch output ------------------------------
該區域內容全部注釋
#----------------------------- Logstash output --------------------------------
##157行,取消注釋
output.logstash:
##159行,取消注釋,指定logstash的IP和端口號
hosts: ["192.168.10.4:5044"]
./filebeat -e -c filebeat.yml
3.5 在logstash組件所在節點(apache節點)上新建一個logstash配置文件
systemctl stop logstash
[root@apache conf.d]# pwd
/etc/logstash/conf.d
[root@apache conf.d]# vim logstash.conf
input {
beats {
port => "5044"
}
}
output {
elasticsearch {
hosts => ["192.168.10.2:9200", "192.168.10.3:9200"]
index => "%{[fields][service_name]}-%{+YYYY.MM.dd}"
}
stdout {
codec => rubydebug
}
}
/usr/share/logstash/bin/logstash -f logstash.conf
3.6 瀏覽器驗證
瀏覽器訪問http://192.168.10.2:5601登錄kibana,
添加"filebeat-*"索引后在"Discover"中查看filebeat日志收集情況。