Linux network - virtual network

10190阅读 0评论2018-03-27 今生路人甲
分类:系统运维

Linux network - Virtual Ethernet

什么是Virtual Ethernet?

我们知道Linux可以通过物理网卡,可以实现两台机器的连通,Linux同样提供了一种机制,可以使两个端口连通,不通过物理接口,Linux称之为virtual ethernet。这种技术需要定义两端,可以分配IP,走指定的路由进行通信。

怎么创建L2层的Virtual Ethernet?

点击(此处)折叠或打开

  1. [root@localhost ~]# ip link add edge_bridge1 type bridge
  2. #创建bridge设备
  3. [root@localhost ~]# ip link set dev ens9 master edge_bridge1
  4. #绑定物理网卡到bridge设备

点击(此处)折叠或打开

  1. [root@localhost ~]# ip link add host_veth1 type veth peer name edge_veth1
  2. [root@localhost ~]# ip link show
  3. 1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
  4. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  5. 2: ens9: mtu 1500 qdisc pfifo_fast master edge_bridge1 state UP mode DEFAULT qlen 1000
  6. link/ether 52:54:00:6b:cc:77 brd ff:ff:ff:ff:ff:ff
  7. 3: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
  8. link/ether 52:54:00:97:1d:2e brd ff:ff:ff:ff:ff:ff
  9. 4: br-87a2e3ee5e64: mtu 1500 qdisc noqueue state DOWN mode DEFAULT
  10. link/ether 02:42:78:1e:0c:7e brd ff:ff:ff:ff:ff:ff
  11. 5: docker0: mtu 1500 qdisc noqueue state DOWN mode DEFAULT
  12. link/ether 02:42:bd:f5:bc:a8 brd ff:ff:ff:ff:ff:ff
  13. 6: edge_bridge1: mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
  14. link/ether 52:54:00:6b:cc:77 brd ff:ff:ff:ff:ff:ff
  15. 7: edge_veth1@host_veth1: mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
  16. link/ether 62:b9:b5:5b:4e:90 brd ff:ff:ff:ff:ff:ff
  17. 8: host_veth1@edge_veth1: mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
  18. link/ether a2:6c:46:37:3f:1f brd ff:ff:ff:ff:ff:ff
  • 为virtual ethernet 绑定到设备  

点击(此处)折叠或打开

  1. [root@localhost ~]# ip link set host_veth1 master host_bridge1
  2. [root@localhost ~]# ip link set edge_veth1 master edge_bridge1
  3. [root@localhost ~]# ip link show
  4. 1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
  5. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  6. 2: ens9: mtu 1500 qdisc pfifo_fast master host_bridge1 state UP mode DEFAULT qlen 1000
  7. link/ether 52:54:00:6b:cc:77 brd ff:ff:ff:ff:ff:ff
  8. 3: eth0: mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
  9. link/ether 52:54:00:97:1d:2e brd ff:ff:ff:ff:ff:ff
  10. 4: br-87a2e3ee5e64: mtu 1500 qdisc noqueue state DOWN mode DEFAULT
  11. link/ether 02:42:78:1e:0c:7e brd ff:ff:ff:ff:ff:ff
  12. 5: docker0: mtu 1500 qdisc noqueue state DOWN mode DEFAULT
  13. link/ether 02:42:bd:f5:bc:a8 brd ff:ff:ff:ff:ff:ff
  14. 6: edge_bridge1: mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
  15. link/ether 62:b9:b5:5b:4e:90 brd ff:ff:ff:ff:ff:ff
  16. 7: edge_veth1@host_veth1: mtu 1500 qdisc noop master edge_bridge1 state DOWN mode DEFAULT qlen 1000
  17. link/ether 62:b9:b5:5b:4e:90 brd ff:ff:ff:ff:ff:ff
  18. 8: host_veth1@edge_veth1: mtu 1500 qdisc noop master host_bridge1 state DOWN mode DEFAULT qlen 1000
  19. link/ether a2:6c:46:37:3f:1f brd ff:ff:ff:ff:ff:ff
  20. 9: host_bridge1: mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
  21. link/ether 52:54:00:6b:cc:77 brd ff:ff:ff:ff:ff:ff
  • 设置这几个设备的状态为up

点击(此处)折叠或打开

  1. [root@localhost ~]# ip link set host_bridge1 up
  2. [root@localhost ~]# ip link set edge_bridge1 up
  3. [root@localhost ~]# ip link set host_veth1 up
  4. [root@localhost ~]# ip link set edge_veth1 up

创建L3层的virtual Ethernet

  • 创建一个bridge  

点击(此处)折叠或打开

  1. [root@localhost ~]# ip link add edge_bridge2 type bridge
  2. [root@localhost ~]# ip link set dev ens9 master edge_bridge2
  • 创建virtual ethernet

点击(此处)折叠或打开

  1. [root@localhost ~]# ip link add host_veth2 type veth peer name edge_veth2
  2. [root@localhost ~]# ip addr add 10.90.241.201/24 dev host_veth2
  3. [root@localhost ~]# ip addr show dev host_veth2
  4. 8: host_veth2@veth0: mtu 1500 qdisc noop state DOWN qlen 1000
  5. link/ether 62:92:e6:31:5b:1e brd ff:ff:ff:ff:ff:ff
  6. inet 10.90.241.201/24 scope global host_veth2
  7. valid_lft forever preferred_lft forever
此时network还是不通的

点击(此处)折叠或打开

  1. [root@localhost ~]# ping 10.90.241.200
  2. PING 10.90.241.200 (10.90.241.200) 56(84) bytes of data.
  3. From 10.90.241.94 icmp_seq=1 Destination Host Unreachable
  4. From 10.90.241.94 icmp_seq=2 Destination Host Unreachable
  5. From 10.90.241.94 icmp_seq=3 Destination Host Unreachable
  • 设置host的对端为bridge的master

点击(此处)折叠或打开

  1. [root@localhost ~]# ip link set edge_veth2 master edge_bridge2
  • 设置端口的状态为up
    

点击(此处)折叠或打开

  1. [root@localhost ~]# ip link set host_veth2 up
  2. [root@localhost ~]# ip link set edge_veth2 up
  3. [root@localhost ~]# ip link set edge_bridge2 up






上一篇:Linux Bridge
下一篇:go标准库(errors)阅读