containerd

history

  • Docker created containerd originally as a component of its own engine to separate “high-level” management from “low-level” execution.
  • Later, Docker donated containerd to the CNCF (Cloud Native Computing Foundation).
  • Kubernetes then adopted containerd as its runtime to remove its dependency on the full Docker daemon (the “Docker Shim” removal), because Kubernetes only needed the “running containers” part, not the full Docker UI/Network/Build stack.

So today:

  1. Docker USES containerd (as its internal engine).
  2. Kubernetes USES containerd (directly, skipping Docker).
    Both tools rely on the same underlying containerd daemon to actually manage processes and images on Linux. If you kill containerd , you break both Docker and Kubernetes on that node.

best practise

change containerd’s default data path

  1. Identify the Current Data Path

    1
    containerd config default | grep "root" # Expected output: root = "/var/lib/containerd"
  2. Modify the following lines in /etc/containerd/config.toml:

    1
    root = "/path/to/new/data/path" # the location where container data (images, volumes) is stored
  3. Move Existing Data (if required)

    1
    2
    3
    sudo systemctl stop containerd  # optional
    sudo mv /var/lib/containerd /data/containerd
    sudo systemctl start containerd

ctr

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# list k8s image
sudo ctr -n k8s.io images ls

# pull k8s image
sudo ctr -n k8s.io images pull xxx
# pull with auth
sudo ctr images pull --user username:password registry.example.com/repository/image:tag

# load tar file
sudo ctr -n k8s.io images import /tmp/my-image.tar
# combine load tar file to specify tag image
sudo ctr -n k8s.io images import --base-name my.registry.com/myproject/my-image:v1.2.3 /path/to/image.tar

# rename tag
sudo ctr -n k8s.io images tag <old-image-name> <new-image-name>

# remove image
sudo ctr -n k8s.io images remove <name>:<tag>
Usage Context Command
Default Namespace ctr container ls
Kubernetes Namespace ctr -n k8s.io container ls
Check Process PIDs ctr task ls
Detailed Inspection ctr container info <id>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# list k8s running container 
ctr -n k8s.io container ls

# check k8s's specify container
ctr -n k8s.io container info xxx

# Forcefully stop the task
ctr task kill -s SIGKILL <taskName>

# Delete the Stopped Task
ctr task rm <taskName>

# Remove the Container Metadata
ctr container rm <taskName>

nerdctl

Converting your Docker command to containerd requires using either ctr (low-level tool) or nerdctl (Docker-compatible CLI). I strongly recommend using nerdctl as ctr is designed for debugging and lacks many Docker features.

nerdctl provides a Docker-compatible interface and supports the features you need.

1
2
3
4
5
6
7
8
9
10
11
# list
nerdctl ps

# exec
nerdctl exec -it <containerID> bash

# stop
nerdctl stop <containerID>

# rm
nerdctl rm <containerID>

ctrctl

1
2
3
4
5
6
7
8
9
10
11
12
13
VERSION="v1.34.0"
curl -L https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz -o crictl.tar.gz
sudo tar zxvf crictl.tar.gz -C /usr/local/bin
rm -f crictl.tar.gz
crictl --version

# optional
sudo tee /etc/crictl.yaml > /dev/null <<EOT
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOT

command

1
crictl images

reference

Containerd daemon is the manager. It handles requests from kubelet and crictl. It does not run your apps itself.

Shim (containerd-shim-runc-v2) is a small helper process, one per pod. It starts the container, watches it, and keeps its logs and exit status.

Container is a normal Linux process (for example node-cache) running in isolated namespaces and cgroups. Its parent is the shim.

The chain is: kubelet → containerd daemon → shim → container process. The shim’s parent is PID 1, so it does not depend on the daemon staying alive.

KillMode

Restarting containerd makes systemd stop the old daemon first. KillMode decides who else gets killed with it.

KillMode Daemon Shims and containers Result
process Killed Kept running Safe. Pods keep running
control-group Killed Killed All pods on the node die
mixed Killed (SIGTERM) Killed (SIGKILL) All pods on the node die
none Not killed Kept running Not normally used

Shims sit in the same systemd cgroup as the daemon. control-group and mixed kill everything in that cgroup, and process kills only the daemon.

Your node has KillMode=process, so a restart is safe. Check the setting on every node before you restart:

1
systemctl show containerd -p KillMode