ELK日誌管理平臺監控nginx/tomcat/docker日誌
囉嗦
隨便看了一眼自己的博客,已經很久沒有更新了。由於工作些許(懶)有(癌)點(晚)忙(期),所以一直沒有在寫。看着孤零零的幾篇文件太過於單調了,所以以後爭取每週上一點新東西。
背景介紹
基於平臺化的產品,項目越來越多,導致日誌管理越來越麻煩,一兩個項目還好管理,運維或者開發需要日誌了可以直接拉取,便於分析問題。但是越來越多的項目,分佈在不同的服務器,不同的tomcat,不同的日誌目錄下的時候。獲取日誌就變得力不從心,每次都要去找這個項目的日誌名稱是什麼,在那個服務器中(心好累)
解決方案
基於我們的需求,目前開源的軟件ELK可以很好地完成日誌的收集、整理、展示。這篇文章用最簡單的方法搭建日誌管理平臺(ElasticSearch + Logstash + Kibana),後面有時間,我會在寫一篇更合理的日誌管理平臺(filebeat + kafka +ElasticSearch + Logstash + Kibana),逐步完善日誌管理平臺。
軟件介紹
-
ElasticSearch:是一個基於Lucene的開源分佈式搜索服務器。它的特點有:分佈式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的,並作爲Apache許可條款下的開放源碼發佈,是第二流行的企業搜索引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。 在elasticsearch中,所有節點的數據是均等的。
-
Logstash:是一個完全開源的工具,它可以對你的日誌進行收集、過濾、分析,支持大量的數據獲取方法,並將其存儲供以後使用(如搜索)。說到搜索,logstash帶有一個web界面,搜索和展示所有日誌。一般工作方式爲c/s架構,client端安裝在需要收集日誌的主機上,server端負責將收到的各節點日誌進行過濾、修改等操作在一併發往elasticsearch上去。
-
Kibana :是一個基於瀏覽器頁面的Elasticsearch前端展示工具,也是一個開源和免費的工具,Kibana可以爲 Logstash 和 ElasticSearch 提供的日誌分析友好的 Web 界面,可以幫助您彙總、分析和搜索重要數據日誌。
監控目標
主要監控一些最常用的日誌,例如:nginx日誌、tomcat日誌、docker日誌等等
實現原理圖
需要軟件工具和環境
- Ubuntu 14.0.4
- elasticsearch-7.1.1.tar.gz
- kibana-7.1.1-linux-x86_64.tar.gz
- logstash-7.1.1.tar.gz
- ELK下載地址
安裝
elasticsearch安裝配置
-
首先解壓elasticsearch-7.1.1.tar.gz
tar -zxvf elasticsearch-7.1.1.tar.gz -
config/elasticsearch.yml配置文件,一般默認IP:0.0.0.0,端口9200,沒有特殊需求的可以不用改。(如果es啓動訪問不了的,可以把IP改成本機的ip試試)
-
啓動elasticsearch(啓動該軟件不能使用root用戶,需要普通用戶,可以新建普通用戶,將 目錄的權限都賦予給該新用戶)
-
創建命令:在linux命令行執行
useradd es
passwd es
123456
es/123456 -
切換成es用戶啓動elasticsearch
在/usr/local/elasticsearch-7.1.1路徑下執行 nohup bin/elasticsearch & (後臺執行)或者 bin/elasticsearch
項目啓動成功可以用瀏覽器查看
-
遇到的安裝問題
- http://ip:9200/ 無法訪問
解決方法:vim config/elasticsearch.yml 增加network.host: 0.0.0.0 - ERROR: [5] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [1024] for user [es] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解決方法:1. vi /etc/security/limits.conf
添加或者修改
soft nofile 65536
hard nofile 65536
2. vi /etc/sysctl.conf
添加 vm.max_map_count=655360
3. 保存後執行
sysctl -p
- http://ip:9200/ 無法訪問
logstash安裝配置
- 首先解壓logstash-7.1.1.tar.gz
- 編輯你自己的安裝目錄/usr/local/logstash-7.1.1/config下的logstash-sample.conf文件,input中輸入
// An highlighted block
file {
path => ["/logs/pCredit.log","/logs/bizPCredit.log","/logs/pCredit.log.*","/logs/bizPCredit.log.*"]
start_position => "beginning"
type => "pcredit"
codec => json {
charset => "UTF-8"
}
}
file {
path => ["/logs/mobileFinance.log"]
start_position => "beginning"
type => "mobileFinance"
codec => json {
charset => "UTF-8"
}
}
file {
path => ["/var/log/nginx/*.log"]
start_position => "beginning"
type => "nginx"
codec => json {
charset => "UTF-8"
}
}
file {
path => ["/var/lib/docker/containers/*/*.log"]
start_position => "beginning"
type => "order"
codec => json {
charset => "UTF-8"
}
}
filter中輸入
// An highlighted block
date {
match => [ "timestamp" , "YYYY-MM-dd HH:mm:ss" ]
}
output中輸入
// An highlighted block
if "_grokparsefailure" in [tags] {
}else{
if [type] == "pcredit"{
elasticsearch {
hosts => ["http://localhost:9200"]
index => "pcredit"
user => "elastic"
password => "changeme"
}
}
if [type] == "mobileFinance"{
elasticsearch {
hosts => ["http://localhost:9200"]
index => "mobilefinance"
user => "elastic"
password => "changeme"
}
}
if [type] == "nginx"{
elasticsearch {
hosts => ["http://localhost:9200"]
index => "nginx"
user => "elastic"
password => "changeme"
}
}
if [type] == "order"{
elasticsearch {
hosts => ["http://localhost:9200"]
index => "order"
user => "elastic"
password => "changeme"
}
}
stdout {
codec => rubydebug
}
- 啓動logstash:使用root用戶,路徑/usr/local/logstash-7.1.1/
命令: nohup bin/logstash &(後臺運行) 或者 bin/logstash
kibana安裝配置
-
首先解壓kibana-7.1.1.tar.gz
-
查看kibana配置/usr/local/kibana-7.1.1-linux-x86_64/config/
-
啓動kibana:路徑/usr/local/kibana-7.1.1-linux-x86_64/
nohup bin/kibana & 或者 bin/kibana
這裏我的路徑是用nginx映射出來的,kibana最新的版本已經沒有登錄管理了,所以要在nginx中配置權限管理,權限管理我們下一章中來講。
大家也可以參考這個文章:Kibana設置登錄認證 -
創建索引
如果kibana中沒有找到你創建的索引,可以在
執行:
curl -X PUT ‘localhost:9200/order/person/1’ -d ’
{
“user”: “張三”,
“title”: “工程師”,
“desc”: “數據庫管理”
}’
然後去查看:linux命令行查看:
curl -X GET ‘http://localhost:9200/_cat/indices?v’
總結
ELK比較簡單的一個框架就搭建好了,目前這個框架適合不是日誌量很大的項目使用。logstash比較消耗內存,所有後面的教程收集日誌會換成filebeat。如果有哪裏寫的不對請大家指正,如果有不瞭解的可以給我留言或者聯繫我,一起學習,共同進步。