Linux 虚拟网络设备 veth-pair 详解,这篇料很足


当前第2页 返回上一页

给 veth-pair 配置 IP,测试连通性:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

# 创建 namespace

ip netns a ns1

ip netns a ns2

 

# 创建一对 veth-pair veth0 veth1

ip l a veth0 type veth peer name veth1

 

# 将 veth0 veth1 分别加入两个 ns

ip l s veth0 netns ns1

ip l s veth1 netns ns2

 

# 给两个 veth0 veth1 配上 IP 并启用

ip netns exec ns1 ip a a 10.1.1.2/24 dev veth0

ip netns exec ns1 ip l s veth0 up

ip netns exec ns2 ip a a 10.1.1.3/24 dev veth1

ip netns exec ns2 ip l s veth1 up

 

# 从 veth0 ping veth1

[root@localhost ~]# ip netns exec ns1 ping 10.1.1.3

PING 10.1.1.3 (10.1.1.3) 56(84) bytes of data.

64 bytes from 10.1.1.3: icmp_seq=1 ttl=64 time=0.073 ms

64 bytes from 10.1.1.3: icmp_seq=2 ttl=64 time=0.068 ms

 

--- 10.1.1.3 ping statistics ---

15 packets transmitted, 15 received, 0% packet loss, time 14000ms

rtt min/avg/max/mdev = 0.068/0.084/0.201/0.032 ms

3.2 通过 Bridge 相连

Linux Bridge 相当于一台交换机,可以中转两个 namespace 的流量,我们看看 veth-pair 在其中扮演什么角色。

如下图,两对 veth-pair 分别将两个 namespace 连到 Bridge 上。

同样给 veth-pair 配置 IP,测试其连通性:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

# 首先创建 bridge br0

ip l a br0 type bridge

ip l s br0 up

 

# 然后创建两对 veth-pair

ip l a veth0 type veth peer name br-veth0

ip l a veth1 type veth peer name br-veth1

 

# 分别将两对 veth-pair 加入两个 ns 和 br0

ip l s veth0 netns ns1

ip l s br-veth0 master br0

ip l s br-veth0 up

 

ip l s veth1 netns ns2

ip l s br-veth1 master br0

ip l s br-veth1 up

 

# 给两个 ns 中的 veth 配置 IP 并启用

ip netns exec ns1 ip a a 10.1.1.2/24 dev veth0

ip netns exec ns1 ip l s veth0 up

 

ip netns exec ns2 ip a a 10.1.1.3/24 dev veth1

ip netns exec ns2 ip l s veth1 up

 

# veth0 ping veth1

[root@localhost ~]# ip netns exec ns1 ping 10.1.1.3

PING 10.1.1.3 (10.1.1.3) 56(84) bytes of data.

64 bytes from 10.1.1.3: icmp_seq=1 ttl=64 time=0.060 ms

64 bytes from 10.1.1.3: icmp_seq=2 ttl=64 time=0.105 ms

 

--- 10.1.1.3 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 999ms

rtt min/avg/max/mdev = 0.060/0.082/0.105/0.024 ms

3.3 通过 OVS 相连

OVS 是第三方开源的 Bridge,功能比 Linux Bridge 要更强大,对于同样的实验,我们用 OVS 来看看是什么效果。

如下图所示:

同样测试两个 namespace 之间的连通性:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

# 用 ovs 提供的命令创建一个 ovs bridge

ovs-vsctl add-br ovs-br

 

# 创建两对 veth-pair

ip l a veth0 type veth peer name ovs-veth0

ip l a veth1 type veth peer name ovs-veth1

 

# 将 veth-pair 两端分别加入到 ns 和 ovs bridge 中

ip l s veth0 netns ns1

ovs-vsctl add-port ovs-br ovs-veth0

ip l s ovs-veth0 up

 

ip l s veth1 netns ns2

ovs-vsctl add-port ovs-br ovs-veth1

ip l s ovs-veth1 up

 

# 给 ns 中的 veth 配置 IP 并启用

ip netns exec ns1 ip a a 10.1.1.2/24 dev veth0

ip netns exec ns1 ip l s veth0 up

 

ip netns exec ns2 ip a a 10.1.1.3/24 dev veth1

ip netns exec ns2 ip l s veth1 up

 

# veth0 ping veth1

[root@localhost ~]# ip netns exec ns1 ping 10.1.1.3

PING 10.1.1.3 (10.1.1.3) 56(84) bytes of data.

64 bytes from 10.1.1.3: icmp_seq=1 ttl=64 time=0.311 ms

64 bytes from 10.1.1.3: icmp_seq=2 ttl=64 time=0.087 ms

^C

--- 10.1.1.3 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 999ms

rtt min/avg/max/mdev = 0.087/0.199/0.311/0.112 ms

相关课程推荐:Linux视频教程

总结

veth-pair 在虚拟网络中充当着桥梁的角色,连接多种网络设备构成复杂的网络。

veth-pair 的三个经典实验,直接相连、通过 Bridge 相连和通过 OVS 相连。

参考

http://www.opencloudblog.com/?p=66

https://segmentfault.com/a/1190000009251098

以上就是Linux 虚拟网络设备 veth-pair 详解,这篇料很足的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

Linux下怎么卸载docker

解决Linux下mysql密码错误的问题

Linux系统中的755权限是什么意思

.tar.xz文件在Linux下的解压方法

查看Linux下所有用户的方法是什么

Linux进程间的通信方式有哪三种

Linux是怎样的

Linux下中文文件名乱码怎么办

Linux本地提权漏洞介绍

Linux怎么进行压缩

更多相关阅读请进入《Linux》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...