Docker:macvlan實現容器跨主機通信 [十四]


一、什么是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地址不會報錯,導致第三台訪問有問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM