将物理接口专门分配给Docker

我想在一个Docker容器中运行一个高性能的网络测试,并且不想要桥接的开销(所以据我所知,pipeworks不适用)。我想要为Docker容器分配一个物理的40GbE网络接口,除了正常的Docker veth设备之外,就像在LXC的“phys”模式下一样。这应该会使物理接口对主机不可见。
3个回答

在我的搜索中,我发现了一些旧的解决方案,涉及将lxc-config参数传递给docker,但是较新版本的docker不再使用lxc工具,所以这种方法行不通。
根据这里的建议:https://groups.google.com/d/msg/docker-user/pL8wlmiuAEU/QfcoFcKI3kgJ 找到了一个解决方案。 我没有像上面提到的那样修改pipework脚本,而是直接使用所需的命令。还请参阅后续的博客文章:http://jason.digitalinertia.net/exposing-docker-containers-with-sr-iov/
以下是可以用于将主机上的接口转移到docker容器的低级(即非docker特定)网络命名空间工具命令:
CONTAINER=slave-play # Name of the docker container
HOST_DEV=ethHOST     # Name of the ethernet device on the host
GUEST_DEV=test10gb   # Target name for the same device in the container
ADDRESS_AND_NET=10.101.0.5/24

# Next three lines hooks up the docker container's network namespace 
# such that the ip netns commands below will work
mkdir -p /var/run/netns
PID=$(docker inspect -f '{{.State.Pid}}' $CONTAINER)
ln -s /proc/$PID/ns/net /var/run/netns/$PID

# Move the ethernet device into the container. Leave out 
# the 'name $GUEST_DEV' bit to use an automatically assigned name in 
# the container
ip link set $HOST_DEV netns $PID name $GUEST_DEV

# Enter the container network namespace ('ip netns exec $PID...') 
# and configure the network device in the container
ip netns exec $PID ip addr add $ADDRESS_AND_NET dev $GUEST_DEV

# and bring it up.
ip netns exec $PID ip link set $GUEST_DEV up

# Delete netns link to prevent stale namespaces when the docker
# container is stopped
rm /var/run/netns/$PID

如果您的主机上有很多ethX设备(我有eth0 -> eth5),那么在接口命名上有一个小问题。例如,假设将eth3移动到容器中,并在容器的命名空间中作为eth1。当停止容器时,内核将尝试将容器的eth1设备移回主机,但注意到已经存在一个eth1设备。然后,它会将接口重命名为任意名称;我花了一些时间才找到它。因此,出于这个原因,我编辑了/etc/udev/rules.d/70-persistent-net.rules文件(我认为这个文件名适用于大多数流行的Linux发行版;我正在使用Debian),为问题接口提供一个独特、明确的名称,并在容器和主机上都使用该名称。

由于我们不使用Docker进行此配置,因此无法使用标准的Docker生命周期工具(例如docker run --restart=on-failure:10 ...)。涉及的主机机器运行的是Debian Wheezy,所以我编写了以下init脚本:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          slave-play
# Required-Start:    $local_fs $network $named $time $syslog $docker
# Required-Stop:     $local_fs $network $named $time $syslog $docker
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       some slavishness
### END INIT INFO

CONTAINER=slave-play
SCRIPT="docker start -i $CONTAINER"
RUNAS=root

LOGFILE=/var/log/$CONTAINER.log
LOGFILE=/var/log/$CONTAINER.log

HOST_DEV=test10gb
GUEST_DEV=test10gb
ADDRESS_AND_NET=10.101.0.5/24


start() {
  if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then
echo 'Service already running' >&2
return 1
  fi
  echo 'Starting service…' >&2
  local CMD="$SCRIPT &> \"$LOGFILE\" &"
  su -c "$CMD" $RUNAS 
  sleep 0.5 # Nasty hack so that docker container is already running before we do the rest
  mkdir -p /var/run/netns
  PID=$(docker inspect -f '{{.State.Pid}}' $CONTAINER)
  ln -s /proc/$PID/ns/net /var/run/netns/$PID
  ip link set $HOST_DEV netns $PID name $GUEST_DEV
  ip netns exec $PID ip addr add $ADDRESS_AND_NET dev $GUEST_DEV
  ip netns exec $PID ip link set $GUEST_DEV up
  rm /var/run/netns/$PID
  echo 'Service started' >&2
}

stop() {
  echo "Stopping docker container $CONTAINER" >&2
  docker stop $CONTAINER
  echo "docker container $CONTAINER stopped" >&2
}


case "$1" in
  start)
start
;;
  stop)
stop
;;
  restart)
stop
start
;;
  *)
echo "Usage: $0 {start|stop|restart}"
esac

有点取巧,但是它能用 :)

为什么你们的网络接口名称以eth开头?Debian不是使用一致的网络设备名称吗? - Michael Hampton
对于其他有疑惑的人来说,为什么需要符号链接/var/run/netns/$PID呢?这是为了让ip netns exec $PID ...命令能够正常工作。 - Donn Lee

pipework可以将物理网络接口从默认的网络命名空间移动到容器的网络命名空间:
    $ sudo pipework --direct-phys eth1 $CONTAINERID 192.168.1.2/24

欲获得更多信息,请点击这里


1我接受了这个答案,因为它看起来很简单,但我自己并没有尝试过(我使用了之前我写的长答案,在 pipeworks 增加这个功能之前)。 - NeilenMarais

我编写了一个Docker网络插件来实现这个功能。

https://github.com/yunify/docker-plugin-hostnic

docker pull qingcloud/docker-plugin-hostnic
docker run -v /run/docker/plugins:/run/docker/plugins -v /etc/docker/hostnic:/etc/docker/hostnic --network host --privileged qingcloud/docker-plugin-hostnic docker-plugin-hostnic
docker network create -d hostnic --subnet=192.168.1.0/24 --gateway 192.168.1.1 hostnic
docker run -it --ip 192.168.1.5 --mac-address 52:54:0e:e5:00:f7 --network hostnic ubuntu:14.04 bash