V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
0o0O0o0O0o
V2EX  ›  Docker

docker bridge network 如何实现类似旁路由的效果?

  •  
  •   0o0O0o0O0o · 2022-04-01 23:09:31 +08:00 · 1197 次点击
    这是一个创建于 749 天前的主题,其中的信息可能已经有所发展或是发生改变。

    思路是创建两个 bridge network ,容器 gateway 相当于具备两张网卡,容器 client 只有一张网卡,并且 client 的网关设定为 gateway 同局域网的 IP ,期望 client 通过 gateway 联网,当 gateway 配置好后,client 也就享受到了透明代理。这个思路和 network_mode: "service:gateway" 有区别,没有使用过旁路由,但看描述,似乎和旁路由很相似。

    运行起来后在 client 里发出请求,在 host 上看 log ,是有 src 为 gateway wan IP 的日志的:

    IN=br-afa9afabafa5 OUT= MAC= SRC=172.30.0.2 DST=114.114.114.114 LEN=79 TOS=0x00 PREC=0x00 TTL=64 ID=43805 PROTO=UDP SPT=36940 DPT=53 LEN=59 MARK=0x1

    但是如果在 gateway 运行 wireguard ,gateway 内可以实现全局代理,而 client 里就没法使用网络了,不清楚 docker 中如何调试 iptables 日志 ,如果希望实现我期望的结果,我应该如何配置呢?希望能描述一下原理,谢谢。

    • docker-compose version 1.25.0

    • Docker version 20.10.12, build e91ed57

    [Interface]
    PrivateKey = <PrivateKey>
    Address = 10.101.0.2/32
    
    [Peer]
    PublicKey = <PublicKey>
    AllowedIPs = 0.0.0.0/0
    Endpoint = <Server>:10000
    
    
    # docker-compose.yml
    
    version: "3"
    
    networks:
      lan:
        driver: bridge
        ipam:
          config:
            - subnet: 172.28.0.0/16
      wan:
        driver: bridge
        ipam:
          config:
            - subnet: 172.30.0.0/16
    
    services:
      gateway:
        container_name: gateway
        hostname: gateway
        build:
          context: .
          dockerfile: Dockerfile
        privileged: true
        sysctls:
          net.ipv4.ip_forward: 1
        cap_add:
          - NET_ADMIN
        networks:
          wan:
            ipv4_address: 172.30.0.2
          lan:
            ipv4_address: 172.28.0.2
        command: >-
          sh -c 'echo &&
          ip route del default &&
          ip route add default via 172.30.0.1 &&
          echo && ip rule show && route -n && echo &&
          iptables -t nat -I POSTROUTING -j MASQUERADE &&
          iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT &&
          iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT &&
          tail -f /dev/null'
    
      client:
        container_name: client
        hostname: client
        build:
          context: .
          dockerfile: Dockerfile
        privileged: true
        cap_add:
          - NET_ADMIN
        networks:
          lan:
            ipv4_address: 172.28.0.3
        command: >-
          sh -c 'echo &&
          ip route del default &&
          ip route add default via 172.28.0.2 &&
          echo "nameserver 114.114.114.114" >/etc/resolv.conf &&
          tail -f /dev/null'
    
    
    # Dockerfile
    FROM ubuntu:focal
    
    RUN apt-get update && apt-get install -y \
      curl ca-certificates \
      iproute2 net-tools iptables \
      dnsutils \
      inetutils-ping curl host mtr-tiny tcpdump \
      rsyslog \
      wireguard-tools openresolv  kmod --no-install-recommends \
      && rm -rf /var/lib/apt/lists/*
    
    anubu
        1
    anubu  
       2022-04-02 08:59:18 +08:00
    似乎使用 macvlan 网络更合适些。
    0o0O0o0O0o
        2
    0o0O0o0O0o  
    OP
       2022-04-02 09:28:27 +08:00 via iPhone
    @anubu 谢谢,由于特殊原因,希望只用 docker bridge network 实现
    0o0O0o0O0o
        3
    0o0O0o0O0o  
    OP
       2022-04-02 09:29:33 +08:00 via iPhone
    昨晚发现把 iptables 命令移到 wg-quick up 后面就可以了…
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1665 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:43 · PVG 00:43 · LAX 09:43 · JFK 12:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.