ELK簡(jiǎn)介
ELK是一套完整的日志解決方案,由ElasticSearch、Logstash、 Kibana這三款開(kāi)源軟件組成。
- EastiSearch是基于Lucene開(kāi)發(fā)的分布式存儲(chǔ)檢引擎,用來(lái)存儲(chǔ)各類日志;
- Logstash對(duì)日志進(jìn)行收集、分析,并將其存儲(chǔ)供以后使用:
- Kibana 是基于Node.js開(kāi)發(fā)的展示工具,為L(zhǎng)ogstah和ElasticSearch提供用于日志展示的Web界面,還用于幫助匯總、分析和搜索重要日志數(shù)據(jù)。
ELK官網(wǎng)https://www.elastic.co/products 分別提供包進(jìn)行下載安裝。
ELK工作原理
- 在所有需要收集日志的服務(wù)上部署Logstash,作為署Logstash agent用于監(jiān)控并過(guò)濾所收集的日志,將過(guò)濾后的內(nèi)容整合在一起,最終全部交給EastiSearch檢索引擎;
- 用EastiSearch進(jìn)行自定義檢索;
- 再通過(guò)Kibana通過(guò)結(jié)合自定義檢索內(nèi)容生成圖表,進(jìn)行日志數(shù)據(jù)展示。
部署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行 節(jié)點(diǎn)名稱
node.name: node1
#33行 工作目錄
path.data: /data/es-data
path.logs: /var/log/elasticsearch/
#43行 防止交換swap分區(qū)
bootstrap.memory_lock: true
#54行 監(jiān)聽(tīng)網(wǎng)絡(luò)
network.host: 0.0.0.0
#58行 端口
http.port: 9200
4.創(chuàng)建配置文件中的工作目錄,并修改其屬性
mkdir -p /data/es-data
chown -R elasticsearch:elasticsearch /data/es-data/
5.啟動(dòng)ElasticSearch服務(wù),并查看其端口9200
systemctl start elasticsearch.service
netstat -ntap | grep 9200 #稍微等待一會(huì)
6.通過(guò)瀏覽器訪問(wèn)9200端口
http://192.168.26.144:9200
7.ES交互
通過(guò)RESTful API (通過(guò)json格式交互)
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界面上輸入數(shù)據(jù),再查詢
9.部署ES多集群
在第二臺(tái)服務(wù)器上同時(shí)安裝ES服務(wù)同上,需要注意的是修改配置文件elasticsearch.yml有些許不同。
cd /etc/elasticsearch/
vim elasticsearch.yml
#17行 集群名稱
cluster.name: yun
#23行 節(jié)點(diǎn)名稱
node.name: node2
#69行 自動(dòng)發(fā)現(xiàn)機(jī)制
discovery.zen.ping.unicast.hosts: [“192.168.26.144”, “192.168.26.147”]
啟動(dòng)elasticsearch
systemctl start elasticsearch.service
netstat -ntap | grep 9200
同樣在第一臺(tái)服務(wù)器node1中配置文件修改,再重啟服務(wù)
#69行 單播列表自動(dòng)發(fā)現(xiàn)機(jī)制
discovery.zen.ping.unicast.hosts: [“192.168.26.147”, “192.168.26.144”]
訪問(wèn)head可以看到ES的主分片和副本分片
http://192.168.26.144:9200/_plugin/head/
訪問(wèn)kopf可以看到ES的集群節(jié)點(diǎn)信息
http://192.168.26.144:9200/_plugin/kopf/
9.生產(chǎn)環(huán)境注意內(nèi)存解鎖和文件限制
less /var/log/elasticsearch/yun.log #查看集群日志提示不讓鎖內(nèi)存
修改配置文件/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.創(chuàng)建鏈接,識(shí)別logstash命令
ln -s /opt/logstash/bin/logstash /usr/bin/
3.配置Logstash收集
Logstash使用input和output定義收集日志時(shí)的輸入和輸出。
- input常用的輸入源有:
file、syslog、redis、log4j、apache log或nginx log,或者其他一些自定義的log格式。- output常用的輸出有:
elasticsearch比較常用,fle:寫(xiě)入文件,redis: 寫(xiě)入隊(duì)列,hdfs: 寫(xiě)入HDFS,需插件支持,zabbix: zabbix監(jiān)控,mongodb: 寫(xiě)入mongodb庫(kù),除此之外編碼插件codes也比較常用,經(jīng)常用來(lái)處理json數(shù)據(jù)或者多行數(shù)據(jù)源。
(1)定義輸入和輸出流,類似管道
logstash -e ‘input { stdin{} } output { stdout{} }’
(2)詳細(xì)格式顯示
logstash -e ‘input { stdin{} } output { stdout{ codec => rubydebug } }’
(3)寫(xiě)入到elasticsearch中
logstash -e ‘input { stdin{} } output { elasticsearch { hosts => [“192.168.26.144:9200”] } }’
(4)寫(xiě)入ES和同時(shí)生成文本
logstash -e ‘input { stdin{} } output { elasticsearch { hosts => [“192.168.26.144:9200”] } stdout { codec => rubydebug } }’
(5)編寫(xiě)配置文件將事件生成到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收集日志(系統(tǒng)日志、java異常日志、事件優(yōu)化處理)
編寫(xiě)配置文件將日志生成到ES中
vim /root/file.conf
input {
file {
path => “/var/log/messages” #系統(tǒng)日志
type => “system”
start_position => “beginning”
}
file {
path => “/var/log/elasticsearch/yun.log” #java異常日志
type => “es-error”
start_position => “beginning”
codec => multiline { #多行優(yōu)化處理
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.啟動(dòng)kibana
yum install screen -y
screen #開(kāi)啟
/usr/local/kibana/bin/kibana
ctrl+a+d 進(jìn)行丟入后臺(tái)