互聯(lián)網(wǎng)軟件外包網(wǎng)站怎么優(yōu)化排名靠前
場景
最近底層api需要上集群,于是用nginx做了轉(zhuǎn)發(fā),但是隨著時間的增長,nginx的日志越來越大,磁盤空間也頂不住了,于是需要對日志進(jìn)行分割,由于nginx原生是不支持日志按天存儲和分割的,網(wǎng)上也介紹了通過配置nginx.conf的方式實現(xiàn)按天存儲,但是經(jīng)過本地簡單測試并未生效,而且感覺不是很靈活,沒有繼續(xù)深挖。
于是選擇了更為強(qiáng)大和靈活的logrotate方式。
logrotate簡要介紹
logrotate是一個日志文件管理工具。用于分割日志文件,刪除舊的日志文件,并創(chuàng)建新的日志文件,起到“轉(zhuǎn)儲”作用。可以節(jié)省磁盤空間。 使用logrotate指令,可以輕松管理系統(tǒng)所產(chǎn)生的記錄文件。每個記錄文件都可被設(shè)置成每日,每周或每月處理,也能在文件太大時立即處理。
下面是logrotate的一些重要概念和功能:
- 配置文件:logrotate的配置文件位于/etc/logrotate.conf和/etc/logrotate.d/目錄下。其中,logrotate.conf是主配置文件,而/etc/logrotate.d/目錄則包含其他應(yīng)用程序的單獨(dú)配置文件,每個配置文件對應(yīng)一個應(yīng)用程序的日志輪轉(zhuǎn)規(guī)則。
- 日志輪轉(zhuǎn)規(guī)則:每個日志文件都可以在配置文件中定義一個輪轉(zhuǎn)規(guī)則。這些規(guī)則包含了日志文件的路徑、輪轉(zhuǎn)周期(如每天、每周、每月等)以及保留的日志文件數(shù)量。
- 預(yù)定義選項:logrotate提供了一些預(yù)定義的選項,例如daily(每天輪轉(zhuǎn))、weekly(每周輪轉(zhuǎn))、monthly(每月輪轉(zhuǎn))等。你可以在配置文件中使用這些選項來定義日志文件的輪轉(zhuǎn)周期。
- 輪轉(zhuǎn)方式:logrotate支持不同的輪轉(zhuǎn)方式,包括copytruncate(復(fù)制并截斷)、rotate(旋轉(zhuǎn),即重命名并創(chuàng)建新文件)等。copytruncate方式會復(fù)制當(dāng)前日志內(nèi)容到新文件,然后截斷當(dāng)前日志文件,這樣應(yīng)用程序可以繼續(xù)寫入新文件。
- postrotate和prerotate:在輪轉(zhuǎn)之前或之后,logrotate可以運(yùn)行用戶定義的腳本。這在你需要在輪轉(zhuǎn)期間執(zhí)行額外操作時非常有用,比如重新啟動服務(wù)或壓縮舊日志文件。
- 配置檢查:logrotate提供了一個-d選項,可以用來檢查配置文件的語法和效果,以幫助你確認(rèn)輪轉(zhuǎn)規(guī)則是否按預(yù)期工作。
- 手動輪轉(zhuǎn):盡管logrotate會定期自動運(yùn)行,但你也可以通過運(yùn)行l(wèi)ogrotate -f /path/to/config命令來手動觸發(fā)日志輪轉(zhuǎn)。
首先我們需要在一個鏡像,鏡像中有nginx、cron、logrotate 等軟件,該鏡像的制作和下載可參考:https://blog.csdn.net/weixin_43702146/article/details/131958486
容器內(nèi)logrotate安裝(Debian)
Linux系統(tǒng)默認(rèn)安裝logrotate工具,logrotate是基于cron來運(yùn)行的,其腳本是/etc/cron.daily/logrotate,日志輪轉(zhuǎn)是系統(tǒng)自動完成的。
實際運(yùn)行時,logrotate會調(diào)用配置文件/etc/logrotate.conf。可以在/etc/logrotate.d目錄里放置自定義好的配置文件,用來覆蓋logrotate的缺省值
docker容器默認(rèn)是不帶logrotate工具的,安裝步驟可參考:https://blog.csdn.net/weixin_43702146/article/details/131958486
logrotate 使用方式以及參數(shù)解釋
-
語法:
logrotate [選項] [配置文件] [配置參數(shù)] -
選項:
-d或–debug:debug 模式,詳細(xì)顯示指令執(zhí)行過程,測試配置文件是否有錯誤
-f或–force :強(qiáng)制轉(zhuǎn)儲文件
-s<狀態(tài)文件>或–state=<狀態(tài)文件>:使用指定的狀態(tài)文件
-v或–version:顯示轉(zhuǎn)儲過程
-m或–mail=command :壓縮日志后,發(fā)送日志到指定郵箱。
-usage:顯示指令基本用法 -
配置文件:
指定lograote指令的配置文件,可缺省,缺省值為/etc/logrotate.conf -
配置參數(shù)
指令 | 含義 |
---|---|
daily | 每天輪轉(zhuǎn)一次。默認(rèn)情況下,輪轉(zhuǎn)周期是每周一次 |
weekly | 指定轉(zhuǎn)儲周期為每周 |
monthly | 指定轉(zhuǎn)儲周期為每月 |
指令 | 含義 |
---|---|
rotate [count] | 保留舊日志文件的數(shù)量。例如,rotate 5表示保留最近的5個舊日志文件。 |
指令 | 含義 |
---|---|
dateext | 使用當(dāng)期日期作為命名格式 |
dateformat .%s | 配合dateext使用,緊跟在下一行出現(xiàn),定義文件切割后的文件名,必須配合dateext使用,只支持 %Y %m %d %s 這四個參數(shù) |
指令 | 含義 |
---|---|
compress | 通過gzip 壓縮轉(zhuǎn)儲以后的日志 |
delaycompress | 在進(jìn)行輪轉(zhuǎn)時不立即壓縮舊日志文件,而是推遲一次壓縮操作。通常與compress選項一起使用,確保應(yīng)用程序不會在壓縮操作進(jìn)行時無法寫入新的日志數(shù)據(jù)。 |
nocompress | 不做gzip壓縮處理 |
指令 | 含義 |
---|---|
create | 輪轉(zhuǎn)時指定創(chuàng)建新文件的屬性,默認(rèn)與之前的權(quán)限一致,也可自定權(quán)限和組,如create 640 nobody nobody |
nocreate | 不建立新的日志文件 |
指令 | 含義 |
---|---|
size [size] | 當(dāng)日志文件大小達(dá)到指定大小時觸發(fā)輪轉(zhuǎn)??梢允褂胟表示KB,M表示MB,G表示GB。例如,size 100M表示日志文件大小達(dá)到100MB時進(jìn)行輪轉(zhuǎn) |
maxsize [size] | 設(shè)置日志文件的最大大小。一旦日志文件大小超過此限制,將強(qiáng)制進(jìn)行輪轉(zhuǎn)。與size選項不同的是,maxsize不會觸發(fā)定期輪轉(zhuǎn),它僅在日志文件大小超過限制時才生效 |
minsize [size] | 設(shè)置日志文件的最小大小。如果日志文件大小小于此限制,則不會進(jìn)行輪轉(zhuǎn) |
指令 | 含義 |
---|---|
ifempty | 即使日志文件為空文件也做輪轉(zhuǎn),這個是logrotate的缺省選項 |
notifempty | 只在日志文件非空時才進(jìn)行輪轉(zhuǎn)。如果日志文件為空,logrotate不會執(zhí)行輪轉(zhuǎn)操作 |
missingok | 如果日志文件不存在,也不會報錯。logrotate會繼續(xù)執(zhí)行其他輪轉(zhuǎn)規(guī)則 |
指令 | 含義 |
---|---|
postrotate [command] endscript | 在輪轉(zhuǎn)之后,postrotate 和 endscript 里面指定的命令將被執(zhí)行,通常用于重新啟動服務(wù)或執(zhí)行其他額外的操作 |
prerotate [command] endscript | 在輪轉(zhuǎn)之前,prerotate 和 endscript 里面指定的命令將被執(zhí),同樣也可用于執(zhí)行一些預(yù)處理操作 |
sharedscripts | 在執(zhí)行postrotate和prerotate時,只運(yùn)行一次共享腳本,而不是每個日志文件分別執(zhí)行 |
dateyesterday | 在執(zhí)行postrotate或prerotate時,使用昨天的日期 |
指令 | 含義 |
---|---|
copytruncate | 在輪轉(zhuǎn)時,將當(dāng)前日志文件復(fù)制到新文件,然后截斷當(dāng)前日志文件。這使得應(yīng)用程序可以繼續(xù)寫入當(dāng)前文件,而不會受到日志文件名變化的影響 |
nocopytruncate | 不使用復(fù)制并截斷方式。舊日志文件會被重命名,這可能導(dǎo)致應(yīng)用程序無法繼續(xù)寫入日志 |
指令 | 含義 |
---|---|
errors [address] | 專儲時的錯誤信息發(fā)送到指定的Email 地址 |
mail [address] | 把轉(zhuǎn)儲的日志文件發(fā)送到指定的E-mail 地址 |
nomail | 轉(zhuǎn)儲時不發(fā)送日志文件 |
指令 | 含義 |
---|---|
olddir [directory] | 轉(zhuǎn)儲后的日志文件放入指定的目錄,必須和當(dāng)前日志文件在同一個文件系統(tǒng) |
noolddir | 轉(zhuǎn)儲后的日志文件和當(dāng)前日志文件放在同一個目錄下 |
使用logrotate測試切割
編輯配置文件
編輯文件 /etc/logrotate.d/nginx
,以下是一個簡要按天存儲的配置
/var/log/nginx/*.log {# 每天輪轉(zhuǎn)daily# 忽略錯誤missingoknotifempty# 保留最近7個rotate 7# 禁用gzip壓縮delaycompress# 創(chuàng)建新的文件create# 輪轉(zhuǎn)后的日志文件后綴名為日期dateext# 輪轉(zhuǎn)后執(zhí)行腳本sharedscriptspostrotateif [ -f /var/run/nginx.pid ]; thenkill -USR1 `cat /var/run/nginx.pid`fiendscript
}
修改狀態(tài)文件
注意,當(dāng)手動執(zhí)行logrotate /etc/logrotate.conf
時,執(zhí)行之后并不會切割log文件。
logrotate的工作原理大概是:每次切割操作、或首次切割,會記錄所有l(wèi)og文件的時間點(diǎn)在狀態(tài)文件內(nèi) /var/lib/logrotate/status
。
下一次執(zhí)行的時候,如果判斷時間減去記錄的時間已經(jīng)超過一定時長(配置的時間),那么就會執(zhí)行切割。
所以如果你想馬上切割,需要自己修改狀態(tài)文件,把時間改為1天前,再執(zhí)行。
修改狀態(tài)文件vim /var/lib/logrotate/status
:
logrotate state -- version 2
"/var/log/nginx/error.log" 2023-8-7-22:35:37
"/var/log/nginx/access.log" 2023-8-7-22:35:37
交互執(zhí)行日志輪轉(zhuǎn)logrotate -vf /etc/logrotate.d/nginx
:
root@75e7c81b599d:/# logrotate -vf /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx
Creating stub state file: /var/lib/logrotate/status
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entriesHandling 1 logsrotating pattern: /var/log/nginx/*.log forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
Creating new stateNow: 2023-08-07 22:35Last rotated at 2023-08-07 22:00log needs rotating
considering log /var/log/nginx/error.log
Creating new stateNow: 2023-08-07 22:35Last rotated at 2023-08-07 22:00log needs rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 7
dateext suffix '-20230807'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
rotating log /var/log/nginx/error.log, log->rotateCount is 7
dateext suffix '-20230807'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/nginx/access.log to /var/log/nginx/access.log-20230807
creating new /var/log/nginx/access.log mode = 0644 uid = 101 gid = 0
renaming /var/log/nginx/error.log to /var/log/nginx/error.log-20230807
creating new /var/log/nginx/error.log mode = 0644 uid = 101 gid = 0
running postrotate script
發(fā)現(xiàn)指定目錄下的日志已經(jīng)變成了:
-rw-r--r-- 1 101 root 40745 Aug 7 22:41 access.log
-rw-r--r-- 1 root root 3173 Aug 7 14:32 access.log-20230806
-rw-r--r-- 1 101 root 61823 Aug 7 22:31 access.log-20230807
-rw-r--r-- 1 101 root 357 Aug 7 22:37 error.log
-rw-r--r-- 1 root root 2635 Aug 7 14:49 error.log-20230806
-rw-r--r-- 1 101 root 15466 Aug 7 22:35 error.log-20230807
配合定時任務(wù)完成每天輪轉(zhuǎn)
啟動定時任務(wù)
service cron start
crontab語法
crontab [-u user] file
或者
crontab [-u user] [ -e | -l | -r ]
參數(shù)說明:
- -u user 是指設(shè)定指定 user 的時程表,這個前提是你必須要有其權(quán)限(比如說是 root)才能夠指定他人的時程表。如果不使用 -u user 的話,就是表示設(shè)定自己的時程表。
- -e 執(zhí)行文字編輯器來設(shè)定時程表
- -l 列出目前的時程表
- -r 刪除目前的時程表(慎用)
設(shè)置每天0點(diǎn)執(zhí)行日志輪轉(zhuǎn)
crontab -e
在彈出的編輯器內(nèi)輸入如下內(nèi)容:
59 23 * * * /usr/sbin/logrotate -f /home/zmq/daily_logrotate
crontab -l
查看當(dāng)前定時任務(wù):
root@75e7c81b599d:/# crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx