kubernetes-handbook/concepts/cri.md

146 lines
9.6 KiB
Markdown
Raw Permalink Normal View History

2021-12-23 20:07:44 +08:00
# 容器运行时接口CRI
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
容器运行时接口Container Runtime Interface简称 CRI。CRI 中定义了 **容器** 和 **镜像** 的服务的接口,因为容器运行时与镜像的生命周期是彼此隔离的,因此需要定义两个服务。该接口使用 [Protocol Buffer](https://developers.google.com/protocol-buffers/),基于 [gRPC](https://grpc.io/),在 Kubernetes v1.10 + 版本中是在 `pkg/kubelet/apis/cri/runtime/v1alpha2``api.proto` 中定义的。
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
## CRI 架构
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
Container Runtime 实现了 CRI gRPC Server包括 `RuntimeService``ImageService`。该 gRPC Server 需要监听本地的 Unix socket而 kubelet 则作为 gRPC Client 运行。
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
![CRI 架构 - 图片来自 kubernetes blog](../images/cri-architecture.png)
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
## 启用 CRI
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
除非集成了 rktnetes否则 CRI 都是被默认启用了,从 Kubernetes 1.7 版本开始,旧的预集成的 docker CRI 已经被移除。
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
要想启用 CRI 只需要在 kubelet 的启动参数重传入此参数:`--container-runtime-endpoint` 远程运行时服务的端点。当前 Linux 上支持 unix socketwindows 上支持 tcp。例如`unix:///var/run/dockershim.sock`、 `tcp://localhost:373`,默认是 `unix:///var/run/dockershim.sock`,即默认使用本地的 docker 作为容器运行时。
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
## CRI 接口
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
Kubernetes 1.9 中的 CRI 接口在 `api.proto` 中的定义如下:
2018-01-22 18:53:38 +08:00
```protobuf
// Runtime service defines the public APIs for remote container runtimes
service RuntimeService {
// Version returns the runtime name, runtime version, and runtime API version.
2021-12-23 20:07:44 +08:00
rpc Version (VersionRequest) returns (VersionResponse) {}
2018-01-22 18:53:38 +08:00
// RunPodSandbox creates and starts a pod-level sandbox. Runtimes must ensure
2021-12-23 20:07:44 +08:00
//the sandbox is in the ready state on success.
rpc RunPodSandbox (RunPodSandboxRequest) returns (RunPodSandboxResponse) {}
2018-01-22 18:53:38 +08:00
// StopPodSandbox stops any running process that is part of the sandbox and
2021-12-23 20:07:44 +08:00
//reclaims network resources (e.g., IP addresses) allocated to the sandbox.
2018-01-22 18:53:38 +08:00
// If there are any running containers in the sandbox, they must be forcibly
2021-12-23 20:07:44 +08:00
//terminated.
2018-01-22 18:53:38 +08:00
// This call is idempotent, and must not return an error if all relevant
2021-12-23 20:07:44 +08:00
//resources have already been reclaimed. kubelet will call StopPodSandbox
//at least once before calling RemovePodSandbox. It will also attempt to
//reclaim resources eagerly, as soon as a sandbox is not needed. Hence,
//multiple StopPodSandbox calls are expected.
rpc StopPodSandbox (StopPodSandboxRequest) returns (StopPodSandboxResponse) {}
2018-01-22 18:53:38 +08:00
// RemovePodSandbox removes the sandbox. If there are any running containers
2021-12-23 20:07:44 +08:00
//in the sandbox, they must be forcibly terminated and removed.
2018-01-22 18:53:38 +08:00
// This call is idempotent, and must not return an error if the sandbox has
2021-12-23 20:07:44 +08:00
//already been removed.
rpc RemovePodSandbox (RemovePodSandboxRequest) returns (RemovePodSandboxResponse) {}
2018-01-22 18:53:38 +08:00
// PodSandboxStatus returns the status of the PodSandbox. If the PodSandbox is not
2021-12-23 20:07:44 +08:00
//present, returns an error.
rpc PodSandboxStatus (PodSandboxStatusRequest) returns (PodSandboxStatusResponse) {}
2018-01-22 18:53:38 +08:00
// ListPodSandbox returns a list of PodSandboxes.
2021-12-23 20:07:44 +08:00
rpc ListPodSandbox (ListPodSandboxRequest) returns (ListPodSandboxResponse) {}
2018-01-22 18:53:38 +08:00
// CreateContainer creates a new container in specified PodSandbox
2021-12-23 20:07:44 +08:00
rpc CreateContainer (CreateContainerRequest) returns (CreateContainerResponse) {}
2018-01-22 18:53:38 +08:00
// StartContainer starts the container.
2021-12-23 20:07:44 +08:00
rpc StartContainer (StartContainerRequest) returns (StartContainerResponse) {}
2018-01-22 18:53:38 +08:00
// StopContainer stops a running container with a grace period (i.e., timeout).
// This call is idempotent, and must not return an error if the container has
2021-12-23 20:07:44 +08:00
//already been stopped.
2018-01-22 18:53:38 +08:00
// TODO: what must the runtime do after the grace period is reached?
2021-12-23 20:07:44 +08:00
rpc StopContainer (StopContainerRequest) returns (StopContainerResponse) {}
2018-01-22 18:53:38 +08:00
// RemoveContainer removes the container. If the container is running, the
2021-12-23 20:07:44 +08:00
//container must be forcibly removed.
2018-01-22 18:53:38 +08:00
// This call is idempotent, and must not return an error if the container has
2021-12-23 20:07:44 +08:00
//already been removed.
rpc RemoveContainer (RemoveContainerRequest) returns (RemoveContainerResponse) {}
2018-01-22 18:53:38 +08:00
// ListContainers lists all containers by filters.
2021-12-23 20:07:44 +08:00
rpc ListContainers (ListContainersRequest) returns (ListContainersResponse) {}
2018-01-22 18:53:38 +08:00
// ContainerStatus returns status of the container. If the container is not
2021-12-23 20:07:44 +08:00
//present, returns an error.
rpc ContainerStatus (ContainerStatusRequest) returns (ContainerStatusResponse) {}
2018-01-22 18:53:38 +08:00
// UpdateContainerResources updates ContainerConfig of the container.
2021-12-23 20:07:44 +08:00
rpc UpdateContainerResources (UpdateContainerResourcesRequest) returns (UpdateContainerResourcesResponse) {}
2018-01-22 18:53:38 +08:00
// ExecSync runs a command in a container synchronously.
2021-12-23 20:07:44 +08:00
rpc ExecSync (ExecSyncRequest) returns (ExecSyncResponse) {}
2018-01-22 18:53:38 +08:00
// Exec prepares a streaming endpoint to execute a command in the container.
2021-12-23 20:07:44 +08:00
rpc Exec (ExecRequest) returns (ExecResponse) {}
2018-01-22 18:53:38 +08:00
// Attach prepares a streaming endpoint to attach to a running container.
2021-12-23 20:07:44 +08:00
rpc Attach (AttachRequest) returns (AttachResponse) {}
2018-01-22 18:53:38 +08:00
// PortForward prepares a streaming endpoint to forward ports from a PodSandbox.
2021-12-23 20:07:44 +08:00
rpc PortForward (PortForwardRequest) returns (PortForwardResponse) {}
2018-01-22 18:53:38 +08:00
// ContainerStats returns stats of the container. If the container does not
2021-12-23 20:07:44 +08:00
//exist, the call returns an error.
rpc ContainerStats (ContainerStatsRequest) returns (ContainerStatsResponse) {}
2018-01-22 18:53:38 +08:00
// ListContainerStats returns stats of all running containers.
2021-12-23 20:07:44 +08:00
rpc ListContainerStats (ListContainerStatsRequest) returns (ListContainerStatsResponse) {}
2018-01-22 18:53:38 +08:00
// UpdateRuntimeConfig updates the runtime configuration based on the given request.
2021-12-23 20:07:44 +08:00
rpc UpdateRuntimeConfig (UpdateRuntimeConfigRequest) returns (UpdateRuntimeConfigResponse) {}
2018-01-22 18:53:38 +08:00
// Status returns the status of the runtime.
2021-12-23 20:07:44 +08:00
rpc Status (StatusRequest) returns (StatusResponse) {}}
2018-01-22 18:53:38 +08:00
// ImageService defines the public APIs for managing images.
service ImageService {
// ListImages lists existing images.
2021-12-23 20:07:44 +08:00
rpc ListImages (ListImagesRequest) returns (ListImagesResponse) {}
2018-01-22 18:53:38 +08:00
// ImageStatus returns the status of the image. If the image is not
2021-12-23 20:07:44 +08:00
//present, returns a response with ImageStatusResponse.Image set to
//nil.
rpc ImageStatus (ImageStatusRequest) returns (ImageStatusResponse) {}
2018-01-22 18:53:38 +08:00
// PullImage pulls an image with authentication config.
2021-12-23 20:07:44 +08:00
rpc PullImage (PullImageRequest) returns (PullImageResponse) {}
2018-01-22 18:53:38 +08:00
// RemoveImage removes the image.
// This call is idempotent, and must not return an error if the image has
2021-12-23 20:07:44 +08:00
//already been removed.
rpc RemoveImage (RemoveImageRequest) returns (RemoveImageResponse) {}
2018-01-22 18:53:38 +08:00
// ImageFSInfo returns information of the filesystem that is used to store images.
2021-12-23 20:07:44 +08:00
rpc ImageFsInfo (ImageFsInfoRequest) returns (ImageFsInfoResponse) {}}
2018-01-22 18:53:38 +08:00
```
2021-12-23 20:07:44 +08:00
这其中包含了两个 gRPC 服务:
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
- **RuntimeService**:容器和 Sandbox 运行时管理。
- **ImageService**:提供了从镜像仓库拉取、查看、和移除镜像的 RPC。
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
## 当前支持的 CRI 后端
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
我们最初在使用 Kubernetes 时通常会默认使用 Docker 作为容器运行时,其实从 Kubernetes 1.5 开始已经支持 CRI通过 CRI 接口可以指定使用其它容器运行时作为 Pod 的后端,目前支持 CRI 的后端有:
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
- [cri-o](https://github.com/kubernetes-incubator/cri-o)cri-o 是 Kubernetes 的 CRI 标准的实现,并且允许 Kubernetes 间接使用 OCI 兼容的容器运行时,可以把 cri-o 看成 Kubernetes 使用 OCI 兼容的容器运行时的中间层。
- [cri-containerd](https://github.com/containerd/cri-containerd):基于 [Containerd](https://github.com/containerd/containerd) 的 Kubernetes CRI 实现
- [rkt](https://coreos.com/rkt/):由 CoreOS 主推的用来跟 docker 抗衡的容器运行时
- [frakti](https://github.com/kubernetes/frakti):基于 hypervisor 的 CRI
- [docker](https://www.docker.com)Kuberentes 最初就开始支持的容器运行时,目前还没完全从 kubelet 中解耦Docker 公司同时推广了 [OCI](https://www.opencontainers.org/) 标准
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
CRI 是由 [SIG-Node](https://kubernetes.slack.com/archives/sig-node) 来维护的。
2018-01-22 18:53:38 +08:00
2021-12-23 20:07:44 +08:00
## 当前通过 CRI-O 间接支持 CRI 的后端
2021-12-23 20:07:44 +08:00
当前同样存在一些只实现了 [OCI](https://www.opencontainers.org/) 标准的容器,但是它们可以通过 CRI-O 来作为 Kubernetes 的容器运行时。CRI-O 是 Kubernetes 的 CRI 标准的实现,并且允许 Kubernetes 间接使用 OCI 兼容的容器运行时。
2021-12-23 20:07:44 +08:00
- [Clear Containers](https://github.com/clearcontainers):由 Intel 推出的兼容 OCI 容器运行时,可以通过 CRI-O 来兼容 CRI。
- [Kata Containers](https://katacontainers.io/):符合 OCI 规范,可以通过 CRI-O 或 [Containerd CRI Plugin](https://github.com/containerd/cri) 来兼容 CRI。
- [gVisor](https://github.com/google/gvisor):由谷歌推出的容器运行时沙箱 (Experimental),可以通过 CRI-O 来兼容 CRI。
2018-01-22 18:53:38 +08:00
## 参考
2021-12-23 20:07:44 +08:00
- [Kubernetes CRI and Minikube - sreeninet.wordpress.com](https://sreeninet.wordpress.com/2017/02/11/kubernetes-cri-and-minikube/)
- [CRI-O and Alternative Runtimes in Kubernetes - projectatomic.io](https://projectatomic.io/blog/2017/02/crio-runtimes/)
2020-02-08 11:48:04 +08:00
- [Docker、Containerd、RunC...:你应该知道的所有](https://www.infoq.cn/article/2017/02/Docker-Containerd-RunC/)
2021-12-23 20:07:44 +08:00
- [Introducing Container Runtime Interface (CRI) in Kubernetes - blog.kubernetes.io](https://kubernetes.io/blog/2016/12/container-runtime-interface-cri-in-kubernetes/)
- [cri-o 官网 - cri-o.io](https://cri-o.io/)
- [Kata Containers Architecture - github.com](https://github.com/kata-containers/documentation/blob/master/design/architecture.md#kubernetes-support)