您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“docker中網(wǎng)絡(luò)插件flannel怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“docker中網(wǎng)絡(luò)插件flannel怎么用”這篇文章吧。
跨節(jié)點(diǎn)通訊,需要通過(guò)NAT,即需要做源地址轉(zhuǎn)換。
k8s網(wǎng)絡(luò)通信:
1) 容器間通信:同一個(gè)pod內(nèi)的多個(gè)容器間的通信,通過(guò)lo即可實(shí)現(xiàn);
2) pod之間的通信,pod ip <---> pod ip,pod和pod之間要不經(jīng)過(guò)任何轉(zhuǎn)換即可通信;
3) pod和service通信:pod ip <----> cluster ip(即service ip)<---->pod ip,他們通過(guò)iptables或ipvs實(shí)現(xiàn)通信,另外大家要注意ipvs取代不了iptables,因?yàn)閕pvs只能做負(fù)載均衡,而做不了nat轉(zhuǎn)換;
4) Service與集群外部客戶(hù)端的通信
[root@master pki]# kubectl get configmap -n kube-system NAME DATA AGE coredns 1 22d extension-apiserver-authentication 6 22d kube-flannel-cfg 2 22d kube-proxy 2 22d kubeadm-config 1 22d kubelet-config-1.11 1 22d kubernetes-dashboard-settings 1 9h
[root@master pki]# kubectl get configmap kube-proxy -o yaml -n kube-system mode: ""
看到mode是空的,我們把它改為ipvs就可以了。
k8s要靠CNI接口接入其他插件來(lái)實(shí)現(xiàn)網(wǎng)絡(luò)通訊。目前比較流行的插件有flannet,callco,canel,kube-router。
這些插件使用的解決方案都如下:
1)虛擬網(wǎng)橋,虛擬網(wǎng)卡,多個(gè)容器共用一個(gè)虛擬網(wǎng)卡進(jìn)行通信;
2)多路復(fù)用:MacVLAN,多個(gè)容器共用一個(gè)物理網(wǎng)卡進(jìn)行通信;
3)硬件交換:SR-LOV,一個(gè)物理網(wǎng)卡可以虛擬出多個(gè)接口,這個(gè)性能最好。
CNI插件存放位置
[root@master ~]# cat /etc/cni/net.d/10-flannel.conflist { "name": "cbr0", "plugins": [ { "type": "flannel", "delegate": { "hairpinMode": true, "isDefaultGateway": true } }, { "type": "portmap", "capabilities": { "portMappings": true } } ] }
flanel只支持網(wǎng)絡(luò)通訊,但是不支持網(wǎng)絡(luò)策略。
callco網(wǎng)絡(luò)通訊和網(wǎng)絡(luò)策略都支持。
canel:flanel+callco合起來(lái)的功能。
我們可以部署flanel提供網(wǎng)絡(luò)通訊,再部署一個(gè)callco只提供網(wǎng)絡(luò)策略。而不用canel。
mtu:是指一種通信協(xié)議的某一層上面所能通過(guò)的最大數(shù)據(jù)包大小。
[root@master ~]# ifconfig cni0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450 inet 10.244.0.1 netmask 255.255.255.0 broadcast 0.0.0.0 inet6 fe80::4097:d5ff:fe28:6b64 prefixlen 64 scopeid 0x20<link> ether 0a:58:0a:f4:00:01 txqueuelen 1000 (Ethernet) RX packets 1609844 bytes 116093191 (110.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1632952 bytes 577989701 (551.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:83:f8:b8:ff txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.16.1.100 netmask 255.255.255.0 broadcast 172.16.1.255 inet6 fe80::9cf3:d9de:59f:c320 prefixlen 64 scopeid 0x20<link> inet6 fe80::5707:6115:267b:bff5 prefixlen 64 scopeid 0x20<link> inet6 fe80::e34:f952:2859:4c69 prefixlen 64 scopeid 0x20<link> ether 00:50:56:a2:4e:cb txqueuelen 1000 (Ethernet) RX packets 5250378 bytes 704067861 (671.4 MiB) RX errors 139 dropped 190 overruns 0 frame 0 TX packets 4988169 bytes 4151179300 (3.8 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450 inet 10.244.0.0 netmask 255.255.255.255 broadcast 0.0.0.0 inet6 fe80::a82c:bcff:fef8:895c prefixlen 64 scopeid 0x20<link> ether aa:2c:bc:f8:89:5c txqueuelen 0 (Ethernet) RX packets 51 bytes 3491 (3.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 53 bytes 5378 (5.2 KiB) TX errors 0 dropped 10 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 59118846 bytes 15473986573 (14.4 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 59118846 bytes 15473986573 (14.4 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 veth7ec94aab: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450 inet6 fe80::487d:5bff:fef7:484d prefixlen 64 scopeid 0x20<link> ether 4a:7d:5b:f7:48:4d txqueuelen 0 (Ethernet) RX packets 88112 bytes 19831802 (18.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 105718 bytes 13343894 (12.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 vethf703483a: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450 inet6 fe80::b06a:eaff:fec3:33a8 prefixlen 64 scopeid 0x20<link> ether b2:6a:ea:c3:33:a8 txqueuelen 0 (Ethernet) RX packets 760882 bytes 59400960 (56.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 763263 bytes 282299805 (269.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 vethff579703: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450 inet6 fe80::d82f:37ff:fe9a:b6d0 prefixlen 64 scopeid 0x20<link> ether da:2f:37:9a:b6:d0 txqueuelen 0 (Ethernet) RX packets 760850 bytes 59398245 (56.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 764016 bytes 282349248 (269.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
通過(guò)ifconfig命令,我們可以看到flannel.1的地址是10.244.0.0,子網(wǎng)掩碼是255.255.255.255,mtu是1450,mtu要留出一部分做封裝疊加,額外開(kāi)銷(xiāo)使用。
cni0只有在pod運(yùn)行時(shí)才會(huì)出現(xiàn)。
兩個(gè)節(jié)點(diǎn)上的pod可以借助flannel隧道進(jìn)行通信。默認(rèn)使用的VxLAN協(xié)議,因?yàn)樗蓄~外開(kāi)銷(xiāo),所以性能有點(diǎn)低。
flannel第二種協(xié)議叫host-gw(host gateway),即Node節(jié)點(diǎn)把自己的網(wǎng)絡(luò)接口當(dāng)做pod的網(wǎng)關(guān)使用,從而使不同節(jié)點(diǎn)上的node進(jìn)行通信,這個(gè)性能比VxLAN高,因?yàn)樗鼪](méi)有額外開(kāi)銷(xiāo)。不過(guò)他有個(gè)缺點(diǎn),就是各node節(jié)點(diǎn)必須在同一個(gè)網(wǎng)段中。
另外,如果兩個(gè)pod所在節(jié)點(diǎn)在同一個(gè)網(wǎng)段中,可以讓VxLAN也支持host-gw的功能,即直接通過(guò)物理網(wǎng)卡的網(wǎng)關(guān)路由轉(zhuǎn)發(fā),而不用隧道flannel疊加,從而提高了VxLAN的性能,這種flannel的功能叫directrouting。
[root@master ~]# kubectl get daemonset -n kube-system NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE kube-flannel-ds-amd64 3 3 3 3 3 beta.kubernetes.io/arch=amd64 22d
[root@master ~]# kubectl get pods -n kube-system -o wide NAME READY STATUS RESTARTS AGE IP NODE kube-flannel-ds-amd64-6zqzr 1/1 Running 8 22d 172.16.1.100 master kube-flannel-ds-amd64-7qtcl 1/1 Running 7 22d 172.16.1.101 node1 kube-flannel-ds-amd64-kpctn 1/1 Running 6 22d 172.16.1.102 node2
看到flannel是以pod的daemonset控制器形式運(yùn)行的(其實(shí)flannel還可以以守護(hù)進(jìn)程的方式運(yùn)行)。
[root@master ~]# kubectl get configmap -n kube-system NAME DATA AGE kube-flannel-cfg 2 22d
[root@master ~]#kubectl get configmap -n kube-system kube-flannel-cfg -o json -n kube-system \\\"10.244.0.0/16\\\",\\n \\\"Backend\\\": {\\n \\\"Type\\\": \\\"vxlan\
flannel的配置參數(shù):
1、network:flannel使用的CIDR格式的網(wǎng)絡(luò)地址,用于為pod配置網(wǎng)絡(luò)功能。
1)10.244.0.0/16--->
master: 10.244.0.0./24
node01: 10.244.1.0/24
....
node255: 10.244.255.0/24
可以支持255個(gè)節(jié)點(diǎn)
2)10.0.0.0/8
10.0.0.0/24
...
10.255.255.0/24
可以支持6萬(wàn)多個(gè)節(jié)點(diǎn)
2、SubnetLen:把network切分為子網(wǎng)供各節(jié)點(diǎn)使用時(shí),使用多長(zhǎng)的掩碼進(jìn)行切分,默認(rèn)為24位;
3、SubnetMin:指明子網(wǎng)中的地址段最小多少可以分給子網(wǎng)使用,比如可以限制10.244.10.0/24,這樣0~9就不讓用;
4、SubnetMax:表示最多使用多少個(gè),比如10.244.100.0/24
5、Backend:
Vxlan,host-gw,udp(最慢)
flannel
支持多種后端
Vxlan
1.valan
2.Dirextrouting
host-gw:Host Gateway #不推薦,只能在二層網(wǎng)絡(luò)中,不支持跨網(wǎng)絡(luò),如果有成千上萬(wàn)的Pod,容易產(chǎn)生廣播風(fēng)暴
UDP:性能差
[root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE myapp-deploy-69b47bc96d-79fqh 1/1 Running 4 7d 10.244.1.97 node1 myapp-deploy-69b47bc96d-tc54k 1/1 Running 4 7d 10.244.2.88 node2
[root@master ~]# kubectl exec -it myapp-deploy-69b47bc96d-79fqh -- /bin/sh / # ping 10.244.2.88 #ping對(duì)方Node上容器的ip PING 10.244.2.88 (10.244.2.88): 56 data bytes 64 bytes from 10.244.2.88: seq=0 ttl=62 time=0.459 ms 64 bytes from 10.244.2.88: seq=0 ttl=62 time=0.377 ms 64 bytes from 10.244.2.88: seq=1 ttl=62 time=0.252 ms 64 bytes from 10.244.2.88: seq=2 ttl=62 time=0.261 ms
在其他節(jié)點(diǎn)上抓包,發(fā)現(xiàn)根本就在ens192上抓不到包。
[root@master ~]# tcpdump -i ens192 -nn icmp
[root@master ~]# yum install bridge-utils -y
[root@master ~]# brctl show docker0 bridge namebridge idSTP enabledinterfaces docker08000.024283f8b8ffno
[root@master ~]# brctl show cni0 bridge namebridge idSTP enabledinterfaces cni08000.0a580af40001noveth7ec94aab vethf703483a vethff579703
可以看到veth這些接口都是橋接到cni0上的。
brctl show表示查看已有網(wǎng)橋。
[root@node1 ~]# tcpdump -i cni0 -nn icmp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on cni0, link-type EN10MB (Ethernet), capture size 262144 bytes 23:40:11.370754 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 4864, seq 96, length 64 23:40:11.370988 IP 10.244.2.88 > 10.244.1.97: ICMP echo reply, id 4864, seq 96, length 64 23:40:12.370888 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 4864, seq 97, length 64 23:40:12.371090 IP 10.244.2.88 > 10.244.1.97: ICMP echo reply, id 4864, seq 97, length 64 ^X23:40:13.371015 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 4864, seq 98, length 64 23:40:13.371239 IP 10.244.2.88 > 10.244.1.97: ICMP echo reply, id 4864, seq 98, length 64 23:40:14.371128 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 4864, seq 99, length 64
可以看到,在node節(jié)點(diǎn),可以在cni0端口上抓到容器里面的Ping時(shí)的包。
其實(shí),上面ping時(shí)的數(shù)據(jù)流是先從cni0進(jìn)來(lái),然后從flannel.1出去,最后借助物理網(wǎng)卡ens32發(fā)出去。所以,我們?cè)趂lannel.1上也能抓到包:
[root@node1 ~]# tcpdump -i flannel.1 -nn icmp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on flannel.1, link-type EN10MB (Ethernet), capture size 262144 bytes 03:12:36.823315 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 4864, seq 12840, length 64 03:12:36.823496 IP 10.244.2.88 > 10.244.1.97: ICMP echo reply, id 4864, seq 12840, length 64 03:12:37.823490 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 4864, seq 12841, length 64 03:12:37.823634 IP 10.244.2.88 > 10.244.1.97: ICMP echo reply, id 4864, seq 12841, length 64
同樣,在ens192物理網(wǎng)卡上也能抓到包:
[root@node1 ~]# tcpdump -i ens192 -nn host 172.16.1.102 #172.16.1.102是node2的物理ip tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens192, link-type EN10MB (Ethernet), capture size 262144 bytes 10:59:24.234174 IP 172.16.1.101.60617 > 172.16.1.102.8472: OTV, flags [I] (0x08), overlay 0, instance 1 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 7168, seq 0, length 64 10:59:24.234434 IP 172.16.1.102.54894 > 172.16.1.101.8472: OTV, flags [I] (0x08), overlay 0, instance 1 IP 10.244.2.88 > 10.244.1.97: ICMP echo reply, id 7168, seq 0, length 64 10:59:25.234301 IP 172.16.1.101.60617 > 172.16.1.102.8472: OTV, flags [I] (0x08), overlay 0, instance 1 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 7168, seq 1, length 64 10:59:25.234469 IP 172.16.1.102.54894 > 172.16.1.101.8472: OTV, flags [I] (0x08), overlay 0, instance 1 IP 10.244.2.88 > 10.244.1.97: ICMP echo reply, id 7168, seq 1, length 64 10:59:26.234415 IP 172.16.1.101.60617 > 172.16.1.102.8472: OTV, flags [I] (0x08), overlay 0, instance 1 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 7168, seq 2, length 64 10:59:26.234592 IP 172.16.1.102.54894 > 172.16.1.101.8472: OTV, flags [I] (0x08), overlay 0, instance 1 IP 10.244.2.88 > 10.244.1.97: ICMP echo reply, id 7168, seq 2, length 64 10:59:27.234528 IP 172.16.1.101.60617 > 172.16.1.102.8472: OTV, flags [I] (0x08), overlay 0, instance 1 IP 10.244.1.97 > 10.244.2.88: ICMP echo request, id 7168, seq 3, length 64
下面我們把flannel的通信模式改成directrouting的方式
[root@master flannel]# cd /root/manifests/flannel
[root@master flannel]# kubectl edit configmap kube-flannel-cfg -n kube-system
{ "Network": "10.244.0.0/16", "Backend": { "Type": "vxlan", "Directrouting": true #加一行這個(gè) } }
[root@master flannel]# ip route show default via 172.16.1.254 dev ens192 proto static metric 100 10.244.0.0/24 dev cni0 proto kernel scope link src 10.244.0.1 #訪問(wèn)10.244.0.0/24要通過(guò)10.244.0.1 10.244.1.0/24 via 10.244.1.0 dev flannel.1 onlink #10.244.1.0是配置在flannel上的地址,表示訪問(wèn)10.244.1.0/24通過(guò)本機(jī)flannel.1上的10.244.1.0送出去,下同 10.244.2.0/24 via 10.244.2.0 dev flannel.1 onlink #10.244.2.0是配置在flannel上的地址 172.16.1.0/24 dev ens192 proto kernel scope link src 172.16.1.100 metric 100
[root@master flannel]# kubectl get configmap kube-flannel-cfg -o json -n kube-system
"net-conf.json": "{\n \"Network\": \"10.244.0.0/16\",\n \"Backend\": {\n \"Type\": \"vxlan\",\n \"Directrouting\": true\n }\n}\n"
看到有Directrouting,說(shuō)明生效了。
重啟整個(gè)k8s,然后再看:
[root@master ~]# ip route show default via 172.16.1.254 dev ens192 proto static metric 100 10.244.0.0/24 dev cni0 proto kernel scope link src 10.244.0.1 #訪問(wèn)本機(jī)直接在本機(jī)直接轉(zhuǎn)發(fā),而不需要其他接口,這就是directrouting 10.244.1.0/24 via 172.16.1.101 dev ens192 #看到現(xiàn)在訪問(wèn)10.244.1.0,通過(guò)本地物理網(wǎng)卡ens192上的172.16.1.101送出去,即通過(guò)物理網(wǎng)卡通信了,而不再通過(guò)隧道flannel通信。 10.244.2.0/24 via 172.16.1.102 dev ens192 172.16.1.0/24 dev ens192 proto kernel scope link src 172.16.1.100 metric 100 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
繼續(xù)登錄到一個(gè)pod中進(jìn)行ping測(cè)試:
[root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE myapp-deploy-69b47bc96d-75g2b 1/1 Running 0 12m 10.244.1.124 node1 myapp-deploy-69b47bc96d-jwgwm 1/1 Running 0 3s 10.244.2.100 node2
[root@master ~]# kubectl exec -it myapp-deploy-69b47bc96d-75g2b -- /bin/sh / # ping 10.244.2.100 PING 10.244.2.100 (10.244.2.100): 56 data bytes 64 bytes from 10.244.2.100: seq=0 ttl=62 time=0.536 ms 64 bytes from 10.244.2.100: seq=1 ttl=62 time=0.206 ms 64 bytes from 10.244.2.100: seq=2 ttl=62 time=0.206 ms 64 bytes from 10.244.2.100: seq=3 ttl=62 time=0.203 ms 64 bytes from 10.244.2.100: seq=4 ttl=62 time=0.210 ms
[root@node1 ~]# tcpdump -i ens192 -nn icmp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on ens192, link-type EN10MB (Ethernet), capture size 262144 bytes 12:31:10.899403 IP 10.244.1.124 > 10.244.2.100: ICMP echo request, id 8960, seq 24, length 64 12:31:10.899546 IP 10.244.2.100 > 10.244.1.124: ICMP echo reply, id 8960, seq 24, length 64 12:31:11.899505 IP 10.244.1.124 > 10.244.2.100: ICMP echo request, id 8960, seq 25, length 64 12:31:11.899639 IP 10.244.2.100 > 10.244.1.124: ICMP echo reply, id 8960, seq 25, length 64
通過(guò)抓包可以看到,現(xiàn)在在pod中進(jìn)行互ping,是從物理網(wǎng)卡ens192進(jìn)出的,這就是directrouting,這種性能比默認(rèn)vxlan高。
以上是“docker中網(wǎng)絡(luò)插件flannel怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。