【问题标题】:Docker macvlan network, unable to access internetDocker macvlan 网络,无法访问互联网
【发布时间】:2017-06-24 07:48:12
【问题描述】:

我有一个具有多个 IP 地址的专用服务器,一些 IP 具有关联的 MAC 地址,而其他(在子网中)没有 MAC 地址。 我使用以下方法创建了 docker macvlan 网络:

docker network create -d macvlan -o macvlan_mode=bridge --subnet=188.40.76.0/26 --gateway=188.40.76.1 -o parent=eth0 macvlan_bridge

我有 ip:88.99.102.115 和 mac:00:50:56:00:60:42。使用以下方法创建了一个容器:

docker run --name cont1 --net=macvlan_bridge --ip=88.99.102.115 --mac-address 00:50:56:00:60:42 -itd nginx

这可行,我可以从外部访问托管在该 IP 地址的 nginx。

IP无mac地址且网关不在子网内的情况。

子网:88.99.114.16/28,网关:88.99.102.103

无法使用以下方式创建网络:

docker network create -d macvlan -o macvlan_mode=bridge --subnet=88.99.114.16/28 --gateway=88.99.102.103 -o parent=eth0 mynetwork

抛出错误:

no matching subnet for gateway 88.99.102.103

尝试增加子网范围以包括网关:

docker network create -d macvlan -o macvlan_mode=bridge --subnet=88.99.0.0/16 --gateway=88.99.102.103 -o parent=eth0 mynetwork

创建了网络,然后使用“mynetwork”启动了 nginx 容器,而且我没有 88.99.114.18 的 mac 地址,所以使用了一些随机的 mac 地址 40:1c:0f:bd:a1:d2。

docker run --name cont1 --net=mynetwork --ip=88.99.114.18 --mac-address 40:1c:0f:bd:a1:d2 -itd nginx

无法访问 nginx(88.99.102.115)。

  1. 如果我的网关不在我的子网中,如何创建 macvlan docker 网络?
  2. 当我只有 IP 地址而没有 mac 地址时,如何使用 macvlan 网络运行容器?

我对网络的了解不多,如果你详细解释一下会很有帮助的。

我的 /etc/network/interfaces 文件:

### Hetzner Online GmbH - installimage
# Loopback device:
auto lo
iface lo inet loopback
iface lo inet6 loopback
# device: eth0
auto  eth0
iface eth0 inet static
  address   88.99.102.103
  netmask   255.255.255.192
  gateway   88.99.102.65
  # default route to access subnet
  up route add -net 88.99.102.64 netmask 255.255.255.192 gw 88.99.102.65 eth0

iface eth0 inet6 static
  address 2a01:4f8:221:1266::2
  netmask 64
  gateway fe80::1

【问题讨论】:

  • IP 和子网是如何分配给您的?您需要该子网中的网关为数据包提供路由,同样重要的是,为其他网络提供返回子网的路由。

标签: linux networking docker containers


【解决方案1】:

您可能想先阅读simple routing conceptssubnets and routing

如果我的网关不在我的子网中,如何创建 macvlan docker 网络?

网关地址必须与接口位于同一子网上。要使用这个新子网,您需要用完其中一个 IP 地址并将其分配到主机上的某个位置作为网关。

子网路由到桥接网络。

从主机屏幕截图中,88.99.114.16/28 子网已设置为通过您的主机 88.99.102.103 进行路由。如果您希望 Docker 使用子网中的其余 IP 地址,则需要在主机的某处创建一个接口用作网关。

创建一个桥接网络供Docker使用,桥接将分配网关地址88.99.114.17

docker network create \
  --driver=bridge \
  --subnet 88.99.114.16/28 \
  --gateway=88.99.114.17 \
  name0

您可能还需要启用 IP 转发才能使路由正常工作。在/etc/sysctl.conf配置ip转发:

net.ipv4.ip_forward = 1

并应用新设置

sysctl -p /etc/sysctl.conf

然后在新网桥上运行一个容器,你的路由网络应该能够访问网关和互联网

docker run --net=name0 --rm busybox \
  sh -c "ip ad sh && ping -c 4 88.99.114.17 && wget api.ipify.org"

您可能需要允许访问 iptables 中的子网,具体取决于您的默认 FORWARD 策略

iptables -I DOCKER -d 88.99.114.16/28 -j ACCEPT

子网上的服务可以从外部访问

docker run --net=name0 busybox \
  nc -lp 80 -e echo -e "HTTP/1.0 200 OK\nContent-Length: 3\n\nHi\n"

然后在外面

○→ ping -c 2  88.99.114.18
PING 88.99.114.18 (88.99.114.18): 56 data bytes
64 bytes from 88.99.114.18: icmp_seq=0 ttl=63 time=0.527 ms
64 bytes from 88.99.114.18: icmp_seq=1 ttl=63 time=0.417 ms

--- 88.99.114.18 ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.417/0.472/0.527/0.055 ms

○→ curl 88.99.114.18
Hi

不需要 macvlan 接口映射。

当我只有 IP 时如何使用 macvlan 网络运行容器 地址但没有mac地址?

macvlan 用于将物理/主机接口映射到容器中。由于您没有这些地址的物理接口,因此很难将其映射到容器中。

【讨论】:

  • 带有地址 88.99.114.18 的 eth0 的 ip 别名正在工作,该子网 (88.99.114.16/28) 将主机 ip 作为网关 (88.99.102.103)。 AFAIK当我使用macvlan创建容器时,由于随机MAC地址,我无法访问互联网,macvlan应该基于mac地址工作,所以如果我们分配随机mac,它如何联系容器。
  • 再一次,“网关”具有非常具体的含义。 88.99.102.103 不是 88.99.114.16/28 的网关。子网可能通过 88.99.114.16 路由,但“网关”必须是子网内的地址。
  • 我确定网关是 88.99.102.103 并且不在我的子网中(这似乎是可能的),使用 macvlan 类型的网络会创建一个单独的虚拟接口,它可以从端口绑定中解放出所有这些东西。
  • 抱歉,88.99.102.103 不可能是 88.99.114.16/28 的网关地址。
  • 我不得不在 AWS、KVM 和 VirtualBox 上设置 macvlan 接口,原因与我遇到 MAC 过滤问题的原因相同。您似乎很幸运,不需要设置 macvlan 接口来避免映射端口。
猜你喜欢
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 2016-02-20
  • 2020-10-18
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多