1. 程式人生 > 實用技巧 >搭建視覺化日誌分析平臺ELK

搭建視覺化日誌分析平臺ELK

https://blog.csdn.net/wangkai_123456/article/details/84796903

https://blog.csdn.net/qq942477618/article/details/52951011?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

文章目錄

ELK是什麼

E=ElasticSearch,一款基於Lucene的分散式搜尋引擎;

L=LogStash, 一款分散式日誌收集系統,支援多輸入源,並內建一些過濾操作,支援多輸入元;
K=Kibana, 一款配合ElasticSearch的web視覺化介面,內建非常各種查詢,聚合操作,並擁有漂亮的圖形化展示功能。

為什麼要用ELK

在實際應用中,我們的日誌是非常重要的,它通常會記錄一些比較重要的資訊,如應用程式的log記錄的error,warn級別的log,通常在量小的情況下,我們可以直接vi/vim/notepad++等定位原因,在量大的時候,這種方式就捉襟見肘了,而且我們還要各種聚合,或者基於多個異常關鍵詞的搜尋,並有且,或,交,並,差,補,排序等一些操作,而且相應速度必須給力,如果線上環境出了故障,能夠立刻準確定位,ELK就是高手,在百萬大軍中取上將首級,猶如探囊取物,所以這時候ELK就非常適合了,當然除此之外,ELK也經常在運維工作中大放光彩,在應用級別的實時監控,非常適合一些重要核心服務的預警。

ELK如何安裝搭建

環境:
Linux系統:Centos7
Java版本:JDK1.8
ELK均為最新版本:

ElasticSearch 安裝

一、下載Elasticsearch原始碼

https://www.elastic.co/cn/downloads/elasticsearch

二、安裝

1、將下載的二進位制包移動到/usr/local目錄下,解壓縮檔案包
tar zxvf elasticsearch-6.5.0.tar.gz

三、Elasticsearch配置

1、進入解壓的logstash目錄/usr/local/elasticsearch-6.5.0/config,修改配置檔案elasticsearch.yml,主要修改 Paths 和 Network 兩部分。

2、配置作業系統vm.max_map_count引數,請使用root使用者登入系統,執行以下命令
sysctl -w vm.max_map_count=655360
注:vm.max_map_count引數的值至少要設定大於等於262144

四、新增執行Elasticsearch的使用者

1、建立elsearch使用者組及elsearch使用者
groupadd elsearch
useradd elsearch -g elsearch -p elasticsearch

2、更改elasticsearch資料夾及內部檔案的所屬使用者及組為elsearch:elsearch
cd /usr/local/
chown -R elsearch:elsearch elasticsearch-6.5.0

cd /data
chown -R elsearch:elsearch elasticsearch

3、切換到elsearch使用者再啟動
su elsearch
cd /usr/local/elasticsearch-6.5.0/bin/
./elasticsearch -d

五、檢查Elasticsearch安裝是否成功

開啟瀏覽器,輸入地址 http://10.62.124.43:9200 出現以下內容則表示安裝成功
{
“name” : “jYXkdwS”,
“cluster_name” : “elasticsearch”,
“cluster_uuid” : “cr_Rj_nxRnywb0warF7tFw”,
“version” : {
“number” : “6.5.0”,
“build_flavor” : “default”,
“build_type” : “tar”,
“build_hash” : “816e6f6”,
“build_date” : “2018-11-09T18:58:36.352602Z”,
“build_snapshot” : false,
“lucene_version” : “7.5.0”,
“minimum_wire_compatibility_version” : “5.6.0”,
“minimum_index_compatibility_version” : “5.0.0”
},
“tagline” : “You Know, for Search”
}

LogStash安裝

一、下載Logstash原始碼

https://www.elastic.co/downloads/logstash

二、安裝

1、將下載的二進位制包移動到/usr/local目錄下,解壓縮檔案包
tar zxvf logstash-6.4.2.tar.gz

三、logstash配置
1、進入解壓的logstash目錄/usr/local/logstash-6.4.2/config,基於模板新建配置檔案mongod-logstash.conf
cp logstash-sample.conf mongod-logstash.conf

2、input配置(一個伺服器節點多個不同日誌檔案輸出到多個不同的索引中)
input {
file {
path => “/data/log_mongos/mongos.log”
type => “26_mongoslog”
}
file {
path => “/data/log/configsvr.log”
type => “configsvr_26_mongodlog”
}
file {
path => “/data/rs1/log/mongodb.log”
type => “rs1_26_mongodlog”
}
beats {
port => 5044
}
}

3、filter配置
filter {
grok {
match => [“message”,"%{TIMESTAMP_ISO8601:timestamp}\s+%{MONGO3_SEVERITY:severity}\s+%{MONGO3_COMPONENT:component}\s+(?:[%{DATA:context}])?\s+%{GREEDYDATA:body}"]
}
if [body] =~ “ms$” {
grok {
match => [“body”,"%{WORD:command_action}\s+%{WORD:db_name}.$?%{WORD:collection_name}\s+%{GREEDYDATA:command_content}\s+%{NUMBER:spend_time:int}ms"]
}
}
date {
match => [ “timestamp”, “UNIX”, “YYYY-MM-dd HH:mm:ss”, “ISO8601”]
remove_field => [ “timestamp” ]
}
}

4、output配置
output {
if [type] == “26_mongoslog” {
elasticsearch {
user => logstash
password => logstash
hosts => [“https://es1:9200”,“https://es2:9200”]
ssl => true
ssl_certificate_verification => true
cacert => “/usr/local/logstash-6.4.2/config/root-ca.pem”
index => “logstash-26_mongoslog”
}
}
if [type] == “configsvr_26_mongodlog” {
elasticsearch {
user => logstash
password => logstash
hosts => [“https://es1:9200”,“https://es2:9200”]
ssl => true
ssl_certificate_verification => true
cacert => “/usr/local/logstash-6.4.2/config/root-ca.pem”
index => “logstash-configsvr_26_mongodlog”
}
}
if [type] == “rs1_26_mongodlog” {
elasticsearch {
user => logstash
password => logstash
hosts => [“https://es1:9200”,“https://es2:9200”]
ssl => true
ssl_certificate_verification => true
cacert => “/usr/local/logstash-6.4.2/config/root-ca.pem”
index => “logstash-rs1_26_mongodlog”
}
}
}

三、啟動

為了可以自動檢測配置檔案的變動和自動重新載入配置檔案,需要在啟動的時候使用以下命令:

./bin/logstash -f config/mongod-logstash.conf --config.reload.automatic
  • 1

預設檢測配置檔案的間隔時間是3秒,可以通過以下命令改變

--config.reload.interval <second>
  • 1

如果已經運行了沒有提供自動重啟的logstash,可以傳送一個掛起命令給logstash重新載入配置檔案:

kill -1 <pid>
  • 1

Kibana安裝

一、下載Kibana原始碼

https://www.elastic.co/cn/downloads/kibana

二、安裝

1、將下載的二進位制包移動到/usr/local目錄下,解壓縮檔案包
tar zxvf kibana-6.5.0-linux-x86_64.tar.gz

三、Kibana配置

1、進入解壓的logstash目錄/usr/local/kibana-6.5.0-linux-x86_64/config,修改配置檔案kibana.yml
#設定server.host
server.host: “10.62.124.25”

#設定elasticsearch.url
elasticsearch.url: “http://10.62.124.25:9200”

2、系統防火牆開放5601埠
firewall-cmd --zone=public --add-port=5601/tcp --permanent
firewall-cmd --reload

四、啟動Kibana

cd /usr/local/kibana-6.5.0-linux-x86_64/bin
nohup ./kibana & //後臺啟動

至此,視覺化日誌查詢分析系統平臺ELK搭建完成!
#停止kibana

netstat -anltp|grep 5601
kill -9 程序號
  • 1
  • 2

附kibana啟動後的一些警告資訊記錄以及解決方法

1、發現的第一個警告資訊

server log [06:55:25.594] [warning][reporting] Generating a random key for xpack.reporting.encryptionKey.
                   To prevent pending reports from failing on restart, please set xpack.reporting.encryptionKey in kibana.yml
根據提示,在配置檔案kibana.yml中新增【xpack.reporting.encryptionKey】屬性:

xpack.reporting.encryptionKey: “a_random_string”
官方文件:https://www.elastic.co/guide/en/kibana/current/reporting-settings-kb.html

2、發現的第二個警告資訊
server log [06:55:25.686] [warning][security] Generating a random key for xpack.security.encryptionKey.
                    To prevent sessions from being invalidated on restart, please set xpack.security.encryptionKey in kibana.yml
根據提示,在配置檔案kibana.yml中新增【xpack.security.encryptionKey】屬性:

xpack.security.encryptionKey: “something_at_least_32_characters”
官方文件:https://www.elastic.co/guide/en/kibana/6.x/using-kibana-with-security.html