使用ksniff分析k8s pod的抓包分析

ksniff是一个kubectl插件,利用tcpdumpWiresharkKubernetes集群中的任何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参数指定,该方式会在目标集群中创建dindpod容器,并且该pod将可以访问节点docker守护进程。ksniff将使用该pod执行一个连接到目标容器网络命名空间的容器,并执行实际的网络捕获。该方法需要maintained/tcpdump:latestdocker:latest两个镜像。

注:大前提是运行ksniff上面安装Wireshark包,ubuntu下使用apt-get install Wireshark进行安装。也可以集成其他工具,例如tshark ,运行命令kubectl sniff pod-name -f "port 80" -o - | tshark -r -

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐