好久沒寫博客啦,關鍵沒成品可分享的。
標題很高端的趕腳有木有,其實就是簡單的ansible+fluentd+es+kibana。
本篇的內容主要是自動化部署日誌收集系統fluentd
簡單介紹下各個工具:
fluentd是一個日誌收集系統,它的特點在於其各部分均是可定製化的,你可以通過簡單的配置,將日誌收集到不同的地方。目前開源社區已經貢獻了下面一些存儲插件:es,hdfs,mongodb,redis,mysql等等。
ElasticSearch是一個基於Lucene構建的開源,分佈式,RESTful搜索引擎。設計用於雲計算中,能夠達到實時搜索,穩定,可靠,快速,安裝使用方便。支持通過HTTP使用JSON進行數據索引。
Kibana 是一個爲 Logstash 和 ElasticSearch 提供的日誌分析的 Web 接口。可使用它對日誌進行高效的搜索、可視化、分析等各種操作。
ansible是一個模型驅動的配置管理器,支持多節點發布、遠程任務執行。默認使用 SSH 進行遠程連接。無需在被管理節點上安裝附加軟件,可使用各種編程語言進行擴展.
架構圖如下(畫的不好,請見諒):
流程如下:
It continuously “tails” the access log file.
It parses the incoming log entries into meaningful fields (such as
ip
,path
, etc.) and buffers them.It writes the buffered data to es periodically.
4. 通過Kibana進行展示,搜索,查看等操作;
---------------------
接下來簡單說說td-agent.conf的設置:詳細語法還是看fluentd官方文檔更清楚,這裏就不解釋了
1. 收集server端本地日誌
<source> type tail format syslog path /var/log/messages pos_file /var/log/td-agent/messages.pos tag system.nginx.message </source> <match *.nginx.*> index_name adminpack type_name nginx type elasticsearch include_tag_key true tag_key @log_name host es-server port 9200 logstash_format true flush_interval 5s </match>
簡單來說就是一個source源文件,對應一個match存儲
2. 收集遠端clientd客戶端服務器日誌
首先server服務器端必須開啓個接收端口
<source> type forward port 24224 bind 0.0.0.0 </source>
和對應的match,這樣才知道存到哪裏
<match *.nginx.*> index_name adminpack type_name nginx type elasticsearch include_tag_key true tag_key @log_name host es-server port 9200 logstash_format true flush_interval 5s </match>
然後client客戶端設置forward,同樣的也是要設置source源文件
以下就是個典型的客戶端配置文件
<source> type tail format apache2 path /var/log/nginx/access.log pos_file /var/log/td-agent/nginx.access.pos tag mysql.nginx.access </source> <source> type tail format syslog path /var/log/messages pos_file /var/log/td-agent/messages.pos tag system.nginx.message </source> <match *.nginx.*> # output type type forward send_timeout 10s recover_wait 5s heartbeat_interval 1s phi_threshold 8 hard_timeout 10s # primary host <server> name collector host 192.168.200.216 port 24224 weight 60 </server> # Failed <secondary> type file path /var/log/fluent/forware-failed </secondary> # Buffer Parameters buffer_type memory flush_interval 3s </match>
這樣就可以了,很簡單,靈活
我們之前是採用fluentd+mongodb的形式,線上跑了快一年,效果不錯,這個比較適合二次開發,挺好。fluentd有收集Mysql慢日誌的插件哦,這個很不錯的,方便查找,結合下我們的監控系統做分析。
那爲什麼不用更常見的logstash+es+kibana類?
主要是線上當初選擇的就是fluentd,一改動得全都改,人懶得改了,還有對fluentd很滿意,不想換了。
這個項目在github上面有,是小鬼子寫的,我fork了稍微改了下,大家可以試試
地址:https://github.com/vTNT/ansible-elasticsearch