我有一个具有多个 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)。
- 如果我的网关不在我的子网中,如何创建 macvlan docker 网络?
- 只有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
最佳答案
您可能想从阅读 simple routing concepts 开始。或 subnets and routing
How do I create a macvlan docker network if my gateway is out of my subnet?
网关地址必须与接口(interface)位于同一子网上。要使用这个新子网,您需要用完其中一个 IP 地址并将其分配到主机上的某个位置作为网关。
子网路由到桥接网络。
从主机屏幕截图中,88.99.114.16/28 子网已设置为通过您的主机 88.99.102.103 进行路由。如果您希望 Docker 使用子网中的其余 IP 地址,则需要在主机的某处创建一个接口(interface)以用作网关。
创建一个桥接网络供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 接口(interface)映射。
How do I run a container using macvlan network when I have only IP address but no mac address?
macvlan 用于将物理/主机接口(interface)映射到容器中。由于您没有这些地址的物理接口(interface),因此很难将其映射到容器中。
关于linux - Docker macvlan 网络,无法访问互联网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42083546/