创建两个 network namespace
[root@localhost go-hello]# sudo ip netns list
[root@localhost go-hello]# sudo ip netns add test1
[root@localhost go-hello]# sudo ip netns list
test1
[root@localhost go-hello]# sudo ip netns add test2
[root@localhost go-hello]# sudo ip netns list
test2
test1
[root@localhost go-hello]# sudo ip netns exec test1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
[root@localhost go-hello]# sudo ip netns exec test2 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
添加两个ip link veth-test1 veth-test2
[root@localhost go-hello]# sudo ip link add veth-test1 type veth peer name veth-test2
[root@localhost go-hello]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:d7:99:69 brd ff:ff:ff:ff:ff:ff
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
link/ether 02:42:7f:7d:d6:b2 brd ff:ff:ff:ff:ff:ff
42: veth-test2@veth-test1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 46:91:c3:1c:ea:50 brd ff:ff:ff:ff:ff:ff
43: veth-test1@veth-test2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 6a:f2:ab:4c:12:fb brd ff:ff:ff:ff:ff:ff
将 veth-test1 veth-test2 分别set 到 network namespace test1 test2
[root@localhost go-hello]# sudo ip link set veth-test1 netns test1
[root@localhost go-hello]# sudo ip link set veth-test2 netns test2
[root@localhost go-hello]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:d7:99:69 brd ff:ff:ff:ff:ff:ff
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
link/ether 02:42:7f:7d:d6:b2 brd ff:ff:ff:ff:ff:ff
[root@localhost go-hello]# sudo ip netns exec test1 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
43: veth-test1@if42: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 6a:f2:ab:4c:12:fb brd ff:ff:ff:ff:ff:ff link-netnsid 1
[root@localhost go-hello]# sudo ip netns exec test2 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
42: veth-test2@if43: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 46:91:c3:1c:ea:50 brd ff:ff:ff:ff:ff:ff link-netnsid 0
分配ip地址
[root@localhost go-hello]# sudo ip netns exec test1 ip addr add 192.168.1.1/24 dev veth-test1
[root@localhost go-hello]# sudo ip netns exec test2 ip addr add 192.168.1.2/24 dev veth-test2
up
[root@localhost go-hello]# sudo ip netns exec test1 ip link set dev veth-test1 up
[root@localhost go-hello]# sudo ip netns exec test2 ip link set dev veth-test2 up
[root@localhost go-hello]# sudo ip netns exec test1 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
43: veth-test1@if42: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether 6a:f2:ab:4c:12:fb brd ff:ff:ff:ff:ff:ff link-netnsid 1
[root@localhost go-hello]# sudo ip netns exec test2 ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
42: veth-test2@if43: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether 46:91:c3:1c:ea:50 brd ff:ff:ff:ff:ff:ff link-netnsid 0
[root@localhost go-hello]# sudo ip netns exec test1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
43: veth-test1@if42: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 6a:f2:ab:4c:12:fb brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet 192.168.1.1/24 scope global veth-test1
valid_lft forever preferred_lft forever
inet6 fe80::68f2:abff:fe4c:12fb/64 scope link
valid_lft forever preferred_lft forever
[root@localhost go-hello]# sudo ip netns exec test2 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
42: veth-test2@if43: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 46:91:c3:1c:ea:50 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.1.2/24 scope global veth-test2
valid_lft forever preferred_lft forever
inet6 fe80::4491:c3ff:fe1c:ea50/64 scope link
valid_lft forever preferred_lft forever
实现了两个network namespace 的互通
[root@localhost go-hello]# sudo ip netns exec test1 ping 192.168.1.2
PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=1.16 ms
64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.108 ms
64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.103 ms
64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.118 ms
64 bytes from 192.168.1.2: icmp_seq=5 ttl=64 time=0.068 ms
64 bytes from 192.168.1.2: icmp_seq=6 ttl=64 time=0.076 ms
64 bytes from 192.168.1.2: icmp_seq=7 ttl=64 time=0.064 ms
64 bytes from 192.168.1.2: icmp_seq=8 ttl=64 time=0.050 ms
64 bytes from 192.168.1.2: icmp_seq=9 ttl=64 time=0.078 ms
^C
--- 192.168.1.2 ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8002ms
rtt min/avg/max/mdev = 0.050/0.203/1.168/0.341 ms
网友评论