自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

兩款超好用的K8S實(shí)時(shí)日志查看工具分享

開發(fā) 開發(fā)工具
只要能想到的,別人肯定也想到了,今天就分享兩款超好用的多容器實(shí)時(shí)日志查看工具 Kubetail 和 Stern。

在我們基于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

-n

指定命名空間的名稱

2

-c

指定多容器 Pod 中的容器具體名稱

3

-k

將輸出的日志內(nèi)容進(jìn)行著色顯示

4

-b

是否使用 line-buffered 特性,默認(rèn)為 false 狀態(tài)

5

-l

標(biāo)簽過濾器,用于忽略 Pod 名稱

6

-t

指定 Kubeconfig 文件中的 Context 內(nèi)容

7

-s

指定返回一個(gè)相對時(shí)間之后的日志;例如 5s/2m/3h,默認(rèn)是 10s

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

--all-namespaces-A

false

If present, tail across all namespaces. A specific namespace is ignored even if specified with --namespace.

--color

auto

Force set color output. 'auto': colorize if tty attached, 'always': always colorize, 'never': never colorize.

--completion


Output stern command-line completion code for the specified shell. Can be 'bash', 'zsh' or 'fish'.

--config

~/.config/stern/config.yaml

Path to the stern config file

--container-c

.*

Container name when multiple containers in pod. (regular expression)

--container-state

all

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.

--context


The name of the kubeconfig context to use

--ephemeral-containers

true

Include or exclude ephemeral containers.

--exclude-e

[]

Log lines to exclude. (regular expression)

--exclude-container-E

[]

Container name to exclude when multiple containers in pod. (regular expression)

--exclude-pod

[]

Pod name to exclude. (regular expression)

--field-selector


Selector (field query) to filter on. If present, default to ".*" for the pod-query.

--highlight-H

[]

Log lines to highlight. (regular expression)

--include-i

[]

Log lines to include. (regular expression)

--init-containers

true

Include or exclude init containers.

--kubeconfig


Path to the kubeconfig file to use for CLI requests.

--max-log-requests

-1

Maximum number of concurrent logs to request. Defaults to 50, but 5 when specifying --no-follow

--namespace-n


Kubernetes namespace to use. Default to namespace configured in kubernetes context. To specify multiple namespaces, repeat this or set comma-separated value.

--no-follow

false

Exit when all logs have been shown.

--node


Node name to filter on.

--only-log-lines

false

Print only log lines

--output-o

default

Specify predefined template. Currently support: [default, raw, json, extjson, ppextjson]

--prompt-p

false

Toggle interactive prompt for selecting 'app.kubernetes.io/instance' label values.

--selector-l


Selector (label query) to filter on. If present, default to ".*" for the pod-query.

--show-hidden-options

false

Print a list of hidden options.

--since-s

48h0m0s

Return logs newer than a relative duration like 5s, 2m, or 3h.

--tail

-1

The number of lines from the end of the logs to show. Defaults to -1, showing all logs.

--template


Template to use for log lines, leave empty to use --output flag.

--template-file-T


Path to template to use for log lines, leave empty to use --output flag. It overrides --template option.

--timestamps-t


Print timestamps with the specified format. One of 'default' or 'short'. If specified but without value, 'default' is used.

--timezone

Local

Set timestamps to specific timezone.

--verbosity

0

Number of the log level verbosity

--version-v

false

Print the version and exit.

總結(jié)

以上兩款工具工具都能實(shí)現(xiàn)同樣的效果,沒有好壞之分,大家根據(jù)可以根據(jù)自己的喜好選擇其一即可。

責(zé)任編輯:武曉燕 來源: 架構(gòu)成長指南
相關(guān)推薦

2020-11-09 16:20:59

Kubernetes工具Linux

2020-12-16 15:56:05

Kubernetes日志工具

2021-12-21 08:31:07

k8s診斷工具kubectl-deb

2015-10-10 14:30:26

PythonSentry 8

2019-07-31 11:30:25

MySQL數(shù)據(jù)庫工具

2015-11-25 14:43:44

2011-06-08 22:43:41

2023-10-10 14:54:31

插件開發(fā)

2024-03-01 19:59:17

2020-08-12 09:35:23

Docker工具開源

2024-02-01 09:48:17

2023-11-06 01:17:25

主機(jī)容器選項(xiàng)

2010-07-12 15:43:35

UML建模工具

2010-05-26 09:10:44

Windows清理工具

2024-07-22 08:16:15

2022-04-22 13:32:01

K8s容器引擎架構(gòu)

2014-08-19 10:14:47

App應(yīng)用原型設(shè)計(jì)工具

2016-11-09 08:49:39

Linux桌面錄制

2010-06-28 09:53:43

UML建模工具Rose

2023-11-06 07:16:22

WasmK8s模塊
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號