美文网首页
CRI(Container Runtime Interface)

CRI(Container Runtime Interface)

作者: Lis_ | 来源:发表于2019-11-14 15:35 被阅读0次

    什么是CRI?

    CRI(Container Runtime Interface)容器运行时接口,它定义了一系列的规范和要求,通过它,不同的容器运行时可以与kubelet集成,无需重新编译而直接调用。直观来看,它就是一些列接口的定义:
    以kubernetes release-1.17 branch作为参考。CRI归到一个独立的repo叫做cri-api 来管理。https://github.com/kubernetes/cri-api/
    CRI接口定义位于cri-api/pkg/apis/services.go,氛围RuntimeServie和ImageManagerService

    // RuntimeService interface should be implemented by a container runtime.
    // The methods should be thread-safe.
    type RuntimeService interface {
        RuntimeVersioner
        ContainerManager
        PodSandboxManager
        ContainerStatsManager
    
        // UpdateRuntimeConfig updates runtime configuration if specified
        UpdateRuntimeConfig(runtimeConfig *runtimeapi.RuntimeConfig) error
        // Status returns the status of the runtime.
        Status() (*runtimeapi.RuntimeStatus, error)
    }
    
    // ImageManagerService interface should be implemented by a container image
    // manager.
    // The methods should be thread-safe.
    type ImageManagerService interface {
        // ListImages lists the existing images.
        ListImages(filter *runtimeapi.ImageFilter) ([]*runtimeapi.Image, error)
        // ImageStatus returns the status of the image.
        ImageStatus(image *runtimeapi.ImageSpec) (*runtimeapi.Image, error)
        // PullImage pulls an image with the authentication config.
        PullImage(image *runtimeapi.ImageSpec, auth *runtimeapi.AuthConfig, podSandboxConfig *runtimeapi.PodSandboxConfig) (string, error)
        // RemoveImage removes the image.
        RemoveImage(image *runtimeapi.ImageSpec) error
        // ImageFsInfo returns information of the filesystem that is used to store images.
        ImageFsInfo() ([]*runtimeapi.FilesystemUsage, error)
    }
    

    对应的rpc接口定义位于cri-api/pkg/apis/runtime/v1alpha2/api.proto文件中:

    // Runtime service defines the public APIs for remote container runtimes
    service RuntimeService {
        // Version returns the runtime name, runtime version, and runtime API version.
        rpc Version(VersionRequest) returns (VersionResponse) {}
    
        // RunPodSandbox creates and starts a pod-level sandbox. Runtimes must ensure
        // the sandbox is in the ready state on success.
        rpc RunPodSandbox(RunPodSandboxRequest) returns (RunPodSandboxResponse) {}
        // StopPodSandbox stops any running process that is part of the sandbox and
        // reclaims network resources (e.g., IP addresses) allocated to the sandbox.
        // If there are any running containers in the sandbox, they must be forcibly
        // terminated.
        // This call is idempotent, and must not return an error if all relevant
        // 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) {}
        // RemovePodSandbox removes the sandbox. If there are any running containers
        // in the sandbox, they must be forcibly terminated and removed.
        // This call is idempotent, and must not return an error if the sandbox has
        // already been removed.
        rpc RemovePodSandbox(RemovePodSandboxRequest) returns (RemovePodSandboxResponse) {}
        // PodSandboxStatus returns the status of the PodSandbox. If the PodSandbox is not
        // present, returns an error.
        rpc PodSandboxStatus(PodSandboxStatusRequest) returns (PodSandboxStatusResponse) {}
        // ListPodSandbox returns a list of PodSandboxes.
        rpc ListPodSandbox(ListPodSandboxRequest) returns (ListPodSandboxResponse) {}
    
        // CreateContainer creates a new container in specified PodSandbox
        rpc CreateContainer(CreateContainerRequest) returns (CreateContainerResponse) {}
        // StartContainer starts the container.
        rpc StartContainer(StartContainerRequest) returns (StartContainerResponse) {}
        // 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
        // already been stopped.
        // TODO: what must the runtime do after the grace period is reached?
        rpc StopContainer(StopContainerRequest) returns (StopContainerResponse) {}
        // RemoveContainer removes the container. If the container is running, the
        // container must be forcibly removed.
        // This call is idempotent, and must not return an error if the container has
        // already been removed.
        rpc RemoveContainer(RemoveContainerRequest) returns (RemoveContainerResponse) {}
        // ListContainers lists all containers by filters.
        rpc ListContainers(ListContainersRequest) returns (ListContainersResponse) {}
        // ContainerStatus returns status of the container. If the container is not
        // present, returns an error.
        rpc ContainerStatus(ContainerStatusRequest) returns (ContainerStatusResponse) {}
        // UpdateContainerResources updates ContainerConfig of the container.
        rpc UpdateContainerResources(UpdateContainerResourcesRequest) returns (UpdateContainerResourcesResponse) {}
        // ReopenContainerLog asks runtime to reopen the stdout/stderr log file
        // for the container. This is often called after the log file has been
        // rotated. If the container is not running, container runtime can choose
        // to either create a new log file and return nil, or return an error.
        // Once it returns error, new container log file MUST NOT be created.
        rpc ReopenContainerLog(ReopenContainerLogRequest) returns (ReopenContainerLogResponse) {}
    
        // ExecSync runs a command in a container synchronously.
        rpc ExecSync(ExecSyncRequest) returns (ExecSyncResponse) {}
        // Exec prepares a streaming endpoint to execute a command in the container.
        rpc Exec(ExecRequest) returns (ExecResponse) {}
        // Attach prepares a streaming endpoint to attach to a running container.
        rpc Attach(AttachRequest) returns (AttachResponse) {}
        // PortForward prepares a streaming endpoint to forward ports from a PodSandbox.
        rpc PortForward(PortForwardRequest) returns (PortForwardResponse) {}
    
        // ContainerStats returns stats of the container. If the container does not
        // exist, the call returns an error.
        rpc ContainerStats(ContainerStatsRequest) returns (ContainerStatsResponse) {}
        // ListContainerStats returns stats of all running containers.
        rpc ListContainerStats(ListContainerStatsRequest) returns (ListContainerStatsResponse) {}
    
        // UpdateRuntimeConfig updates the runtime configuration based on the given request.
        rpc UpdateRuntimeConfig(UpdateRuntimeConfigRequest) returns (UpdateRuntimeConfigResponse) {}
    
        // Status returns the status of the runtime.
        rpc Status(StatusRequest) returns (StatusResponse) {}
    }
    
    // ImageService defines the public APIs for managing images.
    service ImageService {
        // ListImages lists existing images.
        rpc ListImages(ListImagesRequest) returns (ListImagesResponse) {}
        // ImageStatus returns the status of the image. If the image is not
        // present, returns a response with ImageStatusResponse.Image set to
        // nil.
        rpc ImageStatus(ImageStatusRequest) returns (ImageStatusResponse) {}
        // PullImage pulls an image with authentication config.
        rpc PullImage(PullImageRequest) returns (PullImageResponse) {}
        // RemoveImage removes the image.
        // This call is idempotent, and must not return an error if the image has
        // already been removed.
        rpc RemoveImage(RemoveImageRequest) returns (RemoveImageResponse) {}
        // ImageFSInfo returns information of the filesystem that is used to store images.
        rpc ImageFsInfo(ImageFsInfoRequest) returns (ImageFsInfoResponse) {}
    }
    
    

    实际上,在Kubernetes 1.6版本之前kubelet是直接调用 Docker 的 API 来创建和管理容器的,但是随着运行时的增加,加入kubelet的代码变得越来越多,难以维护,所以社区成立了一个sig-node小组来重构这部分代码。
    在kubernetes 1.7版本,将对容器的操作抽象成一系列接口的定义,即CRI(Container Runtime Interface),通过gRPC的方式来实现CRI,这样做即解耦屏蔽下层容器运行时带来的差异。
    有了接口的定义,那谁来实现这些接口呢?答案就是CRI-shim!首先kubelet通过一个generatic-runtime的组件发送创建容器的请求给grpc-client调用CRI中的对应创建容器接口,CRI-shim 即gRPC server响应请求,将请求封装成runtime能识别的形式,调用runtime 创建容器。

    image.png

    CRI-shim

    CRI-shim 作为CRI中定义的接口的实现,本质上其实就是一个gRPC server。对于docker来说这个shim就是docker-shim,对于cri-containerd来说,cri-shim就是cri-containerd:


    image.png

    后来cri-containerd 重构进containerd中,合为一个containerd进程:


    image.png

    作为cri-o来说:

    docker-shim是由kubernetes开发维护的,目前代码组织形式还是放在kubelet中:
    kubernetes/pkg/kubelet/docker-shim
    其中在docker_container.go中,可以看到对于CRI的接口的实现,如:

    // CreateContainer creates a new container in the given PodSandbox
    // Docker cannot store the log to an arbitrary location (yet), so we create an
    // symlink at LogPath, linking to the actual path of the log.
    // TODO: check if the default values returned by the runtime API are ok.
    func (ds *dockerService) CreateContainer(_ context.Context, r *runtimeapi.CreateContainerRequest) (*runtimeapi.CreateContainerResponse, error) {
        podSandboxID := r.PodSandboxId
        config := r.GetConfig()
        sandboxConfig := r.GetSandboxConfig()
    
        if config == nil {
            return nil, fmt.Errorf("container config is nil")
        }
        if sandboxConfig == nil {
            return nil, fmt.Errorf("sandbox config is nil for container %q", config.Metadata.Name)
        }
    
        labels := makeLabels(config.GetLabels(), config.GetAnnotations())
        // Apply a the container type label.
        labels[containerTypeLabelKey] = containerTypeLabelContainer
        // Write the container log path in the labels.
        labels[containerLogPathLabelKey] = filepath.Join(sandboxConfig.LogDirectory, config.LogPath)
        // Write the sandbox ID in the labels.
        labels[sandboxIDLabelKey] = podSandboxID
    
    ......
    
    image.png

    相关文章

      网友评论

          本文标题:CRI(Container Runtime Interface)

          本文链接:https://www.haomeiwen.com/subject/tcgtictx.html