Linux 中的 Log Rotation

最近在 Linux 主機中 /var/log 資料夾底下發現有許多的 xxx.log.1, xxx.log.2 的檔案,好奇去查並記錄下來 這些檔案是 log rotate 產生的檔案,主要功能是做日誌檔案的輪替 logrotate 設定檔 主要設定檔的路徑是在 /etc/logrotate.conf,會載入 /etc/logrotate.d/ 底下的檔案,根據設定檔進行 rotate 執行 logrotate logrotate 預設會在每日的 crontab 中執行 (/etc/cron.daily/logrotate),如果想要手動執行 logrotate 的話,可以直接呼叫 logrotate --force 指令,--force 是強制執行 rotate 檔案,可搭配 --debug 一起使用並觀察 logrotate 範例 在 /etc/logrotate.conf 檔案中定義 logrotate 的預設值,範例如下 # 設定頻率對日誌檔做 rotate (hourly, daily, weekly, monthly, yearly) (hourly 需要改變 logrotate 的頻率) # weekly [weekday] <- default 0 (0 means Sunday, 1 means Monday, ... , 6 means Saturday) weekly # 日誌被 rotate 了 4 次後刪除舊日誌 rotate 4 # rotate 舊日誌文件後創建新日誌文件權限 # create [mode(octal)] [owner] [group]....

2020-11-05 · Jett

用 KIND 搭建本地 Kubernetes Cluster

最近在學習 Kubernetes 相關知識,需要建立 cluster 環境,開始學習時會建議使用單節點的 minikube 當作環境,但是有些功能是在單節點環境做不到,需要建立多節點環境進行測試,剛好有看到有人推薦 KIND (Kubernetes IN Docker),這套工具可以快速建置多節點的環境 安裝 KIND 環境中已經預設安裝 docker,接下來只要下載 KIND-cli brew install kind KIND-cli autocomplete cat << EOF >> ~/.zshrc # kind-cli autocomplete source <(kind completion zsh) EOF 建置 cluster 在 KIND 的 repo 中的有提供範例 config 設定 kind-example-config.yaml ,可以快速建立四個節點 (一個 control-plane 節點, 三個 workers 節點) kind create cluster --name mykind --config ./kind-example-config.yaml 刪除 cluster kind delete cluster --name mykind happy using kubernetes clusters …

2020-10-12 · Jett

Linux 中的 tar 指令

在 Linux 中常常看到副檔名 .tar.gz 的檔案,紀錄一下過程 打包檔案 副檔名包含 .tar 的檔案,都是使用 tar 指令進行打包,也代表未使用壓縮的檔案 看一下怎麼打包檔案 tar -c -v -f a.tar /etc -c, --create: create a new ARCHIVE -f, --file=ARCHIVE: use ARCHIVE file or device ARCHIVE -v, --verbose: verbosely list files processed 壓縮檔案 副檔名 .tar.gz 的檔案,是經過 gzip 壓縮後的 tar 檔案,可以縮寫成 .tgz 看一下怎麼壓縮檔案,多一個參數 -z tar -c -z -v -f a.tar.gz /etc -z, --gzip Filter the archive through gzip(1) 解壓縮檔案 看一下怎麼解壓縮檔案,使用 -x 參數 tar -x -v -f a....

2020-10-04 · Jett

Linux 中的 sed 指令

在 Linux 的 sed 版本是 GNU,在 macOS 的 sed 版本是 BSD,兩者在使用上略有不同,踩到這個雷,紀錄一下 -i 參數的差異 最常見的就是 -i,如果沒有提供後綴,則原始文件將被覆蓋而不進行備份(in place) 下面的指令在 macOS 中是可以執行的 cat << EOF > test.txt foo bar baz EOF sed -e 's/foo/bar/g' -i '' test.txt 但是在 Linux 中卻會噴錯 sed: can't read : No such file or directory 在 Linux 中只需要使用 -i 即可 sed -e 's/foo/bar/g' -i test.txt 更改 separator 另外一個技巧就是更換 separator s/regular expression/replacement/flags 如果使用 / 當作 separator,在正規表示式中有用到 / 的地方,需要使用 \ 跳脫,這會造成很難閱讀...

2020-09-10 · Jett

Linux 中的 tee 指令

在設定 config 檔案時,第一次可能會用編輯器進行修改,但第二、三次就會用指令的方式進行修改 常常遇到因為權限問題而沒辦法寫入檔案中,沒辦法指令化,範例如下 sudo echo 'foo' > /foo -bash: /foo: Permission denied 在 這篇 剛好有人問,因此紀錄一下 tee tee 指令是從 standard input (stdin) 讀取並輸出 standard output (stdout) 和文件,同時「螢幕輸出」及「輸出檔案」 echo 'foo' | sudo tee /foo 上面的方式會直接覆蓋掉檔案 /foo,如果想要追加寫入檔案的話,可以使用 -a 或 --append 參數 (類似於 >> 的方式) echo 'foo' | sudo tee -a /foo 如果不想要看到 tee 輸出在螢幕的內容,可以將 tee 的 stdout 丟到黑洞中 /dev/null echo 'foo' | sudo tee -a /foo > /dev/null

2020-08-09 · Jett