基于Elasticsearch+Fluentd+Kibana的日志收集分析系統(tǒng)

我們平時(shí)分析log直接在日志文件中 grep、awk 就可以獲得自己想要的信息,此方法效率低下,生產(chǎn)中需要集中化的日志管理,所有服務(wù)器上的日志收集匯總

Elasticsearch
一個(gè)節(jié)點(diǎn)(node)就是一個(gè)Elasticsearch實(shí)例,一個(gè)集群(cluster)由一個(gè)或多個(gè)節(jié)點(diǎn)組成,它們具有相同的cluster.name,它們協(xié)同工作,分享數(shù)據(jù)和負(fù)載。當(dāng)加入新的節(jié)點(diǎn)或者刪除一個(gè)節(jié)點(diǎn)時(shí),集群就會(huì)感知到并平衡數(shù)據(jù)。
集群中一個(gè)節(jié)點(diǎn)會(huì)被選舉為主節(jié)點(diǎn)(master),它將臨時(shí)管理集群級(jí)別的一些變更,例如新建或刪除索引、增加或移除節(jié)點(diǎn)等。主節(jié)點(diǎn)不參與文檔級(jí)別的變更或搜索,這意味著在流量增長(zhǎng)的時(shí)候,該主節(jié)點(diǎn)不會(huì)成為集群的瓶頸。
做為用戶,我們能夠與集群中的任何節(jié)點(diǎn)通信,包括主節(jié)點(diǎn)。每一個(gè)節(jié)點(diǎn)都知道文檔存在于哪個(gè)節(jié)點(diǎn)上,它們可以轉(zhuǎn)發(fā)請(qǐng)求到相應(yīng)的節(jié)點(diǎn)上。我們?cè)L問(wèn)的節(jié)點(diǎn)負(fù)責(zé)收集各節(jié)點(diǎn)返回的數(shù)據(jù),最后一起返回給客戶端。這一切都由Elasticsearch處理。

一個(gè)完整的集中式日志系統(tǒng),需要包含以下幾個(gè)主要特點(diǎn):
收集-能夠采集多種來(lái)源的日志數(shù)據(jù)
傳輸-能夠穩(wěn)定的把日志數(shù)據(jù)傳輸?shù)街醒胂到y(tǒng)
存儲(chǔ)-如何存儲(chǔ)日志數(shù)據(jù)
分析-可以支持 UI 分析
警告-能夠提供錯(cuò)誤報(bào)告,監(jiān)控機(jī)制

fluentd基于CRuby實(shí)現(xiàn),并對(duì)性能表現(xiàn)關(guān)鍵的一些組件用C語(yǔ)言重新實(shí)現(xiàn),整體性能不錯(cuò)。
fluentd支持所有主流日志類型,插件支持較多,性能表現(xiàn)較好
logstash支持所有主流日志類型,插件支持最豐富,可以靈活DIY,但性能較差,JVM容易導(dǎo)致內(nèi)存使用量高。
Elasticsearch是個(gè)開源分布式搜索引擎,提供搜集、分析、存儲(chǔ)數(shù)據(jù)三大功能
Kibana 也是一個(gè)開源和免費(fèi)的工具,Kibana可以為 td-agent和 ElasticSearch 提供的日志分析友好的 Web 界面,可以幫助匯總、分析和搜索重要數(shù)據(jù)日志。

# yum install java
# java -version

Node1

# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.1.rpm
# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: my-application
node.name: node-1
node.master: true
network.host: 172.21.0.9
http.port: 9200

Node2
# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: my-application
node.name: node-2
node.master: false
network.host: 172.21.0.37
http.port: 9200
discovery.zen.ping.unicast.hosts: ["host1", "172.21.0.9”]

# /etc/init.d/elasticsearch start
# /etc/init.d/elasticsearch status
# curl http://172.21.0.9:9200/_cat/health
# curl http://172.21.0.9:9200/_cat/nodes


Fluentd (tdagent)

# wget http://packages.treasuredata.com.s3.amazonaws.com/3/redhat/7/x86_64/td-agent-3.2.0-0.el7.x86_64.rpm
# rpm -ivh td-agent-3.2.0-0.el7.x86_64.rpm  --force --nodeps
# yum install -y libcurl-devel
# /opt/td-agent/embedded/bin/fluent-gem install fluent-plugin-elasticsearch

# cd /etc/td-agent/
# cat td-agent.conf
<source>
  @type forward
  port 24224
</source>

####################################
<source>
  @type tail
  path /var/log/httpd/access_log
  pos_file /var/log/td-agent/httpd-access.log.pos
  tag apache.access
  <parse>
    @type apache2
  </parse>
</source>
####################################
<match debug.**>
  @type stdout
</match>
####################################

<match *.**>
  @type copy
  <store>
    @type elasticsearch
    host 172.21.0.9
    port 9200
    logstash_format true
    logstash_prefix fluentd-${tag}
    logstash_dateformat %Y%m%d
    include_tag_key true
    type_name access_log
    tag_key @log_name
    flush_interval 1s
  </store>
  <store>
    @type stdout
  </store>
</match>

# /etc/init.d/td-agent restart
# chmod 777 -R /var/log/httpd/
# tail -f /var/log/td-agent/td-agent.log
# curl 'http://172.21.0.9:9200/_cat/indices?v’

Kibana

# wget https://artifacts.elastic.co/downloads/kibana/kibana-6.3.1-x86_64.rpm
# rpm -ivh kibana-6.3.1-x86_64.rpm

# vim /etc/kibana/kibana.yml
server.port: 5601
server.host: “172.21.0.9"
elasticsearch.url: "http://172.21.0.9:9200”
kibana.index: ".kibana”

# /etc/init.d/kibana restart
# tail -f /var/log/kibana/kibana.stderr

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容