文章轉(zhuǎn)載自: https://cloud.tencent.com/info/8e82f882356bd2c6d734e4ef572c30aa.html
Centos ulimit 調(diào)整: http://smilejay.com/2016/06/centos-7-systemd-conf-limits/
臨時(shí)設(shè)置 ulimit -SHn 65536
一直以來(lái)我采用supervisord來(lái)進(jìn)行第三方服務(wù)的管理践宴,百試不爽莫瞬。所謂的第三方服務(wù)汉柒,我這里把不能通過(guò)yum或apt進(jìn)行安裝的,統(tǒng)統(tǒng)歸攏為第三方贯溅;當(dāng)然使用systemd來(lái)進(jìn)行管理也很不錯(cuò),不過(guò)在服務(wù)exit的時(shí)候需要自己寫(xiě)腳本來(lái)進(jìn)行重啟和重試操作夹供。不例外的袭厂,Prometheus我還是采用了supervisord來(lái)進(jìn)行啟動(dòng)管理,但是過(guò)完年后上班發(fā)現(xiàn)媳纬,我的prometheus無(wú)法收集targets的監(jiān)控?cái)?shù)據(jù)了嘹悼,8百多個(gè)監(jiān)控接口的數(shù)據(jù)丟了三天叛甫;排查后發(fā)現(xiàn)是由于supervisor引起的。于是整理出來(lái)杨伙,大家分享一下我的排查路線。
1萌腿、發(fā)現(xiàn)Prometheus監(jiān)控的Targets狀態(tài)全部變?yōu)镈own限匣,查看頁(yè)面詳細(xì)報(bào)錯(cuò)信息和服務(wù)日志:
從頁(yè)面和日志中發(fā)現(xiàn)基本被"file closed"的報(bào)錯(cuò)信息所淹沒(méi):“err="WAL log samples: log series: write data/wal/002911: file already closed"。
2毁菱、本著優(yōu)先恢復(fù)業(yè)務(wù)的運(yùn)維原則米死,嘗試重啟其中一臺(tái)Prometheus服務(wù),發(fā)現(xiàn)重啟后服務(wù)日志依舊報(bào)錯(cuò)贮庞,信息如下:
level=error ts=2018-02-26T02:26:38.80774862Z caller=db.go:265 component=tsdb msg="compaction failed" err="compact [/data/prometheus/prometheus/data/01C6S070KRF0V2202ZKPB4BTG4 /data/prometheus/prometheus/data/01C6TY0J7RAQAA34JJQVQZ3D46 /data/prometheus/prometheus/data/01C6WVSZB66DBAXBSYPMZGJ29W]: open /data/prometheus/prometheus/data/01C6TY0J7RAQAA34JJQVQZ3D46/tombstones: too many open files
看到日志關(guān)鍵字段:“too many open files”!
3峦筒、根據(jù)重啟前和重啟后的日志關(guān)鍵字眼,初步判定跟文件描述符有關(guān)窗慎,Linux系統(tǒng)對(duì)單進(jìn)程可最大打開(kāi)文件數(shù)默認(rèn)限制為1024,我這里監(jiān)控了800多個(gè)節(jié)點(diǎn),每個(gè)節(jié)點(diǎn)暴露上百個(gè)監(jiān)控?cái)?shù)據(jù)瘫俊,平均沒(méi)分鐘抓取一次克婶,需要定期的進(jìn)行數(shù)據(jù)落盤(pán)操作,可能是系統(tǒng)的文件描述符過(guò)低導(dǎo)致的术吗。
4尉辑、檢查系統(tǒng)文件描述符情況:
①檢查inodes使用情況:
系統(tǒng)inode使用情況正常。
②查看系統(tǒng)的限制情況:
# ulimit -n
81920
系統(tǒng)級(jí)別的打開(kāi)文件數(shù)限制已經(jīng)調(diào)整至81920较屿,正常隧魄。
③既然系統(tǒng)層面沒(méi)有限制已經(jīng)放開(kāi),那么會(huì)不會(huì)是Prometheus進(jìn)程的限制呢隘蝎,驗(yàn)證:
# pidof prometheus
17626
# cat /proc/17624/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 63472 63472 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 63472 63472 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
查看進(jìn)程的限制max open files的Soft和Hard分別為1024和4096购啄,這就很奇怪了,系統(tǒng)明明已經(jīng)放開(kāi)了open file限制末贾,為什么Prometheus進(jìn)程還被限制在1024呢闸溃?
思考之后想到Prometheus本來(lái)也有自己的監(jiān)控接口,查下本身的監(jiān)控?cái)?shù)據(jù)拱撵,其中“process_max_fds”監(jiān)控條目跟最大文件數(shù)相關(guān)辉川,然后看到如下數(shù)據(jù):
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 1024
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 1022
當(dāng)前已經(jīng)open了1022個(gè),最大限制1024拴测,再次驗(yàn)證了是進(jìn)程的可用文件數(shù)限制過(guò)低導(dǎo)致乓旗。
④那么問(wèn)題來(lái)了?是什么原因?qū)е碌倪M(jìn)程open file受限呢集索。答案可能只有一個(gè):supervisor屿愚!因?yàn)槲业腜rometheus是用supervisor啟動(dòng)的汇跨。先驗(yàn)證一下,棄用守護(hù)進(jìn)程妆距,手動(dòng)啟動(dòng)Prometheus服務(wù)看下穷遂,文件數(shù)限制是否放開(kāi);再次啟動(dòng)后娱据,查看監(jiān)控情況:
可見(jiàn)手動(dòng)重啟后限制已經(jīng)放開(kāi)蚪黑。由此確定是supervisor引起的被守護(hù)進(jìn)程文件數(shù)限制。
⑤定位到supervisor后中剩,查看supervisor的啟動(dòng)參數(shù)忌穿,有沒(méi)有類(lèi)似文件描述符的限制字眼,在配置文件/etc/supervisord.conf中搜索關(guān)鍵字file结啼、nf掠剑、fds等字眼:
發(fā)現(xiàn)在supervisord的配置區(qū)域內(nèi)有"mindfs"的配置,解釋如下:“avail startup file descriptors;default 1024”郊愧。順便查了下官方文檔里的解釋?zhuān)?/p>
The minimum number of file descriptors that must be available before supervisord will start successfully. A call to setrlimit will be made to attempt to raise the soft and hard limits of the supervisord process to satisfy minfds. The hard limit may only be raised if supervisord is run as root. supervisord uses file descriptors liberally, and will enter a failure mode when one cannot be obtained from the OS, so it’s useful to be able to specify a minimum value to ensure it doesn’t run out of them during execution. These limits will be inherited by the managed subprocesses. This option is particularly useful on Solaris, which has a low per-process fd limit by default.
文檔中指出朴译,如果用root用戶啟動(dòng)的supervisord,可以嘗試增大此值糕珊。
⑥修復(fù):增大supervisord的mindfs值至65535动分,之后再次用supervisor啟動(dòng)Prometheus。啟動(dòng)后監(jiān)控恢復(fù)红选,能夠正常抓取數(shù)據(jù)澜公。再次查看進(jìn)程的打開(kāi)文件數(shù)限制,已調(diào)整至65535:
# cat /proc/17626/limits
總結(jié):真正實(shí)踐中問(wèn)題的排查喇肋,可能沒(méi)有文章中整理的那么簡(jiǎn)單坟乾,6步定位問(wèn)題并恢復(fù)故障。實(shí)際可能會(huì)走許多彎路蝶防,在問(wèn)題過(guò)后的整理歸納可以讓梳理思路甚侣、積累經(jīng)驗(yàn),為下一次更快间学、更準(zhǔn)確的定位問(wèn)題埋下希望的種子殷费。