misterli's Blog.

Kubernetes 的 API 流量查看器---mizu

字数统计: 1.1k阅读时长: 5 min
2021/09/28

介绍

一个简单但功能强大的 Kubernetes中的 API 流量查看器,可以用来查看pod之间的所有 API 通信,以帮助调试和排除故障。可以理解为TCPDump 和 Chrome Dev Tools 的结合

image-20210928131450804

安装

mac

1
2
3
curl -Lo mizu \
https://github.com/up9inc/mizu/releases/latest/download/mizu_darwin_amd64 \
&& chmod 755 mizu

linux

1
2
3
curl -Lo mizu \
https://github.com/up9inc/mizu/releases/latest/download/mizu_linux_amd64 \
&& chmod 755 mizu

使用

前提

mizu使用KUBECONFIG环境变量来寻找使用的kubeconfig文件位置,如果未设置,则默认使用${HOME}/.kube/config

mizu运行时候会在kubernetes集群创建资源(pod,service,namespace), 在命令终止时会自行清理资源。

配置

在运行前我们先创建使用mizu config -r创建一下配置文件

1
2
[root@master-01 local]# ./mizu config -r 
Template File written to /root/.mizu/config.yaml

将配置文件中的image-pull-policy: Always改为image-pull-policy: IfNotPresent 这是因为mizu运行时创建的pod使用的镜像是gar.io上的,无法正常拉取。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
root@i-tsfhx8p6:~# cat .mizu/config.yaml
tap:
dest: up9.app
upload-interval: 10
regex: .*
gui-port: 8899
namespaces: []
analysis: false
all-namespaces: false
regex-masking: []
ignored-user-agents: []
no-redact: false
max-entries-db-size: 200MB
dry-run: false
traffic-validation-file: ""
test-rules: ""
api-server-resources:
cpu-limit: 750m
memory-limit: 1Gi
cpu-requests: 50m
memory-requests: 50Mi
tapper-resources:
cpu-limit: 750m
memory-limit: 1Gi
cpu-requests: 50m
memory-requests: 50Mi
version:
debug: false
view:
gui-port: 8899
logs:
file: ""
image-pull-policy: IfNotPresent
mizu-resources-namespace: mizu
telemetry: true
dump-logs: false
kube-config-path: ""

运行

运行mizu 有下面几步

  1. 在 Kubernetes 集群中查找您想要点击的 Pod
  2. 运行mizu tapmizu tap PODNAME
  3. 在 CLI上http://localhost:8899/mizu 按照 CLI 中的说明打开浏览器
  4. 观察 API 流量
  5. 键入 ^C 停止

这里我把要使用的镜像上传到了dockerhub,运行之前我们需要在每个节点先下载镜像,然后修改镜像名称

1
2
docker pull misterli/mizu-main:0.14.0 
docker tag misterli/mizu-main:0.14.0 gcr.io/up9-docker-hub/mizu/main:0.14.0

我们运行mizu tap命令后会在mizu空间临时创建pod

1
2
3
4
➜  ~ kubectl get pod -n mizu
NAME READY STATUS RESTARTS AGE
mizu-api-server 1/1 Running 0 5m39s
mizu-tapper-daemon-set-4w5hc 1/1 Running 0 5m39s

查看所有pod

我们查看一下monitoring空间的pod的流量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
➜  ~ ./mizu tap  -n monitoring
Mizu will store up to 200MB of traffic, old traffic will be cleared once the limit is reached.
Tapping pods in namespaces "monitoring"
+alertmanager-main-0
+blackbox-exporter-79488fcf46-lmzvg
+event-exporter-669dd577c5-pjnzg
+grafana-66777cc868-2q2jw
+kube-state-metrics-78c5867fb-r2b2h
+node-exporter-9hvtd
+node-exporter-hwf24
+node-exporter-nhrnk
+prometheus-adapter-59df95d9f5-2lwjc
+prometheus-adapter-59df95d9f5-kvqx6
+prometheus-k8s-0
+prometheus-operator-84dc795dc8-bvqtk
+webhook-dingtalk-686d45f95d-p85t6
Mizu is available at http://localhost:8899/mizu

这里我们可以打开http://localhost:8899/mizu 进行访问

image-20210928133319718

查看特定pod

我们查看一下monitoring空间prometheus-k8s-0 这个pod的流量

1
2
3
4
5
➜  ~ ./mizu tap  -n monitoring  prometheus-k8s-0
Mizu will store up to 200MB of traffic, old traffic will be cleared once the limit is reached.
Tapping pods in namespaces "monitoring"
+prometheus-k8s-0
Mizu is available at http://localhost:8899/mizu

image-20210928133526944

使用正则过滤pod

我们查看一下monitoring空间名称中包含node 的pod的流量

1
➜  ~ ./mizu tap  -n monitoring "^node.*"Mizu will store up to 200MB of traffic, old traffic will be cleared once the limit is reached.Tapping pods in namespaces "monitoring"+node-exporter-9hvtd+node-exporter-hwf24+node-exporter-nhrnkMizu is available at http://localhost:8899/mizu

image-20210928133732645

过滤user-agent

我们可以过滤某些user-agent的请求,例如

1
mizu tap " ^ca.* " --set tap.ignored-user-agents=kube-probe --set tap.ignored-user-agents=prometheus

任何包含User-Agent具有指定值 (kube-probeprometheus)之一的标头的请求都不会被捕获

流量验证规则

此功能允许您定义一组简单的规则,并针对这些规则测试流量。此类验证可能会测试特定 JSON 字段、标头等的响应。

要使用此功能 - 创建简单的规则文件,并将此文件作为参数传递给mizu tap命令

流量验证文件的结构是:

  • name: 字符串,规则名称
  • type: 字符串,规则类型,必须是jsonorheaderlatency
  • key: string, jsonpath只能在jsonorheader类型中使用
  • value: 字符串,正则表达式仅用于jsonheader类型
  • service: 字符串,要过滤的正则表达式服务名称
  • path: 字符串,要过滤的正则表达式URL 路径
  • latency:整数,以毫秒为单位的预期延迟时

我们这里创建一个规则文件如下

1
rules:- name: test  type: json  key: "$.database"  value: "down"  path: "/api/health"

这里定义了我们对路径/api/health的response中的json中的字段database做校验,如果值为down才成功。

image-20210928141422514

image-20210928141527751

  • 未通过验证的 HTTP 请求(REST API 调用)以红色突出显示
  • 详细信息窗格显示验证规则详细信息以及它是通过还是失败
CATALOG
  1. 1. 介绍
  2. 2. 安装
  3. 3. 使用
    1. 3.1. 前提
    2. 3.2. 配置
    3. 3.3. 运行
      1. 3.3.1. 查看所有pod
      2. 3.3.2. 查看特定pod
      3. 3.3.3. 使用正则过滤pod
      4. 3.3.4. 过滤user-agent
      5. 3.3.5. 流量验证规则