您好,登录后才能下订单哦!
1、在纯Docker的环境,Docker支持4类网络模式:
● host模式:使用宿主机的IP和端口
● container模式:和已存在的容器共享网络
● none模式: 不进行网络配置
● bridge模式: 默认模式,使用桥接网络,Kubernetes使用此模式。
2、Docker 网络模型
通过上图,可以清楚的表示容器的网络结构,其中容器中的网卡eth0和绑定在Docker0网桥上的vethxxx设备是一对veth设备对。其中vethxxx由于绑定到docker0网桥,所以没有IP地址,容器中的eth0分配了和docker0同一网段的地址,这样就实现了容器的互联。
3、查看运行两个容器的宿主:
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:15:c2:12 brd ff:ff:ff:ff:ff:ff
inet 192.168.20.17/24 brd 192.168.20.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fe15:c212/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:fa:6f:13:18 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:faff:fe6f:1318/64 scope link
valid_lft forever preferred_lft forever
7: veth47e9040@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
link/ether f2:4e:50:a5:fb:b8 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::f04e:50ff:fea5:fbb8/64 scope link
valid_lft forever preferred_lft forever
19: veth46fb1f6@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
link/ether 7a:96:bc:c7:03:d8 brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::7896:bcff:fec7:3d8/64 scope link
valid_lft forever preferred_lft forever
4、通过查看桥接网卡信息,可以验证这两个veth绑定在docker0上:
# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.0242fa6f1318 no veth46fb1f6
veth47e9040
5、查看容器的network namespace
# docker inspect 506a694d09fb|grep Pid
"Pid": 2737,
"PidMode": "",
"PidsLimit": 0,
# mkdir /var/run/netns
# ln -s /proc/2737/ns/net /var/run/netns/506a694d09fb
# ip netns list
506a694d09fb (id: 0)
6d9742fb3c2d (id: 1)
分别查看两个容器的IP:
# ip netns exec 506a694d09fb ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
6: eth0@if7: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft foreve
# ip netns exec 6d9742fb3c2d ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 scope global eth0
valid_lft forever preferred_lft forever
可以发现这两个容器属于不同网络命名空间,但是在同一网段,通过veth设备对,绑定docker0互联。
通过ethtool -S veth-name 可以查看到对应的peer端,这里就不再演示,其实通过veth的名称(vethxxx@ifNO)也可以发现所指的接口信息。
6、如果执行ip netns exec命令报错,说明docker把网络netns又隐藏了一层,可以先进入docker守护进程的mount命名空间,在进入对应的网络命名空间,执行下面的nsenter命令。
[root@jumpserver ~]# nsenter -t 6807 -m nsenter --net=/var/run/docker/netns/a4fdba0d4cd4 ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
46: eth0@if47: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever
6807为docker主进程pid,可以通过pstree -p命令获取。
参考文章:
https://blog.51cto.com/tryingstuff/2165805
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。