# 添加网络命名空间 ip netns add ns1 # 查看网络命名空间 ip netns list # 删除网络命名空间 ip netns delete ns1
# 在 ns1 里执行 ip link 命令 ip netns exec ns1 ip link # 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1 # link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# 在 ns1 里执行 ip link set dev lo up 命令, 将 lo 网卡 up 起来 ip netns exec ns1 ip linkset dev lo up ip netns exec ns1 ip link # 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1 # link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
# 1. 添加2个network namespace ip netns add ns1 ip netns add ns2
# 2. 添加一对 veth pair # ip link add <p1-name> type veth peer name <p2-name> ip link add veth-test1 type veth peer name veth-test2
# 3. 绑定到对应的网络命名空间上 # ip link set <Veth-pair> netns <network-namespace> ip linkset veth-test1 netns ns1 ip linkset veth-test2 netns ns2
# 4. 为不同网络命名空间下的 veth pair 添加 ip 地址 ip netns exec ns1 ip addr add 192.168.1.1/24 dev veth-test1 ip netns exec ns2 ip addr add 192.168.1.2/24 dev veth-test2
# 5. 启用 veth pair ip netns exec ns1 ip linkset dev veth-test1 up ip netns exec ns2 ip linkset dev veth-test2 up
# 6. ping ip netns exec ns1 ping 192.168.1.2 ip netns exec ns2 ping 192.168.1.1 ip netns exec ns1 ip link ip netns exec ns2 ip link
# 1. 创建 Bridge 并启动, 创建3个 network namespace ip link add ahao-bridge type bridge ip linkset dev ahao-bridge up ip netns add ns1 ip netns add ns2 ip netns add ns3
# 2. 创建 3对 Veth pair ip link add veth-bridge1 type veth peer name veth-ns1 ip link add veth-bridge2 type veth peer name veth-ns2 ip link add veth-bridge3 type veth peer name veth-ns3
# 3. 绑定到各自的 network namespace ip linkset veth-ns1 netns ns1 ip linkset veth-ns2 netns ns2 ip linkset veth-ns3 netns ns3
# 4. 添加ip地址 ip netns exec ns1 ip addr add 192.168.1.1/24 dev veth-ns1 ip netns exec ns2 ip addr add 192.168.1.2/24 dev veth-ns2 ip netns exec ns3 ip addr add 192.168.1.3/24 dev veth-ns3
# 5. 连接到bridge ip linkset dev veth-bridge1 master ahao-bridge ip linkset dev veth-bridge2 master ahao-bridge ip linkset dev veth-bridge3 master ahao-bridge
# 5. 启用 ip netns exec ns1 ip linkset dev veth-ns1 up ip netns exec ns2 ip linkset dev veth-ns2 up ip netns exec ns3 ip linkset dev veth-ns3 up ip linkset dev veth-bridge1 up ip linkset dev veth-bridge2 up ip linkset dev veth-bridge3 up
# 6. ping测试 ip netns exec ns1 ping 192.168.154.2 ip netns exec ns1 ping 192.168.154.3
# TODO 跑不起来 ip netns delete ns1 ip netns delete ns2 ip netns delete ns3 ip link delete ahao-bridge ip netns list ip link
结果这3台机器都可以两两相互通信. 使用bridge link命令可以查看连接信息.
1 2 3 4 5 6
# brctl 命令需要安装依赖 yum install -y bridge-utils bridge link # 50: veth-bridge1 state UP @(null): <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master bridge-ns state forwarding priority 32 cost 2 # 52: veth-bridge2 state UP @(null): <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master bridge-ns state forwarding priority 32 cost 2 # 56: veth-bridge3 state UP @(null): <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master bridge-ns state forwarding priority 32 cost 2