ELK實踐(一):基礎入門
雖然用了ELK很久了,但一直苦於沒有自己嘗試搭建過,所以想抽時間嘗試嘗試。原本打算按照教程 《ELK集中式日誌平臺之二 — 部署》(作者:樊浩柏科學院) 進行測試的,沒想到一路出了很多坑,所以又按照自己成功搭建的流程寫了本文。
《ELK集中式日誌平臺之二 — 部署》一文參考價值非常大,圖文並茂,大家可以在學習過程中參考參考。作者一上來就安裝ELK外掛,實際可以按需選擇安裝,但作為初學者,無疑增加了複雜度。ELK外掛後續我會單獨拿出來講解,本文略過。
簡介
ELK 指的是一套解決方案,是 Elasticsearch、Logstash 和 Kibana 三種軟體產品的首字母縮寫,Beats 是 ELK 協議棧的新成員。
- E:代表 Elasticsearch,負責日誌的儲存和檢索;
- L:代表 Logstash,負責日誌的收集、過濾和格式化;
- K:代表 Kibana,負責日誌資料的視覺化;
- Beats:是一類輕量級資料採集器;
本來ELK是沒有Beats的。
最開始的架構中,由 Logstash 承擔資料採集器和過濾功能,並部署在應用伺服器。由於 Logstash 對大量日誌進行過濾操作,會消耗應用系統的部分效能,帶來不合理的資源分配問題;另一方面,過濾日誌的配置,分佈在每臺應用伺服器,不便於集中式配置管理。
所以就有了Beats。
由於 Beats 的系統性能開銷更小,所以應用伺服器效能開銷可以忽略不計;另一方面,Beats 可以作為資料採集外掛形式工作,可以按需啟用 Beats 下不同功能的外掛,更靈活,擴充套件性更強。例如,應用伺服器只啟用 Filebeat,則只收集日誌檔案資料,如果某天需要收集系統性能資料時,再啟用 Metricbeat 即可,並不需要太多的修改和配置。
其中,目前 Beats 家族根據功能劃分,主要有:
Filebeat
Real-time insight into log data.
負責收集檔案資料。Packetbeat
Analyze network packet data.
負責收集網路流量資料。Winlogbeat
Analyze Windows event logs.
負責收集 Windows 事件日誌資料。Metricbeat
Ship and analyze metrics.
負責收集系統級的 CPU 使用率、記憶體、檔案系統、磁碟 IO 和網路 IO 統計資料。Heartbeat
Ping your Infrastructure.Auditbeat
Send audit data to Elasticsearch.
如果日誌量巨大,可能還會引入Kafka用以均衡網路傳輸,從而降低了網路閉塞,尤其是丟失資料的可能性;另一方面,這樣可以系統解耦,具有更好的靈活性和擴充套件性。
環境說明
環境:CentOS6.8
elk版本
ELK各自版本最好都是一致的,否則可能會有相容性問題。
本次都是使用5.6.2版本。截止到2018-09-23,官網的最新版本是6.4.1。
官網下載地址:
https://www.elastic.co/downloads
- elasticsearch
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.2.tar.gz - kibana
https://artifacts.elastic.co/downloads/kibana/kibana-5.6.2-linux-x86_64.tar.gz - logstash
https://artifacts.elastic.co/downloads/logstash/logstash-5.6.2.tar.gz - filebeat
https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.6.2-linux-x86_64.tar.gz
歷史版本彙集頁:
https://www.elastic.co/downloads/past-releases
安裝方式
安裝有兩種方法:
- yum安裝
- 二進位制包手動安裝
建議第二種方法,可以自由安裝版本及定義安裝目錄。
啟動方式
另外需要注意的是:ELK系列軟體啟動服務的時候不允許以root使用者啟動。這裡我們統一建立使用者 elk
:
useradd elk
Mac因為預設使用者就不是root,可以忽略這一步。
啟動軟體的時候,有2種方式啟動:
- 切換到elk使用者執行命名
- 配置service,使用
service
啟動
測試的時候建議以第一種,這樣可以及時檢視錯誤日誌。生產環境可以配置service
啟動。
特點
1、ElasticSearch、Kibana都支援安裝外掛:例如x-pack。
2、ElasticSearch、Kibana的配置檔案一般在安裝目錄的config裡(yml格式),二進位制檔案在bin裡。ElasticSearch和Logstatsh config目錄都有jvm.options
,測試環境建議修改:
-Xms256m
-Xmx1g
預設可能是2g,測試環境改小一點。後面這點還會說明。
配置JDK
安裝JDK1.8:
yum install -y java-1.8.0-openjdk
配置環境變數:
vi /etc/profile
增加:
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el6_10.x86_64
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
JAVACMD=/usr/bin/java
export JAVA_HOME JAVACMD CLASSPATH PATH
然後使之生效:
source /etc/profile
驗證版本資訊:
# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
安裝 ElasticSearch
二進位制安裝
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.2.tar.gz
大小32.2M。安裝很簡單,解壓即可:
mkdir -p /usr/local/elk
tar zxvf elasticsearch-5.6.2.tar.gz -C /usr/local/elk
mv /usr/local/elk/elasticsearch-5.6.2 /usr/local/elk/elasticsearch
配置許可權:
chown -R elk:elk /usr/local/elk/elasticsearch
簡單配置
對目錄/usr/local/elk/elasticsearch
的增刪改操作建議使用上面建立的elk使用者,否則還要使用chown命令修改許可權。
su -e elk
修改config/jvm.options
:
vi /usr/local/elk/elasticsearch/config/jvm.options
根據實際情況修改:預設是2g:
# 最小值
-Xms256m
#最大值
-Xmx1g
我測試的時候忘記修改了,測試機器卡死了。
啟動
如果沒有配置service
啟動方式,需要切換使用者。
su - elk
# -d表示後臺執行
/usr/local/elk/elasticsearch/bin/elasticsearch -d
檢視是否已執行:
$ netstat -tulnp | grep 9200
tcp 0 0 127.0.0.1:9200 0.0.0.0:* LISTEN 121/java
測試
預設是 9200 埠。如果需要修改,可以編輯config/elasticsearch.yml
。
$ curl http://127.0.0.1:9200
{
"name" : "6e1PMnr",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "Uahv9iLnSLyMjIxX57lbMQ",
"version" : {
"number" : "5.6.2",
"build_hash" : "bd92e7f",
"build_date" : "2017-12-17T20:23:25.338Z",
"build_snapshot" : false,
"lucene_version" : "7.1.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
防盜版宣告:本文系原創文章,釋出於公眾號飛鴻影的部落格
(fhyblog)及部落格園,轉載需作者同意。
安裝 Kibana
Kibana負責日誌資料的視覺化。Kibana從ElasticSearch裡讀取資料。
二進位制安裝
wget https://artifacts.elastic.co/downloads/kibana/kibana-5.6.2-linux-x86_64.tar.gz
大小48.5M。同樣是解壓:
tar zxvf kibana-5.6.2-linux-x86_64.tar.gz -C /usr/local/elk/
mv /usr/local/elk/kibana-5.6.2-linux-x86_64 /usr/local/elk/kibana
chown -R elk:elk /usr/local/elk/kibana
簡單配置
對目錄/usr/local/elk/kibana
的增刪改操作建議使用上面建立的elk使用者,否則還要使用chown命令修改許可權。
su -e elk
修改配置:
vim /usr/local/elk/kibana/config/kibana.yml
預設配置都是註釋了的,預設埠是5601,預設訪問 elasticsearch 的9200埠。我們可以按需修改。例如:
server.port: 5601 # 監聽埠
server.host: "0.0.0.0" # 繫結地址
#server.name: "elk.fanhaobai.com" # 域名
#elasticsearch.url: "http://127.0.0.1:9200" # es
#kibana.index: ".kibana" # 索引名
#elasticsearch.username: "elastic" # 使用者名稱
#elasticsearch.password: "changeme" # 密碼
這裡修改了server.host
,預設是localhost
。
啟動kibana
/usr/local/elk/kibana/bin/kibana
注意:預設是前臺執行的,如果需要後臺執行:
nohup /usr/local/elk/kibana/bin/kibana &
可以使用前臺執行的方式,沒有報錯誤,然後使用後臺執行的方式。
檢視是否已執行:
$ netstat -tulnp | grep 5601
tcp 0 0 0.0.0.0:5601 0.0.0.0:* LISTEN 248/./kibana/bin/..
配置web
預設開啟瀏覽器 http://127.0.0.1:5601 就可以看到 kibana 的介面了:
也可以配置Nginx代理到別的域名:
server {
listen 80;
server_name elk.cc; #當前主機名
# auth_basic "Restricted Access";
# auth_basic_user_file /usr/local/nginx/conf/htpasswd.users; #登入驗證
location / {
proxy_pass http://127.0.0.1:5601; #轉發到kibana
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
到現在還沒有資料可以展示,因為ES裡我們還沒有匯入或者傳入資料。接下來我們使用 Logstash+Filebeat 匯入日誌資料。
安裝 Logstash
二進位制方式安裝
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.6.2.tar.gz
tar zxvf logstash-5.6.2-linux.gz -C /usr/local/elk/
mv /usr/local/elk/logstash-5.6.2/ /usr/local/elk/logstash
chown -R elk:elk /usr/local/elk/logstash
logstash-5.6.2.tar.gz 大小96.8M。
簡單配置
對目錄/usr/local/elk/logstash
的增刪改操作建議使用上面建立的elk使用者,否則還要使用chown命令修改許可權。
# 切換使用者
su - elk
1、修改 config/jvm.options
:
vim /usr/local/elk/logstash/config/jvm.options
這裡我修改為:
-Xms128m
-Xmx512m
2、修改 config/logstash.yml
:
vim /usr/local/elk/logstash/config/logstash.yml
預設全是註釋的,這裡僅修改一處:
path.config: /usr/local/elk/logstash/config/conf.d
這樣就支援子配置了,我們可以把新建的配置都放在conf.d
目錄裡。
注意:
path.config:
後面有一個空格。
然後建立子資料夾:
mkdir /usr/local/elk/logstash/config/conf.d
測試logstash
由於現在我們還沒有安裝filebeat,也不打算直接使用logstash收集日誌,所以先簡單測試下標準輸入輸出,只要正常就行了:
$ ./logstash/bin/logstash -e 'input { stdin { } } output { stdout {}}'
稍等幾秒鐘:
Sending Logstash's logs to /usr/local/elk/logstash/logs which is now configured via log4j2.properties
[2018-09-24T23:07:35,424][INFO ][logstash.modules.scaffold] Initializing module {:module_name=>"netflow", :directory=>"/usr/local/elk/logstash/modules/netflow/configuration"}
[2018-09-24T23:07:35,434][INFO ][logstash.modules.scaffold] Initializing module {:module_name=>"fb_apache", :directory=>"/usr/local/elk/logstash/modules/fb_apache/configuration"}
[2018-09-24T23:07:35,657][INFO ][logstash.pipeline ] Starting pipeline {"id"=>"main", "pipeline.workers"=>4, "pipeline.batch.size"=>125, "pipeline.batch.delay"=>5, "pipeline.max_inflight"=>500}
[2018-09-24T23:07:35,683][INFO ][logstash.pipeline ] Pipeline main started
The stdin plugin is now waiting for input:
[2018-09-24T23:07:35,776][INFO ][logstash.agent ] Successfully started Logstash API endpoint {:port=>9600}
hello
2018-09-24T15:07:42.760Z 2106567e5bce hello
welcome
2018-09-24T15:07:49.501Z 2106567e5bce welcome
我們輸入了hello、welcome,終端實時的輸出了內容。後面的內容裡,我們將會修改 stdin 為為beats,修改 stdout 為ElasticSearch。
如果新增配置,需要後臺常駐執行,可以使用下列命令:
/usr/local/elk/logstash/bin/logstash &
檢視是否已執行(需要過10s左右,可以多刷幾次):
$ netstat -tulnp | grep 5044
tcp 0 0 0.0.0.0:5044 0.0.0.0:* LISTEN 1040/java
安裝 Filebeat
Filebeat不依賴JDK。
二進位制安裝
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.6.2-linux-x86_64.tar.gz
filebeat 8.4M。解壓、設定 :
mkdir /usr/local/elk/beats
tar zxvf filebeat-5.6.2-linux-x86_64.tar.gz -C /usr/local/elk/beats/
mv /usr/local/elk/beats/filebeat-5.6.2-linux-x86_64/ /usr/local/elk/beats/filebeat
chown -R elk:elk /usr/local/elk/beats
注意:後續對目錄/usr/local/elk/beats
的增刪改操作建議使用上面建立的elk使用者,否則還要使用chown命令修改許可權。
# 切換使用者
su - elk
filebeat採集內容傳送到ElasticSearch
為了讓測試簡單,我們手動模擬日誌的生成:
echo "test - test2" >> /tmp/test1.log
echo "test - test2" >> /tmp/test2.log
生成了2個日誌檔案test1.log
、test2.log
,各有一行日誌。
新建一個filebeat配置檔案:
cd /usr/local/elk
vim beats/filebeat/filebeat.test.yml
配置內容:
filebeat.prospectors:
- type: log
paths:
- /tmp/test1.log
tags: ["test1"]
document_type: test1
- type: log
paths:
- /tmp/test2.log
tags: ["test2"]
document_type: test2
output.elasticsearch:
hosts: ["127.0.0.1:9200"]
index: "test-filebeat"
配置說明:
filebeat.prospectors:
- type 日誌型別,預設log
- input_type 輸入型別,預設log
- paths 採集的日誌,可以使用萬用字元。支援多個
- tags 自定義標籤,是個陣列。自定義
- document_type 自定義欄位,用於Logsatsh區分來源,在Logsatsh裡用變數
type
表示
一個-
表示一個filebeat.prospector
,這裡設定了2個。日誌傳送到elasticsearch,索引index 是test-filebeat
。
我們執行 filebeat:
# 修改許可權
chmod 600 beats/filebeat/filebeat.test.yml
# 指定配置檔案前臺執行
./beats/filebeat/filebeat -c beats/filebeat/filebeat.test.yml
我們新開終端檢視ES裡是否新增了內容:
curl http://127.0.0.1:9200/test-filebeat/_search?q=*
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test-filebeat",
"_type": "test2",
"_id": "AWYL9s4WJzfnbYlB_DSO",
"_score": 1,
"_source": {
"@timestamp": "2018-09-24T14:23:30.652Z",
"beat": {
"hostname": "2106567e5bce",
"name": "2106567e5bce",
"version": "5.6.2"
},
"input_type": "log",
"message": "test - test2",
"offset": 13,
"source": "/tmp/test2.log",
"tags": [
"test2"
],
"type": "test2"
}
},
{
"_index": "test-filebeat",
"_type": "test1",
"_id": "AWYL9s4WJzfnbYlB_DSP",
"_score": 1,
"_source": {
"@timestamp": "2018-09-24T14:23:30.652Z",
"beat": {
"hostname": "2106567e5bce",
"name": "2106567e5bce",
"version": "5.6.2"
},
"input_type": "log",
"message": "test - test2",
"offset": 13,
"source": "/tmp/test1.log",
"tags": [
"test1"
],
"type": "test1"
}
}
]
}
}
新開命令列追加一行日誌:
echo "new msg" >> /tmp/test1.log
curl http://127.0.0.1:9200/test-filebeat/_search?q=*&size=1&[email protected]:desc
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "test-filebeat",
"_type": "test1",
"_id": "AWYL-BjvJzfnbYlB_DSQ",
"_score": null,
"_source": {
"@timestamp": "2018-09-24T14:24:55.654Z",
"beat": {
"hostname": "2106567e5bce",
"name": "2106567e5bce",
"version": "5.6.2"
},
"input_type": "log",
"message": "new msg",
"offset": 21,
"source": "/tmp/test1.log",
"tags": [
"test1"
],
"type": "test1"
},
"sort": [
1537799095654
]
}
]
}
}
這說明直接使用filebeat,也是可以傳送資料到ES的,為什麼還用logstash呢?原因是filebeat採集的是原始日誌內容,傳送到ES的也是原始內容,如果不需要處理的話,使用filebeat也行。
我們可以看到返回的文件json裡有一個欄位 message
,這個是日誌原始內容。filebeat還預設加了一些欄位:
- @timestamp 時間
- beat filebeat相關資訊,陣列型別
- input_type 日誌型別,一般是log
- message 日誌原文
- offset 當前採集的日誌的偏移量
- source 日誌所在檔案
- tags 自定義標籤,陣列型別
- type document_type欄位定義的內容
kibana裡檢視
開啟kibana web地址:http://127.0.0.1:5601,依次開啟:Management
-> Kibana
-> Index Patterns
,選擇Create Index Pattern
:
a. Index pattern 輸入:test-filebeat
;
b. Time Filter field name 選擇 @timestamp
。
c. 點選Create。
然後開啟Discover,選擇 test-filebeat 就能看到日誌資料了。
這時候我們去訪問下自己配置了nginx日誌的應用,這裡能實時更新,再也不用去命令列使用tail查看了。
filebeat內容傳送到logstash
接下來,我們將日誌使用filebeat傳送到logstash,然後通過logstash處理後傳送到ElasticSearch。
首先我們要修改上一節裡filebeat的配置:
vim beats/filebeat/filebeat.test.yml
改為:
filebeat.prospectors:
- type: log
paths:
- /tmp/test1.log
tags: ["test1"]
document_type: test1
- type: log
paths:
- /tmp/test2.log
tags: ["test2"]
document_type: test2
output.logstash:
hosts: ["127.0.0.1:5046"]
#output.elasticsearch:
# hosts: ["127.0.0.1:9200"]
# index: "test-filebeat"
我們把output.elasticsearch
註釋了,新增了output.logstash
。
然後新增logstash配置:
vim logstash/config/conf.d/filebeat.test.conf
input {
beats {
port => 5046
}
}
output {
elasticsearch {
hosts => ["127.0.0.1:9200"]
index => "test-filebeat-%{type}"
}
stdout { codec => rubydebug }
}
這裡的type
變數就是filebeat裡面的document_type
。埠指定為5046(自定義即可),和filebeat裡面配置的一致。logstash可以有多個子配置,所以也就能配置多個埠。此時,logstash是作為服務端執行的,filebeat是客戶端。
接下來我們啟動logstash和filebeat:
./logstash/bin/logstash &
&
表示後臺執行:
./beats/filebeat/filebeat -c beats/filebeat/filebeat.test.yml
我們新開終端往日誌里加點新內容:
echo "new msg" >> /tmp/test2.log
echo "new msg3" >> /tmp/test2.log
稍等幾秒鐘,我們可以檢視ES裡的資料:
curl http://127.0.0.1:9200/test-filebeat-test2/_search?q=*&[email protected]:desc
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "test-filebeat-test2",
"_type": "test2",
"_id": "AWYMF-PeJzfnbYlB_DSo",
"_score": null,
"_source": {
"@timestamp": "2018-09-24T14:59:38.188Z",
"offset": 49,
"@version": "1",
"input_type": "log",
"beat": {
"name": "2106567e5bce",
"hostname": "2106567e5bce",
"version": "5.6.2"
},
"host": "2106567e5bce",
"source": "/tmp/test2.log",
"message": "new msg3",
"type": "test2",
"tags": [
"test2",
"beats_input_codec_plain_applied",
"_grokparsefailure"
]
},
"sort": [
1537801178188
]
},
{
"_index": "test-filebeat-test2",
"_type": "test2",
"_id": "AWYMF-PeJzfnbYlB_DSn",
"_score": null,
"_source": {
"@timestamp": "2018-09-24T14:59:38.186Z",
"offset": 40,
"@version": "1",
"input_type": "log",
"beat": {
"name": "2106567e5bce",
"hostname": "2106567e5bce",
"version": "5.6.2"
},
"host": "2106567e5bce",
"source": "/tmp/test2.log",
"message": "new msg",
"type": "test2",
"tags": [
"test2",
"beats_input_codec_plain_applied",
"_grokparsefailure"
]
},
"sort": [
1537801178186
]
}
]
}
}
本節裡我們雖然用到了logstash,但沒有發揮它的強大處理功能。僅僅是演示了將日誌使用filebeat傳送到logstash,然後通過logstash處理後傳送到ElasticSearch。處理功能後續再做講解。
提示:其實我們可以在logstash里根據filebeat裡的document_type做條件判斷:
# 根據tags判斷
if "test1" in [tags] {
# something
}
# type 就是filebeat裡的document_type
if [type] == 'test1'{
}else if [type] == 'test2'{
}
總結
ELK我們一共安裝了4個軟體:
- elasticsearch-5.6.2
- kibana-5.6.2-linux-x86_64
- logstash-5.6.2
- filebeat-5.6.2-linux-x86_64
其中es、kibana、logstash三者作為服務端安裝在一起,filebeat安裝在應用伺服器,用於收集日誌。
我們看一下共起了哪些埠:
$ netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 13/php-fpm
tcp 0 0 127.0.0.1:9200 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10/nginx
tcp 0 0 0.0.0.0:5046 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:9300 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:9600 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:5601 0.0.0.0:* LISTEN -
- 9200 elasticsearch預設埠
- 5601 kibana 預設埠
- 5046 logstash 收集日誌
參考
1、ELK集中式日誌平臺之二 — 部署 - 樊浩柏's Blog
https://www.fanhaobai.com/2017/12/elk-install.html
2、ElasticSearch 5學習(1)——安裝Elasticsearch、Kibana和X-Pack - wuxiwei - 部落格園
https://www.cnblogs.com/wxw16/p/6150681.html
3、Logstash Filebeat 安裝配置之使用 Kibana 分析日誌資料 - 任我樂
https://renwole.com/archives/661
4、ELK集中式日誌平臺之一 — 平臺架構 - 樊浩柏's Blog
https://www.fanhaobai.com/2017/12/elk.html
5、ELK集中式日誌平臺之三 — 進階 - 樊浩柏's Blog
https://www.fanhaobai.com/2017/12/elk-advanced.html
6、ELK+Filebeat 集中式日誌解決方案詳解
https://www.ibm.com/developerworks/cn/opensource/os-cn-elk-filebeat/index.html