ksniff
分析k8s pod
的抓包分析ksniff
是一个kubectl
插件,利用tcpdump
和Wireshark
对Kubernetes
集群中的任何pod
进行远程捕获。
可以得到Wireshark
的全部功能,而对你正在运行的pods
影响最小。
介绍
当使用微服务时,很多时候获取微服务和它的依赖之间的网络活动的捕获是非常有用的。
ksniff
使用kubectl
上传静态编译的tcpdump
二进制文件到你的pod
,并将它的输出重定向到你的本地Wireshark
,以获得流畅的网络调试体验。
从github
上下载ksniff
的发布包,执行以下命令安装
unzip ksniff.zip
make install
ksniff
使用kubectl < 1.12:
kubectl plugin sniff <POD_NAME> [-n <NAMESPACE_NAME>] [-c <CONTAINER_NAME>] [-i <INTERFACE_NAME>] [-f <CAPTURE_FILTER>] [-o OUTPUT_FILE] [-l LOCAL_TCPDUMP_FILE] [-r REMOTE_TCPDUMP_FILE]
kubectl >= 1.12:
kubectl sniff <POD_NAME> [-n <NAMESPACE_NAME>] [-c <CONTAINER_NAME>] [-i <INTERFACE_NAME>] [-f <CAPTURE_FILTER>] [-o OUTPUT_FILE] [-l LOCAL_TCPDUMP_FILE] [-r REMOTE_TCPDUMP_FILE]
POD_NAME: Required. the name of the kubernetes pod to start capture it's traffic.
NAMESPACE_NAME: Optional. Namespace name. used to specify the target namespace to operate on.
CONTAINER_NAME: Optional. If omitted, the first container in the pod will be chosen.
INTERFACE_NAME: Optional. Pod Interface to capture from. If omitted, all Pod interfaces will be captured.
CAPTURE_FILTER: Optional. specify a specific tcpdump capture filter. If omitted no filter will be used.
OUTPUT_FILE: Optional. if specified, ksniff will redirect tcpdump output to local file instead of wireshark. Use '-' for stdout.
LOCAL_TCPDUMP_FILE: Optional. if specified, ksniff will use this path as the local path of the static tcpdump binary.
REMOTE_TCPDUMP_FILE: Optional. if specified, ksniff will use the specified path as the remote path to upload static tcpdump to.
ksniff
具有两种方式,第一种static-tcpdump
,直接上传static-tcpdump
到目标pod
的容器中,然后启动本地的Wireshark
对目标容器进行抓包分析。该方法要求目标容器具有sh
命令执行工具。
第二种特权模式privileged
,使用-p
参数指定,该方式会在目标集群中创建dind
的pod
容器,并且该pod
将可以访问节点docker
守护进程。ksniff
将使用该pod
执行一个连接到目标容器网络命名空间的容器,并执行实际的网络捕获。该方法需要maintained/tcpdump:latest
和docker:latest
两个镜像。
注:大前提是运行ksniff
上面安装Wireshark
包,ubuntu
下使用apt-get install Wireshark
进行安装。也可以集成其他工具,例如tshark
,运行命令kubectl sniff pod-name -f "port 80" -o - | tshark -r -
。