无法通过 VPN 在容器中访问互联网

无法通过 VPN 在容器中访问互联网

当我的主机上启动 VPN 时,我无法在容器中执行 ping 操作。

我尝试这样做:

docker run adiazmor/docker-ubuntu-with-ping ping 8.8.8.8

VPN 启动时会失败,但这样可以工作:

docker run --net=host adiazmor/docker-ubuntu-with-ping ping 8.8.8.8

我无法总是有这些--net=host选项,因为我们无法links在 docker-compose 中使用。

我使用 IKE 来启动我的 VPN。以下是 VPN 的配置(没有可见数据):

n:version:4
n:network-ike-port:500
n:network-mtu-size:1380
n:client-addr-auto:1
n:network-natt-port:4500
n:network-natt-rate:10
n:network-frag-size:540
n:network-dpd-enable:1
n:client-banner-enable:1
n:network-notify-enable:1
n:client-dns-used:1
n:client-dns-auto:0
n:client-dns-suffix-auto:1
n:client-splitdns-used:1
n:client-splitdns-auto:1
n:client-wins-used:1
n:client-wins-auto:1
n:phase1-dhgroup:2
n:phase1-keylen:256
n:phase1-life-secs:86400
n:phase1-life-kbytes:0
n:vendor-chkpt-enable:0
n:phase2-keylen:256
n:phase2-life-secs:28800
n:phase2-life-kbytes:0
n:policy-nailed:0
n:policy-list-auto:0
b:auth-mutual-psk:----
n:phase2-pfsgroup:-1
s:network-host:[network-host-ip]
s:client-auto-mode:pull
s:client-iface:virtual
s:network-natt-mode:enable
s:network-frag-mode:enable
s:client-dns-addr:8.8.8.8,8.8.4.4
s:auth-method:mutual-psk-xauth
s:ident-client-type:address
s:ident-server-type:any
s:phase1-exchange:main
s:phase1-cipher:aes
s:phase1-hash:sha1
s:phase2-transform:esp-aes
s:phase2-hmac:sha1
s:ipcomp-transform:disabled
s:policy-level:auto
s:policy-list-include:0.0.0.0 / 0.0.0.0

结果如下route -n

当 VPN 未运行时,在主机上:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.254   0.0.0.0         UG    100    0        0 enp4s0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enp4s0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-c57946727b62
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-e5b5cdaf12ea
172.20.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-0f8aa3757cdc
172.21.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-89f7a8041283
172.22.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-9313de2b3cda
172.23.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-5eb78801c6be
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 enp4s0

当VPN运行时:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.226.1   0.0.0.0         UG    0      0        0 tap0
0.0.0.0         192.168.1.254   0.0.0.0         UG    100    0        0 enp4s0
78.109.86.184   192.168.1.254   255.255.255.255 UGH   0      0        0 enp4s0
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enp4s0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-c57946727b62
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-e5b5cdaf12ea
172.20.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-0f8aa3757cdc
172.21.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-89f7a8041283
172.22.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-9313de2b3cda
172.23.0.0      0.0.0.0         255.255.0.0     U     0      0        0 br-5eb78801c6be
192.168.1.0     0.0.0.0         255.255.255.0   U     100    0        0 enp4s0

在容器上(无论容器是否安装,均不会发生变化):

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.19.0.1      0.0.0.0         UG    0      0        0 eth0
172.19.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0

我检查了其他问题,但发现没有找到适合我的情况的方法。我不认为这与 DNS 有关,因为我尝试访问 IP 地址。

我能做些什么?

答案1

事实:

  • 没有使用 --net=host 模式的容器的出站流量将被转换(NAT/PAT)为 192.168.1.0 网络上的主机 IP 地址(桥接模式)
  • 您的 VPN 在 192.168.226.X 网络中创建一个 tap 接口(很可能是 .1/32)。
  • 您的 VPN 将创建第二条默认路由,其度量低于通常的默认路由,以通过 VPN(点击接口)发送所有流量。

注意事项:

当主机为新连接发送出站流量(即不回复传入流量)时,它必须决定应用哪条路由以及哪个接口适合该流量。

根据上面的路由表,主机上生成的流量将选择 tap 接口,因为它的度量比 en4ps0 接口低。

问题根源:

从容器内部生成的流量将转换为 en4ps0 接口中的主机 IP 地址,因此它将应用适合该接口的路由,从而完全跳过 VPN。

由于不对称流量、您的 VPN 软件将往返于 en4ps0 的流量设为黑洞等原因,ping 可能会失败 - 但这里的重点是,由于 NAT/PAT,您的容器的流量不会通过隧道。

根据您的底层网络和其他要求,macvlan 驱动程序可能比网桥或主机更好。或者,您必须重新考虑您的架构或是否需要在主机上放置 VPN(而不是在单独的主机/设备上放置)

相关内容