Docker 具有 MACVLAN 网络的 Swarm 容器获取错误的网关 - 无法访问互联网

Docker Swarm container with MACVLAN network gets wrong gateway - no internet access

我需要 Docker Swarm 堆栈中的一项服务,它有一个基于 macvlan 网络的附加接口。这是因为此服务中的 JBoss 集群需要通过 IP 多播进行通信,目前覆盖网络不支持这种方式。

我创建了这样的 macvlan 网络:

# Worker 1:
docker network create --config-only --subnet 10.140.0.0/16 -o parent=ens224.800 --ip-range 10.140.1.0/24 swarm-multicast-config-only

# Worker 2:
docker network create --config-only --subnet 10.140.0.0/16 -o parent=ens224.800 --ip-range 10.140.2.0/24 swarm-multicast-config-only

# Worker 3:
docker network create --config-only --subnet 10.140.0.0/16 -o parent=ens224.800 --ip-range 10.140.3.0/24 swarm-multicast-config-only

# Master:
docker network create -d macvlan --scope swarm --internal --config-from swarm-multicast-config-only swarm-multicast

组播就这样工作得很好,集群形式。

但是: 一旦我将这个 macvlan 网络分配给我的一个容器,这个容器就不能再访问互联网。 没有 macvlan 网络的所有容器都可以正常工作。

这是我的堆栈文件:

version: '3.3'
services:
  ### Backend ###
  petshop-backend:
    image: com-registry.xxx.local/petshop-backend:100
    extra_hosts:
      - "petshop-db:10.164.210.214"
    networks:
      - backend
      - external_access
    deploy:
      mode: replicated
      replicas: 3

  ### USER INTERFACE ###
  petshop-ui:
    image: com-registry.xxx.local/petshop-ui:107
    networks:
      external_access:
      backend:
      swarm-multicast:
        aliases:
          - ui-multicast
    ports:
      - "1002:8080"
    deploy:
      mode: replicated
      replicas: 3


networks:
  external_access:
    driver: overlay
    internal: false
  backend:
    driver: overlay
    internal: true
  swarm-multicast:
    external: true

如何使 petshop-ui 的容器能够访问互联网? 他们获得默认网关 10.140.1.0,该网关位于 macvlan 网络范围内,但不存在。这是 petshop-ui 容器之一的路由 table:

[root@f477c7cb8048 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use     Iface
0.0.0.0         10.140.1.0      0.0.0.0         UG    0      0        0 eth2
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth4
10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.140.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth2
10.255.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth3

可正常访问互联网的容器,例如petshop-backend172.18.0.1 作为默认网关。下面是这样一个路由 table:

[root@ddb42ef836f3 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use     Iface
0.0.0.0         172.18.0.1      0.0.0.0         UG    0      0        0 eth2
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
172.18.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth2

你需要改变

networks:
  external_access:
    driver: overlay
    internal: false
  backend:
    driver: overlay
    internal: true
  swarm-multicast:
    external: true

networks:
  backend:
    driver: overlay
    internal: true
  swarm-multicast:
    external: true
  external_access:
    driver: overlay
    internal: false

目前似乎最后一个连接的网络接管了网关路由。同样有一个未解决的问题

https://github.com/moby/moby/issues/20179