docker启动时,会在宿主主机上创建一个名为docker0的虚拟网络接口。默认选择172.17.42.1/16,一个16位的子网掩码给容器提供了65534个IP地址。docker0仅仅是一个在绑定到这上面的其它网卡间自己主动转发数据包的虚拟以太网桥,它能够使容器和主机相互通信,容器与容器间通信。问题是,怎样让位于不同主机上的docker容器能够通信。怎样有效配置docker网络眼下来说还是一个较复杂的工作,因而也涌现了非常多的开源项目来解决问题,如flannel、Kubernetes、weave、pipework等等。

1. flannel

CoreOS团队出品,是一个基于etcd的覆盖网络(overlay network)并为每台主机提供一个独立子网的服务。Rudder简化了集群中Docker容器的网络配置,避免了多主机上容器子网冲突的问题,更能够大幅度降低端口映射方面的工作。详细代码见https://github.com/coreos/flannel。其工作原理为:

An overlay network is first configured with an IP range and the size of the subnet for each host. For example, one could configure the overlay to use 10.100.0.0/16 and each host to receive a /24 subnet. Host A could then receive 10.100.5.0/24 and host B could get 10.100.18.0/24. flannel uses etcd to maintain a mapping between allocated subnets and real host IP addresses. For the data path, flannel uses UDP to encapsulate IP datagrams to transmit them to the remote host. We chose UDP as the transport protocol for its ease of passing through firewalls. For example, AWS Classic cannot be configured to pass IPoIP or GRE traffic as its security groups only support TCP/UDP/ICMP.(摘自https://coreos.com/blog/introducing-rudder/)

2. Kubernetes

Kubernetes是由Google推出的针对容器管理和编排的开源项目,它让用户能够在跨容器主机集群的情况下轻松地管理、监測、控制容器化应用部署。

Kubernete有一个特殊的与SDN非常类似的网络化概念:通过一个服务代理创建一个能够分配给随意数目容器的IP地址。前端的应用程序或使用该服务的用户仅通过这一IP地址调用服务。不须要关心其它的细节。这样的代理方案有点SDN的味道,可是它并非构建在典型的SDN的第2-3层机制之上。

Kubernetes uses a proxying method, whereby a particular service — defined as a query across containers — gets its own IP address. Behind that address could be hordes of containers that all provide the same service — but on the front end, the application or user tapping that service just uses the one IP address.

This means the number of containers running a service can grow or shrink as necessary, and no customer or application tapping the service has to care. Imagine if that service were a mobile network back-end process, for instance; during traffic surges, more containers running the process could be added, and they could be deleted once traffic returned to normal. Discovery of the specific containers running the service is handled in the background, as is the load balancing among those containers. Without the proxying, you could add more containers, but you’d have to tell users and applications about it; Google’s method eliminates that need for configuration. (https://www.sdncentral.com/news/docker-kubernetes-containers-open-sdn-possibilities/2014/07/)

3. 为不同宿主机上全部容器配置同样网段的IP地址,配置方法见http://www.cnblogs.com/feisky/p/4063162.html,这篇文章是基于Linux bridge的,当然也能够用其它的方法,如用OpenvSwitch+GRE建立宿主机之间的连接:


# From http://goldmann.pl/blog/2014/01/21/connecting-docker-containers-on-multiple-hosts/

# Edit this variable: the 'other' host.
REMOTE_IP=188.226.138.185
# Edit this variable: the bridge address on 'this' host.
BRIDGE_ADDRESS=172.16.42.1/24
# Name of the bridge (should match /etc/default/docker).
BRIDGE_NAME=docker0
# bridges
# Deactivate the docker0 bridge
ip link set $BRIDGE_NAME down
# Remove the docker0 bridge
brctl delbr $BRIDGE_NAME
# Delete the Open vSwitch bridge
ovs-vsctl del-br br0
# Add the docker0 bridge
brctl addbr $BRIDGE_NAME
# Set up the IP for the docker0 bridge
ip a add $BRIDGE_ADDRESS dev $BRIDGE_NAME
# Activate the bridge
ip link set $BRIDGE_NAME up
# Add the br0 Open vSwitch bridge
ovs-vsctl add-br br0
# Create the tunnel to the other host and attach it to the
# br0 bridge
ovs-vsctl add-port br0 gre0 -- set interface gre0 type=gre options:remote_ip=$REMOTE_IP
# Add the br0 bridge to docker0 bridge
brctl addif $BRIDGE_NAME br0
# iptables rules
# Enable NAT
iptables -t nat -A POSTROUTING -s 172.16.42.0/24 ! -d 172.16.42.0/24 -j MASQUERADE
# Accept incoming packets for existing connections
iptables -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# Accept all non-intercontainer outgoing packets
iptables -A FORWARD -i docker0 ! -o docker0 -j ACCEPT
# By default allow all outgoing traffic
iptables -A FORWARD -i docker0 -o docker0 -j ACCEPT
# Restart Docker daemon to use the new BRIDGE_NAME
service docker restart

4. 使用weave为容器配置IP(用法见http://www.cnblogs.com/feisky/p/4093717.html)。weave的特性包含

  • 应用隔离:不同子网容器之间默认隔离的。即便它们位于同一台物理机上也相互不通;不同物理机之间的容器默认也是隔离的
  • 物理机之间容器互通:weave connect $OTHER_HOST
  • 动态增加网络:对于不是通过weave启动的容器,能够通过weave attach 10.0.1.1/24 $id来增加网络(detach删除网络)
  • 安全性:能够通过weave launch -password wEaVe设置一个密码用于weave peers之间加密通信
  • 与宿主机网络通信:weave expose 10.0.1.102/24。这个IP会配在weave网桥上
  • 查看weave路由状态:weave ps
  • 通过NAT实现外网訪问docker容器

5. 改动主机docker默认的虚拟网段,然后在各自主机上分别把对方的docker网段增加到路由表中,配合iptables就可以实现docker容器夸主机通信。配置方法例如以下:

设有两台虚拟机

  • v1: 192.168.124.51
  • v2: 192.168.124.52

更改虚拟机docker0网段。v1为172.17.1.1/24,v2为172.17.2.1/24

#v1
sudo ifconfig docker0 172.17.1.1 netmask 255.255.255.0sudo bash -c 'echo DOCKER_OPTS="-B=docker0" >> /etc/default/docker'
sudo service docker restart# v2
sudo ifconfig docker0 172.17.2.1 netmask 255.255.255.0sudo bash -c 'echo DOCKER_OPTS="-B=docker0" >> /etc/default/docker'sudo service docker restart

然后在v1上把v2的docker虚拟网段增加到路由表中,在v2上将v1的docker虚拟网段增加到自己的路由表中

# v1 192.168.124.51
sudo route add -net 172.17.2.0 netmask 255.255.255.0 gw 192.168.124.52
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -s 172.17.1.0/24 ! -d 172.17.0.0/16 -j MASQUERADE# v2 192.168.124.52
sudo route add -net 172.17.1.0  netmask 255.255.255.0  gw 192.168.124.51
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -s 172.17.2.0/24 ! -d 172.17.0.0/16 -j MASQUERADE

至此。两台虚拟机中的docker容器能够互相訪问了。

转载于:https://www.cnblogs.com/yxwkf/p/5394577.html

docker网络配置方法总结相关推荐

  1. Ubuntu 命令行修改网络配置方法

    转载链接:http://www.jb51.net/article/15807.htm Ubuntu 命令行修改网络配置方法 /etc/network/interfaces 打开后里面可设置DHCP或手 ...

  2. 车载网络测试 - 车载以太网 - 网络配置方法

    VN5xxx 网络配置方法(Network-base access) 随着车载以太网的发展,vector的工具配置也逐渐由传统的channel-base access向Network-base acc ...

  3. Docker网络配置

    一.网络相关: IP(网络协议). 子网掩码 .网关 .DNS .端口号; 1.子网掩码: 互联网是由许多小型网络构成的,每个网络上都有许多主机,这样便构成了一个有层次的结构. IP 地址在设计时就考 ...

  4. 燕山大学计算机网络实验(windows网络配置方法及基本网络命令、交换机和路由器的使用、小型校园网络模拟搭建)

    项目源码以及报告获取,可看我专栏简介 实验1 windows网络配置方法及基本网络命令 1.1 实验内容和要求 1.查看本机网络配置,根据配置信息,把本机地址改为静态地址,并使用ipconfig.pi ...

  5. Docker网络配置进阶

    Docker启动会默认创建docker0虚拟网桥,是Linux的一个bridge,可以理解成一个软件交换机.它会在挂载到它的网口之间进行转发. 之后所有容器都是在172.17.0.x的网段上,并且可以 ...

  6. 两种Linux CentOS 6.5 网络配置方法

    方法一.修改网络配置文件 cd /etc/sysconfig/network-scripts cp ifcfg-eth0 ./ifcfg-eth0.bak //修改前先备份 第一个以太网卡对应的网络配 ...

  7. FC网络学习笔记02 -网络配置方法

    随着新一代飞机的综合化航电系统对通信需求的不断提高,传统的ARINC429.1553B总线的传输速率分别只有100Kbps和1Mbps,其带宽已远远不 论文联盟 http://Www.LWlm.cOm ...

  8. docker 网络配置_Kafka的AWS Docker网络设置

    Kafka是一个分布式流处理平台,最近几年获得了长足的发展和进步.这篇文章主要针对在AWS 上部署 Kafka Docker镜像的注意事项.其中最容易出问题的部分就是Kafka的listeners配置 ...

  9. Docker网络配置再学习之Host和none模式

    在之前的文章中,壹哥跟大家说过,关于Docker网络这一块的内容有很多,为了让大家搞清楚这个问题,壹哥准备搞几篇系列文章,来为各位小伙伴解惑.今天壹哥给大家带来的是Docker网络中host和none ...

最新文章

  1. PAT(甲级)2018年秋季考试 7-1 Werewolf - Simple Version
  2. 基于用户评价的评分模型
  3. python原生字符串可以参与比较_正则表达式中对于原生字符串的理解
  4. 【引用】Json 定义与操作
  5. c语言万年历报告ppt,万年历设计报告
  6. 互联网世界的“人工智能”——探秘“深度学习”的前世今生
  7. eclipse中添加Json Editor Plugin 插件的方法
  8. java判断来源_java中判断applet 来源的方法有()
  9. 震网三代在metasploit-framework上的复现与利用
  10. 老船履带工具使用方法_眉山小型履带车使用方法
  11. 对自定义View的Measure和onMeasure的一点心得
  12. 计算机的主板显卡内存条怎么查,电脑显卡在哪看?查看自己电脑显卡的显存等信息的方法...
  13. Spring中的Interceptor拦截器中使用@Autowired注解,在运行时会出现空指针
  14. ajax hack,Ajax Hacks-hack9 深入了解HTTP Respon_jquery
  15. java 读取pdf签名域_Java 获取PDF中的数字签名信息
  16. 计算机视觉算法——基于Transformer的目标检测(DETR / Deformable DETR / DETR 3D)
  17. Redhat7升级内核(含安装yum)
  18. php处理jpg图片背景色,将白色处理为透明色
  19. 下一跳—数据包的下一跳目的地址
  20. 云米与友阿股份达成战略合作,家庭物联网落地湖南

热门文章

  1. CTFshow php特性 web148
  2. poj 2553 The Bottom of a Graph 未完
  3. 10.2.1 关于vc++不支持把类的成员函数定义为类的友元函数的处理
  4. stl-vector详解
  5. c++常量函数的理解
  6. sklearn学习(二)
  7. 13.8 线程的安全问题和解决方法
  8. 第一篇T语言实例开发(版本5.3),带错误检测的加减乘除运算器
  9. Panda和numpy库和matplotlib库的安装
  10. MySQL的简单使用-(一)