ELK學習筆記之容器filebeat佔用過多/var/log目錄(filebeat進程寫滿磁盤)

0x00 概述

容器平臺最近發佈有點問題,整個平臺每日產生日誌量大約在300GB ,filebeat採用sidecar的方式採集std管道內的日誌;

如此巨大的日誌量對filebeat的pod性能造成巨大的壓力,剛剛開始filebeat每個daemonset的性能配置爲200M Hz的cpu和200 MB內存,在此次衝擊之下部分filebeat的pod性能不足直接hang死;

後續將filbeat的cpu性能提升到1G Hz的cpu和2GB的內存,大規模的日誌量測試中發現filebeat性能正常,未出現hang死的情況;

但是又發現了另外一種情況,由於短時間內部分容器平臺的主機節點產生了大量的docker日誌,導致節點主機的/var/log目錄被大量佔用,觸發監控告警;

按照docker日誌的log rotate的原理,docker自己會控制日誌量,即使段短時間內堆積了大量日誌,docker會通過加快刪除舊日誌的方式,維持本地磁盤的日誌文件佔用量;

但是在此次的場景中,發現docker日誌的log rotate功能好像並未正常應用,通過lsof命令發現,filebeat保持着的文件資源,可以發現許多被filebeat佔用空間的失效文件(deleted)文件。

此時,通過直接kill掉filebeat的daemonset可以快速釋放這些deleted文件,但是此方法並非長久之計,需要通過改變filebeat的文件句柄佔用時長參數。

 

0x01 解決方案

對於我上面的這個問題,之所以有大量的(deleted),未釋放文件句柄,還有個背景,就是由於磁盤空間非常有限,臨時加了任務,每小時刪除12小時前的日誌,換句話說,定時任務會自動刪除此時filebeat正在打開着的一些文件,於是這些文件,就變爲了未釋放的文件,因此實際文件刪除了,但空間未被釋放。

解決方案1:

# 爲了迅速釋放空間佔用,最直接的方法,就是kill -9 filebeat進程,此時空間會釋放。但並不是從根本解決,定時任務還會刪除這些,filebeat打開的文件,導致空間滿。

 

解決方案2:

filebeat的配置文件filebeat.yml,其實有兩個參數,

# close_older: 1h 說明:Close older closes the file handler for which were not modified for longer then close_older. Time strings like 2h (2 hours), 5m (5 minutes) can be used.

 

即如果一個文件在某個時間段內沒有發生過更新,則關閉監控的文件handle,默認1小時。

# force_close_files: false 
說明:This option closes a file, as soon as the file name changes.
This config option is recommended on windows only. Filebeat keeps the files it's reading open.
This can cause issues when the file is removed, as the file will not be fully removed until also Filebeat closes the reading.
Filebeat closes the file handler after ignore_older. During this time no new file with the same name can be created.
Turning this feature on the other hand can lead to loss of data on rotate files.
It can happen that after file rotation the beginning of the new file is skipped, as the reading starts at the end.
We recommend to leave this option on false but lower the ignore_older value to release files faster.

即當文件名稱有變化時,包括改名和刪除,會自動關閉一個文件。

這兩個參數結合起來,根據應用需求,一個文件30分鐘內不更新,則需要關閉句柄,文件改名或刪除,需要關閉句柄,

close_older: 10m      #釋放速度加快6倍
force_close_files: true

可以滿足,filebeat採集日誌,以及定時刪除歷史文件,這兩個任務的基本要求。

 

0x02 參考

Filebeat holding deleted files which consumes disk space

Filebeat holds open deleted file descriptions with close_removed

filebeat進程寫滿磁盤的情況處理

filebeat佔用Linux空間未釋放的問題解決

filebeat佔用文件句柄磁盤滿

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章