首頁 編程技術K8s 日志高效查看神器,提升運維效率10倍!

K8s 日志高效查看神器,提升運維效率10倍!

運維派隸屬馬哥教育旗下專業運維社區,是國內成立最早的IT運維技術社區,歡迎關注公眾號:yunweipai
領取學習更多免費Linux云計算、Python、Docker、K8s教程關注公眾號:馬哥linux運維

通常情況下,在部署了 K8S 服務之后,為了更好地監控服務的運行情況,都會接入對應的日志系統來進行檢測和分析,比如常見的 Filebeat + ElasticSearch + Kibana 這一套組合來完成。

雖然該組合可以滿足我們對于服務監控的要求,但是如果只是部署一個內部單服務用的話,未免顯得大材小用,而且部署服務還會帶來大量的資源消耗。那么有沒有簡單查看 K8S 中多個 Pod 中的日志工具呢?咳咳咳,那么今天就介紹兩款超好用的多容器實時日志查看工具 Kubetail 和 Stern。

1. Kubetail 工具

Bash script to tail Kubernetes logs from multiple pods at the same time


Kubetail 項目其實是一個簡單 Shell 腳本,它可以將多個 Pod 中的日志信息聚合到一起進行展示,并支持彩色輸出和條件過濾。

K8s 日志高效查看神器,提升運維效率10倍!插圖

查看K8S容器日志內容 – Kubetail

1.1 工具安裝

安裝非常簡單,而且針對不同平臺適配不同類型的工具。

  • 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

1.2 工具使用

使用起來也非常的簡單,短短 2 分鐘就可以上手!

  • 示例說明前數據信息準備
# show all your pods
$ kubectl get pods -n test
NAME                   READY     STATUS    RESTARTS   AGE
app1-v1-aba8y          1/1       Running   0          1d
app1-v1-gc4st          1/1       Running   0          1d
app1-v1-m8acl          1/1       Running   0          6d
app1-v1-s20d0          1/1       Running   0          1d
app2-v31-9pbpn         1/1       Running   0          1d
app2-v31-q74wg         1/1       Running   0          1d
my-demo-v5-0fa8o       1/1       Running   0          3h
my-demo-v5-yhren       1/1       Running   0          2h
  • 介紹了工具的使用方式
# 同時跟蹤兩個"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

# 使用正則表達式
$ kubetail "^app1|.*my-demo.*" --regex
  • 通過使用 -k 參數,您可以指定 kubetail 如何使用顏色
# pod:   只有Pod名稱著色且其他輸出均使用終端默認顏色
# line:  整行是彩色的(默認)
# false: 所有輸出都不著色
$ kubetail app2 -k pod
$ kubetail app2 -k line
$ kubetail app2 -k false
  • 常見命令行參數說明
編號命令行參數對應含義解析
1-n指定命名空間的名稱
2-c指定多容器 Pod 中的容器具體名稱
3-k將輸出的日志內容進行著色顯示
4-b是否使用 line-buffered 特性,默認為 false 狀態
5-l標簽過濾器,用于忽略 Pod 名稱
6-t指定 Kubeconfig 文件中的 Context 內容
7-s指定返回一個相對時間之后的日志;例如 5s/2m/3h,默認是 10s

2. Stern 工具

Multi pod and container log tailing for Kubernetes

Stern 是使用 Go 語言開發的一款開箱即用的簡單工具,它可以將多個 Pod 中的日志信息聚合到一起進行展示,并支持彩色輸出和條件過濾。需要說的是,改工具已經很久都沒有更新過了,所有不建議使用了。

K8s 日志高效查看神器,提升運維效率10倍!插圖1

2.1 工具安裝

安裝非常簡單,而且針對不同平臺適配不同類型的工具。

  • Homebrew
# install kubetail using brew
$ brew install stern
  • Linux
# download and to go
# https://github.com/wercker/stern/tags
$ wget https://github.com/wercker/stern/releases/download/1.11.0/stern_linux_amd64
$ chmod +x stern_linux_amd64
$ mv stern_linux_amd64 /usr/local/bin
  • zsh plugin
# bash-completion
$ brew install bash-completion
$ source <(brew --prefix)/etc/bash-completion
$ source <(stern --completion=bash)

# .zshrc
$ source <(stern --completion=zsh)

2.2 工具使用

使用起來也非常的簡單,短短 2 分鐘就可以上手!

  • 示例說明前數據信息準備
# show all your pods
$ kubectl get pods -n test
NAME                   READY     STATUS    RESTARTS   AGE
app1-v1-aba8y          1/1       Running   0          1d
app1-v1-gc4st          1/1       Running   0          1d
app1-v1-m8acl          1/1       Running   0          6d
app1-v1-s20d0          1/1       Running   0          1d
app2-v31-9pbpn         1/1       Running   0          1d
app2-v31-q74wg         1/1       Running   0          1d
my-demo-v5-0fa8o       1/1       Running   0          3h
my-demo-v5-yhren       1/1       Running   0          2h
  • 介紹了工具的使用方式
# 查看默認名稱空間下的所有Pod日志
$ stern  .

# 查看 Pod 中指定容器的日志
$ stern app2 --container container1

# 查看指定命名空間中容器的日志
$ stern app2 --namespace namespace1

# 查看指定命名空間中除指定容器外的所有容器的日志
$ stern --namespace namespace1 --exclude-container container1 .

# 查看指定時間范圍內容器的日志(15分鐘內)
$ stern app2 -t --since 15m

# 查看所有命名空間中符合指定標簽容器的日志
$ stern --all-namespaces -l run=nginx

# 查找前端Pod中版本為canary的日志
$ stern frontend --selector release=canary

# 將日志消息通過管道傳輸到jq命令
$ 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
  • 常見命令行參數說明
flagdefaultpurpose
--container.*Container name when multiple containers in pod (regular expression)
--exclude-containerContainer name to exclude when multiple containers in pod (regular expression)
--container-staterunningTail containers with status in running, waiting or terminated. Default to running.
--timestampsPrint timestamps
--sinceReturn logs newer than a relative duration like 52, 2m, or 3h. Displays all if omitted
--contextKubernetes context to use. Default to kubectl config current-context
--excludeLog lines to exclude; specify multiple with additional --exclude; (regular expression)
--namespaceKubernetes namespace to use. Default to namespace configured in Kubernetes context
--kubeconfig~/.kube/configPath to kubeconfig file to use
--all-namespacesIf present, tail across all namespaces. A specific namespace is ignored even if specified with –namespace.
--selectorSelector (label query) to filter on. If present, default to .* for the pod-query.
--tail-1The number of lines from the end of the logs to show. Defaults to -1, showing all logs.
--colorautoForce set color output. auto: colorize if tty attached, always: always colorize, never: never colorize
--outputdefaultSpecify predefined template. Currently support: [default, raw, json] See templates section
templateTemplate to use for log lines, leave empty to use –output flag

3. 參考鏈接地址

  • johanhaleby/kubetail
  • wercker/stern

本文鏈接:http://www.royaladd.com/43529.html

網友評論comments

發表回復

您的電子郵箱地址不會被公開。

暫無評論

Copyright ? 2012-2022 YUNWEIPAI.COM - 運維派 京ICP備16064699號-6
掃二維碼
掃二維碼
返回頂部
欧美激情视频一区二区|国产精品毛片va一区二区|999国内精品永久免费|国产无码sm视频在线观看