如何将自定义网络命名空间连接到根网络命名空间?

如何将自定义网络命名空间连接到根网络命名空间?

我想将根网络命名空间连接到我的 Ubuntu 机器中的自定义网络命名空间。这是我的网络图:

在此处输入图片描述

基本上,我的自定义网络命名空间是控制虚拟机网络的命名空间(例如分配 IP 等)。

我想看看是否可以设置一个 veth 对,以便与 eth1(10.0.1.0/24)位于同一子网的其他主机可以访问它。

我尝试执行 ip link set 命令,但没有成功,因为我想要连接的接口 (qg-1ee92648-d5) 位于自定义网络命名空间中

这是我的主机网络配置:

vagrant@ubuntu:~$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode   DEFAULT group default 
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:cd:e0:99 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:cd:e0:a3 brd ff:ff:ff:ff:ff:ff

vagrant@ubuntu:~$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:cd:e0:99 brd ff:ff:ff:ff:ff:ff
    inet 192.168.106.129/24 brd 192.168.106.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fecd:e099/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:cd:e0:a3 brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.10/24 brd 10.0.1.255 scope global eth1
       valid_lft forever preferred_lft forever

我的自定义命名空间配置:

vagrant@ubuntu:~$ sudo ip netns exec  qrouter-7646dc10-7727-41b4-addd-8d08888cd764 ip
 link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: qr-d174c5e5-c7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether fa:16:3e:b3:cc:c2 brd ff:ff:ff:ff:ff:ff
3: qg-1ee92648-d5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether fa:16:3e:f2:1d:2f brd ff:ff:ff:ff:ff:ff


vagrant@ubuntu:~$ sudo ip netns exec  qrouter-7646dc10-7727-41b4-addd-8d08888cd764 ip
 addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: qr-d174c5e5-c7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:b3:cc:c2 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.1/24 brd 10.0.0.255 scope global qr-d174c5e5-c7
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:feb3:ccc2/64 scope link 
       valid_lft forever preferred_lft forever
3: qg-1ee92648-d5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:f2:1d:2f brd ff:ff:ff:ff:ff:ff
    inet 10.0.1.102/24 brd 10.0.1.255 scope global qg-1ee92648-d5
       valid_lft forever preferred_lft forever
    inet 10.0.1.101/32 brd 10.0.1.101 scope global qg-1ee92648-d5
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:fef2:1d2f/64 scope link 
       valid_lft forever preferred_lft forever

答案1

  1. 使用 Linux Bridge 并通过 veth 对连接命名空间。
  2. 使用 OpenVSwitch 及其内部端口

请参阅此分步指南: http://www.opencloudblog.com/?p=66

例如(引用上面的帖子)如果您想用一个 Linux 桥和两个 v​​eth 对连接两个命名空间,为了实现此图中的拓扑: 在此处输入图片描述

# add the namespaces
ip netns add ns1
ip netns add ns2
# create the switch
BRIDGE=br-test
brctl addbr $BRIDGE
brctl stp   $BRIDGE off
ip link set dev $BRIDGE up
#
#### PORT 1
# create a port pair
ip link add tap1 type veth peer name br-tap1
# attach one side to linuxbridge
brctl addif br-test br-tap1 
# attach the other side to namespace
ip link set tap1 netns ns1
# set the ports to up
ip netns exec ns1 ip link set dev tap1 up
ip link set dev br-tap1 up
#
#### PORT 2
# create a port pair
ip link add tap2 type veth peer name br-tap2
# attach one side to linuxbridge
brctl addif br-test br-tap2
# attach the other side to namespace
ip link set tap2 netns ns2
# set the ports to up
ip netns exec ns2 ip link set dev tap2 up
ip link set dev br-tap2 up
#

有关 OVS 的更多详细信息,请参阅原始帖子。谢谢

答案2

和您一样,我也对 veth 感到很困惑,但没能让它发挥作用。

然后我发现,其中写道:

Before MACVLAN, if you wanted to connect to physical network from a VM or namespace, you would have needed to create TAP/VETH devices and attach one side to a bridge and attach a physical interface to the bridge on the host at the same time, as shown below.

Now, with MACVLAN, you can bind a physical interface that is associated with a MACVLAN directly to namespaces, without the need for a bridge.

所以我尝试了这个:

$ sudo ip netns add ns0
$ sudo ip netns exec ns0 ip link set lo up
$ sudo ip link add macvlan0 link eth0 type macvlan mode bridge
$ sudo ip link set macvlan0 netns ns0
$ sudo ip netns exec ns0 ip link set macvlan0 up
$ sudo ip netns exec ns0 ip addr add 172.29.6.123/21 dev macvlan0
$ sudo ip netns exec ns0 ping 172.29.0.1
PING 172.29.0.1 (172.29.0.1) 56(84) bytes of data.
64 bytes from 172.29.0.1: icmp_seq=1 ttl=64 time=0.360 ms
64 bytes from 172.29.0.1: icmp_seq=2 ttl=64 time=0.412 ms

这是工作!

相关内容