概述

CRI - 图1

Kubernetes 依赖于底层的 Container Runtime 来进行容器的创建、启动、停止、删除等操作。Container Runtime 最典型的代表为 Docker。除此之外,还有如:

等 Container Runtime 的实现。

为了支持更多的 Container Runtime,K8S 引入了 CRI 规范。

CRI 规范主要由以下几个部分组成:

主要实现有:

CRI Service

CRI 主要定义了两个 Service。

  • RuntimeService: 管理 Container 生命周期
  1. service RuntimeService {
  2. // Version returns the runtime name, runtime version, and runtime API version.
  3. rpc Version(VersionRequest) returns (VersionResponse) {}
  4. // RunPodSandbox creates and starts a pod-level sandbox. Runtimes must ensure
  5. // the sandbox is in the ready state on success.
  6. rpc RunPodSandbox(RunPodSandboxRequest) returns (RunPodSandboxResponse) {}
  7. // StopPodSandbox stops any running process that is part of the sandbox and
  8. // reclaims network resources (e.g., IP addresses) allocated to the sandbox.
  9. // If there are any running containers in the sandbox, they must be forcibly
  10. // terminated.
  11. // This call is idempotent, and must not return an error if all relevant
  12. // resources have already been reclaimed. kubelet will call StopPodSandbox
  13. // at least once before calling RemovePodSandbox. It will also attempt to
  14. // reclaim resources eagerly, as soon as a sandbox is not needed. Hence,
  15. // multiple StopPodSandbox calls are expected.
  16. rpc StopPodSandbox(StopPodSandboxRequest) returns (StopPodSandboxResponse) {}
  17. // RemovePodSandbox removes the sandbox. If there are any running containers
  18. // in the sandbox, they must be forcibly terminated and removed.
  19. // This call is idempotent, and must not return an error if the sandbox has
  20. // already been removed.
  21. rpc RemovePodSandbox(RemovePodSandboxRequest) returns (RemovePodSandboxResponse) {}
  22. // PodSandboxStatus returns the status of the PodSandbox. If the PodSandbox is not
  23. // present, returns an error.
  24. rpc PodSandboxStatus(PodSandboxStatusRequest) returns (PodSandboxStatusResponse) {}
  25. // ListPodSandbox returns a list of PodSandboxes.
  26. rpc ListPodSandbox(ListPodSandboxRequest) returns (ListPodSandboxResponse) {}
  27. // CreateContainer creates a new container in specified PodSandbox
  28. rpc CreateContainer(CreateContainerRequest) returns (CreateContainerResponse) {}
  29. // StartContainer starts the container.
  30. rpc StartContainer(StartContainerRequest) returns (StartContainerResponse) {}
  31. // StopContainer stops a running container with a grace period (i.e., timeout).
  32. // This call is idempotent, and must not return an error if the container has
  33. // already been stopped.
  34. // TODO: what must the runtime do after the grace period is reached?
  35. rpc StopContainer(StopContainerRequest) returns (StopContainerResponse) {}
  36. // RemoveContainer removes the container. If the container is running, the
  37. // container must be forcibly removed.
  38. // This call is idempotent, and must not return an error if the container has
  39. // already been removed.
  40. rpc RemoveContainer(RemoveContainerRequest) returns (RemoveContainerResponse) {}
  41. // ListContainers lists all containers by filters.
  42. rpc ListContainers(ListContainersRequest) returns (ListContainersResponse) {}
  43. // ContainerStatus returns status of the container. If the container is not
  44. // present, returns an error.
  45. rpc ContainerStatus(ContainerStatusRequest) returns (ContainerStatusResponse) {}
  46. // UpdateContainerResources updates ContainerConfig of the container.
  47. rpc UpdateContainerResources(UpdateContainerResourcesRequest) returns (UpdateContainerResourcesResponse) {}
  48. // ReopenContainerLog asks runtime to reopen the stdout/stderr log file
  49. // for the container. This is often called after the log file has been
  50. // rotated. If the container is not running, container runtime can choose
  51. // to either create a new log file and return nil, or return an error.
  52. // Once it returns error, new container log file MUST NOT be created.
  53. rpc ReopenContainerLog(ReopenContainerLogRequest) returns (ReopenContainerLogResponse) {}
  54. // ExecSync runs a command in a container synchronously.
  55. rpc ExecSync(ExecSyncRequest) returns (ExecSyncResponse) {}
  56. // Exec prepares a streaming endpoint to execute a command in the container.
  57. rpc Exec(ExecRequest) returns (ExecResponse) {}
  58. // Attach prepares a streaming endpoint to attach to a running container.
  59. rpc Attach(AttachRequest) returns (AttachResponse) {}
  60. // PortForward prepares a streaming endpoint to forward ports from a PodSandbox.
  61. rpc PortForward(PortForwardRequest) returns (PortForwardResponse) {}
  62. // ContainerStats returns stats of the container. If the container does not
  63. // exist, the call returns an error.
  64. rpc ContainerStats(ContainerStatsRequest) returns (ContainerStatsResponse) {}
  65. // ListContainerStats returns stats of all running containers.
  66. rpc ListContainerStats(ListContainerStatsRequest) returns (ListContainerStatsResponse) {}
  67. // UpdateRuntimeConfig updates the runtime configuration based on the given request.
  68. rpc UpdateRuntimeConfig(UpdateRuntimeConfigRequest) returns (UpdateRuntimeConfigResponse) {}
  69. // Status returns the status of the runtime.
  70. rpc Status(StatusRequest) returns (StatusResponse) {}
  71. }
  • ImageService: 镜像的查看、拉取、删除操作
  1. service ImageService {
  2. // ListImages lists existing images.
  3. rpc ListImages(ListImagesRequest) returns (ListImagesResponse) {}
  4. // ImageStatus returns the status of the image. If the image is not
  5. // present, returns a response with ImageStatusResponse.Image set to
  6. // nil.
  7. rpc ImageStatus(ImageStatusRequest) returns (ImageStatusResponse) {}
  8. // PullImage pulls an image with authentication config.
  9. rpc PullImage(PullImageRequest) returns (PullImageResponse) {}
  10. // RemoveImage removes the image.
  11. // This call is idempotent, and must not return an error if the image has
  12. // already been removed.
  13. rpc RemoveImage(RemoveImageRequest) returns (RemoveImageResponse) {}
  14. // ImageFSInfo returns information of the filesystem that is used to store images.
  15. rpc ImageFsInfo(ImageFsInfoRequest) returns (ImageFsInfoResponse) {}
  16. }

References