首页 > 其他分享 >Kubernetes日志查看工具

Kubernetes日志查看工具

时间:2023-09-15 18:39:58浏览次数:37  
标签:Kubernetes 查看 -- app2 kubetail Running stern 日志

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 中的日志信息聚合到一起进行展示,并支持彩色输出和条件过滤。

Kubernetes日志查看工具_stern

查看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 中的日志信息聚合到一起进行展示,并支持彩色输出和条件过滤。需要说的是,该工具已经很久都没有更新过了,所以不建议使用了。

Kubernetes日志查看工具_k8s_02

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 --completinotallow=bash)

# .zshrc
$ source <(stern --completinotallow=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
  • 常见命令行参数说明

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 实时日志查看工具

标签:Kubernetes,查看,--,app2,kubetail,Running,stern,日志
From: https://blog.51cto.com/jowin/7484324

相关文章

  • 前端生成二维码,qrcode使用说明,canvas查看大图
    生成二维码用于vue项目通过字符串转换生成二维码的三方插件安装插件npminstall--saveqrcode引入使用importQRCodefrom"qrcode"页面<!--放置二维码的容器--><canvas:id="'qrCode_id'+stringxxxxx"class="qrCode_style"></canvas><!--可......
  • Kubernetes初探[1]:部署您的第一个ASP.NET Core应用到k8s集群
    原文:https://www.cnblogs.com/wl-blog/p/16936019.htmlKubernetes简介Kubernetes是Google基于Borg开源的容器编排调度引擎,作为CNCF(CloudNativeComputingFoundation)最重要的组件之一,它的目标不仅仅是一个编排系统,而是提供一个规范,可以让你来描述集群的架构,定义服务的最终状态,K......
  • 如何查看手机CPU类型
    如何查看手机CPU类型是armeabi,armeabi-v7a,还是arm64-v8a-知乎(zhihu.com)安卓软件APK安装包arm64-v8a、armeabi-v7a、x86、x86_64有何区别?如何选择?-『白云居』(baiyunju.cc)armeabi:第5代、第6代的ARM处理器,早期的手机用的比较多。armeabi-v7a:第7代及以上的ARM处理器......
  • 【Kubernetes】Kubernetes日志收集最佳实践及开源工具盘点
    Kubernetes是一种流行的开源容器编排平台,被开发人员和DevOps团队广泛用于部署和管理容器化应用程序。在Kubernetes上运行任何应用程序的一个关键方面是日志收集,它有助于监控应用程序的健康和性能,并快速解决问题。在本文中,我们将讨论Kubernetes日志收集以及Kubernetes环境中的最佳实......
  • Linux如何查看指定进程是否阻塞
    使用ps命令查找进程的进程ID(PID):psaux|grep<进程名或关键字>这将显示与指定进程名或关键字匹配的进程列表。注意,可以根据需要使用其他参数来过滤和查找特定的进程。找到你要检查的进程的PID,并记下它。使用top命令查看进程的状态:top-p<PID>这将显示指定PID的......
  • docker部署 grafana Loki 轻量级日志
    这是一篇关于讲解如何正确使用51CTO博客-Markdown的排版示例,希望通过此,大家都能轻松上手,都能通过Markdown能够让自己的文章有更加出色、更清晰明了的排版。什么是MarkdownMarkdown(MD)是现在普遍使用的一种文档书写语言格式,只需用一些非常简单易记的符号,如(#*/>[]()\),......
  • 工程管理之二:日志初始化工具
    现在公司工作中使用的日志工具是slf4j+logback。初始化工具类实现如下:publicclassLogUtil{ privatestaticfinalorg.slf4j.LoggerLOGGER=LoggerFactory.getLogger(LogUtil.class); publicstaticfinalString__CONF_DIR__="conf"; publicstaticvoidloadLogback......
  • controller方法入参出参加日志打印
    importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.serializer.SerializerFeature;importlombok.extern.slf4j.Slf4j;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect......
  • git 查看分支/切换分支命令
     1、查看所有分支  gitbranch-a 2、查看当前分支gitbranch 3、切换分支gitcheckout分支名 4、查看当前分支gitbranch 5、切换完分支之后拉取当前分支最新代码gitpull 6、如果你在当前分支修改了东西,想要切换分支一定要先将......
  • 1、查看索引命中情况
    --查看索引命中详情:setstatisticstimeonsetstatisticsioonsetstatisticsprofileonselect*from表名使用索引好处:执行原理(https://blog.csdn.net/m0_38128121/article/details/79663261)b+树:非叶子节点不存储真实的数据,只存储指引搜索方向的数据项b+树的查找过程......