【问题标题】:Docker: Route Traffic from One Container to Another Container Through a VPN ContainerDocker:通过 VPN 容器将流量从一个容器路由到另一个容器
【发布时间】:2021-08-19 13:10:59
【问题描述】:

我设置了一个 SSTP 客户端容器 (172.17.0.3),它通过ppp0 接口与一个 SSTP 服务器容器 (172.17.0.2) 通信。来自 SSTP 客户端容器的所有流量都通过其ppp0 接口路由,如在 SSTP 客户端容器上使用 netstat 所见(192.168.20.1 是 SSTP 服务器容器的ppp0 IP 地址):

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.20.1    0.0.0.0         UG        0 0          0 ppp0
0.0.0.0         172.17.0.1      0.0.0.0         UG        0 0          0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 eth0
192.168.20.1    0.0.0.0         255.255.255.255 UH        0 0          0 ppp0

现在,我有一个 HTTP 服务器容器 (172.17.0.4) 正在运行,我想使用另一个客户端容器(例如,一个运行 Apache Benchmark ab 的容器)通过SSTP 服务器容器。为此,我在ab 客户端容器上使用--net=container:sstp-client,因此它使用SSTP 客户端容器的网络。然而,ab 客户端容器似乎无法访问 HTTP 服务器容器,尽管 它能够对 Internet 上的服务器进行基准测试(例如,8.8.8.8)。再举个例子,如果我从一个容器通过 SSTP 客户端容器执行traceroute8.8.8.8

 docker run -it --name alpine --net=container:sstp-client alpine ash
/ # traceroute -s 192.168.20.2 google.com
traceroute to google.com (142.250.65.206) from 192.168.20.2, 30 hops max, 46 byte packets
 1  192.168.20.1 (192.168.20.1)  1.088 ms  1.006 ms  1.077 ms
 2  *  *  *
 3  10.0.2.2 (10.0.2.2)  1.710 ms  1.695 ms  0.977 ms
 4  *  *  *
...

我终于可以联系到 Google。 但如果我 traceroute 到我的 HTTP 服务器容器:

/ # traceroute -s 192.168.20.2 172.17.0.4
traceroute to 172.17.0.4 (172.17.0.4) from 192.168.20.2, 30 hops max, 46 byte packets
 1  *  *  *
 2  *  *  *
 3  *  *  *
 4  *  *  *
 5  *  *  *
...

失败了。

我怀疑 SSTP 服务器容器上的路由配置不正确,但我不确定如何修复它以使其正常工作。我的目标是能够到达外部世界和内部容器。我已经搞砸了iptablesroute 很多,但仍然无法使其工作。这是我当前对 SSTP 服务器容器的配置:

/ # netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.17.0.1      0.0.0.0         UG        0 0          0 eth0
172.17.0.0      0.0.0.0         255.255.0.0     U         0 0          0 eth0
192.168.20.0    172.17.0.2      255.255.255.255 UGH       0 0          0 eth0
192.168.20.0    172.17.0.2      255.255.255.0   UG        0 0          0 eth0
192.168.20.2    0.0.0.0         255.255.255.255 UH        0 0          0 ppp0
/ # iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -i ppp+ -j ACCEPT
-A FORWARD -j ACCEPT
-A OUTPUT -o ppp+ -j ACCEPT

我已经看到许多关于如何通过 VPN 容器路由到 Internet 的在线解决方案,但对其他容器却一无所知。在这个领域非常新手。欢迎任何建议!谢谢。

【问题讨论】:

    标签: docker networking containers


    【解决方案1】:

    我不得不选择类似的方法来解决这个问题。不过我在 gitlab-ci 里面有它...

    启动 VPN 容器并找到它的 IP

    docker run -it -d --rm \
      --name vpn-client-${SOME_VARIABLE} \
      --privileged \
      --net gitlab-runners \
      -e VPNADDR=... \
      -e VPNUSER=... \
      -e VPNPASS=... \
      auchandirect/forticlient || true && sleep 2
    
    export VPN_CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' vpn-client-${SOME_VARIABLE})
    

    接下来您必须将新变量添加到您的应用程序容器中并添加新 ROUTE,例如 docker-compose:

    version: "3"
    services:
      test-server:
        image: alpine
        variables:
          ROUTE_IP: "${VPN_CONTAINER_IP}"
        command: "ip route add <CIDR_TARGET_SUBNET> via ${ROUTE_IP}"
    networks:
      default:
        name: gitlab-runners
        external: true
    

    但请注意,上面提到的 IP 必须是 CIDR 格式(例如 192.168.1.0/24)并且也必须在相同的 docker 网络中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      相关资源
      最近更新 更多