使用weave實現跨主機容器連接
Weave是由weaveworks公司開發的解決Docker跨主機網絡的解決方案,它能夠創建一個虛擬網絡,用於連接多臺主機上的Docker容器,這樣容器就像被接入了同一個網絡交換機,那些使用網絡的應用程序不必去配置端口映射和鏈接等信息。
外部設備能夠訪問Weave網絡上的應用程序容器所提供的服務,同時已有的內部系統也能夠暴露到應用程序容器上。Weave能夠穿透防火牆並運行在部分連接的網絡上,另外,Weave的通信支持加密,所以用戶可以從一個不受信任的網絡連接到主機。
Weave會在主機上創建一個網橋,每一個容器通過 veth pair 連接到該網橋上,同時網橋上有個 Weave router 的容器與之連接,該router會通過連接在網橋上的接口來抓取網絡包(該接口工作在Promiscuous模式)。
在每一個部署Docker的主機(可能是物理機也可能是虛擬機)上都部署有一個W(即Weave router),它本身也可以以一個容器的形式部署。Weave run的時候就可以給每個veth的容器端分配一個ip和相應的掩碼。veth的網橋這端就是Weave router容器,並在Weave launch的時候分配好ip和掩碼。
Weave網絡是由這些weave routers組成的對等端點(peer)構成,每個對等的一端都有自己的名字,其中包括一個可讀性好的名字用於表示狀態和日誌的輸出,一個唯一標識符用於運行中相互區別,即使重啓Docker主機名字也保持不變,這些名字默認是mac地址。
每個部署了Weave router的主機都需要將TCP和UDP的6783端口的防火牆設置打開,保證Weave router之間控制面流量和數據面流量的通過。控制面由weave routers之間建立的TCP連接構成,通過它進行握手和拓撲關係信息的交換通信。 這個通信可以被配置爲加密通信。而數據面由Weave routers之間建立的UDP連接構成,這些連接大部分都會加密。這些連接都是全雙工的,並且可以穿越防火牆。
對每一個weave網絡中的容器,weave都會創建一個網橋,並且在網橋和每個容器之間創建一個veth pair,一端作爲容器網卡加入到容器的網絡命名空間中,併爲容器網卡配置ip和相應的掩碼,一端連接在網橋上,最終通過宿主機上weave router將流量轉發到對端主機上。
其基本過程如下:
1,容器流量通過veth pair到達宿主機上weave router網橋上。
2,weave router在混雜模式下使用pcap在網橋上截獲網絡數據包,並排除由內核直接通過網橋轉發的數據流量,例如本子網內部,本地容器之間的數據以及宿主機和本地容器之間的流量。捕獲的包通過UDP轉發到所其他主機的weave router端。
3,在接收端,weave router通過pcap將包注入到網橋上的接口,通過網橋的上的veth pair,將流量分發到容器的網卡上。
weave默認基於UDP承載容器之間的數據包,並且可以完全自定義整個集羣的網絡拓撲,但從性能和使用角度來看,還是有比較大的缺陷的:
weave自定義容器數據包的封包解包方式,不夠通用,傳輸效率比較低,性能上的損失也比較大。
集羣配置比較負載,需要通過weave命令行來手工構建網絡拓撲,在大規模集羣的情況下,加重了管理員的負擔。
Weave優劣勢:
- Weave優勢
- 支持主機間通信加密。
- 支持container動態加入或者剝離網絡。
- 支持跨主機多子網通信。
- Weave劣勢
只能通過weave launch或者weave connect加入weave網絡。
配置操作
- 安裝weave
- 啓動weave(veave launch)
- 連接不同主機
- 通過weave啓動主機
下載安裝weave
[root@localhost docker]# curl -L git.io/weave -o /usr/bin/weave
[root@localhost docker]# chmod a+x /usr/bin/weave
[root@localhost docker]# weave version
weave script 2.3.0
weave 2.3.0
啓動weave,weave默認不會下載對應容器,初次運行時會下載容器。
[root@localhost docker]# weave launch
2.3.0: Pulling from weaveworks/weave
88286f41530e: Pull complete
ad4e50ed2c08: Pull complete
b3f4c952e7c2: Pull complete
5e27cb7f1c2b: Pull complete
f9dfb03c1d7b: Pull complete
Digest: sha256:02914df933ffd52c6daf8c4ced33e48dad36e4d4fd9e684d5004cd72236ced60
Status: Downloaded newer image for weaveworks/weave:2.3.0
latest: Pulling from weaveworks/weavedb
ecb15a45f93b: Pull complete
Digest: sha256:7fac063be31d48980361b7020d39ff91493c0bc6c844314b3e71f447bc8dff39
Status: Downloaded newer image for weaveworks/weavedb:latest
Unable to find image 'weaveworks/weaveexec:2.3.0' locally
2.3.0: Pulling from weaveworks/weaveexec
88286f41530e: Already exists
ad4e50ed2c08: Already exists
b3f4c952e7c2: Already exists
5e27cb7f1c2b: Already exists
f9dfb03c1d7b: Already exists
21771db04786: Pull complete
5fbda086495f: Pull complete
80427f885b22: Pull complete
0c4698905755: Pull complete
Digest: sha256:eb8eb1d83fc58716b20621d397d63737a18f86cbed1fedb1d71671cfc486517b
Status: Downloaded newer image for weaveworks/weaveexec:2.3.0
afe02ac7b3654226a09328c8688c8c3e5b6e4226d600dd6120c955ba90537e54
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
afe02ac7b365 weaveworks/weave:2.3.0 "/home/weave/weave..." 2 minutes ago Up 2 minutes weave
[root@localhost docker]# docker images |grep weave
weaveworks/weavedb latest b5c2d24a7b71 6 weeks ago 282 B
weaveworks/weaveexec 2.3.0 c2030610fb92 6 weeks ago 79.1 MB
weaveworks/weave 2.3.0 6b9ea60b76e7 6 weeks ago 61.7 MB
運行結束後,會生成一個運行的容器和兩個data-only的容器,和三個鏡像
[root@localhost backup]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
afe02ac7b365 weaveworks/weave:2.3.0 "/home/weave/weave..." 13 minutes ago Up 13 minutes weave
c42cc69d7cf2 weaveworks/weaveexec:2.3.0 "data-only" 13 minutes ago Created weavevolumes-2.3.0
7ce56150aa56 weaveworks/weavedb:latest "data-only" 13 minutes ago Created weavedb
其中,weave的這些數據是保存在每臺機器上分配的名爲 weavedb 的容器上的,它是一個 data volume 容器,只負責數據的持久化。
還會生成一個名爲weave的虛擬網絡設備
[root@localhost backup]# ifconfig weave
weave: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1376
inet6 fe80::5cec:96ff:fe30:6abc prefixlen 64 scopeid 0x20<link>
ether 5e:ec:96:30:6a:bc txqueuelen 1000 (Ethernet)
RX packets 14 bytes 920 (920.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 648 (648.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker中也會生成一個使用weave的網橋的自定義網絡。
[root@localhost backup]# docker network ls
NETWORK ID NAME DRIVER SCOPE
f6bdadf61ce7 bridge bridge local
1239bac87642 host host local
c0c6d15cdc49 none null local
37c69c2e8a78 weave weavemesh local
使用weave啓動一個鏡像
weave2.0之後刪除了run命令,所以先使用docker run啓動好容器,然後使用weave attach命令給容器綁定IP地址
配置宿主機1的容器:
[root@localhost system]# docker run -it --name weave_test ubuntu /bin/bash
[root@localhost system]# weave attach 172.25.11.23/24 weave_test
Address 172.25.11.23 overlaps with existing route 172.25.11.0/24 on host
172.25.11.23
[root@localhost system]# docker exec -it weave_test /bin/bash
//可以看到weave添加了一個網絡設備,並綁定了ip地址
root@273bba12b3b9:/# ifconfig ethwe
ethwe Link encap:Ethernet HWaddr fe:11:53:ea:3e:ba
inet addr:172.25.11.23 Bcast:172.25.11.255 Mask:255.255.255.0
inet6 addr: fe80::fc11:53ff:feea:3eba/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1376 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:948 (948.0 B) TX bytes:300 (300.0 B)
配置宿主機2的容器:
[root@localhost volumes]# docker run -it --name weave_another ubuntu /bin/bash
[root@localhost volumes]# weave attach 172.25.11.112/24 weave_another
Address 172.25.11.112 overlaps with existing route 172.25.11.0/24 on host
172.25.11.112
root@44028ad4bdc2:/# ifconfig ethwe
ethwe Link encap:Ethernet HWaddr 86:ba:c8:b9:cc:eb
inet addr:172.25.11.112 Bcast:172.25.11.255 Mask:255.255.255.0
inet6 addr: fe80::84ba:c8ff:feb9:cceb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1376 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:948 (948.0 B) TX bytes:300 (300.0 B)
宿主機1的容器weave_test和宿主機2的容器weave_another可以互相訪問,驗證如下:
//使用宿主機2的容器對宿主機1的容器進行ping訪問
root@44028ad4bdc2:/# ping 172.25.11.23
PING 172.25.11.23 (172.25.11.23) 56(84) bytes of data.
64 bytes from 172.25.11.23: icmp_seq=1 ttl=64 time=0.077 ms
^C
--- 172.25.11.23 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.077/0.077/0.077/0.000 ms
//同樣,使用宿主機1的容器對宿主機2的容器進行ping訪問
root@273bba12b3b9:/# ping 172.25.11.112
PING 172.25.11.112 (172.25.11.112) 56(84) bytes of data.
64 bytes from 172.25.11.112: icmp_seq=1 ttl=64 time=0.072 ms
64 bytes from 172.25.11.112: icmp_seq=2 ttl=64 time=0.086 ms
^C
--- 172.25.11.112 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.072/0.079/0.086/0.007 ms