elkb+redis建立日誌收集分析系統
elastic提供了一套非常高級的工具ELKB來滿足以上這幾個需求。ELKB指的是用於日誌分析或者說數據分析的四個軟件,各自擁有獨立的功能又可以組合在一起。先來簡單介紹一下這四個軟件。
Elastic Search: 從名稱可以看出,Elastic Search 是用來進行搜索的,提供數據以及相應的配置信息(什麽字段是什麽數據類型,哪些字段可以檢索等),然後你就可以自由地使用API搜索你的數據。
Logstash:。日誌文件基本上都是每行一條,每一條裏面有各種信息,這個軟件的功能是將每條日誌解析為各個字段。
Kibana:提供一套Web界面用來和 Elastic Search 進行交互,這樣我們不用使用API來檢索數據了,可以直接在 Kibana 中輸入關鍵字,Kibana 會將返回的數據呈現給我們,當然,有很多漂亮的數據可視化圖表可供選擇。
Beats:安裝在每臺需要收集日誌的服務器上,將日誌發送給Logstash進行處理,所以Beats是一個“搬運工”,將你的日誌搬運到日誌收集服務器上。 Filebeat是一個日誌文件托運工具,在你的服務器上安裝客戶端後,filebeat會監控日誌目錄或者指定的日誌文件,追蹤讀取這些文件(追蹤文件的變化,不停的讀),並且轉發這些信息到elasticsearch或者logstarsh中存放。所以說filebeat就是logstash-agent端的取代產品。且性能優良。
日誌系統架構
開始搭建日誌系統
首先去ELK官網下載相應的壓縮包 :https://www.elastic.co/downloads
安裝
這裏使用CentOS 7為例來說明怎麽裝這幾個軟件。其中ELK只需要安裝在進行日誌收集分析的服務器(server)上,而Beats是每一臺產生日誌的機器(client)都需要安裝,當然也可能包括日誌收集服務器本身。安裝之前關閉防火墻,關閉selinux
1.首先配置 filebeat。
安裝filebeat。只需 yum -y install filebeat
curl -L -O https://download.elastic.co/beats/filebeat/filebeat-5.0.0-x86_64.rpm
yum -y install filebeat-5.0.0-x86_64.rpm
安裝完成後,修改配置文件 /etc/filebeat/filebeat.yml
內容如下:
filebeat.prospectors:
- type: log
enabled: true ###啟用此配置
paths:
- /var/log/nginx/*.log ###指明讀取文件的位置,可指定多個
- /var/log/tomcat/*.log
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
setup.template.settings:
index.number_of_shards: 3
setup.kibana:
output.redis:
hosts: ["192.168.10.10:6379"]
key: "nginx-log"
password: "" ###如果沒有密碼可以省略
db: 0 ###指定redis數據庫,默認第一個數據庫是0,可以省略
timeout: 5 ###可以省略
這樣的配置是 從 /var/log/nginx下讀取所有以.log結尾的文件。且發送到redis。且redis中以list方式存儲。鍵名為 nginx-log配置完成後。先啟動redis,再啟動filebeat。
systemctl enable filebeat
systemctl start filebeat
2.安裝配置啟動redis
vim /opt/soft/redis-4.0.8/redis.conf
bind 0.0.0.0
port 6379
daemonize yes ### 開啟守護進程模式
redis采用的是單進程多線程的模式。當redis.conf中選項daemonize設置成yes時,代表開啟守護進程模式。在該模式下,redis會在後臺運行,並將進程pid號寫入至redis.conf選項pidfile設置的文件中,此時redis將一直運行,除非手動kill該進程。但當daemonize選項設置成no時,當前界面將進入redis的命令行界面,exit強制退出或者關閉連接工具(putty,xshell等)都會導致redis進程退出。
logfile "/var/log/redis.log"
redis-server /opt/soft/redis-4.0.8/redis.conf
3.安裝配置啟動logstash
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
cat << EOF > /etc/yum.repos.d/logstash.repo
[logstash-6.x]
name=Elastic repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF
創建好倉庫後使用yum安裝
yum -y install logstash
systemctl enable logstash
systemctl start logstash
chown -R logstash:logstash /etc/logstash/*
mkdir /usr/share/logstash/config
ln -s /etc/logstash/* /usr/share/logstash/config/
測試
./logstash -e "input{stdin{}}output{stdout{codec=>rubydebug}}"
this is a test !Sending Logstash's logs to /var/log/logstash which is now configured via log4j2.properties
The stdin plugin is now waiting for input:
{
"host" => "test03",
"@version" => "1",
"message" => "this is a test !",
"@timestamp" => 2018-04-10T10:48:34.858Z
}
說明正常
接下來應該讓logstash從redis中間鍵中讀取數據。那麽配置logstash。配置文件為 /etc/logstash/conf.d/logstash.conf
vim /etc/logstash/conf.d/logstash.conf
input {
redis {
data_type => "list"
key => "nginx-log"
host => "192.168.10.10"
port => 6379
}
}
output {
elasticsearch {
hosts => ["192.168.10.8:9200"]
manage_template => false
index => "nginx-log-%{+YYYY.MM.dd}"
}
}
systemctl enable logstash
systemctl start logstash
安裝elasticsearch
yum -y install elasticsearch-6.2.3.rpm
vim /etc/elasticsearch/elasticsearch.yml
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 192.168.10.8
http.port: 9200
systemctl enable elasticsearch
systemctl start elasticsearch
安裝kibana
yum -y install kibana-6.2.3-x86_64.rpm
vim /etc/kibana/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://192.168.10.8:9200"
systemctl enable kibana
systemctl start kibana
kibana漢化方法:https://github.com/anbai-inc/Kibana_Hanization
git clone https://github.com/anbai-inc/Kibana_Hanization.git
python main.py /usr/share/kibana/
漢化之後需要重啟服務
時間久了,日誌文件過多占用空間,需要清理,命令如下:
清理2018年4月12號的日誌
curl -XDELETE 'http://192.168.10.8:9200/nginx-log-2018.04.12'
清理2018年4月的日誌
curl -XDELETE 'http://192.168.10.8:9200/nginx-log-2018.04.*'
清除所有索引日誌
curl -XDELETE 'http://192.168.10.8:9200/*'
如果清理了所有的索引日誌,kibana裏面沒有了索引,無法查看日誌,重啟logstash服務即可自動生成當天的日誌索引。
可以寫到腳本裏面並添加計劃任務
elkb+redis建立日誌收集分析系統