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

开发 开发工具
只要能想到的,别人肯定也想到了,今天就分享两款超好用的多容器实时日志查看工具 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.

总结

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

责任编辑:武晓燕 来源: 架构成长指南
相关推荐

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数据库工具

2011-06-08 22:43:41

2023-10-10 14:54:31

插件开发

2020-08-12 09:35:23

Docker工具开源

2024-03-01 19:59:17

2010-07-12 15:43:35

UML建模工具

2010-05-26 09:10:44

Windows清理工具

2023-11-06 01:17:25

主机容器选项

2024-02-01 09:48:17

2010-06-28 09:53:43

UML建模工具Rose

2015-11-25 14:43:44

2022-04-22 13:32:01

K8s容器引擎架构

2023-11-06 07:16:22

WasmK8s模块

2016-11-08 09:43:02

Linux桌面录制工具

2020-11-12 10:00:56

Kubernetes工具Linux

2015-07-16 09:46:57

新浪实时日志分析分布式架构
点赞
收藏

51CTO技术栈公众号