一、什么是macvlan
1、macvlan 本身是 linux kernel 模块,其功能是允许在同一个物理网卡上配置多个 MAC 地址,
2、即多个 interface,每个 interface 可以配置自己的 IP。
3、macvlan 本质上是一种网卡虚拟化技术
二、跨主机通信
1、创建macvlan
1、主机luoahong
1[root@luoahong ~]# docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=eth0 macvlan_1 2ffbecd2f57ce5be49b95fa05f0a4566f02d6a006d9ab664f44857997d6c0605b 3[root@luoahong ~]# docker network ls 4NETWORK ID NAME DRIVER SCOPE 5d766f2526542 bridge bridge local 630b37ec09ff2 host host local 7ffbecd2f57ce macvlan_1 macvlan local 8ad2616372f01 mywordpress_default bridge local 903062097926d none null local
2、主机luoahong2
1[root@luoahong2 ~]# docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=eth0 macvlan_1 26b0642df662e35293727542fea1b6d524f0f564f8162d21283d668a778a1d63f 3[root@luoahong2 ~]# docker network ls 4NETWORK ID NAME DRIVER SCOPE 505d195b7d60b bridge bridge local 6eb998505004d host host local 76b0642df662e macvlan_1 macvlan local 89bd801b589c5 none null local
2、创建测试容器
1、主机luoahong
1[root@luoahong ~]# docker run -it --network macvlan_1 --ip=10.0.0.111 busybox:latest /bin/sh 2/ # 3[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主机上测试
1[root@luoahong2 ~]# docker run -it --network macvlan_1 --ip=10.0.0.112 busybox:latest /bin/sh 2/ # ping 10.0.0.188 3PING 10.0.0.188 (10.0.0.188): 56 data bytes 464 bytes from 10.0.0.188: seq=0 ttl=64 time=5.945 ms 564 bytes from 10.0.0.188: seq=1 ttl=64 time=0.699 ms 664 bytes from 10.0.0.188: seq=2 ttl=64 time=0.553 ms 764 bytes from 10.0.0.188: seq=3 ttl=64 time=0.611 ms 864 bytes from 10.0.0.188: seq=4 ttl=64 time=0.724 ms 9^C 10--- 10.0.0.188 ping statistics --- 115 packets transmitted, 5 packets received, 0% packet loss 12round-trip min/avg/max = 0.553/1.706/5.945 ms 13/ # ping 10.0.0.111 14PING 10.0.0.111 (10.0.0.111): 56 data bytes 1564 bytes from 10.0.0.111: seq=0 ttl=64 time=4.076 ms 1664 bytes from 10.0.0.111: seq=1 ttl=64 time=0.670 ms 1764 bytes from 10.0.0.111: seq=2 ttl=64 time=0.793 ms 18^C 19--- 10.0.0.111 ping statistics --- 203 packets transmitted, 3 packets received, 0% packet loss 21round-trip min/avg/max = 0.670/1.846/4.076 ms
三、注意事项及优缺点
1、两台容器无法通信解决方案
1、主机名相同会导致此故障
解决方案:注意主机名的唯一性
2、网卡开启混杂模式
1[root@luoahong ~]# ip link show eth0 22: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 3 link/ether 00:0c:29:e7:b9:46 brd ff:ff:ff:ff:ff:ff 4[root@luoahong ~]# ip link set eth0 promisc on 5[root@luoahong ~]# ip link show eth0 62: eth0: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 7 link/ether 00:0c:29:e7:b9:46 brd ff:ff:ff:ff:ff:ff 8[root@luoahong ~]# ip link set eth0 promisc off 9[root@luoahong ~]# ip link show eth0 102: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 11 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地址不会报错,导致第三台访问有问题