赞
踩
本环境是基于 Centos 7.8 系统构建Docker-19.03.13环境
具体构建,请参考 构建Docker-19.03.13
docker四种网络模式如下:
- Bridge contauner 桥接式网络模式
- Host(open) container 开放式网络模式
- Container(join) container 联合挂载式网络模式,是host网络模式的延伸
- None(Close) container 封闭式网络模式
查看docker网络模式
[root@node01 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
5b12a627fc9c bridge bridge local
bbcc7925419d host host local
a595e136fbe1 none null local
当Docker进程启动时,会在主机上创建一个名为docker0的虚拟网桥,此主机上启动的Docker容器会连接到这个虚拟网桥上,所以有默认地址172.17.0.0/16的地址。虚拟网桥的工作方式和物理交换机类似,这样主机上的所有容器就通过交换机连在了一个二层网络中。
从docker0子网中分配一个IP给容器使用,并设置docker0的IP地址为容器的默认网关。在主机上创建一对虚拟网卡veth pair设备,Docker将veth pair设备的一端放在新创建的容器中,并命名为eth0(容器的网卡),另一端放在主机中,以vethxxx这样类似的名字命名,并将这个网络设备加入到docker0网桥中。
查看网桥模式的网络
[root@node01 ~]# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.02427962e915 no veth7f7506a
vethf7a2c9c
bridge模式是docker的默认网络模式,不写–net参数,就是bridge模式。使用docker run -p时,
docker实际是在iptables做了DNAT规则,实现端口转发功能。可以使用iptables -t nat -vnL查看。
[root@node01 ~]# iptables -t nat -vnL Chain PREROUTING (policy ACCEPT 26 packets, 1681 bytes) pkts bytes target prot opt in out source destination 3 156 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL Chain INPUT (policy ACCEPT 8 packets, 564 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 257 packets, 16926 bytes) pkts bytes target prot opt in out source destination 0 0 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL Chain POSTROUTING (policy ACCEPT 257 packets, 16926 bytes) pkts bytes target prot opt in out source destination 18 1117 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0 Chain DOCKER (2 references) pkts bytes target prot opt in out source destination 0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
创建网桥模式的docker
[root@node01 ~]# docker run -it --rm busybox:latest /bin/sh / # / # / # ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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 20: eth0@if21: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever / # netstat -n Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node Path / # netstat -nr Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 172.17.0.1 0.0.0.0 UG 0 0 0 eth0 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 / # / # ping www.baidu.com PING www.baidu.com (61.135.185.32): 56 data bytes 64 bytes from 61.135.185.32: seq=0 ttl=127 time=43.594 ms 64 bytes from 61.135.185.32: seq=1 ttl=127 time=42.321 ms ^C --- www.baidu.com ping statistics --- 3 packets transmitted, 2 packets received, 33% packet loss round-trip min/avg/max = 42.321/42.957/43.594 ms / # exit
如果启动容器的时候使用host模式,那么这个容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。但是,容器的其他方面,如文件系统、进程列表等还是和宿主机隔离的
创建host模式的容器
[root@node01 ~]# docker run -it --rm --network host busybox:latest /bin/sh / # ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:3e:37:82 brd ff:ff:ff:ff:ff:ff inet 192.168.5.11/24 brd 192.168.5.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::d27a:afc6:3925:54ba/64 scope link valid_lft forever preferred_lft forever 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue link/ether 02:42:79:62:e9:15 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:79ff:fe62:e915/64 scope link valid_lft forever preferred_lft forever 15: vethf7a2c9c@if14: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 link/ether aa:64:32:48:44:e4 brd ff:ff:ff:ff:ff:ff inet6 fe80::a864:32ff:fe48:44e4/64 scope link valid_lft forever preferred_lft forever 19: veth7f7506a@if18: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 link/ether 5a:7d:fc:e9:9e:e8 brd ff:ff:ff:ff:ff:ff inet6 fe80::587d:fcff:fee9:9ee8/64 scope link valid_lft forever preferred_lft forever / # netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.5.2 0.0.0.0 UG 0 0 0 ens33 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0 192.168.5.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 / # ping www.baidu.com PING www.baidu.com (61.135.169.121): 56 data bytes 64 bytes from 61.135.169.121: seq=0 ttl=128 time=29.214 ms 64 bytes from 61.135.169.121: seq=1 ttl=128 time=323.149 ms ^C --- www.baidu.com ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 29.214/176.181/323.149 ms / # exit [root@node01 ~]#
这个模式指定新创建的容器和已经存在的一个容器共享一个 Network Namespace,而不是和宿主机共享。新创建的容器不会创建自己的网卡,配置自己的 IP,而是和一个指定的容器共享 IP、端口范围等。同样,两个容器除了网络方面,其他的如文件系统、进程列表等还是隔离的。两个容器的进程可以通过 lo 网卡设备通信
运行两个容器,它们共用相同的ip地址
容器 c1
[root@node01 ~]# docker run -it --rm --name c1 busybox:latest /bin/sh / # ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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 28: eth0@if29: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever / # echo 'hello word' > /tmp/index.html / # httpd -h /tmp/ / # netstat -lnutp | grep 80 tcp 0 0 :::80 :::* LISTEN 13/httpd / # netstat -lnutp | grep httpd tcp 0 0 :::80 :::* LISTEN 13/httpd / # wget -O - -q 127.0.0.1 hello word / #
容器 c2
[root@node01 ~]# docker run -it --rm --name c2 --network container:c1 busybox:latest /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
28: eth0@if29: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # wget -O - -q 127.0.0.1
hello word
/ # ls /tmp/
/ #
使用none模式,Docker容器拥有自己的Network Namespace,但是,并不为Docker容器进行任何网络配置。也就是说,这个Docker容器没有网卡、IP、路由等信息,只有lo 网络接口。需要我们自己为Docker容器添加网卡、配置IP等。
不参与网络通信,运行于此类容器中的进程仅能访问本地回环接口;仅适用于进程无须网络通信的场景中,例如:备份、进程诊断及各种离线任务等。
创建none模式的容器
[root@node01 ~]# docker run -it --rm --name busybox_none --network none busybox:latest /bin/sh
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
/ # netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
/ # ping www.baidu.com
ping: bad address 'www.baidu.com'
/ # exit
[root@node01 ~]#
解决方案
1、配置端口映射
2、k8s的应用
配置端口映射
[root@node01 ~]# docker run -it --name nginx -p 192.168.5.11:8888:80 nginx:1.14-alpine /bin/sh / # 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 34: eth0@if35: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever / # netstat -nr Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 172.17.0.1 0.0.0.0 UG 0 0 0 eth0 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 #查看运行中的容器 [root@node01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 18b91fc84c88 nginx:1.14-alpine "/bin/sh" 2 minutes ago Up 2 minutes 192.168.5.11:8888->80/tcp nginx b490c631df09 busybox:latest "/bin/sh" 3 hours ago Up 3 hours busybox1
浏览器访问容器中的服务
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。