shell腳本查看k8s日志介紹
查看日志:
kubectl logs -f podName --tail 100
比如我們?nèi)绻氩橹付ǖ膒od,指定行數(shù),指定的內(nèi)容,
每次都需要輸入
kubectl logs -f xxx --tail yyy | grep zzz
為了方便,可自定義腳本,輸入
sh .sh xxx yyy zzz
即可,并且xxx
支持RE
;
占位符的方式
#!/bin/bash # kubectl get pods #notification x="kubectl logs -f" y="--tail" g="|grep" name=`kubectl get pods | grep ^$1 | awk '{print $1}'` x="eval $x $name $y $2 $g $3" ${x} # sh log.sh podName 20 content # 最終:kubectl logs -f podName --tail 20 | grep content
指定參數(shù) getopts
#!/bin/bash # ":":如果某個選項(option)后面出現(xiàn)了冒號(":"),則表示這個選項后面可以接參數(shù) x="kubectl logs -f" y="--tail" g="|grep" while getopts ":n:f:c:" opt do case $opt in n) name=`kubectl get pods | grep ^$OPTARG | awk '{print $1}'` x="$x $name" ;; f) x="$x $y $OPTARG" ;; c) x="$x $g $OPTARG" ;; ?) echo "未知參數(shù)" exit 1;; esac done x="eval $x" ${x} # sh log.sh -n podName -f 20 -c content # 最終:kubectl logs -f podName --tail 20 | grep content
問題
1.執(zhí)行 shell 腳本\r問題
腳本是在window下編輯完成后上傳到linux上執(zhí)行的,win下的換行是回車符+換行符,也就是\r\n,而unix下是換行符\n。linux下不識別\r為回車符,所以導致每行的配置都多了個\r,因此是腳本編碼的問題。
2.命令中的grep
可以發(fā)現(xiàn)最終拼接出來的字符串,是一條正確的命令,但是通過${CMD}
執(zhí)行該變量報錯。
原因:
如果在shell中定義一個命令,帶了管道,例如
CMD=“ls -l | grep xx”
直接執(zhí)行$CMD,會出現(xiàn)如下報錯
ls: cannot access |: No such file or directory
ls: cannot access grep: No such file or directory
管道符會被解釋為普通字符
加上eval
CMD=“eval ls -l | grep xx”
到此這篇關于shell腳本查看k8s日志介紹的文章就介紹到這了,更多相關shell查看k8s日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
linux 中more、less 和 most 的區(qū)別
more 是一個老式的、基礎的終端分頁閱讀器,它可以用于打開指定的文件并進行交互式閱讀。這篇文章主要給大家介紹linux 中more、less 和 most 的區(qū)別,感興趣的朋友跟隨小編一起看看吧2018-11-11Shell腳本實現(xiàn)檢查服務器安全狀態(tài)(用戶、登錄IP、防火墻檢查)
這篇文章主要介紹了Shell腳本實現(xiàn)檢查服務器安全狀態(tài),本文主要檢查3個方面,分別是系統(tǒng)用戶檢查、登錄IP檢查、防火墻狀態(tài)檢查,需要的朋友可以參考下2014-12-12