Docker入门与应用系列(四)网络管理

一、Docker的五种网络模式

  在使用docker run创建docker容器时,可以用--net选项指定容器的网络模式,Docker有以下5种网络模式:

   1. bridge模式

   使用docker run --net=bridge指定,bridge模式是Docker默认的网络设置,此模式会为每一个容器分配Network Namespace、设置IP等,并将一个主机上的Docker容器连接到一个虚拟网桥上。 此模式与外界通信使用NAT协议,增加了通讯的复杂性,在复杂场景下使用会有诸多限制。

   2. host模式

  使用docker run --net=host指定,这种模式Docker Server将不为Docker容器创建网络协议栈,即不会创建独立的network namespace,Docker容器中的进程处于宿主机的网络环境中,相当于Docker容器的宿主机共用同一个network namespace,使用宿主机的网卡、IP、端口等信息。此模式没有网络隔离性,同时会引起网络资源的竞争与冲突。 

docker run -it --rm --name=centos-test --net=host centos:7 /bin/bash

# --rm :当进入到容器后,退出时将删除该容器

yum -y install net-tools

[root@localhost ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:f9:fe:32:41  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.200.208  netmask 255.255.255.0  broadcast 172.16.200.255
        inet6 fe80::20c:29ff:fefc:9a4b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:fc:9a:4b  txqueuelen 1000  (Ethernet)
        RX packets 47243  bytes 18062368 (17.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 17177  bytes 2678186 (2.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 112  bytes 6172 (6.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 112  bytes 6172 (6.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  使用host模式container 和宿主机的IP一样

  3.container模式

   使用docker run --net=container:othercontainer_name指定,这种模式与host模式相似,指定新创建的容器和已经存在的某个容器共享同一个network namespace, 以下两种模式都共享network namespace,区别就在于host模与宿主机共享,而container模式与某个存在的容器共享。 在container模式下,两个容器的进程可以通过lo回环网络设备通讯,增加了容器间通讯的便利性和效率。container模式的应用场景就在于可以将一个应用的多个组件放在不同的容器趾,这些 容器配成container模式的网络,这样它们可以作为一个整体对外提供服务。同时,这种模式也降低了容器间的隔离性。

  

# 1. 启动一个现有容器
[bigberg@localhost ~]$ docker-enter my-test
Last login: Mon Feb 26 03:42:28 UTC 2018
[root@0cd0fd089ea0 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 16  bytes 1296 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# 新建一个容器

[bigberg@localhost ~]$ docker run -it --net=container:my-test --name=my-test2 centos:7 /bin/bash

[root@0cd0fd089ea0 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 9021  bytes 13565843 (12.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7500  bytes 503022 (491.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  可以看到两个容器eth0的IP地址一样

   4.none模式

  使用docker run --net=none指定,在这种模式下,Docker容器拥有自己的Network Namespace,但是,并不为Docker容器进行任何网络配置。也就是说,这个Docker容器没有网卡、IP、路由等信息。需要我们自己为Docker容器添加网卡、配置IP等。这种模式如果不进行特定的配置是无法正常使用的,但它也给了用户最大的自由度来自定义容器的网络环境。 

[bigberg@localhost ~]$ docker run --rm -it --net=none --name=test01 test/centos:v1.0 /bin/bash
[root@006f8d383dd3 /]# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  

  5.overlay模式

overlay网络特点:

  • 跨主机通讯
  • 无需做端口映射
  • 无需担心IP冲突

  

原文地址:https://www.cnblogs.com/bigberg/p/8472454.html