?
This document uses PHP Chinese website manual Release
Libnetwork 使用戶可以完全控制 IPv4 和I Pv6 尋址。VLAN 驅動程序建立在此基礎之上,為運營商提供對感興趣進行底層網(wǎng)絡集成的用戶的二層 VLAN 標記的完全控制。對于抽象掉物理約束的重疊部署,請參閱多主機重疊驅動程序。
Macvlan 是經(jīng)過驗證的真正網(wǎng)絡虛擬化技術的新轉折點。Linux實施非常輕便,因為它們不是使用傳統(tǒng)的 Linux橋接進行隔離,而是與 Linux 以太網(wǎng)接口或子接口關聯(lián),以實現(xiàn)網(wǎng)絡間的分離和與物理網(wǎng)絡的連接。
Macvlan 提供了許多獨特的功能,并為各種模式的進一步創(chuàng)新提供了充足的空間。這些方法的兩個高級優(yōu)勢是,繞過 Linux 橋接的積極性能影響以及移動部件少的簡單性。移除 Docker 主機 NIC 和容器接口之間傳統(tǒng)上的橋接會留下一個非常簡單的設置,由容器接口組成,直接連接到 Docker 主機接口。由于在這些場景中沒有端口映射,因此該結果易于訪問面向外部的服務。
這個頁面上的例子都是使用 Docker 1.12.0+的單個主機和設置
所有示例都可以在運行 Docker 的單個主機上執(zhí)行。任何使用子接口的例子eth0.10
都可以替換eth0
為 Docker 主機上的任何其他有效的父接口。動態(tài).
創(chuàng)建子接口。-o parent
接口也可以docker network create
全部排除在外,驅動程序將創(chuàng)建一個dummy
接口,使本地主機連接性能夠執(zhí)行示例。
內(nèi)核要求:
- To check your current kernel version, use `uname -r` to display your kernel version- Macvlan Linux kernel v3.9–3.19 and 4.0+
Macvlan Bridge 模式每個容器都有一個唯一的 MAC 地址,用于跟蹤 Docker 主機的 MAC 映射到端口映射。
Macvlan 驅動程序網(wǎng)絡連接到父 Docker主機接口。例如物理接口,如eth0
用于 802.1q VLAN標記的子接口eth0.10
(.10
代表VLAN 10
),甚至包含將兩個以太網(wǎng)接口捆綁為單個邏輯接口的捆綁主機適配器。
指定的網(wǎng)關在網(wǎng)絡基礎結構提供的主機外部。
每個 Macvlan Bridge 模式 Docker 網(wǎng)絡彼此隔離,一次只能有一個網(wǎng)絡連接到父接口。Docker網(wǎng)絡可以連接的每個主機適配器有4,094個子接口的理論限制。
同一子網(wǎng)內(nèi)的任何容器都可以在沒有網(wǎng)關的情況下與同一網(wǎng)絡中的任何其他容器通話macvlan bridge
。
相同的docker network
命令適用于vlan驅動程序。
在 Macvlan 模式下,不通過兩個網(wǎng)絡/子網(wǎng)之間的外部流程路由,單獨網(wǎng)絡上的容器無法到達彼此。這也適用于同一個碼頭網(wǎng)絡中的多個子網(wǎng)。
在以下示例中,eth0
泊塢窗主機在172.16.86.0/24
網(wǎng)絡上有一個IP,并且有一個默認網(wǎng)關172.16.86.1
。網(wǎng)關是一個地址為的外部路由器172.16.86.1
。eth0
在bridge
模式下,Docker 主機接口上不需要IP地址,只需將其置于正確的上游網(wǎng)絡中即可通過網(wǎng)絡交換機或網(wǎng)絡路由器轉發(fā)。
注意:對于 Macvlan 橋接模式,子網(wǎng)值需要與 Docker 主機的 NIC 接口相匹配。例如,使用該
-o parent=
選項指定的 Docker 主機以太網(wǎng)接口的相同子網(wǎng)和網(wǎng)關。
此示例中使用的父接口是eth0
并且位于子網(wǎng)上172.16.86.0/24
。docker network
意志中的容器也需要與父代在同一個子網(wǎng)上-o parent=
。網(wǎng)關是網(wǎng)絡上的外部路由器,不是任何ip偽裝或任何其他本地代理。
驅動程序是用-d driver_name
選項指定的。在這種情況下-d macvlan
父接口-o parent=eth0
配置如下:
ip addr show eth03: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 inet 172.16.86.250/24 brd 172.16.86.255 scope global eth0
創(chuàng)建 macvlan 網(wǎng)絡并運行連接到它的幾個容器:
# Macvlan (-o macvlan_mode= Defaults to Bridge mode if not specified)docker network create -d macvlan \ --subnet=172.16.86.0/24 \ --gateway=172.16.86.1 \ -o parent=eth0 pub_net # Run a container on the new network specifying the --ip address.docker run --net=pub_net --ip=172.16.86.10 -itd alpine /bin/sh # Start a second container and ping the first docker run --net=pub_net -it --rm alpine /bin/sh ping -c 4 172.16.86.10
看看容器IP和路由表:
ip a show eth0 eth0@if3: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UNKNOWN link/ether 46:b2:6b:26:2f:69 brd ff:ff:ff:ff:ff:ff inet 172.16.86.2/24 scope global eth0 ip route default via 172.16.86.1 dev eth0 172.16.86.0/24 dev eth0 src 172.16.86.2# NOTE: the containers can NOT ping the underlying host interfaces as# they are intentionally filtered by Linux for additional isolation.# In this case the containers cannot ping the -o parent=172.16.86.250
您可以明確指定bridge
模式選項-o macvlan_mode=bridge
。這是默認設置,因此將以bridge
任何一種方式進行。
雖然eth0
接口不需要在 Macvlan Bridge 中擁有 IP 地址,但在接口上使用 IP 地址并不罕見。通過使用該--aux-address=x.x.x.x
標志,可以將地址從默認的內(nèi)置 IPAM 中獲取地址。這會將指定的地址黑名單分發(fā)給容器。上面的同一網(wǎng)絡示例阻止將-o parent=eth0
地址分發(fā)到容器。
docker network create -d macvlan \ --subnet=172.16.86.0/24 \ --gateway=172.16.86.1 \ --aux-address="exclude_host=172.16.86.250" \ -o parent=eth0 pub_net
在由默認的 Docker IPAM 驅動程序提供的網(wǎng)絡中選擇子池IP地址的另一個選項是使用--ip-range=
。這將指定驅動程序從此池中分配容器地址,而不是--subnet=
從網(wǎng)絡創(chuàng)建的參數(shù)中分配更廣泛的范圍,如以下示例中所示,該示例將分配192.168.32.128
從此處開始并向上遞增的地址。
docker network create -d macvlan \ --subnet=192.168.32.0/24 \ --ip-range=192.168.32.128/25 \ --gateway=192.168.32.254 \ -o parent=eth0 macnet32 # Start a container and verify the address is 192.168.32.128docker run --net=macnet32 -it --rm alpine /bin/sh
網(wǎng)絡可以通過以下方式刪除:
docker network rm <network_name or id>
注意:在 Macvlan 中,您無法使用默認名稱空間IP地址進行 ping 或通信。例如,如果您創(chuàng)建一個容器,并嘗試 ping 多克爾主機
eth0
將無法正常工作。該流量被內(nèi)核模塊明確過濾以提供額外的提供者隔離和安全性。
有關 Docker 網(wǎng)絡命令的更多信息,請參閱使用 Docker 網(wǎng)絡命令
VLAN(虛擬局域網(wǎng))長期以來一直是虛擬化數(shù)據(jù)中心網(wǎng)絡的主要手段,并且現(xiàn)在仍然在幾乎所有現(xiàn)有網(wǎng)絡中。VLAN 通過標記第2層隔離域,并使用從1-4094范圍內(nèi)插入到包頭中的12位標識符來實現(xiàn),該包頭允許對
IPv4 和 IPv6 的單個或多個子網(wǎng)進行邏輯分組。這是很常見的網(wǎng)絡運營商使用基于子網(wǎng)(或多個)功能或安全簡檔,如 VLAN 分隔交通web
,db
或任何其它隔離的需要。
計算主機要求在主機上同時運行多個虛擬網(wǎng)絡是非常普遍的。長期以來,Linux 網(wǎng)絡支持 VLAN 標記(也稱為802.1q),用于維護網(wǎng)絡之間的數(shù)據(jù)通路隔離。連接到 Docker 主機的以太網(wǎng)鏈路可以配置為支持802.1q VLAN ID,方法是創(chuàng)建 Linux 子接口,每個子接口專用于一個唯一的 VLAN ID。
將 802.1q 中繼到 Linux 主機對于許多操作來說非常痛苦。它需要更改配置文件才能在重新啟動后保持不變。如果涉及網(wǎng)橋,則需要將物理網(wǎng)卡移入網(wǎng)橋,然后網(wǎng)橋獲取IP地址。這導致了許多擱淺的服務器,因為在該復雜過程中切斷訪問的風險很高。
像所有的 Docker 網(wǎng)絡驅動程序一樣,總體目標是減輕管理網(wǎng)絡資源的操作痛苦。為此,當網(wǎng)絡接收到不存在的父接口的子接口時,驅動程序會在創(chuàng)建網(wǎng)絡時創(chuàng)建帶 VLAN 標記的接口。
在主機重啟的情況下,當 Docker 守護進程重新啟動時,驅動程序將重新創(chuàng)建所有網(wǎng)絡鏈接,而不需要修改經(jīng)常復雜的網(wǎng)絡配置文件。驅動程序會跟蹤它是否創(chuàng)建了最初使用網(wǎng)絡創(chuàng)建的 VLAN 標記的子接口,并且只會在重新啟動后重新創(chuàng)建子接口,或者docker network rm
如果首先使用鏈接創(chuàng)建鏈接,則會刪除該鏈接docker network create
。
如果用戶不想讓 Docker 修改-o parent
子接口,用戶只需要傳遞一個已經(jīng)存在的現(xiàn)有鏈接作為父接口。父接口eth0
不會被刪除,只有不是主鏈接的子接口。
驅動程序需要添加/刪除 vlan子接口interface_name.vlan_tag
。
例如:eth0.50
表示與vlan id標記eth0
的從站的父接口。等效的命令是。eth0.5050ip linkip link add link eth0 name eth0.50 type vlan id 50
Vlan ID 50
在由 Docker 主機標記和隔離的第一個網(wǎng)絡中,eth0.50
父節(jié)點是使用50
指定的 vlan id 標記的-o parent=eth0.50
??梢允褂闷渌袷?,但需要使用ip link
或 Linux 配置文件手動添加和刪除鏈接。只要-o parent
存在任何可以使用的東西,只要符合 Linux netlink。
# now add networks and hosts as you would normally by attaching to the master (sub)interface that is tagged docker network create -d macvlan \ --subnet=192.168.50.0/24 \ --gateway=192.168.50.1 \ -o parent=eth0.50 macvlan50 # In two separate terminals, start a Docker container and the containers can now ping one another.docker run --net=macvlan50 -it --name macvlan_test5 --rm alpine /bin/sh docker run --net=macvlan50 -it --name macvlan_test6 --rm alpine /bin/sh
Vlan ID 60
在第二個網(wǎng)絡中,由 Docker 主機進行標記和隔離,eth0.60
是使用60
指定的vlan id標記的父接口-o parent=eth0.60
。該macvlan_mode=
默認macvlan_mode=bridge
。它也可以顯式設置,結果與下一個示例中顯示的結果相同。
# now add networks and hosts as you would normally by attaching to the master (sub)interface that is tagged.docker network create -d macvlan \ --subnet=192.168.60.0/24 \ --gateway=192.168.60.1 \ -o parent=eth0.60 \ -o macvlan_mode=bridge macvlan60 # In two separate terminals, start a Docker container and the containers can now ping one another.docker run --net=macvlan60 -it --name macvlan_test7 --rm alpine /bin/sh docker run --net=macvlan60 -it --name macvlan_test8 --rm alpine /bin/sh
Example: Multi-Subnet Macvlan 802.1q Trunking
與之前的示例相同,除了綁定到網(wǎng)絡的附加子網(wǎng)用戶可以選擇放置容器。在MacVlan / Bridge模式下,除非在兩個子網(wǎng)之間有路由流量的外部路由器(回答ARP等),否則容器只能在相同的子網(wǎng)/廣播域上互相ping通。
### Create multiple L2 subnets docker network create -d ipvlan \ --subnet=192.168.210.0/24 \ --subnet=192.168.212.0/24 \ --gateway=192.168.210.254 \ --gateway=192.168.212.254 \ -o ipvlan_mode=l2 ipvlan210 # Test 192.168.210.0/24 connectivity between containers docker run --net=ipvlan210 --ip=192.168.210.10 -itd alpine /bin/sh docker run --net=ipvlan210 --ip=192.168.210.9 -it --rm alpine ping -c 2 192.168.210.10# Test 192.168.212.0/24 connectivity between containers docker run --net=ipvlan210 --ip=192.168.212.10 -itd alpine /bin/sh docker run --net=ipvlan210 --ip=192.168.212.9 -it --rm alpine ping -c 2 192.168.212.10
例如: Macvlan Bridge模式,802.1q中繼,VLAN ID:218,多子網(wǎng),雙協(xié)議棧
# Create multiple bridge subnets with a gateway of x.x.x.1:docker network create -d macvlan \ --subnet=192.168.216.0/24 --subnet=192.168.218.0/24 \ --gateway=192.168.216.1 --gateway=192.168.218.1 \ --subnet=2001:db8:abc8::/64 --gateway=2001:db8:abc8::10 \ -o parent=eth0.218 \ -o macvlan_mode=bridge macvlan216 # Start a container on the first subnet 192.168.216.0/24docker run --net=macvlan216 --name=macnet216_test --ip=192.168.216.10 -itd alpine /bin/sh # Start a container on the second subnet 192.168.218.0/24docker run --net=macvlan216 --name=macnet216_test --ip=192.168.218.10 -itd alpine /bin/sh # Ping the first container started on the 192.168.216.0/24 subnet docker run --net=macvlan216 --ip=192.168.216.11 -it --rm alpine /bin/sh ping 192.168.216.10# Ping the first container started on the 192.168.218.0/24 subnet docker run --net=macvlan216 --ip=192.168.218.11 -it --rm alpine /bin/sh ping 192.168.218.10
查看其中一個容器的詳細信息:
docker run --net=macvlan216 --ip=192.168.216.11 -it --rm alpine /bin/sh root@526f3060d759:/# ip a show eth0 eth0@if92: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default link/ether 8e:9a:99:25:b6:16 brd ff:ff:ff:ff:ff:ff inet 192.168.216.11/24 scope global eth0 valid_lft forever preferred_lft forever inet6 2001:db8:abc4::8c9a:99ff:fe25:b616/64 scope link tentative valid_lft forever preferred_lft forever inet6 2001:db8:abc8::2/64 scope link nodad valid_lft forever preferred_lft forever # Specified v4 gateway of 192.168.216.1root@526f3060d759:/# ip route default via 192.168.216.1 dev eth0 192.168.216.0/24 dev eth0 proto kernel scope link src 192.168.216.11# Specified v6 gateway of 2001:db8:abc8::10root@526f3060d759:/# ip -6 route 2001:db8:abc4::/64 dev eth0 proto kernel metric 256 2001:db8:abc8::/64 dev eth0 proto kernel metric 256 default via 2001:db8:abc8::10 dev eth0 metric 1024