CRI: How kubelet Calls the Container Runtime
#cri#kubernetes#kubelet#crictl#container-runtime
Contents
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.
| Component | Role | Examples |
|---|---|---|
| kubelet | CRI client. Asks for the pods assigned to the node to be started | the Kubernetes node agent |
| CRI runtime | CRI server. Pulls images and creates containers | containerd, CRI-O |
| OCI runtime | starts the actual container with kernel namespaces and cgroups | runc, crun |
2. How CRI Is Structured: Two gRPC Services
CRI consists of two gRPC services.
| Service | Main requests | What it does |
|---|---|---|
| RuntimeService | RunPodSandbox, CreateContainer, StartContainer, StopPodSandbox | prepares the pod environment, creates and starts containers, cleans up |
| ImageService | PullImage, ListImages, RemoveImage | pulls, 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).
| Runtime | Default socket |
|---|---|
| containerd | unix:///run/containerd/containerd.sock |
| CRI-O | unix:///var/run/crio/crio.sock |
3. The Order in Which a Pod Starts
When a pod is assigned to a node, kubelet sends requests in this order.
- 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. - PullImage: pulls the container image.
- CreateContainer: creates the container inside the sandbox.
- StartContainer: starts the container.
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
| containerd | CRI-O | |
|---|---|---|
| Origin | general-purpose runtime split out of Docker | runtime built only for Kubernetes |
| CRI support | built-in CRI plugin | the runtime itself implements CRI |
| Clients besides CRI | Docker, nerdctl and others | none |
| Container monitor process | containerd-shim | conmon |
| Default OCI runtime | runc | crun |
| Versioning | its own versions | matches Kubernetes minor versions |
| Where it is mostly used | managed Kubernetes such as EKS, GKE, AKS | OpenShift, RHEL-family nodes |
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
| Task | docker | crictl |
|---|---|---|
| Running containers | docker ps | crictl ps |
| Pods | none | crictl pods |
| Images | docker images | crictl images |
| Logs | docker logs <id> | crictl logs <id> |
| Run a command in a container | docker exec -it <id> sh | crictl exec -it <id> sh |
| Details | docker 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.