veth 设备对实验

首先创建2个netns

[root@zabbix-server ~]# ip netns add  netns1
[root@zabbix-server ~]# ip netns add  netns2

创建1对veth 

[root@zabbix-server ~]# ip link add veth1 type veth  peer name veth2

使用 ip link show 可以看到2个veth

[root@zabbix-server ~]# ip link show
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether 00:50:56:be:11:34 brd ff:ff:ff:ff:ff:ff
4: veth2@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 02:8d:80:1d:9e:63 brd ff:ff:ff:ff:ff:ff
5: veth1@veth2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether ce:6a:a5:1d:8c:6c brd ff:ff:ff:ff:ff:ff

 

我们首先核实一下2个网络命名空间里面的网络 里面都只有一个lo回环设备

[root@zabbix-server ~]# ip netns exec netns1 bash
[root@zabbix-server ~]# 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@zabbix-server ~]# exit
exit
[root@zabbix-server ~]# ip netns exec netns2 bash
[root@zabbix-server ~]# 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@zabbix-server ~]# exit
exit

我们现在把veth一头分给netns1  使用命令 ip link show 查看只剩下一个了

[root@zabbix-server ~]# ip link set veth1 netns netns1
[root@zabbix-server ~]# ip link show
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether 00:50:56:be:11:34 brd ff:ff:ff:ff:ff:ff
12: veth2@if13: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether de:05:80:c6:9b:6b brd ff:ff:ff:ff:ff:ff link-netnsid 0

我们把veth2 分给 netns2  使用命令 ip link show  在宿主机上已经看不到veth

[root@zabbix-server ~]# ip link set veth2 netns netns2
[root@zabbix-server ~]# ip link show
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether 00:50:56:be:11:34 brd ff:ff:ff:ff:ff:ff

我们现在登录2个netns去查看一下,都多了一个veth  

[root@zabbix-server ~]# ip netns exec netns1 bash
[root@zabbix-server ~]# 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
13: veth1@if12: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether fe:5f:58:a3:43:67 brd ff:ff:ff:ff:ff:ff link-netnsid 1
[root@zabbix-server ~]# ifconfig
[root@zabbix-server ~]# exit
exit
[root@zabbix-server ~]# ip netns exec netns2 bash
[root@zabbix-server ~]# 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
12: veth2@if13: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether de:05:80:c6:9b:6b brd ff:ff:ff:ff:ff:ff link-netnsid 0
[root@zabbix-server ~]# exit
exit

我们现在分别给2个netns配置上ip 

[root@zabbix-server ~]# ip netns exec netns1 ip addr add 10.1.1.1/24 dev veth1
[root@zabbix-server ~]# ip netns exec netns2 ip addr add 10.1.1.2/24 dev veth2

启动设备

[root@zabbix-server ~]# ip netns exec netns1 ip link set dev veth1 up
[root@zabbix-server ~]# ip netns exec netns2 ip link set dev veth2 up

2个网络命名空间已经可以正常通讯

[root@zabbix-server ~]# ip netns exec netns2 ping 10.1.1.1
PING 10.1.1.1 (10.1.1.1) 56(84) bytes of data.
64 bytes from 10.1.1.1: icmp_seq=1 ttl=64 time=0.060 ms
64 bytes from 10.1.1.1: icmp_seq=2 ttl=64 time=0.050 ms
64 bytes from 10.1.1.1: icmp_seq=3 ttl=64 time=0.045 ms
^C
--- 10.1.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.045/0.051/0.060/0.010 ms
[root@zabbix-server ~]# ip netns exec netns1 ping 10.1.1.2
PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data.
64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=0.037 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from 10.1.1.2: icmp_seq=3 ttl=64 time=0.042 ms
^C
--- 10.1.1.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.037/0.043/0.052/0.009 ms
[root@zabbix-server ~]# ip netns exec netns1 bash
[root@zabbix-server ~]# 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
13: veth1@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether fe:5f:58:a3:43:67 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet 10.1.1.1/24 scope global veth1
       valid_lft forever preferred_lft forever
    inet6 fe80::fc5f:58ff:fea3:4367/64 scope link 
       valid_lft forever preferred_lft forever
[root@zabbix-server ~]# ifconfig
veth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.1.1  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::fc5f:58ff:fea3:4367  prefixlen 64  scopeid 0x20<link>
        ether fe:5f:58:a3:43:67  txqueuelen 1000  (Ethernet)
        RX packets 16  bytes 1328 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1328 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@zabbix-server ~]# exit
exit
[root@zabbix-server ~]# ip netns exec netns2 bash
[root@zabbix-server ~]# 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
12: veth2@if13: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether de:05:80:c6:9b:6b brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.1.1.2/24 scope global veth2
       valid_lft forever preferred_lft forever
    inet6 fe80::dc05:80ff:fec6:9b6b/64 scope link 
       valid_lft forever preferred_lft forever
[root@zabbix-server ~]# ifconfig
veth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.1.1.2  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::dc05:80ff:fec6:9b6b  prefixlen 64  scopeid 0x20<link>
        ether de:05:80:c6:9b:6b  txqueuelen 1000  (Ethernet)
        RX packets 16  bytes 1328 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1328 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@zabbix-server ~]# exit
exit
[root@zabbix-server ~]# 

 

posted @ 2020-07-10 16:08  caonw  阅读(382)  评论(0编辑  收藏  举报