【Docker网络原理分析三】直接路由实现bridge之间以及跨主机bridge之间通信

在第一篇,我们创建了两个ns,能通过bridge实现两个ns之间通信。在第二篇文章,我们能实现ns和宿主机以及外部的网络通信。这个文章,主要实现两个网桥之间的通信以及跨主机网桥之间的通信。

之前创建了网桥bridge0设置的地址是10.10.1.1,现在我们再创建一个网桥地址设置成10.10.2.1,并且启动它。

#创建网桥bridge1
brctl addbr  bridge1
#配置ip
ip addr add 10.10.2.1/24 dev bridge1
#启动bridge1
ip link set bridge1 up

为了便于测试,我们创建net3和net4,并且创建veth-pair把bridge1和net3和net4连接起来。

[root@localhost ~]# ip netns add net3
[root@localhost ~]# ip netns add net4
[root@localhost ~]# ip link add veth3 type veth peer name veth_peer3
[root@localhost ~]# ip link add veth4 type veth peer name veth_peer4
[root@localhost ~]# ip link set veth3 netns net3
[root@localhost ~]# ip link set veth4 netns net4 
[root@localhost ~]# ip link set veth_peer3 master bridge1
[root@localhost ~]# ip link set veth_peer4  master bridge1
[root@localhost ~]# ip link set veth_peer3 up
[root@localhost ~]# ip link set veth_peer4  up
[root@localhost ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
bridge0		8000.466b412e3d8e	no		veth_peer1
							            veth_peer2
bridge1		8000.66fe14cff66c	no		veth_peer3
							            veth_peer4
[root@localhost ~]# ip netns exec net3 ip addr add  10.10.2.2/24 dev  veth3
[root@localhost ~]# ip netns exec net4 ip addr add  10.10.2.3/24 dev  veth4
[root@localhost ~]# ip netns exec net3 ip link set veth3 up
[root@localhost ~]# ip netns exec net4 ip link set veth4 up
[root@localhost ~]# ip netns exec net4 ping 10.10.2.2
PING 10.10.2.2 (10.10.2.2) 56(84) bytes of data.
64 bytes from 10.10.2.2: icmp_seq=1 ttl=64 time=0.072 ms
64 bytes from 10.10.2.2: icmp_seq=2 ttl=64 time=0.051 ms
64 bytes from 10.10.2.2: icmp_seq=3 ttl=64 time=0.056 ms
--- 10.10.2.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.051/0.059/0.072/0.012 ms

在新创建的ns中只有一条自己网段的路由:

[root@localhost ~]# ip netns exec net4 route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.10.2.0       0.0.0.0         255.255.255.0   U     0      0        0 veth4

我们需要把这个路由删除,重新制定默认路由:

[root@localhost ~]# ip netns exec net4 route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.10.2.0       0.0.0.0         255.255.255.0   U     0      0        0 veth4
[root@localhost ~]# ip netns exec net4  ip route add default via 10.10.2.1
[root@localhost ~]# ip netns exec net4   route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.10.2.1       0.0.0.0         UG    0      0        0 veth4
10.10.2.0       0.0.0.0         255.255.255.0   U     0      0        0 veth4
[root@localhost ~]# ip netns exec net4 ip   route del 10.10.2.0/24
[root@localhost ~]# ip netns exec net4  route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.10.2.1       0.0.0.0         UG    0      0        0 veth4

测试如下:

[root@localhost ~]# ip netns exec net4  ping 10.10.1.3
PING 10.10.1.3 (10.10.1.3) 56(84) bytes of data.
64 bytes from 10.10.1.3: icmp_seq=1 ttl=63 time=0.106 ms
64 bytes from 10.10.1.3: icmp_seq=2 ttl=63 time=0.080 ms
--- 10.10.1.3 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.080/0.093/0.106/0.013 ms

同理,对net3做类似的操作也能实现这样的功能。

以上我们实现了同一宿主机不同网桥之间的通信,接来下实现跨主机网桥之间的通信:

我们在13服务器上创建一个新的net1,并且用veth-pair绑定到bridge0上,设置网桥IP是10.10.4.1。

接下来,我们就实现14服务器上的net1(10.10.1.2)和13服务器上的net1(10.10.4.2)通信。

在14上添加路由

[root@localhost ~]# route add -net 10.10.4.0 netmask 255.255.255.0 gw 192.168.159.13
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.159.2   0.0.0.0         UG    100    0        0 ens33
10.10.1.0       0.0.0.0         255.255.255.0   U     0      0        0 bridge0
10.10.4.0       192.168.159.13  255.255.255.0   UG    0      0        0 ens33
192.168.159.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33 

在13上添加路由

[root@localhost ~]#  route add -net 10.10.1.0 netmask 255.255.255.0 gw 192.168.159.14
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.159.2   0.0.0.0         UG    100    0        0 ens33
10.10.1.0       192.168.159.14  255.255.255.0   UG    0      0        0 ens33
10.10.4.0       0.0.0.0         255.255.255.0   U     0      0        0 bridge0
192.168.159.0   0.0.0.0         255.255.255.0   U     100    0        0 ens3

直接就实现通信了。验证如下:

[root@localhost ~]# ip netns exec net1 ping 10.10.1.2
PING 10.10.1.2 (10.10.1.2) 56(84) bytes of data.
64 bytes from 10.10.1.2: icmp_seq=1 ttl=62 time=0.406 ms
64 bytes from 10.10.1.2: icmp_seq=2 ttl=62 time=0.627 ms
64 bytes from 10.10.1.2: icmp_seq=3 ttl=62 time=0.472 ms
--- 10.10.1.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 0.406/0.501/0.627/0.096 ms
[root@localhost ~]# ip netns exec net1 ping 10.10.4.2
PING 10.10.4.2 (10.10.4.2) 56(84) bytes of data.
64 bytes from 10.10.4.2: icmp_seq=1 ttl=62 time=0.541 ms
64 bytes from 10.10.4.2: icmp_seq=2 ttl=62 time=0.603 ms

--- 10.10.4.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms  rtt min/avg/max/mdev = 0.541/0.572/0.603/0.031 ms

我们现在实现了跨主机ns之间互联,接下来到了容器中,如何实现两个网桥之间互联以及跨主机两个网桥之间互联呢?

你可能感兴趣的:(Cloud,Computing,Networking)