一、什么是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地址不会报错,导致第三台访问有问题