基於ElasticSearch+Logstash+Kibana的日誌分析、儲存、展示
ELK簡介
ELK是一套完整的日誌解決方案,由ElasticSearch、Logstash、 Kibana這三款開源軟體組成。
- EastiSearch是基於Lucene開發的分散式儲存檢引擎,用來儲存各類日誌;
- Logstash對日誌進行收集、分析,並將其儲存供以後使用:
- Kibana 是基於Node.js開發的展示工具,為Logstah和ElasticSearch提供用於日誌展示的Web介面,還用於幫助彙總、分析和搜尋重要日誌資料。
ELK官網https://www.elastic.co/products 分別提供包進行下載安裝。
ELK工作原理
- 在所有需要收集日誌的服務上部署Logstash,作為署Logstash agent用於監控並過濾所收集的日誌,將過濾後的內容整合在一起,最終全部交給EastiSearch檢索引擎;
- 用EastiSearch進行自定義檢索;
- 再通過Kibana通過結合自定義檢索內容生成圖表,進行日誌資料展示。
部署ElasticSearch
1.安裝ElasticSearch
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
vim elasticsearch.repo #配置yum源
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=http://packages.elastic.co/elasticsearch/2.x/CentOS
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1
yum install elasticsearch -y #yum安裝elasticsearch
2.安裝Java
yum install java -y
java -version
3.修改配置檔案elasticsearch.yml
cd /etc/elasticsearch/
vim elasticsearch.yml
#17行 叢集名稱
cluster.name: yun
#23行 節點名稱
node.name: node1
#33行 工作目錄
path.data: /data/es-data
path.logs: /var/log/elasticsearch/
#43行 防止交換swap分割槽
bootstrap.memory_lock: true
#54行 監聽網路
network.host: 0.0.0.0
#58行 埠
http.port: 9200
4.建立配置檔案中的工作目錄,並修改其屬性
mkdir -p /data/es-data
chown -R elasticsearch:elasticsearch /data/es-data/
5.啟動ElasticSearch服務,並檢視其埠9200
systemctl start elasticsearch.service
netstat -ntap | grep 9200 #稍微等待一會
6.通過瀏覽器訪問9200埠
http://192.168.26.144:9200
7.ES互動
curl -i -XGET 'http://192.168.175.132:9200/_count?pretty' -d '{
"query": {
"match_all": {}
}
}'
`
8.安裝外掛
#安裝ES外掛elasticsearch-head
/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
#安裝ES外掛elasticsearch-kopf
/usr/share/elasticsearch/bin/plugin install lmenezes/elasticsearch-kopf
安裝完成後在plugins目錄下可以看到head和kopf

可以在web介面上輸入資料,再查詢


9.部署ES多叢集
在第二臺伺服器上同時安裝ES服務同上,需要注意的是修改配置檔案elasticsearch.yml有些許不同。
cd /etc/elasticsearch/
vim elasticsearch.yml
#17行 叢集名稱
cluster.name: yun
#23行 節點名稱
node.name: node2
#69行 自動發現機制
discovery.zen.ping.unicast.hosts: ["192.168.26.144", "192.168.26.147"]
啟動elasticsearch
systemctl start elasticsearch.service
netstat -ntap | grep 9200
同樣在第一臺伺服器node1中配置檔案修改,再重啟服務
#69行 單播列表自動發現機制
discovery.zen.ping.unicast.hosts: ["192.168.26.147", "192.168.26.144"]
訪問head可以看到ES的主分片和副本分片
http://192.168.26.144:9200/_plugin/head/
訪問kopf可以看到ES的叢集節點資訊
http://192.168.26.144:9200/_plugin/kopf/
9.生產環境注意記憶體解鎖和檔案限制
less /var/log/elasticsearch/yun.log #檢視叢集日誌提示不讓鎖記憶體
修改配置檔案/etc/security/limits.conf
vim /etc/security/limits.conf
#末尾插入
# allow user 'elasticsearch' mlockall
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
* soft nofile 65535
* hard nofile 65535
重啟生效
systemctl restart elasticsearch.service
部署Logstash
1.安裝Logstash
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
vim /etc/yum.repos.d/ogstash.repo #配置yum源
[logstash-2.1]
name=Logstash repository for 2.1.x packages
baseurl=http://packages.elastic.co/logstash/2.1/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enable=1
yum install logstash -y
2.建立連結,識別logstash命令
ln -s /opt/logstash/bin/logstash /usr/bin/
3.配置Logstash收集
Logstash使用input和output定義收集日誌時的輸入和輸出。
- input常用的輸入源有:
file、syslog、redis、log4j、apache log或nginx log,或者其他一些自定義的log格式。 - output常用的輸出有:
elasticsearch比較常用,fle:寫入檔案,redis: 寫入佇列,hdfs: 寫入HDFS,需外掛支援,zabbix: zabbix監控,mongodb: 寫入mongodb庫,除此之外編碼外掛codes也比較常用,經常用來處理json資料或者多行資料來源。
(1)定義輸入和輸出流,類似管道
logstash -e 'input { stdin{} } output { stdout{} }'
(2)詳細格式顯示
logstash -e 'input { stdin{} } output { stdout{ codec => rubydebug } }'
(3)寫入到elasticsearch中
logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.26.144:9200"] } }'
(4)寫入ES和同時生成文字
logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.26.144:9200"] } stdout { codec => rubydebug } }'
(5)編寫配置檔案將事件生成到ES
vim /etc/logstash/conf.d/01-logstash.conf
input { stdin { } }
output {
elasticsearch { hosts => ["localhost:9200"] }
stdout { codec => rubydebug }
}logstash -f /etc/logstash/conf.d/01-logstash.conf #配置檔案生效
5.使用logstash收集日誌(系統日誌、java異常日誌、事件優化處理)
編寫配置檔案將日誌生成到ES中
vim /root/file.conf
input {
file {
path => "/var/log/messages" #系統日誌
type => "system"
start_position => "beginning"
}
file {
path => "/var/log/elasticsearch/yun.log" #java異常日誌
type => "es-error"
start_position => "beginning"
codec => multiline { #多行優化處理
pattern => "^\["
negate => true
what => "previous"
}
}
}
output {
if [type] == "system" {
elasticsearch {
hosts => ["192.168.26.144:9200"]
index => "system-%{+YYYY.MM.dd}"
}
}
if [type] == "es-error" {
elasticsearch {
hosts => ["192.168.26.144:9200"]
index => "es-error-%{+YYYY.MM.dd}"
}
}
}
logstash -f /root/file.conf #日誌生成到ES上
部署kibana
1.安裝kibana
wget https://download.elastic.co/kibana/kibana/kibana-4.3.1-linux-x64.tar.gz
tar zxvf kibana-4.3.1-linux-x64.tar.gz -C /opt/
mv /opt/kibana-4.3.1-linux-x64/ /usr/local/
cd /usr/local
mv kibana-4.3.1-linux-x64/ kibana2.編輯kibana配置檔案
vim /usr/local/kibana/config/kibana.yml
#2行
server.port: 5601
#5行
server.host: "0.0.0.0"
#12行 ES地址
elasticsearch.url: "http://192.168.26.144:9200"
#20行
kibana.index: ".kibana"
3.啟動kibana
yum install screen -y
screen #開啟
/usr/local/kibana/bin/kibana
ctrl+a+d 進行丟入後臺
4.訪問kibana
Linux公社的RSS地址 : ofollow,noindex" target="_blank">https://www.linuxidc.com/rssFeed.aspx
本文永久更新連結地址: https://www.linuxidc.com/Linux/2018-11/155518.htm