ELK快速搭建日誌平臺

1.  拋磚引入html


Elasticsearchlinux

Logstash》    git

Filebeatgithub

Filebeat模塊與配置spring

Kibana安全

Kibana安全特性之權限控制服務器

 

2.  前言less


2.1.  現狀elasticsearch

之前,查看日誌都是經過SSH客戶端登服務器去看,使用較多的命令就是 less 或者 tail。若是服務部署了好幾臺,就要分別登陸到這幾臺機器上看,還要注意日誌打印的時間(好比,有可能一個操做過來產生好的日誌,這些日誌還不是在同一臺機器上,此時就須要根據時間的前後順序推斷用戶的操做行爲,就要在這些機器上來回切換)。並且,搜索的時候不方便,必須對vi,less這樣的命令很熟悉,還容易看花了眼。爲了簡化日誌檢索的操做,能夠將日誌收集並索引,這樣方便多了,用過Lucene的人應該知道,這種檢索效率是很高的。基本上每一個互聯網公司都會有本身的日誌管理平臺和監控平臺(好比,Zabbix),不管是本身搭建的,仍是用的阿里雲這樣的雲服務提供的,反正確定有。下面,咱們利用ELK搭建一個相對真實的日誌管理平臺。ide

2.2.  日誌格式

咱們的日誌,如今是這樣的:

每條日誌的格式,相似於這樣:

2018-08-22 00:34:51.952 [INFO ] [org.springframework.kafka.KafkaListenerEndpointContainer#0-1-C-1] [com.cjs.handler.MessageHandler][39] - 監聽到註冊事件消息:

2.3.  logback.xml

Logback配置

2.4.  環境介紹

在本例中,各個系統的日誌都在/data/logs/${projectName},好比:

Filebeat,Logstash,Elasticsearch,Kibana都在一臺虛擬機上,並且都是單實例,並且沒有別的中間件

因爲,日誌天天都會歸檔,且實時日誌都是輸出在info.log或者error.log中,因此Filebeat採集的時候只須要監視這兩個文件便可。

 

3.  Filebeat配置


Filebeat的主要配置在於filebeat.yml配置文件中的 filebeat.inputs 和 output.logstash 區域:

#=========================== Filebeat inputs =============================
filebeat.inputs:
- type: log
  enabled: true
  # 要抓取的文件路徑 
  paths:
    - /data/logs/oh-coupon/info.log
    - /data/logs/oh-coupon/error.log
  # 添加額外的字段
  fields:
    log_source: oh-coupon
  fields_under_root: true
  # 多行處理
  # 不以"yyyy-MM-dd"這種日期開始的行與前一行合併 
  multiline.pattern: ^\d{4}-\d{1,2}-\d{1,2}
  multiline.negate: true
  multiline.match: after

  # 5秒鐘掃描一次以檢查文件更新
  scan_frequency: 5s
  # 若是文件1小時都沒有更新,則關閉文件句柄
  close_inactive: 1h  
  # 忽略24小時前的文件
  #ignore_older: 24h


- type: log
  enabled: true
  paths:
    - /data/logs/oh-promotion/info.log
    - /data/logs/oh-promotion/error.log
  fields:
    log_source: oh-promotion
  fields_under_root: true
  multiline.pattern: ^\d{4}-\d{1,2}-\d{1,2}
  multiline.negate: true
  multiline.match: after
  scan_frequency: 5s
  close_inactive: 1h  
  ignore_older: 24h

#================================ Outputs =====================================

#-------------------------- Elasticsearch output ------------------------------
#output.elasticsearch:
  # Array of hosts to connect to.
  # hosts: ["localhost:9200"]

  # Optional protocol and basic auth credentials.
  #protocol: "https"
  #username: "elastic"
  #password: "changeme"

#----------------------------- Logstash output --------------------------------
output.logstash:
  # The Logstash hosts
  hosts: ["localhost:5044"]

  # Optional SSL. By default is off.
  # List of root certificates for HTTPS server verifications
  #ssl.certificate_authorities: ["/etc/pki/root/ca.pem"]

  # Certificate for SSL client authentication
  #ssl.certificate: "/etc/pki/client/cert.pem"

  # Client Certificate Key
  #ssl.key: "/etc/pki/client/cert.key"

 

4.  Logstash配置


4.1.  logstash.yml

# X-Pack Monitoring
# https://www.elastic.co/guide/en/logstash/current/monitoring-logstash.html
xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.username: "logstash_system"
xpack.monitoring.elasticsearch.password: "123456"
xpack.monitoring.elasticsearch.url: ["http://localhost:9200"]

4.2.  管道配置

input {
    beats {
        port => "5044"
    }
}
filter {
    grok {
        match => { "message" => "%{TIMESTAMP_ISO8601:log_date}\s+\[%{LOGLEVEL:log_level}" }
    }
    date {
        match => ["log_date", "yyyy-MM-dd HH:mm:ss.SSS"]
        target => "@timestamp"
    }
}
output {
    
    if [log_source] == "oh-coupon" {
        elasticsearch {
            hosts => [ "localhost:9200" ]
            index => "oh-coupon-%{+YYYY.MM.dd}"
            user => "logstash_internal"
            password => "123456"
        }
    }

    if [log_source] == "oh-promotion" {
        elasticsearch {
            hosts => [ "localhost:9200" ]
            index => "oh-promotion-%{+YYYY.MM.dd}"
            user => "logstash_internal"
            password => "123456"
        }
    }

}

4.3.  插件

Logstash針對輸入、過濾、輸出都有好多插件

關於Logstash的插件在以前的文章中不曾說起,由於都是配置,因此不打算再單獨寫一篇了,這裏稍微重點的提一下,下面幾篇文章對此特別有幫助:

https://www.elastic.co/guide/en/logstash/current/input-plugins.html

https://www.elastic.co/guide/en/logstash/current/plugins-inputs-beats.html

https://www.elastic.co/guide/en/logstash/current/plugins-inputs-kafka.html

https://www.elastic.co/guide/en/logstash/current/filebeat-modules.html

https://www.elastic.co/guide/en/logstash/current/output-plugins.html

https://www.elastic.co/guide/en/logstash/current/logstash-config-for-filebeat-modules.html

https://www.elastic.co/guide/en/logstash/current/filter-plugins.html

本例中,到了輸入插件:beats,過濾插件:grok和date,輸出插件:elasticsearch

這裏,最最重要的是 grok ,利用這個插件咱們能夠從消息中提取一些咱們想要的字段

grok

https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html

https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns

date

https://www.elastic.co/guide/en/logstash/current/plugins-filters-date.html#plugins-filters-date-target

字段引用

https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html#logstash-config-field-references

 

5.  Elasticsearch配置


5.1.  elasticsearch.yml

xpack.security.enabled: true

其它均爲默認

 

6.  Kibana配置


6.1.  kibana.yml

server.port: 5601

server.host: "192.168.101.5"

elasticsearch.url: "http://localhost:9200"

kibana.index: ".kibana"

elasticsearch.username: "kibana"
elasticsearch.password: "123456"

xpack.security.enabled: true
xpack.security.encryptionKey: "4297f44b13955235245b2497399d7a93"

 

7.  啓動服務


7.1.  啓動Elasticsearch

[root@localhost ~]# su - cheng
[cheng@localhost ~]$ cd $ES_HOME
[cheng@localhost elasticsearch-6.3.2]$ bin/elasticsearch

7.2.  啓動Kibana

[cheng@localhost kibana-6.3.2-linux-x86_64]$ bin/kibana

7.3.  啓動Logstash

[root@localhost logstash-6.3.2]# bin/logstash -f second-pipeline.conf --config.test_and_exit
[root@localhost logstash-6.3.2]# bin/logstash -f second-pipeline.conf --config.reload.automatic

7.4.  啓動Filebeat

[root@localhost filebeat-6.3.2-linux-x86_64]# rm -f data/registry
[root@localhost filebeat-6.3.2-linux-x86_64]# ./filebeat -e -c filebeat.yml -d "publish"

 

8.  演示


 

9.  參考


http://www.javashuo.com/article/p-skaeilme-ee.html

相關文章
相關標籤/搜索