本日誌平臺主要用於收集、傳輸、存儲和分析主要應用程序日誌,對收集到的日誌進行格式化輸出和圖形化展示,並且對檢測到的“ERROR”字段進行郵件告警。
一、完整的日誌數據具有非常重要的作用
1)信息查找。通過檢索日誌信息,定位相應的bug,找出解決方案。 2)服務診斷。通過對日誌信息進行統計、分析,瞭解服務器的負荷和服務運行狀態,找出耗時請求進行優化等等。 3)數據分析。如果是格式化的log,可以做進一步的數據分析,統計、聚合出有意義的信息,比如根據請求中的商品id,找出TOP10用戶感興趣商品。
二、採用FiletBeats 作為日誌蒐集器
這種架構解決了 Logstash 在各服務器節點上佔用系統資源高的問題。相比 Logstash,Beats 所佔系統的 CPU 和內存幾乎可以忽略不計。
因為免費的 ELK 沒有任何安全機制,所以這裡使用了 Nginx 作反向代理,避免用戶直接訪問 Kibana 服務器。加上配置 Nginx 實現簡單的用戶認證,一定程度上提高安全性。另外,Nginx 本身具有負載均衡的作用,能夠提高系統訪問性能。
三、系統信息
基礎環境
本環境採用Centos 7.4 x64,OpenJDK 1.8
軟件版本
- Filebeat:5.6.9;
- Logstash:5.6.9;
- Elasticsearch:5.6.9;
- Kibana:5.6.9;
- Nginx:1.12.2。
四、Filebeat+ELK安裝過程
1)playbook批量安裝Filebeat
本環境已經在運維服務器部署hosts、playbook:
/etc/ansible/hosts
/etc/ansible/playbooks/install_filebeat.yaml
使用方法如下(需要把'host':*變更為具體的host配置):
ansible-playbook install_filebeat.yaml --extra-vars "{'host':*}"
<code>[root@VM_4_5_centos filebeat]# tree -a /etc/ansible/roles/filebeat/|-- defaults| `-- main.yml|-- files|-- handlers| `-- main.yml|-- meta|-- tasks| `-- main.yml|-- templates| |-- filebeat.repo.j2| |-- filebeat.yml.j2`-- vars/<code>
主要配置文件
[root@VM_2_15_centos ~]# grep -vP '^$|^#|^ #' /etc/filebeat/filebeat.yml
<code>filebeat.prospectors:- input_type: log paths: #- /var/log/*.log - /opt/jboss-4.2.3.GA/bin/ifcsmg.out fields: service: "xxx" output.logstash: hosts: ["x.x.x.x:5044"]/<code>
測試配置文件語法是否正確
<code>[root@VM_2_15_centos bin]# cd /usr/bin/[root@VM_2_15_centos bin]# ./filebeat.sh -configtest -e2018/05/18 02:07:58.588928 beat.go:297: INFO Home path: [/usr/share/filebeat] Config path: [/etc/filebeat] Data path: [/var/lib/filebeat] Logs path: [/var/log/filebeat]2018/05/18 02:07:58.588954 beat.go:192: INFO Setup Beat: filebeat; Version: 5.6.92018/05/18 02:07:58.589030 metrics.go:23: INFO Metrics logging every 30s2018/05/18 02:07:58.589042 logstash.go:91: INFO Max Retries set to: 32018/05/18 02:07:58.589094 outputs.go:108: INFO Activated logstash as output plugin.2018/05/18 02:07:58.589150 publish.go:300: INFO Publisher name: VM_2_15_centos2018/05/18 02:07:58.589242 async.go:63: INFO Flush Interval set to: 1s2018/05/18 02:07:58.589252 async.go:64: INFO Max Bulk Size set to: 2048Config OK/<code>
服務命令路徑、日誌路徑
/usr/share/filebeat
/var/log/filebeat/filebeat
服務啟動命令
systemctl start filebeat.service
2)ELK服務端系統環境
最大文件描述符(如果用戶級別的1024太小要求是65536以上),本環境默認為100001無需修改:
<code>[root@VM_4_8_centos ~]# tail /etc/security/limits.conf -n4* soft nofile 100001* hard nofile 100002root soft nofile 100001root hard nofile 100002/<code>
3)ELK服務端yum安裝Logstash
[root@VM_4_8_centos yum.repos.d]# cat logstash.repo
<code>[logstash-5.x]name=Elastic repository for 5.x packagesbaseurl=https://artifacts.elastic.co/packages/5.x/yumgpgcheck=1gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearchenabled=1autorefresh=1type=rpm-md/<code>
主要配置文件(Port:5044)
[root@VM_4_8_centos ~]# grep -vP '^$|^#|^ #' /etc/logstash/logstash.yml
<code>path.data: /var/lib/logstashpath.config: /etc/logstash/conf.dhttp.host: "x.x.x.x"path.logs: /var/log/logstash/<code>
[root@VM_4_8_centos bin]# cat /usr/share/logstash/bin/all.conf.email
<code>input { beats { port => 5044 }}output { if [message] =~ "ERROR" { email { port => "587" #address => "smtp.macroflag.com" address => "58.247.137.138" authentication => "plain" subject => "警告:%{[service]} 出現 Jboss Log Error" username => "username" password => "password" from => "[email protected]" to => "[email protected],[email protected]" via => "smtp" body => "主機:%{[service]} \\n 日誌類型:%{type} \\n error_message:%{message}" } } }/<code>
服務命令路徑、日誌路徑
/usr/share/logstash/
/var/log/logstash/logstash-plain.log
服務啟動命令
bash /usr/share/logstash/bin/start_logstash.sh
4)ELK服務端tar包安裝Elasticsearch
<code>tar -zxvf elasticsearch-5.6.9.tar.gz -C /opt/groupadd elasticsearchuseradd elasticsearch -g elasticsearchchown elasticsearch. /opt/elasticsearch-5.6.9/ -Rln -s /etc/logstash/ /usr/share/logstash/config/<code>
主要配置文件(Port:9200,9300)
grep -vP '^$|^#|^ #' /opt/elasticsearch-5.6.9/config/elasticsearch.yml
<code>network.host: x.x.x.x/<code>
服務啟動命令(使用elasticsearch用戶啟動)
bash /opt/elasticsearch-5.6.9/bin/start_elasticsearch.sh
<code>#!/bin/bashnohup su - elasticsearch -s /bin/sh /opt/elasticsearch-5.6.9/bin/elasticsearch >> nohup.out 2>&1 &/<code>
5)ELK服務端yum安裝Kibana
[root@VM_4_8_centos yum.repos.d]# cat kibana.repo
<code>[kibana-5.x]name=Kibana repository for 5.x packagesbaseurl=https://artifacts.elastic.co/packages/5.x/yumgpgcheck=1gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearchenabled=1autorefresh=1type=rpm-md/<code>
主要配置文件(Port:5601)
grep -vP '^$|^#|^ #' /etc/kibana/kibana.yml
<code>server.host: "127.0.0.1"elasticsearch.url: "http://x.x.x.x:9200"/<code>
服務命令路徑、日誌路徑
/usr/share/kibana/
/var/log/kibana/kibana.stdout
服務啟動命令
service kibana start
#修改部分增加了日誌過濾
<code>input {redis {port => "6379"host => "10.89.4.8"type => "log"key => "filebeat"data_type => "list"}}filter { grok { match => { "message" => "%{TIME:timestamp} %{LOGLEVEL:loglevel} %{GREEDYDATA:log}" } #修改了正則 }}output { #stdout { codec => rubydebug } elasticsearch { hosts => ["10.89.4.8:9200"] index => "logstash-%{type}-%{+YYYY.MM}" } if [message] =~ "exception" and !("urlRewriteAction" in [log]) { ##修改了匹配條件,新加條件直接後面and就好 #if [LOGLEVEL] == "INFO" { http { url => "XXXXXXXXXXXXXXXX" http_method => "post" content_type => "application/json; charset=utf-8" format => "message" message => '{"msgtype":"text","text":{"content":"主機:%{[app]} \\n日誌級別:%{loglevel} \\n錯誤簡述:%{message}"}}' }} }/<code>
<code>input {redis {port => "6379"host => "10.89.4.8"type => "log"key => "filebeat"data_type => "list"}}filter { grok { match => { "message" => "%{TIME:timestamp} %{LOGLEVEL:loglevel} %{GREEDYDATA:log}" } #修改了正則 }}output { #stdout { codec => rubydebug } elasticsearch { hosts => ["10.89.4.8:9200"] index => "logstash-%{type}-%{+YYYY.MM}" } if [message] =~ "exception" and !("urlRewriteAction" in [log]) { ##修改了匹配條件,新加條件直接後面and就好 #if [LOGLEVEL] == "INFO" { http { url => "XXXXXXXXXXXXXXXX" http_method => "post" content_type => "application/json; charset=utf-8" format => "message" message => '{"msgtype":"text","text":{"content":"主機:%{[app]} \\n日誌級別:%{loglevel} \\n錯誤簡述:%{message}"}}' }} }/<code>
閱讀更多 帥氣駿哥 的文章