解析 | K8S之网络插件exec
K8S支持3種類型的網(wǎng)絡(luò)插件。
今天繼續(xù)介紹下另一種插件exec?
K8S網(wǎng)絡(luò)插件支持exec、CNI、kubenet 3種類型。插件滿足K8S網(wǎng)絡(luò)插件接口即可。
type NetworkPlugin interface{
? ? ? ? Init(host Host, hairpinModecomponentconfig.HairpinMode) error
? ? ? ? Event(name string,details map[string]interface{})
? ? ? ? Name() string
? ? ? ? Capabilities() utilsets.Int
? ? ? ? SetUpPod(namespace string, name string,podInfraContainerID kubecontainer.ContainerID) error
? ? ? ? TearDownPod(namespace string, name string,podInfraContainerID kubecontainer.ContainerID) error
? ? ? ? GetPodNetworkStatus(namespace string, name string,podInfraContainerID kubecontainer.ContainerID) (*PodNetworkStatus,error)
? ? ? ? Status() error
}
exec 類型插件分別在初始化,pod創(chuàng)建,銷毀,查詢時(shí)調(diào)用可執(zhí)行程序,由可執(zhí)行程序完成pod的網(wǎng)絡(luò)管理。
在初始化,pod創(chuàng)建,銷毀,查詢時(shí),調(diào)用的subcommand分別為 “init”,”setup”,”teardown”,”status”,所以要求可執(zhí)行程序能解析上述subcommand 。
這里以一個(gè)簡(jiǎn)單的K8S插件為例子,介紹如何實(shí)現(xiàn)exec 類型的網(wǎng)絡(luò)插件。
需求場(chǎng)景如下:
要求k8s創(chuàng)建pod 時(shí),在pod內(nèi)還會(huì)創(chuàng)建一個(gè)網(wǎng)絡(luò)接口,該接口IP固定,該接口的另一端veth在ovs 網(wǎng)橋上,且有vlan tag,改tag 號(hào)可從pod name 取出。
該插件代碼見(jiàn):
https://github.com/liuliuzi/k8s-netplugin/tree/master/exec
更改權(quán)限為可執(zhí)行,同時(shí)下載:
https://github.com/jpetazzo/pipework
更改pipework 權(quán)限為可執(zhí)行,并移動(dòng)到系統(tǒng)PATH路徑下。
在每臺(tái)worknode修改kebelet 啟動(dòng)參數(shù),添加網(wǎng)絡(luò)插件參數(shù)。
?–-network-plugin=myplugin ? \插件名
? –network-plugin-dir=/root/kubplugin ?\插件位置
在每臺(tái)worknode 下載安裝OVS。
在K8S上創(chuàng)建pod:
kubectl run 22-my-nginx –image=busybox –replicas=2
77a11ec3-545a-42cc-bc7a-e3f54ff0c63b
? ? Bridge”k8s_br”
? ? ? ? Port”k8s_br”
? ? ? ? ? ? Interface”k8s_br”
? ? ? ? ? ? ? ? type:internal
? ? ? ? Port”veth1pl29198″
? ? ? ? ? ? tag: 22
? ? ? ? ? ? Interface”veth1pl29198″
? ? ovs_version:”2.5.0″
可見(jiàn)一個(gè)vlan tag22的IF已經(jīng)創(chuàng)建,veth另一端在pod內(nèi)。容器內(nèi)有兩張網(wǎng)絡(luò),一個(gè)是默認(rèn)的K8S網(wǎng)絡(luò),另外一個(gè)通過(guò)OVS的vlan網(wǎng)絡(luò)。這樣就實(shí)現(xiàn)了一個(gè)容器多網(wǎng)絡(luò)。對(duì)于這個(gè)插件實(shí)現(xiàn)原理,在init的時(shí)候,確保k8s_br 的ovs 網(wǎng)橋存在。在setup的時(shí)候,先通過(guò)pod獲取vlan名字,然后通過(guò)pipework添加veth到容器內(nèi)。
這里的場(chǎng)景非常簡(jiǎn)單,固定IP,而且vlan tag從pod name獲取。在復(fù)雜的場(chǎng)景里,這里的IP和vlan tag可以從一個(gè)專門的服務(wù)獲取,比如ETCD等。
在teardown的時(shí)候,刪除veth,刪除OVSport。
相對(duì)單獨(dú)使用pipework,有一個(gè)缺點(diǎn)就是pipework創(chuàng)建的veth和port無(wú)法伴隨容器的生命周期,比如容器銷毀后veth仍舊存在。現(xiàn)在通過(guò)K8S的網(wǎng)絡(luò)插件結(jié)構(gòu),插件通過(guò)pipework創(chuàng)建的veth 和port可以伴隨POD的生命周期,包括POD的遷移。
本文轉(zhuǎn)移K8S技術(shù)社區(qū)-解析 | K8S之網(wǎng)絡(luò)插件exec
總結(jié)
以上是生活随笔為你收集整理的解析 | K8S之网络插件exec的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 短短30分钟!计算机轻松判断J. K.
- 下一篇: 实现单台测试机6万websocket长连