CRI: How kubelet Calls the Container Runtime

infra4 min한국어

#cri#kubernetes#kubelet#crictl#container-runtime

I went back and forth on whether to write this, and decided to keep it as a note.

Assume you create a pod.

kubectl run web --image=nginx

Once the scheduler picks a node for the pod, the kubelet on that node starts it.
But kubelet does not create containers itself.
The kubelet configuration holds the address of the runtime it hands containers off to.

# part of /var/lib/kubelet/config.yaml
containerRuntimeEndpoint: unix:///run/containerd/containerd.sock

kubelet sends requests to the container runtime over this socket, following an interface called CRI.

1. CRI

CRI (Container Runtime Interface) is the gRPC interface kubelet uses to manage pods and containers through the container runtime.
Any runtime that implements CRI can sit under kubelet without rebuilding Kubernetes.

ComponentRoleExamples
kubeletCRI client. Asks for the pods assigned to the node to be startedthe Kubernetes node agent
CRI runtimeCRI server. Pulls images and creates containerscontainerd, CRI-O
OCI runtimestarts the actual container with kernel namespaces and cgroupsrunc, crun

2. How CRI Is Structured: Two gRPC Services

CRI consists of two gRPC services.

ServiceMain requestsWhat it does
RuntimeServiceRunPodSandbox, CreateContainer, StartContainer, StopPodSandboxprepares the pod environment, creates and starts containers, cleans up
ImageServicePullImage, ListImages, RemoveImagepulls, lists and removes images

kubelet connects to the runtime over a Unix socket on the node.
The socket is set with containerRuntimeEndpoint in the kubelet configuration (or the --container-runtime-endpoint command-line flag).

RuntimeDefault socket
containerdunix:///run/containerd/containerd.sock
CRI-Ounix:///var/run/crio/crio.sock
kubelet CRI client CRI runtime (containerd, CRI-O) RuntimeService pods, containers ImageService images gRPC Unix socket OCI runtime runc, crun container the socket is set with containerRuntimeEndpoint
kubelet calls the CRI runtime's RuntimeService and ImageService over a Unix socket. The CRI runtime starts containers with an OCI runtime.

3. The Order in Which a Pod Starts

When a pod is assigned to a node, kubelet sends requests in this order.

  1. RunPodSandbox: creates the environment the pod shares (the sandbox). It starts a small infra container (pause) that holds the network namespace, and a CNI plugin gives the pod an IP.
  2. PullImage: pulls the container image.
  3. CreateContainer: creates the container inside the sandbox.
  4. StartContainer: starts the container.
kubelet CRI runtime containerd, CRI-O ① RunPodSandbox prepare the pod environment ② PullImage pull the image ③ CreateContainer create the container ④ StartContainer start the container
kubelet sends RunPodSandbox, PullImage, CreateContainer and StartContainer to the CRI runtime in order.

CNI (Container Network Interface) is the plugin interface for attaching networks to containers and detaching them. Kubernetes network implementations such as Calico, Cilium and Flannel run as CNI plugins.

Container logs are handled through CRI as well.
The runtime writes the container’s standard output and standard error to a path kubelet specifies (under /var/log/pods/), and kubectl logs reads those files through kubelet.

4. Runtimes That Implement CRI: containerd and CRI-O

containerdCRI-O
Origingeneral-purpose runtime split out of Dockerruntime built only for Kubernetes
CRI supportbuilt-in CRI pluginthe runtime itself implements CRI
Clients besides CRIDocker, nerdctl and othersnone
Container monitor processcontainerd-shimconmon
Default OCI runtimerunccrun
Versioningits own versionsmatches Kubernetes minor versions
Where it is mostly usedmanaged Kubernetes such as EKS, GKE, AKSOpenShift, RHEL-family nodes
CRI-O containerd kubelet CRI-O Kubernetes only CRI kubelet dockerd nerdctl containerd general purpose
CRI-O takes only kubelet's CRI requests. containerd also takes requests from other clients such as Docker and nerdctl.

containerd keeps the containers created by Kubernetes in the k8s.io namespace.
Even if Docker is installed on the same node, Docker uses a different namespace (moby), so Kubernetes containers do not show up in docker ps.

sudo ctr -n k8s.io containers list   # containers created by kubelet

5. Inspecting a Node with crictl

crictl is a CLI for inspecting and debugging CRI runtimes.
It talks CRI regardless of the runtime, so the same commands work on containerd nodes and CRI-O nodes.

# /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
Taskdockercrictl
Running containersdocker pscrictl ps
Podsnonecrictl pods
Imagesdocker imagescrictl images
Logsdocker logs <id>crictl logs <id>
Run a command in a containerdocker exec -it <id> shcrictl exec -it <id> sh
Detailsdocker inspect <id>crictl inspect <id>

crictl is for inspection and debugging.
Pods and containers created with crictl are unknown to kubelet and may be cleaned up, so create workloads with kubectl.

References