一厌均、ELK簡介
實(shí)際上ELK是三款軟件的簡稱愈犹,分別是Elasticsearch奥邮、 Logstash、Kibana組成丁存。
Elasticsearch 基于java,是個(gè)開源分布式搜索引擎柴我,它的特點(diǎn)有:分布式解寝,零配置,自動(dòng)發(fā)現(xiàn)艘儒,索引自動(dòng)分片聋伦,索引副本機(jī)制,restful風(fēng)格接口界睁,多數(shù)據(jù)源觉增,自動(dòng)搜索負(fù)載等。
Kibana 基于nodejs翻斟,也是一個(gè)開源和免費(fèi)的工具逾礁,Kibana可以為Logstash和ElasticSearch提供的日志分析友好的Web 界面,可以匯總访惜、分析和搜索重要數(shù)據(jù)日志嘹履。
Logstash 基于java腻扇,是一個(gè)開源的用于收集,分析和存儲(chǔ)日志的工具。
二砾嫉、Elasticsearch安裝部署
2.1 下載
官網(wǎng)下載地址: https://www.elastic.co/cn/downloads/elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.2-linux-x86_64.tar.gz
2.2 安裝
解壓到相應(yīng)目錄
tar -zxvf elasticsearch-7.10.2-linux-x86_64.tar.gz -C /usr/local
修改配置
cd /usr/local/elasticsearch-7.10.2/config/
vim elasticsearch.yml
node.name: node-1
path.data: /usr/local/elasticsearch-7.10.2/data
path.logs: /usr/local/elasticsearch-7.10.2/logs
network.host: 127.0.0.1
http.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["127.0.0.1"]
cluster.initial_master_nodes: ["node-1"]
# 緩存回收大小幼苛,無默認(rèn)值
# 有了這個(gè)設(shè)置,最久未使用(LRU)的 fielddata 會(huì)被回收為新數(shù)據(jù)騰出空間
# 控制fielddata允許內(nèi)存大小焕刮,達(dá)到HEAP 20% 自動(dòng)清理舊cache舶沿,默認(rèn)是無限緩存下去,肯定會(huì)超內(nèi)存然后內(nèi)存溢出
indices.fielddata.cache.size: 20%
# -----------------------------------主要是添加上面的那一個(gè)配置-------------------------------
indices.breaker.total.use_real_memory: false
# fielddata 斷路器默認(rèn)設(shè)置堆的 60% 作為 fielddata 大小的上限配并。
indices.breaker.fielddata.limit: 40%
# request 斷路器估算需要完成其他請求部分的結(jié)構(gòu)大小括荡,例如創(chuàng)建一個(gè)聚合桶,默認(rèn)限制是堆內(nèi)存的 40%荐绝。
indices.breaker.request.limit: 40%
# total 揉合 request 和 fielddata 斷路器保證兩者組合起來不會(huì)使用超過堆內(nèi)存的 70%(默認(rèn)值)一汽。
indices.breaker.total.limit: 95%
創(chuàng)建es用戶 因?yàn)镋lasticSearch不支持Root用戶直接操作,因此我們需要?jiǎng)?chuàng)建一個(gè)es用戶
useradd es
chown -R es:es /usr/local/elasticsearch-7.10.2
2.3 啟動(dòng)
切換用戶成es用戶進(jìn)行操作
su - es
/usr/local/elasticsearch-7.10.2/bin/elasticsearch
后臺(tái)啟動(dòng)
/usr/local/elasticsearch-7.10.2/bin/elasticsearch -d
2.4 驗(yàn)證
在瀏覽器訪問9200
端口地址:http://127.0.0.1:9200低滩,如果返回了elasticsearch信息召夹,就表示已經(jīng)啟動(dòng)成功
三、Logstash安裝部署
3.1 下載
官網(wǎng)下載地址: https://www.elastic.co/cn/downloads/logstash
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.10.2-linux-x86_64.tar.gz
2.2 安裝
解壓到相應(yīng)目錄
tar -zxvf logstash-7.10.2-linux-x86_64.tar.gz -C /usr/local
新增配置文件恕沫,收集elasticsearch日志
cd /usr/local/logstash-7.10.2/bin
vim logstash-elasticsearch.conf
input {
stdin {}
}
output {
elasticsearch {
hosts => '127.0.0.1:9200'
}
stdout {
codec => rubydebug
}
}
2.3 啟動(dòng)
./logstash -f logstash-elasticsearch.conf
后臺(tái)啟動(dòng)
nohup ./logstash -f logstash-elasticsearch.conf>/dev/null 2>&1 &
2.4 日志收集
Apache對應(yīng)服務(wù)器安裝logstash监憎,配置規(guī)則,例如新建logstash-apache.conf
input {
file {
path => "/usr/local/dlmz-adminlogs/sys-*.log"
start_position => beginning
sincedb_path => "/dev/null"
codec => multiline {
pattern => "^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"
negate => true
auto_flush_interval => 3
what => previous
}
type => "project1-log"
}
}
file {
path => "/usr/local/dlmz-adminlogs/info-*.log"
start_position => beginning
sincedb_path => "/dev/null"
codec => multiline {
pattern => "^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"
negate => true
auto_flush_interval => 3
what => previous
}
type => "project2-log"
}
}
filter {
if [path] =~ "info" {
mutate { replace => { type => "sys-info" } }
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
date {
match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
}
} else if [path] =~ "error" {
mutate { replace => { type => "sys-error" } }
} else {
mutate { replace => { type => "random_logs" } }
}
}
output {
elasticsearch {
hosts => '127.0.0.1:9200'
#index => "logstash-project-%{+YYYY.MM.dd}"
#user => 'elastic'
#password => 'password'
}
stdout { codec => rubydebug }
}
啟動(dòng)logstash
./logstash -f logstash-apache.conf
Thinkphp對應(yīng)服務(wù)器安裝logstash婶溯,配置規(guī)則鲸阔,例如新建logstash-thinkphp.conf
input {
file {
path => "/www/wwwroot/project/runtime/log/*/*.log"
start_position => beginning
sincedb_path => "/dev/null"
codec => multiline {
pattern => "^-"
negate => true
auto_flush_interval => 3
what => previous
}
type => "project-log"
}
}
output {
elasticsearch {
hosts => 'http://127.0.0.1:9200'
#index => "logstash-project-%{+YYYY.MM.dd}"
#user => 'elastic'
#password => 'password'
}
stdout { codec => rubydebug }
}
啟動(dòng)logstash
./logstash -f logstash-thinkphp.conf
四、Kibana安裝部署
4.1 下載
官網(wǎng)下載地址: https://www.elastic.co/cn/downloads/kibana
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.10.3-linux-x86_64.tar.gz
2.2 安裝
解壓到相應(yīng)目錄
tar -zxvf kibana-7.10.2-linux-x86_64.tar.gz -C /usr/local
mv /usr/local/kibana-7.10.2-linux-x86_64 /usr/local/kibana-7.10.2
修改配置
cd /usr/local/kibana-7.10.2/config
vim kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://127.0.0.1:9200"]
kibana.index: ".kibana"
i18n.locale: "zh-CN"
授權(quán)es用戶
chown -R es:es /usr/local/kibana-7.10.2/
2.3 啟動(dòng)
切換用戶成es用戶進(jìn)行操作
su - es
/usr/local/kibana-7.10.2/bin/kibana
后臺(tái)啟動(dòng)
/usr/local/kibana-7.10.2/bin/kibana &
2.4 驗(yàn)證
在瀏覽器訪問5601
端口地址:http://127.0.0.1:5601迄委,如果打開了Kibana系統(tǒng)界面褐筛,就表示已經(jīng)啟動(dòng)成功。然后可以通過Discover可視化檢索各個(gè)服務(wù)日志數(shù)據(jù)叙身。
五渔扎、ELK開啟密碼
5.1 修改elasticsearch.yml配置
cd /usr/local/logstash-7.10.2/bin
vim logstash-elasticsearch.conf
加入這兩個(gè)參數(shù)
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
5.2 重新啟動(dòng)elasticsearch,然后要設(shè)置6個(gè)賬號和密碼信轿,包括elasticsearch晃痴、kibana等
./bin/elasticsearch-setup-passwords interactive
5.3 重新啟動(dòng)elasticsearch,驗(yàn)證瀏覽器重新輸入密碼正確進(jìn)入
5.4 Kibana設(shè)置密碼
ElasticSearch設(shè)置密碼后刷新Kibana财忽,
發(fā)現(xiàn)報(bào)錯(cuò): Kibana server is not ready yet倘核,這就是因?yàn)閗ibana沒設(shè)置密碼導(dǎo)致
修改kibana.yml
cd /usr/local/kibana-7.10.2/config
vim kibana.yml
加入這兩個(gè)參數(shù)
elasticsearch.username: "elastic"
elasticsearch.password: "password"