如何嗅探从本地主机到我的 k8s 集群(运行 本地)的流量?
How do I sniff traffic from the localhost to my k8s cluster (running locally)?
我在本地有一个 minikube 集群 运行 和一个 pod,集群 ip 172.17.0.8
。
我正在使用 ksniff 嗅探该 pod 上的流量。
在pod的时候我ping www.google.com
。我可以看到,在 wireshark 捕获中,ICMP 请求进入 to/from:
pod (172.17.0.8) <--> google 服务器(一些 IP)
我知道有一个中间步骤。我的 macbook(集群主机)代表 pod 发出请求并接收响应以发送到正确的 pod。
pod (172.17.0.8) <--> 集群主机 (macbook) <--> google 服务器(一些 IP)
如何抓取pod和集群主机(如macbook)之间的流量?
我没有使用过 minikube 也没有 mac一本书所以你的里程可能会有所不同,但让我们试着弄清楚这一点。
据我所知,pods 通常由主机提供默认网关。换句话说,主机充当其托管的 pods 的 路由器 。我们怎么知道的?当 运行 K8s 集群上的 Ubuntu 图像(并安装 iproute2)时,我得到以下路由 table:
root@ubuntu:/# ip route sh
default via 10.244.2.1 dev eth0
10.244.2.0/24 dev eth0 proto kernel scope link src 10.244.2.17
看到 default via 10.244.2.1
行了吗?那就是主机提供的“路由器”。我们甚至可以查找它的 mac 地址:
root@ubuntu:/# cat /proc/net/arp
IP address HW type Flags HW address Mask Device
10.244.2.5 0x1 0x2 fa:26:3d:e1:10:f5 * eth0
10.244.2.1 0x1 0x2 8e:f7:54:7b:15:51 * eth0
所以 pod 和互联网之间的流量可能就像来自 NAT 路由器后面的主机的流量。
Pod 为您处理 arp request/arp response dance, then after learning the "router's" mac address, it'll send IP packets towards the external server with the external server's IP address as the destination and its own IP address as the source. The destination MAC address of these packets would be MAC address of the host's virtual network card. So a capture performed at the pod will actually show you the traffic between the pod and the host as if the host is a router. Once the hosts gets these packets, it probably NATs 它们并通过互联网发送它们。
所以在主机上,如果您在 10.244.2.1
上捕获,您应该看到与 pod 看到的相同的流量。但是,如果您在真实的面向互联网的接口上进行捕获,您可能会看到 pod 的流量通过了 NAT(即以您的真实 IP 地址作为源 IP)。
我在本地有一个 minikube 集群 运行 和一个 pod,集群 ip 172.17.0.8
。
我正在使用 ksniff 嗅探该 pod 上的流量。
在pod的时候我ping www.google.com
。我可以看到,在 wireshark 捕获中,ICMP 请求进入 to/from:
pod (172.17.0.8) <--> google 服务器(一些 IP)
我知道有一个中间步骤。我的 macbook(集群主机)代表 pod 发出请求并接收响应以发送到正确的 pod。
pod (172.17.0.8) <--> 集群主机 (macbook) <--> google 服务器(一些 IP)
如何抓取pod和集群主机(如macbook)之间的流量?
我没有使用过 minikube 也没有 mac一本书所以你的里程可能会有所不同,但让我们试着弄清楚这一点。
据我所知,pods 通常由主机提供默认网关。换句话说,主机充当其托管的 pods 的 路由器 。我们怎么知道的?当 运行 K8s 集群上的 Ubuntu 图像(并安装 iproute2)时,我得到以下路由 table:
root@ubuntu:/# ip route sh
default via 10.244.2.1 dev eth0
10.244.2.0/24 dev eth0 proto kernel scope link src 10.244.2.17
看到 default via 10.244.2.1
行了吗?那就是主机提供的“路由器”。我们甚至可以查找它的 mac 地址:
root@ubuntu:/# cat /proc/net/arp
IP address HW type Flags HW address Mask Device
10.244.2.5 0x1 0x2 fa:26:3d:e1:10:f5 * eth0
10.244.2.1 0x1 0x2 8e:f7:54:7b:15:51 * eth0
所以 pod 和互联网之间的流量可能就像来自 NAT 路由器后面的主机的流量。
Pod 为您处理 arp request/arp response dance, then after learning the "router's" mac address, it'll send IP packets towards the external server with the external server's IP address as the destination and its own IP address as the source. The destination MAC address of these packets would be MAC address of the host's virtual network card. So a capture performed at the pod will actually show you the traffic between the pod and the host as if the host is a router. Once the hosts gets these packets, it probably NATs 它们并通过互联网发送它们。
所以在主机上,如果您在 10.244.2.1
上捕获,您应该看到与 pod 看到的相同的流量。但是,如果您在真实的面向互联网的接口上进行捕获,您可能会看到 pod 的流量通过了 NAT(即以您的真实 IP 地址作为源 IP)。