Day 28 - Kubernetes 第三方好用工具介绍

本文将於赛後同步刊登於笔者部落格
有兴趣学习更多 Kubernetes/DevOps/Linux 相关的资源的读者,欢迎前往阅读
更多相关科技的技术分享,欢迎追踪 矽谷牛的耕田笔记

今天则要来介绍一些其他关於 kubernetes 操作的好工具,每个工具都有自己适合的地方与场景,每个人就根据自己的习惯选择

Stern/Kail

第一个要分享的工具是跟观看 log 有关的, Kubernetes 由於提供很多个副本,同时透过 deployment/replicaset 创建出来的 Pod 名称上面都会有一些不好阅读的乱数,举例来说

$ kubectl get pods
NAME                      READY   STATUS    RESTARTS   AGE
ithome-6564f65698-947rv   1/1     Running   0          84s
ithome-6564f65698-fglr9   1/1     Running   0          84s
ithome-6564f65698-k5wtg   1/1     Running   0          84s
ithome-6564f65698-rrvk4   1/1     Running   0          84s
ithome-6564f65698-zhwlj   1/1     Running   0          84s

这种情况下我们如果使用 kubectl 来观察个别 Pod 的 log 就必须要於不同的 pod 之间来回切换,可时候有时候要除错问题时,就希望可以同时观看这些 Pod 的 log。

因此今天要介绍的工具就是再处理这方面的需求,主要是针对多个 Pod 同时存取相关的 log 并且整理後显示出来,这方面的工具满多的,譬如 Stern, Kube-tail, Kail 等都可以。 而今天则是会介绍 Stern 的用法

从其官网上可以看到说明

Stern allows you to tail multiple pods on Kubernetes and multiple containers within the pod. Each result is color coded for quicker debugging.

The query is a regular expression so the pod name can easily be filtered and you don't need to specify the exact id (for instance omitting the deployment id). If a pod is deleted it gets removed from tail and if a new pod is added it automatically gets tailed.

特别的是你可以透过正规表达式的方式来选择你想要符合的 Pod

安装

直接到官方 Github Release Page 抓去每个平台的 binary 版本

使用

举例来说,上述范例会有五个 pod,而且这五个pod的名称都是 ithome开头,因此我可以直接用 stern ithom 的方式来抓取这些 pod 的资讯,结果如下图

$ stern ithome
...
ithome-6564f65698-zhwlj netutils Hello! 369 secs elapsed...
ithome-6564f65698-fglr9 netutils Hello! 369 secs elapsed...
ithome-6564f65698-947rv netutils Hello! 367 secs elapsed...
ithome-6564f65698-k5wtg netutils Hello! 368 secs elapsed...
ithome-6564f65698-rrvk4 netutils Hello! 369 secs elapsed...
ithome-6564f65698-zhwlj netutils Hello! 370 secs elapsed...
ithome-6564f65698-fglr9 netutils Hello! 370 secs elapsed...
ithome-6564f65698-947rv netutils Hello! 368 secs elapsed...
ithome-6564f65698-k5wtg netutils Hello! 370 secs elapsed...
ithome-6564f65698-rrvk4 netutils Hello! 370 secs elapsed...
ithome-6564f65698-zhwlj netutils Hello! 371 secs elapsed...
ithome-6564f65698-fglr9 netutils Hello! 371 secs elapsed...
ithome-6564f65698-947rv netutils Hello! 369 secs elapsed...

ithome-6564f65698-k5wtg netutils Hello! 371 secs elapsed...
ithome-6564f65698-rrvk4 netutils Hello! 371 secs elapsed...
ithome-6564f65698-zhwlj netutils Hello! 372 secs elapsed...
ithome-6564f65698-fglr9 netutils Hello! 372 secs elapsed...
^C

实际上观看的时候,不同 Pod 的名称还会有不同的颜色标注,帮助使用者更快的区别这些文字。

K9S

过往总是透过 kubectl 指令於各个资源,各 namespace 间切来切去,特别是要使用 exec, get, describe, logs, delete 等指令时,常常打的手忙脚乱或是觉得心累,有这种困扰的人可以考虑使用看看 k9s 这个工具

K9s 官网介绍

K9s provides a terminal UI to interact with your Kubernetes clusters. The aim of this project is to make it easier to navigate, observe and manage your applications in the wild. K9s continually watches Kubernetes for changes and offers subsequent commands to interact with your observed resources.

基本上就是基於 Terminal 去提供一个友善的操作画面,让你可以透过键盘来轻松的完成上面提到的事情,不论是切换 namespace, 砍掉资源,执行 Shell, 观看 log 等都可以轻松达成。

使用

上述五个 pod 的范例透过 k9s 执行後可以得到下面的画面,画面中可以清楚地看到

  1. Pod 的名称
  2. 有没有开 Port-Forward
  3. 当前 Continers's READY 状态
  4. 当前 Pod 状态
  5. 当前 IP
  6. 运行节点资讯
  7. 存活时间
    这些指令其实都可以用 kubectl 获得,但是操作起来可能就相对繁琐,需要比较多的指令

此外画面上方还会有一些基本资讯,譬如 Context/Cluster/User 等 Kubeconfig 内的资讯,右边还有可以使用的快捷键,除了上述提到的功能之外,还可以透过 port-forward 来使用,个人觉得相当不错。

一路往下点选後,还可以看到每个 Pod 里面每个 Container 各自的 log, 使用上非常方便,过往有多个 containers 的时候都要於 kubectl logs -f $Pod_name -c $container_name 来读取,特别是没有仔细去看 Pod 的设定都会忘记 Container Name,这时候又要再跑别的指令查询一次。

透过 k9s 这工具可以提供一个满不错的视窗管理工具,让你一目了然 kubernetes 当前的状态,并且提供基本功能让你进行操作

Ksniff

接下来要介绍的是一个抓取网路封包的工具,过往我们分析封包的时候都会使用 tcpdupm 或是 wireshark 这些工具来辅助,而 Ksniff 就是一个将这些工具整合到 Kubernetes 系统内的工具

Ksniff 的介绍如下

A kubectl plugin that utilize tcpdump and Wireshark to start a remote capture on any pod in your Kubernetes cluster.

You get the full power of Wireshark with minimal impact on your running pods.

基本上本身也是一个 kubectl 的 plugin ,所以也是可以透过前述的 krew 来安装管理。这边就不再赘述其安装过程

使用

其使用上的概念是,选择一个想要观察的 Pod,然後 Ksniff 这个工具会尝试帮你将 tcpdump 的执行档案给复制到该 Pod的某个 Container 里面(预设是第一个),接下来根据你的参数帮你运行 tcpdump,最後将结果复制出来到本机上面的 wireshark 来呈现。

但是假如系统中没有 wireshark 可以呈现这些结果,可以改用命令列的工具,譬如 tshark 来取代

$ sudo apt install tshark
$ kubectl sniff ithome-6564f65698-947rv -o - | tshark -r -
$ kubectl sniff ithome-6564f65698-947rv -o - | tshark -r -
INFO[0000] sniffing method: upload static tcpdump
INFO[0000] using tcpdump path at: '/home/ubuntu/.krew/store/sniff/v1.4.2/static-tcpdump'
INFO[0000] no container specified, taking first container we found in pod.
INFO[0000] selected container: 'netutils'
INFO[0000] sniffing on pod: 'ithome-6564f65698-947rv' [namespace: 'default', container: 'netutils', filter: '', interface: 'any']
INFO[0000] uploading static tcpdump binary from: '/home/ubuntu/.krew/store/sniff/v1.4.2/static-tcpdump' to: '/tmp/static-tcpdump'
INFO[0000] uploading file: '/home/ubuntu/.krew/store/sniff/v1.4.2/static-tcpdump' to '/tmp/static-tcpdump' on container: 'netutils'
INFO[0000] executing command: '[/bin/sh -c ls -alt /tmp/static-tcpdump]' on container: 'netutils', pod: 'ithome-6564f65698-947rv', namespace: 'default'
INFO[0000] command: '[/bin/sh -c ls -alt /tmp/static-tcpdump]' executing successfully exitCode: '0', stdErr :''
INFO[0000] file found: '-rwxr-xr-x 1 root root 2696368 Jan  1  1970 /tmp/static-tcpdump
'
INFO[0000] file was already found on remote pod
INFO[0000] tcpdump uploaded successfully
INFO[0000] output file option specified, storing output in: '-'
INFO[0000] start sniffing on remote container
INFO[0000] executing command: '[/tmp/static-tcpdump -i any -U -w - ]' on container: 'netutils', pod: 'ithome-6564f65698-947rv', namespace: 'default'

从上面可以观察到这些资讯就代表系统开始运行了,这时候我们可以开启第二个视窗,进入到该 Container 内透过 ping 8.8.8.8 往外送封包,并且观察上述的输出

$ kubectl exec ithome-6564f65698-947rv -- ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=97 time=9.42 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=97 time=9.44 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=97 time=9.36 ms
...
------
$ kubectl sniff ithome-6564f65698-947rv -o - | tshark -r -
...
    2  38.393757   10.244.1.8 → 8.8.8.8      ICMP 100 Echo (ping) request  id=0x04f5, seq=1/256, ttl=64
    3  38.403163      8.8.8.8 → 10.244.1.8   ICMP 100 Echo (ping) reply    id=0x04f5, seq=1/256, ttl=97 (request in 2)
    4  39.394274   10.244.1.8 → 8.8.8.8      ICMP 100 Echo (ping) request  id=0x04f5, seq=2/512, ttl=64
    5  39.403697      8.8.8.8 → 10.244.1.8   ICMP 100 Echo (ping) reply    id=0x04f5, seq=2/512, ttl=97 (request in 4)
    6  40.395882   10.244.1.8 → 8.8.8.8      ICMP 100 Echo (ping) request  id=0x04f5, seq=3/768, ttl=64
    7  40.405230      8.8.8.8 → 10.244.1.8   ICMP 100 Echo (ping) reply    id=0x04f5, seq=3/768, ttl=97 (request in 6)
    8  41.397387   10.244.1.8 → 8.8.8.8      ICMP 100 Echo (ping) request  id=0x04f5, seq=4/1024, ttl=64
...

可以看到另外一个视窗很及时地将相关的封包内容都给显示出来。

我认为这个工具最方便的地方就是帮你上传 tcpdump 的档案,因为大部分的 Container 内建都没有这个执行档案,甚至也不好安装,所以要录制封包的时候都不太方便,然而透过这个工具可以帮忙解决这个问题

除此之外还有很多有趣好用的工具,就留待大家自己挖掘罗


<<:  Day30 - 述词和完赛结语

>>:  远端连线 GCE 的 MySQL 资料库

Day 6 jinja (1)

前言 今天要来看 jinja 这个模板引擎。简单来说,它的功能就是在 HTML 里面执行一般程序,等...

Consistency and Consensus (2-2) - Implementing Linearizable

续 Day 15 依赖线性一致性的场景 锁和 leader 选举 若系统是 single-lead...

为了转生而点技能-JavaScript,day14(this下篇: Call、apply呼叫及bind

Call呼叫:呼叫函式的方法,并且能指定 this 值。 用法:函式名.call(指定的this变数...

最短路径问题 (8)

10.10 Thorup’s 无向非负整数权重 SSSP 演算法 今天来介绍 Thorup 在 19...

当你真心渴望某件事,整个宇宙都会联合起来帮助你完成。

当你真心渴望某件事,整个宇宙都会联合起来帮助你完成。 —保罗‧科尔贺(Paulo Coelho)巴西...