Docs/CloudNative/Kubernetes/Base/k8s抓包.md

30 lines
No EOL
982 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 通过 ephemeral containers 抓包
```shell
# 临时容器
# 1.16引入该功能1.23以上版本默认开启该功能
# 修改POD_NAME及CONTAINER_NAME
kubectl debug -i ${POD_NAME} --image=nicolaka/netshoot --target=${CONTAINER_NAME} -- tcpdump -i eth0 -w - | wireshark -k -i -
```
## ksniff
ksniff 是一个 kubectl 的插件,它利用 tcpdump 和 Wireshark 对 Kubernetes 集群中的任何 Pod 启动远程抓包
#### 安装 Krew
```shell
( set -x; cd "$(mktemp -d)" && \
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/krew.tar.gz" && \
tar zxvf krew.tar.gz && \
KREW=./krew-"$(uname | tr '[:upper:]' '[:lower:]')_$(uname -m | sed -e 's/x86_64/amd64/' -e 's/arm.*$/arm/')" && \
"$KREW" install krew )
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
```
#### 安装 sniff 插件
```shell
kubectl krew install sniff
```
#### 远程抓包
```shell
# 修改POD_NAME及NAMESPACE
kubectl sniff ${POD_NAME} -n ${NAMESPACE} -o tcpdump.pcap
```