自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Kubernetes 容器運(yùn)行時(shí)接口 CRI

云計(jì)算 云原生
Kubernetes 作為一個(gè)跨云、跨平臺(tái)和多環(huán)境的容器編排系統(tǒng),在不同的環(huán)境和場(chǎng)景下使用不同的容器平臺(tái)。CRI 的出現(xiàn),保證平臺(tái)的多樣性和靈活性。

寫這篇文章是來填 很久之前挖下的坑[1]。

本文涉及組件的源碼版本如下:

  • Kubernetes 1.24
  • CRI 0.25.0
  • Containerd 1.6

容器運(yùn)行時(shí)(Container Runtime)是負(fù)責(zé)管理和執(zhí)行容器的組件。它負(fù)責(zé)將容器鏡像轉(zhuǎn)化為在主機(jī)上運(yùn)行的實(shí)際容器進(jìn)程,提供鏡像管理、容器的生命周期管理、資源隔離、文件系統(tǒng)、網(wǎng)絡(luò)配置等功能。

圖片圖片

常見容器運(yùn)行時(shí)有下面這幾種,這些容器運(yùn)行時(shí)都提供了不同程度的功能和性能。但他們都遵循容器運(yùn)行時(shí)接口(CRI),以便能夠與 Kubernetes 或其他容器編排系統(tǒng)集成,實(shí)現(xiàn)容器的調(diào)度和管理。

  • containerd[2]
  • CRI-O[3]
  • Docker Engine[4]
  • Mirantis Container Runtime[5]

有了 CRI,我們也可以“隨意”地在幾種容器運(yùn)行時(shí)之間進(jìn)行切換,而無需重新編譯 Kubernetes。簡(jiǎn)單來講,CRI 定義了所有對(duì)容器的操作,作為容器編排系統(tǒng)與容器運(yùn)行時(shí)間的標(biāo)準(zhǔn)接口存在。

CRI 的前生今世

圖片圖片

CRI 的首次引入是在 Kubernets 1.5[6],初始版本是 v1alpha1。在這之前,Kubernetes 需要在 kubelet 源碼中維護(hù)對(duì)各個(gè)容器運(yùn)行時(shí)的支持。

有了 CRI 之后,在 kubelet 中僅需支持 CRI 即可,然后通過一個(gè)中間層 CRI shim(grpc 服務(wù)器)與容器運(yùn)行時(shí)進(jìn)行交互。因?yàn)榇藭r(shí)各家容器運(yùn)行時(shí)實(shí)現(xiàn)還未支持 CRI。

在去年發(fā)布的 Kubernetes 1.24 中,正式移除了 Dockershim[7],與容易運(yùn)行時(shí)的交互得到了簡(jiǎn)化。

Kubernetes 目前支持 CRI 的 v1alpha2 和 v1。其中 v1 版本是在 Kubernetes 1.23 版本中引入的。

每次 kubelet 啟動(dòng)時(shí),首先會(huì)嘗試使用 v1 的 API 與容器運(yùn)行時(shí)進(jìn)行連接。如果失敗,才會(huì)嘗試使用 v1alpha2。

kubelet 與 CRI

在之前做過的 kubelet 源碼分析[8] 會(huì)持續(xù)監(jiān)控來自 文件、apiserver、http 的變更,來更新 pod 的狀態(tài)。寫那篇文章的時(shí)候,分析到這里就結(jié)束了。因?yàn)檫@之后的工作就交給 容器運(yùn)行時(shí)[9] 來完成 sandbox 和各種容器的創(chuàng)建和運(yùn)行,見 `kubeGenericRuntimeManager#SyncPod()`[10]。

kubelet 啟動(dòng)時(shí)便會(huì) 初始化 CRI 客戶端[11],與容器運(yùn)行時(shí)建立連接并確認(rèn) CRI 的版本。

創(chuàng)建 pod 的過程中,都會(huì)通過 CRI 與容器運(yùn)行時(shí)進(jìn)行交互:

  • 創(chuàng)建 sandbox
  • 創(chuàng)建容器
  • 拉取鏡像

參考源碼

  • pkg/kubelet/kuberuntime/kuberuntime_sandbox.go#L39[12]
  • pkg/kubelet/kuberuntime/kuberuntime_container.go#L176[13]
  • pkg/kubelet/images/image_manager.go#L89[14]

接下來我們以 Containerd 為例,看下如何處理 kubelet 的請(qǐng)求。

Containerd 與 CRI

Containerd 的 `criService`[15] 實(shí)現(xiàn)了 CRI 接口 `RuntimeService`[16] 和 `ImageService `[17] 的 RuntimeServiceServer 和 ImageServiceServer。

cirService 會(huì)進(jìn)一步包裝成 `instrumentedService`[18],保證所有的操作都是在 k8s.io命名空間下執(zhí)行的

RuntimeServiceServer


ImageServiceServer

ImageServiceServer[20]

type ImageServiceServer interface {  
    // ListImages lists existing images.    ListImages(context.Context, *ListImagesRequest) (*ListImagesResponse, error)  
    // ImageStatus returns the status of the image. If the image is not    // present, returns a response with ImageStatusResponse.Image set to    // nil.    ImageStatus(context.Context, *ImageStatusRequest) (*ImageStatusResponse, error)  
    // PullImage pulls an image with authentication config.    PullImage(context.Context, *PullImageRequest) (*PullImageResponse, error)  
    // RemoveImage removes the image.    // This call is idempotent, and must not return an error if the image has    // already been removed.    RemoveImage(context.Context, *RemoveImageRequest) (*RemoveImageResponse, error)  
    // ImageFSInfo returns information of the filesystem that is used to store images.  
    ImageFsInfo(context.Context, *ImageFsInfoRequest) (*ImageFsInfoResponse, error)  
}

下面以創(chuàng)建 sandbox 為例看一下 Containerd 的源碼。

Containerd 源碼分析

創(chuàng)建 sandbox 容器的請(qǐng)求通過 CRI 的 UDS(Unix domain socket)[21] 接口 /runtime.v1.RuntimeService/RunPodSandbox,進(jìn)入到 criService 的處理流程中。在 criService#RunPodSandbox(),負(fù)責(zé)創(chuàng)建和運(yùn)行 sandbox 容器,并保證容器狀態(tài)正常。

  • 下載 sandobx 容器鏡像
  • 初始化容器元數(shù)據(jù)
  • 初始化 pod 網(wǎng)絡(luò)命名空間,詳細(xì)內(nèi)容可參考之前的文章 源碼解析:從 kubelet、容器運(yùn)行時(shí)看 CNI 的使用[22]
  • 更新容器元數(shù)據(jù)
  • 寫入文件系統(tǒng)

參考源碼

  • pkg/cri/server/sandbox_run.go#L61[23]
  • services/tasks/local.go#L156[24]

總結(jié)

CRI 提供了一種標(biāo)準(zhǔn)化的接口,用于與底層容器運(yùn)行時(shí)進(jìn)行交互。這對(duì)與發(fā)展和狀大 Kubernetes 生態(tài)系統(tǒng)非常重要:

  • Kubernetes 控制平面與容器管理的具體實(shí)現(xiàn)解耦,可以獨(dú)立升級(jí)或者切換容器運(yùn)行時(shí),方便擴(kuò)展和優(yōu)化。
  • Kubernetes 作為一個(gè)跨云、跨平臺(tái)和多環(huán)境的容器編排系統(tǒng),在不同的環(huán)境和場(chǎng)景下使用不同的容器平臺(tái)。CRI 的出現(xiàn),保證平臺(tái)的多樣性和靈活性。

參考資料

[1] 很久之前挖下的坑: https://atbug.com/how-kubelete-container-runtime-work-with-cni/#創(chuàng)建-pod

[2] containerd: https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd

[3] CRI-O: https://kubernetes.io/docs/setup/production-environment/container-runtimes/#cri-o

[4] Docker Engine: https://kubernetes.io/docs/setup/production-environment/container-runtimes/#docker

[5] Mirantis Container Runtime: https://kubernetes.io/docs/setup/production-environment/container-runtimes/#mcr

[6] Kubernets 1.5: https://kubernetes.io/blog/2016/12/container-runtime-interface-cri-in-kubernetes/

[7] 正式移除了 Dockershim: https://kubernetes.io/blog/2022/05/03/dockershim-historical-context/

[8] kubelet 源碼分析: https://mp.weixin.qq.com/s/O7k3MlgyonNtOUxNPrN8lg

[9] 容器運(yùn)行時(shí): https://kubernetes.io/docs/setup/production-environment/container-runtimes/

[10] kubeGenericRuntimeManager#SyncPod(): https://github.com/kubernetes/kubernetes/blob/023d6fb8f4a7d130bf5c8e725ca310df9e663cd0/pkg/kubelet/kuberuntime/kuberuntime_manager.go#L711

[11] 初始化 CRI 客戶端: https://github.com/kubernetes/kubernetes/blob/14fcab83adf319b8ef8e82e1054412309c46f535/pkg/kubelet/kubelet.go#L285

[12] pkg/kubelet/kuberuntime/kuberuntime_sandbox.go#L39: https://github.com/kubernetes/kubernetes/blob/ea929715339da4553589df61c8638bac3bcae618/pkg/kubelet/kuberuntime/kuberuntime_sandbox.go#L39

[13] pkg/kubelet/kuberuntime/kuberuntime_container.go#L176: https://github.com/kubernetes/kubernetes/blob/3946d99904fe37ea04b231a8d101085b9b80b221/pkg/kubelet/kuberuntime/kuberuntime_container.go#L176

[14] pkg/kubelet/images/image_manager.go#L89: https://github.com/kubernetes/kubernetes/blob/de37b9d293613aac194cf522561d19ee1829e87b/pkg/kubelet/images/image_manager.go#L89

[15] criService: https://github.com/containerd/containerd/blob/1764ea9a2815ddbd0cde777b557f97171b84cd02/pkg/cri/server/service.go#L77

[16] RuntimeService: https://github.com/kubernetes/cri-api/blob/master/pkg/apis/runtime/v1/api.proto#L34

[17] ImageService : https://github.com/kubernetes/cri-api/blob/master/pkg/apis/runtime/v1/api.proto#L128

[18] instrumentedService: https://github.com/containerd/containerd/blob/d3c7e31c8a8f7dc3f0ef0d189fda5a7caca42ce2/pkg/cri/server/instrumented_service.go#L32

[19] RuntimeServiceServer: https://github.com/kubernetes/cri-api/blob/v0.25.0/pkg/apis/runtime/v1/api.pb.go#L9301

[20] ImageServiceServer: https://github.com/kubernetes/cri-api/blob/v0.25.0/pkg/apis/runtime/v1/api.pb.go#L10131C9-L10131C9

[21] UDS(Unix domain socket): https://en.wikipedia.org/wiki/Unix_domain_socket

[22] 源碼解析:從 kubelet、容器運(yùn)行時(shí)看 CNI 的使用: https://atbug.com/how-kubelete-container-runtime-work-with-cni/#創(chuàng)建-sandbox-容器

[23] pkg/cri/server/sandbox_run.go#L61: https://github.com/containerd/containerd/blob/f2376e659ffa55e4ff2578baf4e4c7aab54042e4/pkg/cri/server/sandbox_run.go#L61

[24] services/tasks/local.go#L156: https://github.com/containerd/containerd/blob/bbe46b8c43fc2febe316775bc2d4b9d697bbf05c/services/tasks/local.go#L156


責(zé)任編輯:武曉燕 來源: 云原生指北
相關(guān)推薦

2021-10-22 00:09:16

Kubernetes容器接口

2019-07-12 09:30:12

DashboardDockerDNS

2021-12-23 07:58:06

Kubelet容器運(yùn)行

2023-04-03 13:01:14

UbuntuCRI-O

2024-03-20 10:46:00

云原生容器

2021-09-11 15:38:23

容器運(yùn)行鏡像開放

2025-03-03 08:05:14

2023-01-03 09:10:21

2021-09-02 05:37:22

Containerd Kubernetes 容器

2020-08-11 08:59:20

容器虛擬化技術(shù)

2015-07-20 15:44:46

Swift框架MJExtension反射

2021-08-18 06:40:54

KubernetesDocker Containerd

2024-03-21 09:15:58

JS運(yùn)行的JavaScrip

2020-12-07 08:14:17

KubernetesDocker容器

2021-11-05 08:07:57

kubeletKubernetesContainerd

2009-09-24 17:19:06

運(yùn)行時(shí)多態(tài)性

2021-08-18 08:32:09

代碼運(yùn)行時(shí)間示波器

2013-11-26 16:49:55

Android開發(fā)運(yùn)行時(shí)KitKat

2020-12-07 13:31:43

GoMutex開發(fā)者

2023-07-28 10:42:43

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)