创建具有与子网不同的网关的 Docker macvlan 网络?

创建具有与子网不同的网关的 Docker macvlan 网络?

我正在尝试创建一个 Docker macvlan 接口来为容器分配一个公共 IP,并允许它直接访问互联网,使用下面的命令

docker network create -d macvlan --subnet=212.xx.xxx.xxx/32 --gateway=62.210.0.1  -o parent=eno1 my-macvlan-net

但我得到了下面的错误

no matching subnet for gateway 62.210.0.1

问题是,我正在使用来自提供商的服务器,他们为所有服务器提供单一网关“62.210.0.1”,该网关不在服务器的同一子网上,使用 virtualbox 或 vmware 它可以毫无问题地使用下面的接口文件(debian vm):

auto eth0
iface eth0 inet static
        address 212.xx.xxx.xxx
        netmask 255.255.255.255
        pointopoint 62.210.0.1
        gateway 62.210.0.1

我彻底搜索了互联网但找不到解决方案,我不想使用负载均衡器或反向代理......等等,有没有办法强制 Docker 使用此配置创建网络?!!

答案1

目前可能只能通过以下方式手动完成ip

[tom@archlinux ~]$ sudo docker start test1
test1
[tom@archlinux ~]$ pid=$(sudo docker inspect -f '{{.State.Pid}}' test1)
[tom@archlinux ~]$ sudo ip netns attach test1 "$pid"
[tom@archlinux ~]$ sudo ip l add mvl1 link enp3s0 type macvlan
[tom@archlinux ~]$ sudo ip l set mvl1 netns test1
[tom@archlinux ~]$ sudo ip netns exec test1 ip l set mvl1 up
[tom@archlinux ~]$ sudo ip netns exec test1 ip a add 192.168.1.2/32 peer 192.168.1.1 dev mvl1
[tom@archlinux ~]$ sudo ip netns exec test1 ip r add default via 192.168.1.1
[tom@archlinux ~]$ sudo docker attach test1
[root@ec6541f4fe79 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
10: mvl1@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 12:3a:1a:a6:e6:7f brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.1.2 peer 192.168.1.1/32 scope global mvl1
       valid_lft forever preferred_lft forever
[root@ec6541f4fe79 /]# ip r
default via 192.168.1.1 dev mvl1 
192.168.1.1 dev mvl1 proto kernel scope link src 192.168.1.2 
[root@ec6541f4fe79 /]# ping -c 3 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.533 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.282 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.249 ms

--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2024ms
rtt min/avg/max/mdev = 0.249/0.354/0.533/0.126 ms
[root@ec6541f4fe79 /]# ping -c 3 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=55 time=1.12 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=55 time=1.08 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=55 time=1.09 ms

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 1.075/1.096/1.122/0.019 ms
[root@ec6541f4fe79 /]# exit
[tom@archlinux ~]$ sudo ip netns del test1
[tom@archlinux ~]$ 

相关内容