原文链接

https://github.com/alfredhuang211/study-docker-doc/blob/master/docker%E8%B7%A8%E4%B8%BB%E6%9C%BAmacvlan%E7%BD%91%E7%BB%9C%E9%85%8D%E7%BD%AE.md#搭建环境

-----以下部分转自原文-----

搭建环境

  • virtualbox, ubuntu14.04.4 内核4.2.0 docker 1.12.0-rc1

  • virtualbox上运行两套主机系统,设置使用桥接模式,网卡混杂模式开启全部允许.

  • 主机上配置的eth0网口或者创建的vlan网口,均需要开启混杂模式,命令 ip link set eth0 promisc on ip link set eth0.100 promisc on

注意 : 如果不开启混杂模式,会导致macvlan网络无法访问外界,具体在不使用vlan时,表现为无法ping通路由,无法ping通同一网络内其他主机

搭建过程1-不使用vlan

1. 创建docker macvlan网络

两台主机上 eth0 使用分别为 192.168.15.75/192.168.15.21. 分别在两台主机上使用相同命令 docker network create -d macvlan --subnet=192.168.15.0/24 --gateway=192.168.15.1 -o parent=eth0 -o macvlan_mode=bridge eth0_1 创建eth0_1的macvlan网络.

2. 创建容器

主机1 运行容器 使用命令:

docker run --net=eth0_1 --ip=192.168.15.101 -id --name test101 busybox sh

docker run --net=eth0_1 --ip=192.168.15.102 -id --name test102 busybox sh

主机2 运行容器 使用命令:

docker run --net=eth0_1 --ip=192.168.15.201 -id --name test201 busybox sh

docker run --net=eth0_1 --ip=192.168.15.202 -id --name test202 busybox sh

3. 测试网络

主机1上测试:

运行命令:

docker exec test101 ping 192.168.15.1 ping网关: 通

docker exec test101 ping test102 使用容器名ping本主机容器: 通

docker exec test102 ping 192.168.15.101 ping本主机容器: 通

docker exec test102 ping 192.168.15.199 ping本网络其他主机: 通

docker exec test101 ping 192.168.15.201 ping另一主机容器: 通

docker exec test101 ping test201 使用容器名ping另一主机容器: 不通

ping 192.168.15.101 本主机ping本主机容器: 不通

ping 192.168.15.201 本主机ping另一主机容器: 通

主机2上测试获取相同结果.

搭建过程2-使用vlan

1. 创建vlan

使用命令vconfig add eth0 100 创建eth0.100的vlan.设置两台主机的vlan ip分别为192.168.100.50/192.168.100.51

2. 创建docker macvlan网络

分别在两台主机上使用命令 docker network create -d macvlan --subnet=192.168.100.0/24 --gateway=192.168.100.1 -o parent=eth0.100 -o macvlan_mode=bridge 100_1 创建相同的100_1的macvlan网络.

3. 创建容器

主机1 运行容器 使用命令:

docker run --net=100_1 --ip=192.168.100.101 -id --name test100.101 busybox sh

docker run --net=100_1 --ip=192.168.100.102 -id --name test100.102 busybox sh

主机2 运行容器 使用命令:

docker run --net=100_1 --ip=192.168.100.201 -id --name test100.201 busybox sh

docker run --net=100_1 --ip=192.168.100.202 -id --name test100.202 busybox sh

4. 测试网络

主机1上测试:

运行命令:

docker exec test100.101 ping 192.168.100.1 ping网关: 不通

docker exec test100.101 ping 192.168.100.50 ping本地eth0.100地址: 不通

docker exec test100.101 ping 192.168.100.51 ping另一个主机的eth0.100地址:通

docker exec test100.101 ping 192.168.100.102 ping本主机容器: 通

docker exec test100.101 ping 192.168.100.201 ping另一主机容器: 通

docker exec test100.101 ping test100.102 使用容器名ping本主机容器: 通

docker exec test100.101 ping test100.201 使用容器名ping另一主机容器: 不通

docker exec test100.101 ping 192.168.15.199 ping跨网络主机: 不通

ping 192.168.100.101 本主机ping本主机容器: 不通

ping 192.168.100.201 本主机ping另一主机容器: 通

主机2测试相同

一些问题

macvlan网络在创建时要指定parent.其中parent仅能使用一次,即eth0在创建一个macvlan网络时使用了,则在创建另一个的时候就无法再使用了.

在创建macvlan的时候,如果不指定网段,默认网段为172.18.0.0/16, 此时加入此网络的容器,在同一台主机上时可以互相ping通,无法ping通外部网络,同时加入此网络的容器,创建时不能手工指定ip,仅能使用ipam自动分配.

在使用和主机相同网段的macvlan时,如果在创建容器时不指定ip,则默认ipam从192.168.15.2开始分配,不检查网段内是否已有相同ip,这种情况下会造成容器ip和网络内其他设备ip冲突.具体情况可看如下:

root@ubuntu:~# arp -a 192.168.15.5
bogon (192.168.15.5) at 00:17:61:12:0b:d1 [ether] on eth0
root@ubuntu:~#
root@ubuntu:~#
root@ubuntu:~# arp -a 192.168.15.5
bogon (192.168.15.5) at 02:42:c0:a8:0f:05 [ether] on eth0

如上是在创建的容器对网关ping之前和ping之后查看的arp,可以看到发生了ip冲突

相关链接

http://t.cn/RXYNXqK
http://t.cn/RXQU43D
http://hustcat.github.io/docker-macvlan/

(转) docker跨主机 macvlan 网络配置的更多相关文章

  1. Docker跨主机网络——overlay

    前言 在Docker网络--单host网络一文中,我为大家总结了Docker的单机网络相关知识和操作,单机网络比较容易.本文我为大家总结Docker跨主机通信相关知识.同样本文大部分内容以CloudM ...

  2. Docker 跨主机网络方案分析

    PS:文章首发公众号,欢迎大家关注我的公众号:aCloudDeveloper,专注技术分享,努力打造干货分享平台,二维码在文末可以扫,谢谢大家. 上篇文章介绍了容器网络的单主机网络,本文将进一步介绍多 ...

  3. Docker跨主机网络实践

    Docker使用中网络管理是最麻烦的,在项目初始化前期就需要进行合理的规划,如果在比较理想的单主机的网络通信是比较简单的,但如果涉及到跨主机的网络就需要使用docker自带的overlay netwo ...

  4. 跨 Docker 宿主机 macvlan 类型

    跨 Docker 宿主机 macvlan 类型 前言 a. 本文主要为 Docker的视频教程 笔记. b. 环境为 三台 CentOS 7.0 虚拟机 (Vmware Workstation 15 ...

  5. Docker跨主机通信(九)--技术流ken

    容器网络 在前面的博客中已经详细讲解了几种网络方案: none, host, bridge,user-defined.但是他们只是解决了单个主机间的容器的通信问题,并不能实现多个主机容器之间的通信.本 ...

  6. Docker跨主机通信(九)

    容器网络 在前面的博客中已经详细讲解了几种网络方案: none, host, bridge,user-defined.但是他们只是解决了单个主机间的容器的通信问题,并不能实现多个主机容器之间的通信.本 ...

  7. docker 实践十一:docker 跨主机通讯

    在上一篇了解了关于 docker 的网络模型后,本篇就基于上一篇的基础来实现 docker 的跨主机通信. 注:环境为 CentOS7,docker 19.03. 本篇会尝试使用几种不同的方式来实现跨 ...

  8. DOCKER学习_005:Flannel网络配置

    一 简介 Flannel是一种基于overlay网络的跨主机容器网络解决方案,也就是将TCP数据包封装在另一种网络包里面进行路由转发和通信, Flannel是CoreOS开发,专门用于docker多机 ...

  9. 基于Ceph分布式集群实现docker跨主机共享数据卷

    上篇文章介绍了如何使用docker部署Ceph分布式存储集群,本篇在此基础之上,介绍如何基于Ceph分布式存储集群实现docker跨主机共享数据卷. 1.环境准备 在原来的环境基础之上,新增一台cen ...

随机推荐

  1. SpringInAction--Bean的作用域

    Spring定义了多种作用域,我们在使用的时候可以根据使用的需求来选择对应的作用域,这些作用域,包括(第二个括号中为更安全的注解方法,具体更多参数可查看接口代码) 单例(Singleton)(Conf ...

  2. 2018-2019-2 《网络对抗技术》Exp2 后门原理与应用 20165210

    2018-2019-2 <网络对抗技术>Exp2 后门原理与应用 20165210 实验内容: 使用netcat获取主机操作Shell,cron启动. 使用Socat获取主机操作Shell ...

  3. Spring报错: org.springframework.beans.factory.support.BeanDefinitionValidationException: Couldn't find an init method named 'init' on bean with name 'car'(待解答)

    在Spring工程里,有一个Car类的bean,Main.java主程序,MyBeanPostProcessor.java是Bean后置处理器. 文件目录结构如下: Car.java package ...

  4. reactNative 的一些学习

    手把手视频 学习资料大全 入门系列

  5. win7下pyhton3.6创建django2的pycharm项目

    1.进入python虚拟环境: workon workon oneenv 2. 在虚拟环境中安装django,也可以使用pycharm上的自动安装,但那个比较慢,所有还是在cmd中安装的好 pip i ...

  6. Kotlin Reference (五) Packages

    most from reference 包 源文件可以从包声明开始: package foo.bar fun baz() {} class Goo {} // ... 源文件的所有内容(如类和函数)都 ...

  7. 特征金字塔网络 FPN

    一. 提出背景 论文:Feature Pyramid Networks for Object Detection  [点击下载] 在传统的图像处理方法中,金字塔是比较常用的一种手段,像 SIFT 基于 ...

  8. 简单说说什么是Restful

    在确定要把自己的服务创建成RESTFUL之前,要明白什么样的服务什么是RESTFUL service(https://en.wikipedia.org/wiki/Representational_st ...

  9. Failed to resolve: com.android.support:appcompat-v7:27.+ 报错解决方法

    最近在学习Android方面的编程,这个过程中出现了许多的错误,其中最多的错误是出现在构建工具进行编译的时候.这里分析一个出现的错误,Failed to resolve: com.android.su ...

  10. HDU - 5887:Herbs Gathering (map优化超大背包)

    Collecting one's own plants for use as herbal medicines is perhaps one of the most self-empowering t ...