15 route gateway source bridge docker
背景资料
我有一个带有两个运行 Docker 的网络接口的服务器。Docker 和一些虚拟化工具一样,创建了一个名为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| |
+------------+
Run Code Online (Sandbox Code Playgroud)
问题
我想从路由/任何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
Run Code Online (Sandbox Code Playgroud)
当我调出一个容器时,这样做后我根本无法从中 ping 出来。我不确定桥接接口的处理方式是否与物理接口处理此类路由的方式相同,我只想进行完整性检查以及有关如何完成这项看似简单的任务的任何提示。
我和一个朋友遇到了这个问题,我们想让 docker 支持多个网络接口服务请求。我们专门使用 AWS EC2 服务,我们还附加/配置/启动额外的接口。在这个项目中,有超出您需要的内容,因此我将尝试仅在此包含您需要的内容。
首先,我们所做的是创建一个单独的路由表eth1
:
ip route add default via 192.168.1.2 dev eth1 table 1001
Run Code Online (Sandbox Code Playgroud)
接下来我们配置 mangle 表来设置一些来自以下位置的连接标记eth1
:
iptables -t mangle -A PREROUTING -i eth1 -j MARK --set-xmark 0x1001/0xffffffff
iptables -t mangle -A PREROUTING -i eth1 -j CONNMARK --save-mark --nfmask 0xffffffff --ctmask 0xffffffff
Run Code Online (Sandbox Code Playgroud)
最后我们添加这条规则,让所有fwmark
s都可以使用我们创建的新表。
ip rule add from all fwmark 0x1001 lookup 1001
Run Code Online (Sandbox Code Playgroud)
以下iptables
命令将恢复连接标记,然后允许路由规则使用正确的路由表。
iptables -w -t mangle -A PREROUTING -i docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j CONNMARK --restore-mark --nfmask 0xffffffff --ctmask 0xffffffff
Run Code Online (Sandbox Code Playgroud)
eth1
我相信这就是我们更复杂的示例所需要的全部内容,其中(就像我所说的)我们的项目在启动时附加/配置/调出接口。
现在,此示例不会停止从eth0
服务请求到 的连接docker0
,但我相信您可以添加路由规则来防止这种情况。
伪装不是来自 172.17.42.1,而是来自
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
Run Code Online (Sandbox Code Playgroud)
这意味着这条规则不会正常运作。
ip rule add from 172.17.42.1 table docker
Run Code Online (Sandbox Code Playgroud)
尝试一下
ip rule add from 172.17.0.0/16 table docker
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22109 次 |
最近记录: |