兩款超好用的K8S實(shí)時(shí)日志查看工具分享
在我們基于k8s部署了微服務(wù)服務(wù)之后,為了更好排查系統(tǒng)異?;蛘弑O(jiān)控系統(tǒng)運(yùn)行情況,都會(huì)接入日志系統(tǒng)進(jìn)行在線日志查詢,比如常見的 有ELK、EFK、Loki。雖然這些日志系統(tǒng)都可以對系統(tǒng)進(jìn)行日志收集和展現(xiàn),但是只是部署一個(gè)內(nèi)部單體應(yīng)用或者就是懶不想部署這些,覺得引入這些日志系統(tǒng)有點(diǎn)資源浪費(fèi)。那么有沒有簡單查看 K8S 中多個(gè) Pod 中的日志工具?只要能想到的,別人肯定也想到了,今天就分享兩款超好用的多容器實(shí)時(shí)日志查看工具 Kubetail 和 Stern。
Kubetail
Kubetail 是一個(gè)Shell腳本,很簡單,它能夠?qū)碜远鄠€(gè) Pod 的日志聚合(尾部/跟蹤)到一個(gè)流中進(jìn)行展示,同時(shí)支持色彩和條件過濾,目前在github 有3.2k Star。
項(xiàng)目地址:https://github.com/johanhaleby/kubetail?tab=readme-ov-file
圖片
安裝
Homebrew
# install kubetail using brew
$ brew tap johanhaleby/kubetail && brew install kubetail
Linux
# download and to go
# https://github.com/johanhaleby/kubetail/releases
$ wget https://raw.githubusercontent.com/johanhaleby/kubetail/master/kubetail
$ chmod +x kubetail
$ cp kubetail /usr/local/bin
zsh plugin
# oh-my-zsh
$ cd ~/.oh-my-zsh/custom/plugins/
$ git clone https://github.com/johanhaleby/kubetail.git kubetail
$ vim ~/.zshrc
plugins=( ... kubetail )
$ source ~/.zshrc
使用
# 查詢default命名空間下,mcenter-backend的日志
kubetail msgcenter-backend
# 查詢default命名空間下,mcenter-backend的前5分鐘的日志
kubetail msgcenter-backend -s 5m
圖片
# 對日志內(nèi)容進(jìn)行色彩標(biāo)注,要求是多個(gè)pod才可以
kubetail dp-manager-backend,msgcenter-backend
圖片
#指定命名空間查詢
kubetail loki-0 -n loki-stack
圖片
# 其他使用方式
# 同時(shí)跟蹤兩個(gè)"app2"的Pod的日志
$ kubetail app2
$ kubetail app1,app2
# 指定"app2"的Pod里面的容器名稱
$ kubetail app2 -c container1
$ kubetail app2 -c container1 -c container2
$ kubetail app2 -c container1 -n namespace1
# 使用正則表達(dá)式
$ kubetail "^app1|.*my-demo.*" --regex
通過使用 -k 參數(shù),我們可以指定 kubetail 如何使用顏色
# pod: 只有Pod名稱著色且其他輸出均使用終端默認(rèn)顏色
# line: 整行是彩色的(默認(rèn))
# false: 所有輸出都不著色
$ kubetail app2 -k pod
$ kubetail app2 -k line
$ kubetail app2 -k false
常見命令行參數(shù)說明
編號 | 命令行參數(shù) | 對應(yīng)含義解析 |
1 |
| 指定命名空間的名稱 |
2 |
| 指定多容器 |
3 |
| 將輸出的日志內(nèi)容進(jìn)行著色顯示 |
4 |
| 是否使用 |
5 |
| 標(biāo)簽過濾器,用于忽略 |
6 |
| 指定 |
7 |
| 指定返回一個(gè)相對時(shí)間之后的日志;例如 |
Stern
Stern的功能類似,但是比Kubetail要強(qiáng)大點(diǎn),它是使用 Go 語言開發(fā)的一款開箱即用的簡單工具,它可以將多個(gè) Pod 中的日志信息聚合到一起進(jìn)行展示,并支持彩色輸出和條件過濾,目前github有 2.6k star
項(xiàng)目地址:https://github.com/stern/stern
圖片
安裝
Homebrew (Linux/macOS)
brew install stern
Krew (Linux/macOS/Windows)
kubectl krew install stern
asdf (Linux/macOS)
asdf plugin-add stern
asdf install stern latest
源碼安裝
go install github.com/stern/stern@latest
使用
#查詢前10行日志,默認(rèn)就會(huì)給 pod、容器增加顏色標(biāo)注
stern msgcenter-backend --tail 10
圖片
# 查詢monitoring命名空間下,所有的pod 日志,--tail=0 表示最新日志,不指定從最開始查詢
stern . -n monitoring --tail 0
圖片
# 查詢前2分鐘的日志
stern msgcenter-backend -s 2m
圖片
# 根據(jù)正則匹配查詢
stern "apollo-\w" --tail 0
圖片
命令大全
# 查看默認(rèn)名稱空間下的所有Pod日志
$ stern .
# 查看 Pod 中指定容器的日志
$ stern app2 --container container1
# 查看指定命名空間中容器的日志
$ stern app2 --namespace namespace1
# 查看指定命名空間中除指定容器外的所有容器的日志
$ stern --namespace namespace1 --exclude-container container1 .
# 查看指定時(shí)間范圍內(nèi)容器的日志(15分鐘內(nèi))
$ stern app2 -t --since 15m
# 查看所有命名空間中符合指定標(biāo)簽容器的日志
$ stern --all-namespaces -l run=nginx
# 查找前端Pod中版本為canary的日志
$ stern frontend --selector release=canary
# 將日志消息通過管道傳輸?shù)絡(luò)q命令
$ stern backend -o json | jq .
# 僅輸出日志消息本身
$ stern backend -o raw
# 使用自定義模板輸出
$ stern --template '{{.Message}} ({{.Namespace}}/{{.PodName}}/{{.ContainerName}})' backend
# 使用stern提供的顏色的自定義模板輸出
$ stern --template '{{.Message}} ({{.Namespace}}/{{color .PodColor .PodName}}/{{color .ContainerColor .ContainerName}})' backend
常見命令行參數(shù)說明
flag | default | purpose |
|
| If present, tail across all namespaces. A specific namespace is ignored even if specified with --namespace. |
|
| Force set color output. 'auto': colorize if tty attached, 'always': always colorize, 'never': never colorize. |
| Output stern command-line completion code for the specified shell. Can be 'bash', 'zsh' or 'fish'. | |
|
| Path to the stern config file |
|
| Container name when multiple containers in pod. (regular expression) |
|
| Tail containers with state in running, waiting, terminated, or all. 'all' matches all container states. To specify multiple states, repeat this or set comma-separated value. |
| The name of the kubeconfig context to use | |
|
| Include or exclude ephemeral containers. |
|
| Log lines to exclude. (regular expression) |
|
| Container name to exclude when multiple containers in pod. (regular expression) |
|
| Pod name to exclude. (regular expression) |
| Selector (field query) to filter on. If present, default to ".*" for the pod-query. | |
|
| Log lines to highlight. (regular expression) |
|
| Log lines to include. (regular expression) |
|
| Include or exclude init containers. |
| Path to the kubeconfig file to use for CLI requests. | |
|
| Maximum number of concurrent logs to request. Defaults to 50, but 5 when specifying --no-follow |
| Kubernetes namespace to use. Default to namespace configured in kubernetes context. To specify multiple namespaces, repeat this or set comma-separated value. | |
|
| Exit when all logs have been shown. |
| Node name to filter on. | |
|
| Print only log lines |
|
| Specify predefined template. Currently support: [default, raw, json, extjson, ppextjson] |
|
| Toggle interactive prompt for selecting 'app.kubernetes.io/instance' label values. |
| Selector (label query) to filter on. If present, default to ".*" for the pod-query. | |
|
| Print a list of hidden options. |
|
| Return logs newer than a relative duration like 5s, 2m, or 3h. |
|
| The number of lines from the end of the logs to show. Defaults to -1, showing all logs. |
| Template to use for log lines, leave empty to use --output flag. | |
| Path to template to use for log lines, leave empty to use --output flag. It overrides --template option. | |
| Print timestamps with the specified format. One of 'default' or 'short'. If specified but without value, 'default' is used. | |
|
| Set timestamps to specific timezone. |
|
| Number of the log level verbosity |
|
| Print the version and exit. |
總結(jié)
以上兩款工具工具都能實(shí)現(xiàn)同樣的效果,沒有好壞之分,大家根據(jù)可以根據(jù)自己的喜好選擇其一即可。