docke网络之bridge、host、none

一、bridge网络

1.创建一个测试容器
[root@localhost ~]# docker run -d -it --name busybox_1 busybox /bin/sh -c "while true;do sleep 3600;done"
03b308c847edd23f21ba69afb825d92f7aaeb05b1ff4431dd47ccee439a0361a
2.查看当前机器docker有哪些网络
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
fa30a4d17b5b        bridge              bridge              local
a03aaca35833        host                host                local
d85c50eb947c        none                null                local
3.查看bridge详细信息(如果没有指定网络,默认使用bridge网络)
[root@localhost ~]# docker network inspect fa30a4d17b5b  #fa30a4d17b5b  为bridge的ID
.....
        "Containers": {                    #该字段表示名称为busybox_1的Container网络连接到的是bridge这个网络
            "03b308c847edd23f21ba69afb825d92f7aaeb05b1ff4431dd47ccee439a0361a": {  #容器的ID
                "Name": "busybox_1",        #容器的名称
                "EndpointID": "c850f22941894ef8655a80a96e4be4c5045699b70b4bc17201f80f07a27a3b4d",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",  #地址
                "IPv6Address": ""
            }
        },
......
4.查看宿主机及容器busybox_1这个容器的网络接口,其中宿主机的veth66a7ab0@if110与容器中的eth0@if111网络接口实际上是一对pari,而veth66a7ab0@if110又连接到docker0上
[root@localhost ~]# ip a   #查看宿主网络接口
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:fd:34:4b brd ff:ff:ff:ff:ff:ff
    inet 172.16.150.135/24 brd 172.16.150.255 scope global eth0
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:23:c0:91:f9 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
111: veth66a7ab0@if110: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 7e:59:81:8b:54:a2 brd ff:ff:ff:ff:ff:ff link-netnsid 0

[root@localhost ~]# docker exec busybox_1  ip a  #查看busybox_1容器的网络接口
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
110: eth0@if111: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    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
5.验证veth66a7ab0网络与docker0相连接
[root@localhost ~]# brctl show   #如果没有该命令,yum安装 bridge-utils软件包即可
bridge name    bridge id        STP enabled    interfaces
docker0        8000.024223c091f9    no        veth66a7ab0
6.创建第二测试容器
[root@localhost ~]# docker run -d -it --name busybox_2 busybox /bin/sh -c "while true;do sleep 3600;done"
b884db0bf4a862281b1dfb66457c7f565896fce1a40151619e80c2c5b1499216
7.再次查看bridge网络信息
[root@localhost ~]# docker network  inspect  bridge 
......      
      "Containers": {
            "03b308c847edd23f21ba69afb825d92f7aaeb05b1ff4431dd47ccee439a0361a": {
                "Name": "busybox_1",
                "EndpointID": "c850f22941894ef8655a80a96e4be4c5045699b70b4bc17201f80f07a27a3b4d",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "b884db0bf4a862281b1dfb66457c7f565896fce1a40151619e80c2c5b1499216": {
                "Name": "busybox_2",  #busybox_2 也连接到bridge
                "EndpointID": "a5e56917165daf2965bf7f24cf9ce58c88e4ff3c1118544c49ca5f25172af28d",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
        },
......
8.查看本地网络接口,发现多了113: vethc039e93@if112
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:fd:34:4b brd ff:ff:ff:ff:ff:ff
    inet 172.16.150.135/24 brd 172.16.150.255 scope global eth0
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:23:c0:91:f9 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
111: veth66a7ab0@if110: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 7e:59:81:8b:54:a2 brd ff:ff:ff:ff:ff:ff link-netnsid 0
113: vethc039e93@if112: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether ea:a2:a8:dc:48:78 brd ff:ff:ff:ff:ff:ff link-netnsid 1
9.查看bridge网络信息,发现docker0连接率两个接口了
[root@localhost ~]# brctl show
bridge name    bridge id        STP enabled    interfaces
docker0        8000.024223c091f9    no        veth66a7ab0
                                        vethc039e93
 简单拓扑图:
 
总结:实质上docker容器之间通过与docker0接口连接.实现先互直接通信,感觉有点像交换机?
10.docker访问公网简单拓扑图

小结:实质上docker访问公网网络通过docker0 NAT转发实现

 二、host网络

1.查看当前服务端口
[root@localhost ~]# netstat -tnlp  #除了sshd,没有其他服务端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      965/sshd            
tcp6       0      0 :::22                   :::*                    LISTEN      965/sshd    
2.创建一个测试容器,指定使用host网络
[root@localhost ~]# docker run  -d --privileged --name my_centos  --network host  docker.io/centos /usr/sbin/init  #建议使用centos镜像,后续测试需要
cfb8d105dcb44947ce794d890b67c905df9aa1ba67ef2675fd1a51177d47835d
3.查看host网络信息(注意容器的网络信息)
[root@localhost ~]# docker network inspect host 
......
 "Containers": {
            "cfb8d105dcb44947ce794d890b67c905df9aa1ba67ef2675fd1a51177d47835d": {
                "Name": "my_centos",
                "EndpointID": "4250d74b28f8125688bd7d0f1475a7d107135c0e87367a9c35c197fd981b7cd4",
                "MacAddress": "",   #可以看到我们创建的容器此时并没有Mac和IP地址
                "IPv4Address": "",
                "IPv6Address": ""
            }

......
4.进入容器内部,查看网络信息
[root@localhost ~]# docker exec -it my_centos /bin/bash
[root@localhost /]# ip a    #默认没有相关查看命令
bash: ip: command not found
[root@localhost /]# ifconfig
bash: ifconfig: command not found
[root@localhost /]# yum install net-tools -y  #我们yum安装(是不是很奇怪,没有网络地址怎么安装)
[root@localhost /]# ifconfig    #内容太多不粘了,但是显示的应该是当前宿主机的网络信息
[root@localhost /]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -   
5.在容器总安装httpd服务器,并启动
[root@localhost /]# yum install httpd -y
[root@localhost /]# systemctl restart httpd
[root@localhost /]# netstat -tnlp  #发现多了80端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1305/httpd          
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -   
6.退出容器,在宿主机上查看当前网络连接
[root@localhost ~]# netstat -tnlp #宿主机上80端口也被开放了
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7032/httpd          
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      965/sshd            
tcp6       0      0 :::22                   :::*                    LISTEN      965/sshd   
7.对于host模式的思考:

host模式下是怎么占领端口的?

host模式端口占用模式是你的容器占用你主机上当前所监听的端口(官网描述为publish),比如我们都知道Nginx占用80端口,那么当我们用host模式启动的时候,主机上的80端口会被Nginx占用,这个时候其他的容器就不能指定我们的8080端口了,但是可以指定其他端口,所以说一台主机上可以运行多个host模式的容器,只要彼此监听的端口不一样就行。

host模式下使用-p或者-P会出现WARNING: Published ports are discarded when using host network mode

当你是host模式的时候,主机会自动把他上面的端口分配给容器,这个时候使用-p或者-P是无用的。但是还是可以在Dockerfile中声明EXPOSE端口

host模式设计的原因

host模式设计出来就是为了性能,访问主机的端口就能访问到我们的容器,使容器直接暴露在公网下,但是这却对docker的隔离性造成了破坏,使得安全性大大降低。这种模式有利有弊,对于每个人来说看法都不一样,具体取舍看个人。

 三、none网络

1.创建一个测试容器
[root@localhost ~]#  docker run -d --name test1 --network none busybox  /bin/sh -c "while true;do sleep 36000;done"
ca1771ebfe436137156568cd570c116d12bd85e782dbec365c9f62a70209d028
2.查看none网络信息
[root@localhost ~]# docker network inspect none 
......
        "Containers": {
            "ca1771ebfe436137156568cd570c116d12bd85e782dbec365c9f62a70209d028": {
                "Name": "test1",  #容器无法看到Mac和IP地址
                "EndpointID": "ddcff44cdedb78f59108c6978345a256baa8bb09965461b2ffac58d5334fdba6",
                "MacAddress": "",
                "IPv4Address": "",
                "IPv6Address": ""
            }
......
3.进入容器查看网络信息
[root@localhost ~]# docker exec -it test1 /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

 小结:none 网络的容器只有一个自己的回环接口,没有任何 IP 地址分配,所有这个网络中的每个容器都是孤立的。但是所有网络配置我们都可以自己配置,如IP、网卡等

posted @ 2019-07-11 17:20  百衲本  阅读(1073)  评论(1编辑  收藏  举报
cnblogs_post_body { color: black; font: 0.875em/1.5em "微软雅黑" , "PTSans" , "Arial" ,sans-serif; font-size: 15px; } cnblogs_post_body h1 { text-align:center; background: #333366; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 23px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h2 { text-align:center; background: #006699; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 20px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h3 { background: #2B6695; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 18px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } 回到顶部 博客侧边栏 回到顶部 页首代码 回到顶部 页脚代码