<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 11434字,需瀏覽 23分鐘

           ·

          2023-08-31 08:10


                      

          作者:Escape

          鏈接:https://www.escapelife.site/posts/b6ba9ad8.html

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

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

          1. Kubetail 工具

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


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

          查看K8S容器日志內(nèi)容 - 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 分鐘就可以上手!

          • 示例說明前數(shù)據(jù)信息準備
          # 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 參數(shù),您可以指定 kubetail 如何使用顏色
          # pod:   只有Pod名稱著色且其他輸出均使用終端默認顏色
          # line:  整行是彩色的(默認)
          # false: 所有輸出都不著色
          $ kubetail app2 -k pod
          $ kubetail app2 -k line
          $ kubetail app2 -k false


          • 常見命令行參數(shù)說明
          編號 命令行參數(shù) 對應(yīng)含義解析
          1 -n 指定命名空間的名稱
          2 -c 指定多容器 Pod 中的容器具體名稱
          3 -k 將輸出的日志內(nèi)容進行著色顯示
          4 -b 是否使用 line-buffered 特性,默認為 false 狀態(tài)
          5 -l 標簽過濾器,用于忽略 Pod 名稱
          6 -t 指定 Kubeconfig 文件中的 Context 內(nèi)容
          7 -s 指定返回一個相對時間之后的日志;例如 5s/2m/3h,默認是 10s

          2. Stern 工具

          Multi pod and container log tailing for Kubernetes

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

          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 分鐘就可以上手!

          • 示例說明前數(shù)據(jù)信息準備
          # 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 .

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

          # 查看所有命名空間中符合指定標簽容器的日志
          $ 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
          --container .* Container name when multiple containers in pod (regular expression)
          --exclude-container
          Container name to exclude when multiple containers in pod (regular expression)
          --container-state running Tail containers with status in running, waiting or terminated. Default to running.
          --timestamps
          Print timestamps
          --since
          Return logs newer than a relative duration like 52, 2m, or 3h. Displays all if omitted
          --context
          Kubernetes context to use. Default to kubectl config current-context
          --exclude
          Log lines to exclude; specify multiple with additional --exclude; (regular expression)
          --namespace
          Kubernetes namespace to use. Default to namespace configured in Kubernetes context
          --kubeconfig ~/.kube/config Path to kubeconfig file to use
          --all-namespaces
          If present, tail across all namespaces. A specific namespace is ignored even if specified with –namespace.
          --selector
          Selector (label query) to filter on. If present, default to .* for the pod-query.
          --tail -1 The number of lines from the end of the logs to show. Defaults to -1, showing all logs.
          --color auto Force set color output. auto: colorize if tty attached, always: always colorize, never: never colorize
          --output default Specify predefined template. Currently support: [default, raw, json] See templates section
          template
          Template to use for log lines, leave empty to use –output flag

          3. 參考鏈接地址

          • johanhaleby/kubetail
          • wercker/stern

          • 兩款超好用的 Kubernetes 實時日志查看工具



              

          最近很多小伙伴找我要一些程序員必備資料,于是我翻出了壓箱底的寶藏,免費分享給大家!


          掃描海報二維碼免費獲取。


          瀏覽 1717
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  手机无码在线观看 | 大鸡吧插入视频 | 高清无码日 | 一本色道无码道DVD在线播放 | 麻豆精品无码久久久介绍 |