背景资料
我有一台带有两个运行Docker的网络接口的服务器。Docker和某些虚拟化工具一样,创建了一个名为的Linux桥接接口docker0
。默认情况下,此接口的IP地址为172.17.42.1
,所有Docker容器均以此接口作为网关与该接口通信,并在同一/16
范围内分配IP地址。据我所知,所有的网络流量/从容器中经过一个NAT,因此出站似乎来自172.17.42.1
,和入它被送到172.17.42.1
。
我的安装程序如下所示:
+------------+ /
| | |
+-------------+ Gateway 1 +-------
| | 10.1.1.1 | /
+------+-------+ +------------+ |
| eth0 | /
| 10.1.1.2 | |
| | |
| DOCKER HOST | |
| | | Internet
| docker0 | |
| (bridge) | |
| 172.17.42.1 | |
| | |
| eth1 | |
| 192.168.1.2 | \
+------+-------+ +------------+ |
| | | \
+-------------+ Gateway 2 +-------
| 192.168.1.1| |
+------------+
问题
我想从路由/任何Docker容器列全国第二的所有通信eth1
192.168.1.2
接口的默认网关192.168.1.1
,同时具有自/至主机的所有流量走出eth0
10.1.1.2
接口的默认网关10.1.1.1
。到目前为止,我已经尝试了各种各样的方法,但是我认为最接近正确的一件事是使用iproute2,如下所示:
# Create a new routing table just for docker
echo "1 docker" >> /etc/iproute2/rt_tables
# Add a rule stating any traffic from the docker0 bridge interface should use
# the newly added docker routing table
ip rule add from 172.17.42.1 table docker
# Add a route to the newly added docker routing table that dictates all traffic
# go out the 192.168.1.2 interface on eth1
ip route add default via 192.168.1.2 dev eth1 table docker
# Flush the route cache
ip route flush cache
# Restart the Docker daemon so it uses the correct network settings
# Note, I do this as I found Docker containers often won't be able
# to connect out if any changes to the network are made while it's
# running
/etc/init.d/docker restart
提起容器后,这样做后我根本无法从中取出容器。我不确定桥接口的处理方式是否与物理接口用于此类路由的处理方式相同,并且只需要进行完整性检查以及有关如何完成此看似简单任务的任何提示。