Docker:macvlan实现容器跨主机通信 [十四]
2024-10-18 04:59:43
一、什么是macvlan
1、macvlan 本身是 linux kernel 模块,其功能是允许在同一个物理网卡上配置多个 MAC 地址,
2、即多个 interface,每个 interface 可以配置自己的 IP。
3、macvlan 本质上是一种网卡虚拟化技术
二、跨主机通信
1、创建macvlan
1、主机luoahong
[root@luoahong ~]# docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=eth0 macvlan_1
ffbecd2f57ce5be49b95fa05f0a4566f02d6a006d9ab664f44857997d6c0605b
[root@luoahong ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
d766f2526542 bridge bridge local
30b37ec09ff2 host host local
ffbecd2f57ce macvlan_1 macvlan local
ad2616372f01 mywordpress_default bridge local
03062097926d none null local
2、主机luoahong2
[root@luoahong2 ~]# docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=eth0 macvlan_1
6b0642df662e35293727542fea1b6d524f0f564f8162d21283d668a778a1d63f
[root@luoahong2 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
05d195b7d60b bridge bridge local
eb998505004d host host local
6b0642df662e macvlan_1 macvlan local
9bd801b589c5 none null local
2、创建测试容器
1、主机luoahong
[root@luoahong ~]# docker run -it --network macvlan_1 --ip=10.0.0.111 busybox:latest /bin/sh
/ #
[root@luoahong ~]# docker run -it --network macvlan_1 --ip=10.0.0.188 busybox:latest /bin/sh
2、主机luoahong2
[root@luoahong2 ~]# docker run -it --network macvlan_1 --ip=10.0.0.112 busybox:latest /bin/sh
3、在主机luoahong2主机上测试
[root@luoahong2 ~]# docker run -it --network macvlan_1 --ip=10.0.0.112 busybox:latest /bin/sh
/ # ping 10.0.0.188
PING 10.0.0.188 (10.0.0.188): 56 data bytes
64 bytes from 10.0.0.188: seq=0 ttl=64 time=5.945 ms
64 bytes from 10.0.0.188: seq=1 ttl=64 time=0.699 ms
64 bytes from 10.0.0.188: seq=2 ttl=64 time=0.553 ms
64 bytes from 10.0.0.188: seq=3 ttl=64 time=0.611 ms
64 bytes from 10.0.0.188: seq=4 ttl=64 time=0.724 ms
^C
--- 10.0.0.188 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.553/1.706/5.945 ms
/ # ping 10.0.0.111
PING 10.0.0.111 (10.0.0.111): 56 data bytes
64 bytes from 10.0.0.111: seq=0 ttl=64 time=4.076 ms
64 bytes from 10.0.0.111: seq=1 ttl=64 time=0.670 ms
64 bytes from 10.0.0.111: seq=2 ttl=64 time=0.793 ms
^C
--- 10.0.0.111 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.670/1.846/4.076 ms
三、注意事项及优缺点
1、两台容器无法通信解决方案
1、主机名相同会导致此故障
解决方案:注意主机名的唯一性
2、网卡开启混杂模式
[root@luoahong ~]# ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:e7:b9:46 brd ff:ff:ff:ff:ff:ff
[root@luoahong ~]# ip link set eth0 promisc on
[root@luoahong ~]# ip link show eth0
2: eth0: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:e7:b9:46 brd ff:ff:ff:ff:ff:ff
[root@luoahong ~]# ip link set eth0 promisc off
[root@luoahong ~]# ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:e7:b9:46 brd ff:ff:ff:ff:ff:ff
2、优缺点总结
1、优点
1、性能比较好,
2、有centos可以把它当作物理机用 占的资源更少
2、缺点
1、每次ip地址需要手动设置
2、两台主机共同用一个ip地址不会报错,导致第三台访问有问题
最新文章
- 跟我一起云计算(5)——Shards
- Python学习【第三篇】Python变量
- linux下安装使用libuuid(uuid-generate)
- android显示当前时间
- Mysql 死锁相关操作
- 激活Navicat?如何注册Navicat?
- SVN下错误集锦
- Winsock SPI-Socks5-SSL
- 动态添加试题选项按钮 radioButton(一)
- BZOJ 1486: [HNOI2009]最小圈 [01分数规划]
- nginx解决反向代理超时
- 单元测试系列之九:Sonar 常用代码规则整理(一)
- Java 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
- Yii2 组件
- 1013. Pairs of Songs With Total Durations Divisible by 60总持续时间可被 60 整除的歌曲
- Debug 路漫漫-04
- 问题:bower git is not installed or not in the path
- 【洛谷】3375 KMP字符串匹配
- powerpc平台移植zebra或quagga-0.99.23
- NPOI 导出Excel图片 (网络)