ブリッジとVethのペアを介して名前空間を物理インターフェイスに接続する方法

ブリッジとVethのペアを介して名前空間を物理インターフェイスに接続する方法

私の試みは模倣することでしたこのチュートリアル

物理インターフェイスがブリッジに割り当てられていない場合は、ネームスペースでネットワークをpingできます。

# Create namespace
ip netns add namespace1

# Create veth pair.
ip link add veth1 type veth peer name br-veth1

# Associate the non `br-` side with the namespace.
ip link set veth1 netns namespace1

# Give namespace-side veth ip addresses.
ip netns exec namespace1 ip addr add 192.168.1.11/24 dev veth1

# Create a bridge device naming it `br1` and set it up.
ip link add name br1 type bridge

# Turn up the bridge.
ip link set br1 up

# Set the bridge veth from the default namespace up.
ip link set br-veth1 up

# Set the veth from the namespace up too.
ip netns exec namespace1 ip link set veth1 up

# Add the br-veth1 interface to the bridge by setting the bridge device as their master.
ip link set br-veth1 master br1

# Add the physical interface to the bridge
ip link set enp3s0 master br1

# Set the address of the `br1` interface (bridge device) to 192.168.1.10/24 and also set the broadcast address to 192.168.1.255 (the `+` symbol sets  the host bits to 255).
ip addr add 192.168.1.10/24 brd + dev br1

# add the default gateway in all the network namespace.
ip netns exec namespace1 ip route add default via 192.168.1.10

# Set us up to have responses from the network.
# -t specifies the table to which the commands should be directed to. By default, it's `filter`.
# -A specifies that we're appending a rule to the chain that we tell the name after it.
# -s specifies a source address (with a mask in this case).
# -j specifies the target to jump to (what action to take).
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE
sysctl -w net.ipv4.ip_forward=1

ベストアンサー1

veth+bridgeを使用しないでください!マクブランをお試しください!

私も最近あなたのようにveth + bridgeのために苦労しましたが、幸いに見つけました。このリンク今夜は次のようになります。

MACVLAN以前は、仮想マシンまたは名前空間から物理ネットワークに接続するには、TAP / VETHデバイスを作成し、一方をブリッジに接続し、物理インターフェイスをホストのブリッジに接続する必要がありました。次のようになります。

MACVLANを使用すると、ブリッジングなしでMACVLANに関連付けられている物理インターフェイスを名前空間に直接バインドできます。

これが私がしたことです:

$ 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

これは仕事だ!

おすすめ記事