Linux network - Virtual Ethernet
什么是Virtual Ethernet?
我们知道Linux可以通过物理网卡,可以实现两台机器的连通,Linux同样提供了一种机制,可以使两个端口连通,不通过物理接口,Linux称之为virtual ethernet。这种技术需要定义两端,可以分配IP,走指定的路由进行通信。
怎么创建L2层的Virtual Ethernet?
- 创建用于通信的bridge
点击(此处)折叠或打开
- [root@localhost ~]# ip link add edge_bridge1 type bridge
- #创建bridge设备
- [root@localhost ~]# ip link set dev ens9 master edge_bridge1
- #绑定物理网卡到bridge设备
- 创建virtual Ethernet
点击(此处)折叠或打开
- [root@localhost ~]# ip link add host_veth1 type veth peer name edge_veth1
- [root@localhost ~]# ip link show
-
1: lo:
mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 - link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
-
2: ens9:
mtu 1500 qdisc pfifo_fast master edge_bridge1 state UP mode DEFAULT qlen 1000 - link/ether 52:54:00:6b:cc:77 brd ff:ff:ff:ff:ff:ff
-
3: eth0:
mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 - link/ether 52:54:00:97:1d:2e brd ff:ff:ff:ff:ff:ff
-
4: br-87a2e3ee5e64:
mtu 1500 qdisc noqueue state DOWN mode DEFAULT - link/ether 02:42:78:1e:0c:7e brd ff:ff:ff:ff:ff:ff
-
5: docker0:
mtu 1500 qdisc noqueue state DOWN mode DEFAULT - link/ether 02:42:bd:f5:bc:a8 brd ff:ff:ff:ff:ff:ff
-
6: edge_bridge1:
mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 - link/ether 52:54:00:6b:cc:77 brd ff:ff:ff:ff:ff:ff
-
7: edge_veth1@host_veth1:
mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 - link/ether 62:b9:b5:5b:4e:90 brd ff:ff:ff:ff:ff:ff
-
8: host_veth1@edge_veth1:
mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 - link/ether a2:6c:46:37:3f:1f brd ff:ff:ff:ff:ff:ff
- 为virtual ethernet 绑定到设备
点击(此处)折叠或打开
- [root@localhost ~]# ip link set host_veth1 master host_bridge1
- [root@localhost ~]# ip link set edge_veth1 master edge_bridge1
- [root@localhost ~]# ip link show
-
1: lo:
mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 - link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
-
2: ens9:
mtu 1500 qdisc pfifo_fast master host_bridge1 state UP mode DEFAULT qlen 1000 - link/ether 52:54:00:6b:cc:77 brd ff:ff:ff:ff:ff:ff
-
3: eth0:
mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 - link/ether 52:54:00:97:1d:2e brd ff:ff:ff:ff:ff:ff
-
4: br-87a2e3ee5e64:
mtu 1500 qdisc noqueue state DOWN mode DEFAULT - link/ether 02:42:78:1e:0c:7e brd ff:ff:ff:ff:ff:ff
-
5: docker0:
mtu 1500 qdisc noqueue state DOWN mode DEFAULT - link/ether 02:42:bd:f5:bc:a8 brd ff:ff:ff:ff:ff:ff
-
6: edge_bridge1:
mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 - link/ether 62:b9:b5:5b:4e:90 brd ff:ff:ff:ff:ff:ff
-
7: edge_veth1@host_veth1:
mtu 1500 qdisc noop master edge_bridge1 state DOWN mode DEFAULT qlen 1000 - link/ether 62:b9:b5:5b:4e:90 brd ff:ff:ff:ff:ff:ff
-
8: host_veth1@edge_veth1:
mtu 1500 qdisc noop master host_bridge1 state DOWN mode DEFAULT qlen 1000 - link/ether a2:6c:46:37:3f:1f brd ff:ff:ff:ff:ff:ff
-
9: host_bridge1:
mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000 - link/ether 52:54:00:6b:cc:77 brd ff:ff:ff:ff:ff:ff
- 设置这几个设备的状态为up
点击(此处)折叠或打开
- [root@localhost ~]# ip link set host_bridge1 up
- [root@localhost ~]# ip link set edge_bridge1 up
- [root@localhost ~]# ip link set host_veth1 up
- [root@localhost ~]# ip link set edge_veth1 up
创建L3层的virtual Ethernet
- 创建一个bridge
点击(此处)折叠或打开
- [root@localhost ~]# ip link add edge_bridge2 type bridge
- [root@localhost ~]# ip link set dev ens9 master edge_bridge2
- 创建virtual ethernet
点击(此处)折叠或打开
- [root@localhost ~]# ip link add host_veth2 type veth peer name edge_veth2
- [root@localhost ~]# ip addr add 10.90.241.201/24 dev host_veth2
- [root@localhost ~]# ip addr show dev host_veth2
-
8: host_veth2@veth0:
mtu 1500 qdisc noop state DOWN qlen 1000 - link/ether 62:92:e6:31:5b:1e brd ff:ff:ff:ff:ff:ff
- inet 10.90.241.201/24 scope global host_veth2
- valid_lft forever preferred_lft forever
点击(此处)折叠或打开
- [root@localhost ~]# ping 10.90.241.200
- PING 10.90.241.200 (10.90.241.200) 56(84) bytes of data.
- From 10.90.241.94 icmp_seq=1 Destination Host Unreachable
- From 10.90.241.94 icmp_seq=2 Destination Host Unreachable
- From 10.90.241.94 icmp_seq=3 Destination Host Unreachable
- 设置host的对端为bridge的master
点击(此处)折叠或打开
- [root@localhost ~]# ip link set edge_veth2 master edge_bridge2
- 设置端口的状态为up
点击(此处)折叠或打开
- [root@localhost ~]# ip link set host_veth2 up
- [root@localhost ~]# ip link set edge_veth2 up
- [root@localhost ~]# ip link set edge_bridge2 up