ブリッジと 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 を使用しないでください。macvlan を使用してください。

私も最近あなたと同じようにveth + bridgeに苦労していましたが、幸運にもこのリンク今夜、こう言っています。

MACVLAN 以前は、VM または名前空間から物理ネットワークに接続する場合、次に示すように、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

動作しています!

答え2

最後の 2 つのコマンド (ネットワーク名前空間のデフォルト ゲートウェイ + メイン名前空間のマスカレード) まではすべて正常に見えます。

192.168.1.10これら 2 つをスキップすると、物理インターフェイスが 2 つの内部インターフェイス (メイン名前空間のブリッジの内部インターフェイス 1 つと、 の192.168.1.11インターフェイス1 つ) にブリッジされる構成になりますnamespace1

したがって、これは、メイン名前空間から 1 つ、および からの 2 つの物理ネットワーク インターフェイスが同じサブネットに接続されているのと同じように動作しますnamespace。(veth-pair の代わりに を使用しても同じ効果が得られますmacvlan)。

192.168.1.10転送もマスカレードも必要ありません。メイン名前空間からのデフォルト ルートは間違っています。

両方の名前空間のルートが正しい場合 (確認してください)、他のインターフェース、および物理インターフェースに接続されているものに対して ping を実行できるはずです。

テストの場合は、xtermで etc. を開始することをお勧めします。そうすれば、常にnamespace1入力する必要なく、すべてを直接構成できます。ip netns exec namespace1 ip ...

関連情報