当 VPN 在我的主机上启动时,我无法在容器中执行 ping 操作。
我尝试这样做:
docker run adiazmor/docker-ubuntu-with-ping ping 8.8.8.8
Run Code Online (Sandbox Code Playgroud)
VPN 启动时失败,但这有效:
docker run --net=host adiazmor/docker-ubuntu-with-ping ping 8.8.8.8
Run Code Online (Sandbox Code Playgroud)
我不能总是有--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
Run Code Online (Sandbox Code Playgroud)
以下是结果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
Run Code Online (Sandbox Code Playgroud)
当 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
Run Code Online (Sandbox Code Playgroud)
在容器上(无论是否安装容器都没有变化):
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
Run Code Online (Sandbox Code Playgroud)
我检查了其他问题,但没有发现对我有用的东西。我认为这与 DNS 无关,因为我尝试访问 IP 地址。
我能做什么?
事实:
注意事项:
当主机发送新连接的出站流量(即不回复传入连接)时,它必须决定应用哪条路由以及哪个接口适合该流量。
根据上面的路由表,主机上生成的流量将选择 tap 接口,因为它的度量值低于 en4ps0 接口。
问题的根本原因:
从容器内部生成的流量将被转换为 en4ps0 接口中主机的 IP 地址,因此它将应用适合该接口的路由,从而完全跳过 VPN。
ping 可能会因不对称流量、您的 VPN 软件黑洞流量到/来自 en4ps0 等而失败 - 但这里的要点是,由于 NAT/PAT,您的容器的流量不会通过隧道。
根据您的底层网络和其他要求,macvlan 驱动程序可能是比网桥或主机更好的选择。或者,您必须重新考虑您的架构或是否需要在主机上放置 VPN(而不是在单独的主机/设备上)