【问题标题】:Find out which network interface belongs to docker container找出哪个网络接口属于 docker 容器
【发布时间】:2016-10-18 01:46:21
【问题描述】:

Docker 创建这些虚拟以太网接口veth[UNIQUE ID] 列在ifconfig 中。如何找出哪个接口属于特定的 docker 容器?

我想监听 tcp 流量。

【问题讨论】:

  • 这似乎是一个未解决的问题github.com/docker/docker/issues/14666,并且有一些解决方法。我不擅长网络,也不太了解这些变通方法,所以我决定在使用 tcpdump 时只按主机过滤。为我工作。如果也使用 tcpdump 或者您的工具有类似的过滤器,也许会为您工作。

标签: networking docker ifconfig


【解决方案1】:

定位界面
在我的情况下,从容器中获取价值就像(检查 eth0 到):

$ docker exec -it my-container cat /sys/class/net/eth1/iflink
123

然后:

$ ip ad | grep 123
123: vethd3234u4@if122: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker_gwbridge state UP group default

检查tcpdump -i vethd3234u4


来自http://lxr.free-electrons.com/source/Documentation/ABI/testing/sysfs-class-net的关于神秘iflink的参考:

150 What:           /sys/class/net/<iface>/iflink
151 Date:           April 2005
152 KernelVersion:  2.6.12
153 Contact:        netdev@vger.kernel.org
154 Description:
155                 Indicates the system-wide interface unique index identifier a
156                 the interface is linked to. Format is decimal. This attribute is
157                 used to resolve interfaces chaining, linking and stacking.
158                 Physical interfaces have the same 'ifindex' and 'iflink' values.

【讨论】:

  • 对我不起作用。容器中的 iflink 为 14。主机上的“ip ad”没有接口 14。容器中的“ip ad”有。 Docker 版本 17.03.1-ce。
  • 我注意到容器中的接口 12 恰好是来宾上的接口 13。我的另一个容器中的接口 14 是来宾上的接口 15。我尝试在wireshark中捕获(在主机上运行)并根据流量找出它是哪一个。
  • 我还注意到,如果在容器中我看到,例如 63293 和 ip addr show | grep 63293 没有返回任何内容,那么值得检查 63292 和 63294 的接口。
  • 有没有办法找到非 Linux 特定的容器网络接口?我们 Mac 用户似乎没有 /sys/class/net/eth1/iflink。
【解决方案2】:

根据提供的答案(对我有用),我制作了这个简单的 bash 脚本:

#!/bin/bash

export containers=$(sudo docker ps --format "{{.ID}}|{{.Names}}")
export interfaces=$(sudo ip ad);
for x in $containers
        do
                export name=$(echo "$x" |cut -d '|' -f 2);
                export id=$(echo "$x"|cut -d '|' -f 1)
                export ifaceNum="$(echo $(sudo docker exec -it "$id" cat /sys/class/net/eth0/iflink) | sed s/[^0-9]*//g):"
                export ifaceStr=$( echo "$interfaces" | grep $ifaceNum | cut -d ':' -f 2 | cut -d '@' -f 1);
                echo -e "$name: $ifaceStr";
done

【讨论】:

  • 注意:应该适用于大多数 linux 发行版,不适用于 OSX
【解决方案3】:

我的回答更像是对这个重要主题的改进,因为它没有帮助“找出哪个网络接口属于 docker 容器”,但是,正如作者注意到的那样,他“想要收听 docker 容器内的 tcp 流量” - 在您对网络进行故障排除期间,我会尽力帮助解决这个问题。

考虑到veth 网络设备是关于网络命名空间的,知道我们可以通过nsenter 工具在另一个命名空间中执行程序是很有用的(记住 - 你需要一个特权权限(sudo/root)来做那个):

获取您有兴趣捕获流量的任何容器的 ID,例如它将是 78334270b8f8

然后我们需要获取该容器化应用程序的PID(我假设您在容器内仅运行 1 个与网络相关的进程并希望捕获其流量。否则,该方法很难适用):

sudo docker inspect 78334270b8f8 | grep -i pid

例如,pid 的输出将是 111380 - 这是您的容器化应用程序的 ID,您也可以通过 ps 命令检查它:ps aux | grep 111380 只是出于好奇。

下一步是检查容器内有哪些网络接口:

sudo nsenter -t 111380 -n ifconfig

此命令将返回容器化应用的网络命名空间中的网络设备列表(您的容器上不应有 ifconfig 工具,只能在您的节点/机器上)

例如,您需要在接口eth2 上捕获流量并使用以下命令将其过滤到 tcp 目标端口 80(当然可能会有所不同):

sudo nsenter -t 111380 -n tcpdump -nni eth2 -w nginx_tcpdump_test.pcap 'tcp dst port 80'

请记住,在这种情况下,您不需要在容器中安装 tcpdump 工具。

然后,在捕获数据包后,.pcap 文件将在您的机器/节点上可用,并使用您喜欢的任何工具读取它tcpdump -r nginx_tcpdump_test.pcap

方法的优点:

  • 容器内无需网络工具,仅在 docker 节点上
  • 容器内网络设备与节点之间无需搜索映射

缺点:

  • 您需要在节点/机器上拥有特权用户才能运行nsenter 工具

【讨论】:

    【解决方案4】:

    来自@pbaranski 的单线解决方案

    num=$(docker exec -i my-container cat /sys/class/net/eth0/iflink | tr -d '\r'); ip ad | grep -oE "^${num}: veth[^@]+" | awk '{print $2}'
    

    如果您需要查找不包含 cat 的容器,请尝试使用此工具:https://github.com/micahculpepper/dockerveth

    【讨论】:

      【解决方案5】:

      您还可以通过 /proc/PID/net/igmp 读取接口名称,例如(容器名称作为参数 1):

      #!/bin/bash
      
      NAME=$1
      PID=$(docker inspect $NAME --format "{{.State.Pid}}")
      while read iface id; do
          [[ "$iface" == lo ]] && continue
          veth=$(ip -br addr | sed -nre "s/(veth.*)@if$id.*/\1/p")
          echo -e "$NAME\t$iface\t$veth"
      done < <(</proc/$PID/net/igmp awk '/^[0-9]+/{print $2 " " $1;}')
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-30
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多