两款超好用的K8S实时日志查看工具分享

只要能想到的,别人肯定也想到了,今天就分享两款超好用的多容器实时日志查看工具 Kubetail 和 Stern。

只要能想到的,别人肯定也想到了,今天就分享两款超好用的多容器实时日志查看工具 Kubetail 和 Stern。

在我们基于k8s部署了微服务服务之后,为了更好排查系统异常或者监控系统运行情况,都会接入日志系统进行在线日志查询,比如常见的 有ELK、EFK、Loki。虽然这些日志系统都可以对系统进行日志收集和展现,但是只是部署一个内部单体应用或者就是懒不想部署这些,觉得引入这些日志系统有点资源浪费。那么有没有简单查看K8S中多个Pod中的日志工具?只要能想到的,别人肯定也想到了,今天就分享两款超好用的多容器实时日志查看工具Kubetail和Stern。

Kubetail

Kubetail 是一个Shell脚本,很简单,它能够将来自多个 Pod 的日志聚合(尾部/跟踪)到一个流中进行展示,同时支持色彩和条件过滤,目前在github 有3.2k Star。

项目地址: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

图片图片

# 对日志内容进行色彩标注,要求是多个pod才可以
kubetail dp-manager-backend,msgcenter-backend

图片图片

#指定命名空间查询
kubetail loki-0 -n loki-stack

图片图片

# 其他使用方式
# 同时跟踪两个"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

Stern

Stern的功能类似,但是比Kubetail要强大点,它是使用Go语言开发的一款开箱即用的简单工具,它可以将多个Pod中的日志信息聚合到一起进行展示,并支持彩色输出和条件过滤,目前github有 2.6k star

项目地址: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行日志,默认就会给 pod、容器增加颜色标注
stern msgcenter-backend --tail 10

图片图片

# 查询monitoring命名空间下,所有的pod 日志,--tail=0 表示最新日志,不指定从最开始查询
stern . -n monitoring --tail 0

图片图片

# 查询前2分钟的日志
stern msgcenter-backend -s 2m

图片图片

# 根据正则匹配查询
stern "apollo-\w" --tail 0

图片图片

命令大全

# 查看默认名称空间下的所有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

常见命令行参数说明

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.

总结

以上两款工具工具都能实现同样的效果,没有好坏之分,大家根据可以根据自己的喜好选择其一即可。

©本文为清一色官方代发,观点仅代表作者本人,与清一色无关。清一色对文中陈述、观点判断保持中立,不对所包含内容的准确性、可靠性或完整性提供任何明示或暗示的保证。本文不作为投资理财建议,请读者仅作参考,并请自行承担全部责任。文中部分文字/图片/视频/音频等来源于网络,如侵犯到著作权人的权利,请与我们联系(微信/QQ:1074760229)。转载请注明出处:清一色财经

(0)
打赏 微信扫码打赏 微信扫码打赏 支付宝扫码打赏 支付宝扫码打赏
清一色的头像清一色管理团队
上一篇 2024年2月22日 17:02
下一篇 2024年2月22日 17:04

相关推荐

发表评论

登录后才能评论

联系我们

在线咨询:1643011589-QQbutton

手机:13798586780

QQ/微信:1074760229

QQ群:551893940

工作时间:工作日9:00-18:00,节假日休息

关注微信