(转) docker跨主机 macvlan 网络配置

摘要:
原始文本链接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

原文链接

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 网络配置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Android 让你的 Room 搭上 RxJava 的顺风车 从重复的代码中解脱出来使用 Pandas 的 to_excel() 方法来将多个 csv 文件合并到一个 xlsx 的不同 sheets 内下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

docker环境下Java获取cpu核心数不准确,实际上是宿主机的cpu核心数

docker环境下Java获取cpu核心数异常 一次查询线上BUG时,发现JDK获取cpu核心数不准确,导致程序运行过程中出现了不符合预期的情况,因为在线上docker环境的JDK代码中获取的cpu核心数实际上是docker宿主机的cpu核心数。 于是,网上搜索了一下,确实是存在这个情况的,这么说是因为在后来的jdk版本中,JDK增强了对docker的支持...

docker创建一个容器

而容器技术的核心功能,就是通过约束和修改进程的动态表现,从而为其创造出一个“边界”。 对于 Docker 等大多数 Linux 容器来说,Cgroups 技术是用来制造约束的主要手段,而 Namespace 技术则是用来修改进程视图的主要方法。 你可能会觉得 Cgroups 和 Namespace 这两个概念很抽象, 别担心,接下来我们一起动手实践一下,你...

055、创建macvlan网络 (2019-03-22 周五)

参考https://www.cnblogs.com/CloudMan6/p/7364332.html     创建macvlan网络,需要指定使用哪块物理网卡进行通信   -o parent=ens192   创建macvlan网络,需要指定网络环境已经存在的网关,因为macvlan并不提供网关功能   --gateway 172.16.86.1   因...

k8s部署

环境初始化,所有节点   1.配置hostname hostnamectl set-hostname master hostnamectl set-hostname node      2.配置/etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4....

Docker在Linux上运行NetCore系列(一)配置运行DotNetCore控制台

系列文章:https://www.cnblogs.com/alunchen/p/10121379.html 本篇文章操作系统信息       Linux:ubuntu 16.04.3 amd64 查看NetCore支持的Linux系统       NetCore不是支持Linux的所有系统,只支持部分,所以在Linux上安装NetCore之前要查看系...

用一个实际例子理解Docker volume工作原理

要了解Docker Volume,首先我们需要理解Docker文件系统的工作原理。Docker镜像是由多个文件系统的只读层叠加而成。当一个容器通过命令docker run启动时,Docker会加载只读镜像层并在镜像栈顶部添加一个读写层。如果运行中的容器修改了现有的一个已经存在的文件,那该文件将会从读写层下面的只读层复制到读写层,但是该文件的只读版本依然存在...