后Kubernetes時代的虛擬機(jī)管理技術(shù)之Virtual-Kubelet篇
時間:2023-07-17 05:00:02 | 來源:網(wǎng)站運(yùn)營
時間:2023-07-17 05:00:02 來源:網(wǎng)站運(yùn)營
后Kubernetes時代的虛擬機(jī)管理技術(shù)之Virtual-Kubelet篇:在了解virtual-Kubelet之前,我們先了解下什么是Kubelet。
Kubelet 是在每個Node節(jié)點(diǎn)上運(yùn)行的主要 “節(jié)點(diǎn)代理”。在Kubernetes集群中每個節(jié)點(diǎn)都會啟動一個kubelet進(jìn)程,kubelet基于PodSpec來工作。每個Pod Spec是一個描述Pod的YAML或JSON對象。Kubelet接受通過各種機(jī)制(主要是通過Apiserver)提供的一組Pod Spec,并確保這些Pod Spec中描述的容器處于運(yùn)行狀態(tài)且運(yùn)行狀況良好。同時Kubelet還通過cAdvisor監(jiān)控容器和節(jié)點(diǎn)資源,定期向上報當(dāng)前節(jié)點(diǎn)的健康狀態(tài)以及資源使用情況,可以把Kubelet理解成[Server-Agent]架構(gòu)中的Agent。
Virtual-Kubelet是基于Kubelet的典型特性實(shí)現(xiàn),向上偽裝成Kubelet,從而模擬出Node對象,對接Kubernetes的原生資源對象;向下提供API,可對接其他資源管理平臺提供的Provider。不同的平臺通過實(shí)現(xiàn)Virtual-Kubelet定義的方法,允許節(jié)點(diǎn)由其對應(yīng)的Provider提供(如ACI,AWS Fargate,IoT Edge,Tensile Kube等)支持,實(shí)現(xiàn)Serverless,或者將其擴(kuò)展到如Docker Swarm、Openstack Zun等容器平臺中,也可以通過Provider納管其他Kubernetes集群,甚至是原生的IaaS層平臺(VMware、zstack、openstack)。
最好的描述是Kubernetes API on top,programmable back。
Virtual-Kubelet如何管理虛擬機(jī)是本文討論重點(diǎn)。
Virutal-Kubelet的架構(gòu)
Virtual-Kubelet 模擬了Node資源對象,并負(fù)責(zé)對Pod調(diào)度到Virtual-Kubelet偽裝的虛擬節(jié)點(diǎn)之后,對Pod進(jìn)行生命周期管理。
當(dāng)前支持原生Kubernetes特性:
l 創(chuàng)建,刪除和更新Pod
l Container的日志,管理和監(jiān)控
l 獲取單個Pod或多個Pod狀態(tài)
l 節(jié)點(diǎn)地址,節(jié)點(diǎn)容量,節(jié)點(diǎn)守護(hù)程序端點(diǎn)
l 管理操作系統(tǒng)
l 攜帶私有虛擬網(wǎng)絡(luò)
Virtual-Kubelet如何管理虛擬機(jī)?
虛擬機(jī)生命周期管理
Virtual-Kubelet在虛擬機(jī)調(diào)度和操作方面可以復(fù)用Kubernetes原生的資源對象,但Pod在Kubelet管理下的生命周期僅存在創(chuàng)建、運(yùn)行和銷毀,實(shí)際對于虛擬機(jī)的開關(guān)機(jī)、備份和遷移等操作無法實(shí)現(xiàn)映射關(guān)系,因此對于復(fù)雜的生命周期管理,需要通過自定義CRD方式支持不同類型的IaaS平臺,每一個VM-CR對應(yīng)一個IaaS層VM實(shí)例。
對于VM-CR操作主要可以分為兩類:
l 對VM運(yùn)行狀態(tài)變更
l 創(chuàng)建和銷毀:可以對應(yīng)一個VM-CR的create/delete
l VM啟停操作對應(yīng)VM-CR replicas數(shù)量的變更:開機(jī)0→1關(guān)機(jī)1→0
l VM規(guī)格變更:修改VM-CR Spec資源定義
l kubectl logs/exec VM-pod:實(shí)現(xiàn)對Pod的訪問
l 對VM進(jìn)行備份/遷移
l VM備份采用創(chuàng)建對應(yīng)Backup-Job對象,通過與VM-CR實(shí)例pod親和方式,將Backup-Job調(diào)度置VM實(shí)際節(jié)點(diǎn)所運(yùn)行的Virtual-Kubelet節(jié)點(diǎn)上,備份狀態(tài)與Job執(zhí)行狀態(tài)一致
l VM遷移采用Kubernetes原生的節(jié)點(diǎn)調(diào)度方式,IaaS平臺每一個負(fù)載VM的物理機(jī)對應(yīng)一個Kubernetes集群內(nèi)的Virtual-Kubelet,VM-CR實(shí)例Pod的調(diào)度由Kubernetes控制面管理
虛擬機(jī)存儲管理
由于Virtual-Kubelet中Pod僅作為邏輯概念,IaaS層存儲無法與Kubernetes集群公用,但可抽象為Kubernetes原生定義的PV/PVC,PV的access mode能力依賴IaaS層能力,并需要實(shí)現(xiàn)對應(yīng)平臺和底層存儲的Provider和Provisioner。
Virtual-Kubelet如何實(shí)現(xiàn)容器與虛擬機(jī)交互
容器和虛擬機(jī)互通
l Virtual-Kubelet對應(yīng)的Node會上報節(jié)點(diǎn)上Pod的Endpoint,假定Kubernetes集群和IaaS層平臺部署在同一個二層網(wǎng)絡(luò)下,則集群內(nèi)容器Pod可以訪問VM-Pod,但容器Pod對于VM-Pod不可見;
l 針對上一點(diǎn)可以通過Macvlan等網(wǎng)絡(luò)插件,將容器-Pod,降維至二層網(wǎng)絡(luò)上,實(shí)現(xiàn)容器-Pod和虛擬機(jī)互通,有一定硬件要求。
如何實(shí)現(xiàn)一套集群下虛擬機(jī)與容器的混合調(diào)度與資源隔離
l Virtual-Kubelet提供的是一個虛擬節(jié)點(diǎn)用來向Kubernetes上報Node對象和Pod的狀態(tài)和資源情況,虛擬機(jī)資源和集群內(nèi)節(jié)點(diǎn)資源完全隔離;
l 在引入Virtual-Kubelet的情況下,需要對Virtual-Kubelet節(jié)點(diǎn)配置Taint和Tolerations,保證容器-Pod和VM-Pod調(diào)度分離。
服務(wù)發(fā)現(xiàn)
Virtual-Kubelet,通過Provider實(shí)現(xiàn)的API將IaaS層VM信息抽象成對應(yīng)Pod對象的信息的方式來上報Endpoints,可以通過給CR添加no selector Service,待VM-Pod拉起后補(bǔ)充address至對應(yīng)的Service。
Virutal-Kubelet適用場景
適用場景
Virtual-Kuberlet適合在已有IaaS層管理平臺和Kubernetes集群環(huán)境下進(jìn)行二者的打通,實(shí)現(xiàn)在Kubernetes集群上統(tǒng)一管理容器和非容器平臺,同時由于Virtual-Kubelet在Serverless和納管其他已有容器平臺(Openstack Zun,Docker Swarm)方面也具有很高適配性,Virtual-Kubelet可以提供一套統(tǒng)一的API,方便開發(fā)者打通全流程。
Virtual-Kubelet的優(yōu)缺點(diǎn)
優(yōu)點(diǎn)
l 一個開源的Kubelet實(shí)現(xiàn),使用Kubernetes源語,使構(gòu)建、部署更簡單
l 提供Kubelet典型特性接口,Provider僅需實(shí)現(xiàn)對應(yīng)服務(wù)管理平臺資源到Node和Pod對象特性的實(shí)現(xiàn),不需要考慮如何訪問Kubernetes
l 靈活性高,Severless實(shí)踐、對接現(xiàn)有容器平臺、對接現(xiàn)有IaaS平臺均有一定前景
l Virtual-Kubelet設(shè)計將virtual-kubelet和Provider高度分離,Virtual-Kubelet使對于異構(gòu)服務(wù)平臺具有很高的兼容性(不同架構(gòu)如:ARM、S390x,不同CRI如:Kata、PodMan),不光是可以納關(guān)IaaS平臺對于其他Kubernetes集群也可以實(shí)現(xiàn)管理
缺點(diǎn)
l 將非集群內(nèi)資源抽象成Node和Pod對象對資源使用上有一定局限性,很難提供超出原有kubelet和IaaS平臺能力范疇,IaaS深度整合需要自行實(shí)現(xiàn)CRD
l 僅能作為轉(zhuǎn)換器,用于容器和虛擬機(jī)統(tǒng)一管理時還是需要依托已有的平臺能力,無法像Kubevirt等方案作為一個單獨(dú)的Iaas管理平臺使用
Virtual-Kubelet開發(fā)及部署
開發(fā)自定義的Provider
Virtual-Kubelet項(xiàng)目本身并不提供Provider,而是提供一系列定義Kubelet典型操作的接口,開發(fā)者需要根據(jù)應(yīng)用場景實(shí)現(xiàn)對應(yīng)的Provider。使Kubernetes可以進(jìn)行按需和幾乎即時的Container的計算、調(diào)度,而無需管理VM基礎(chǔ)結(jié)構(gòu),同時仍可利用可移植的KubernetesAPI。
實(shí)現(xiàn)遵循以下三個準(zhǔn)則:
l 提供必要的后端管道(back-end plumbing),以在Kubernetes的Context中支持Pods,Containers和相關(guān)資源的的生命周期管理
l 符合Virtual-Kubelet當(dāng)前提供的API
l 沒有訪問Kubernetes APIServer的權(quán)限,通過實(shí)現(xiàn)具有定義良好的回調(diào)機(jī)制來獲取Secrets或Configmap之類的數(shù)據(jù)
創(chuàng)建一個新的Provider主要需要通過調(diào)用Virtual-Kubelet提供的庫實(shí)現(xiàn)如下三個接口:
l PodLifecylceHandler:用于Pod生命周期的管理
type PodLifecycleHandler
interface {
// CreatePod takes a Kubernetes Pod and deploys it within the provider. CreatePod(ctx context.Context, pod *corev1.Pod)
error// UpdatePod takes a Kubernetes Pod and updates it within the provider. UpdatePod(ctx context.Context, pod *corev1.Pod)
error// DeletePod takes a Kubernetes Pod and deletes it from the provider. DeletePod(ctx context.Context, pod *corev1.Pod)
error// GetPod retrieves a pod by name from the provider (can be cached). GetPod(ctx context.Context, namespace, name
string) (*corev1.Pod,
error)
// GetPodStatus retrieves the status of a pod by name from the provider. GetPodStatus(ctx context.Context, namespace, name
string) (*corev1.PodStatus,
error)
// GetPods retrieves a list of all pods running on the provider (can be cached). GetPods(context.Context) ([]*corev1.Pod,
error)
}
l PodNotifier:該接口允許Provider提供異步通知Virtual-Kubelet有關(guān)Pod狀態(tài)更新的信息,如未實(shí)現(xiàn)該接口的話,Virtual-Kubelet會定期檢查所有Pod的狀態(tài),在計劃運(yùn)行大量Pod的場景中強(qiáng)烈推薦實(shí)現(xiàn)該接口
type PodNotifier
interface {
// NotifyPods instructs the notifier to call the passed in function when// the pod status changes.//// NotifyPods should not block callers. NotifyPods(context.Context,
func(*corev1.Pod))
}
l NodeProvider:NodeProvider負(fù)責(zé)通知虛擬小程序有關(guān)節(jié)點(diǎn)狀態(tài)更新的信息。Virtual-Kubelet將定期檢查節(jié)點(diǎn)的狀態(tài)并相應(yīng)地更新Kubernetes,如果不打算額外定義Node特性,可以直接使用Virtual-Kubelet提供的NativeNodeProvider
type NodeProvider
interface {
// Ping checks if the node is still active.// This is intended to be lightweight as it will be called periodically as a// heartbeat to keep the node marked as ready in Kubernetes. Ping(context.Context)
error// NotifyNodeStatus is used to asynchronously monitor the node.// The passed in callback should be called any time there is a change to the// node's status.// This will generally trigger a call to the Kubernetes API server to update// the status.//// NotifyNodeStatus should not block callers. NotifyNodeStatus(ctx context.Context, cb
func(*corev1.Node))
}
l API Endpoints:用于實(shí)現(xiàn)kubectl logs和kubectl exec
部署
Provider部署簡單僅需要在要添加目標(biāo)集群的主機(jī)中添加二進(jìn)制程序并根據(jù)IaaS層配置啟動即可:
./bin/virtual-kubelet --provider="hc-vmware-provider" --exsi="X.X.X.X"
關(guān)鍵詞:技術(shù),管理,時代,虛擬