Containerd 全面解析与实践指南

一、Containerd 核心概述

1. 定义与定位

Containerd 是一个工业级容器运行时,起源于 Docker Engine(2016 年 Docker 1.11 版本首次集成),后独立为开源项目并捐赠给 CNCF 基金会。它并非面向最终用户,而是专注于嵌入上层容器编排系统(如 Kubernetes、Swarm),提供稳定、轻量的容器生命周期管理能力。

2. 核心特性

  • 功能全面:支持容器生命周期管理、镜像传输与管理、存储 / 网络配置等核心能力。
  • 兼容性强:遵循 OCI 规范,支持 runC、kata 等多种容器运行时。
  • 轻量高效:相比 Docker,去除冗余功能,性能更优(启动、停止、删除容器速度更快)。
  • 无缝集成:原生适配 Kubernetes CRI 接口,成为 Kubernetes 1.20+ 版本的推荐运行时。

3. 生态定位

Containerd 处于容器生态的底层运行时层,上承 Kubernetes、Swarm 等编排系统,下接操作系统与硬件,通过 gRPC API 提供标准化接口,是云原生生态的核心组件之一。

二、Containerd 架构设计

1. 整体架构

采用 C/S 架构,分为三大核心模块:

  • Storage 存储层:负责镜像内容存储、文件系统快照管理(依赖 Snapshot Plugin)。
  • Metadata 元数据层:维护容器、镜像、命名空间等核心元数据。
  • Runtime 运行时层:通过 shim 适配不同运行时(如 runC),执行容器生命周期操作。

2. 核心插件

  • Content Plugin:管理镜像不可变内容的存储与访问。
  • Snapshot Plugin:处理镜像分层解压与文件系统快照(类似 Docker 的 graphdriver)。
  • Metrics Plugin:暴露组件监控指标,支持 Prometheus 集成。
  • Runtime Plugin:适配 OCI 兼容的运行时(如 runC、gVisor)。

3. 与其他工具性能对比

通过 bucketbench 测试,Containerd 在容器启动、停止、删除等操作的耗时上均优于 Docker 和 CRI-O,是更高效的容器运行时选择。

三、Containerd 部署步骤(CentOS Stream 8)

1. YUM 方式安装(推荐单机测试)

# 1. 安装依赖工具
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 vim

# 2. 添加阿里云 YUM 源
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum makecache

# 3. 查看并安装 Containerd
[root@localhost ~]# yum list | grep containerd
[root@localhost ~]# yum -y install containerd.io

# 4. 验证安装
[root@localhost ~]# rpm -qa | grep containerd
containerd.io-1.6.32-3.1.el8.x86_64

# 5. 启动服务并设置开机自启
[root@localhost ~]# systemctl enable containerd --now
[root@localhost ~]# systemctl status containerd  # 查看服务状态

# 6. 验证版本
[root@localhost ~]# ctr version

2. 二进制方式安装(推荐 Kubernetes 集群)

2.1 安装 Containerd
# 1. 下载包含 runC 和 CNI 插件的安装包
[root@localhost ~]# wget https://github.com/containerd/containerd/releases/download/v1.6.32/cri-containerd-cni-1.6.32-linux-amd64.tar.gz

# 2. 解压安装包
[root@localhost ~]# mkdir containerd
[root@localhost ~]# tar xf cri-containerd-cni-1.6.32-linux-amd64.tar.gz -C containerd/
[root@localhost ~]# cd containerd/

# 3. 复制运行时文件到系统路径
[root@localhost containerd]# cp usr/local/bin/* /usr/local/bin

# 4. 配置系统服务
[root@localhost containerd]# cp etc/systemd/system/containerd.service /usr/lib/systemd/system/
[root@localhost containerd]# systemctl daemon-reload

# 5. 生成默认配置文件
[root@localhost containerd]# mkdir /etc/containerd
[root@localhost containerd]# containerd config default > /etc/containerd/config.toml

# 6. 启动并验证
[root@localhost containerd]# systemctl enable containerd --now
[root@localhost containerd]# ctr version
2.2 安装 runC(依赖 seccomp)
# 1. 下载 runC 二进制包
[root@localhost ~]# wget https://github.com/opencontainers/runc/releases/download/v1.3.0/runc.amd64

# 2. 安装并授权
[root@localhost ~]# mv runc.amd64 /usr/sbin/runc
[root@localhost ~]# chmod +x /usr/sbin/runc

# 3. 验证
[root@localhost ~]# runc -v
runc version 1.3.0

四、Containerd 核心操作实践

1. 镜像管理(ctr 命令)

1.1 查看镜像
[root@localhost ~]# ctr images list  # 完整命令
[root@localhost ~]# ctr i ls  # 简写
1.2 拉取镜像
# 支持 OCI 标准镜像,需指定完整镜像地址
[root@localhost ~]# ctr images pull 054b8ac70e8010d90f2ac00ef29e6580.mirror.swr.myhuaweicloud.com/library/nginx:latest
1.3 镜像挂载与查看
# 挂载镜像到 /mnt 目录
[root@localhost ~]# ctr images mount 054b8ac70e8010d90f2ac00ef29e6580.mirror.swr.myhuaweicloud.com/library/nginx:latest /mnt

# 查看镜像内容
[root@localhost ~]# ls /mnt
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  ...

# 卸载
[root@localhost ~]# umount /mnt
1.4 镜像导出与导入
# 导出指定平台镜像
[root@localhost ~]# ctr i export --platform linux/amd64 nginx.img 054b8ac70e8010d90f2ac00ef29e6580.mirror.swr.myhuaweicloud.com/library/nginx:latest

# 导入镜像
[root@localhost ~]# ctr images import --platform linux/amd64 nginx.img
1.5 修改镜像标签
[root@localhost ~]# ctr images tag 054b8ac70e8010d90f2ac00ef29e6580.mirror.swr.myhuaweicloud.com/library/nginx:latest nginx:latest
1.6 删除镜像
[root@localhost ~]# ctr image rm nginx:latest

2. 容器管理(ctr 命令)

2.1 创建静态容器(未运行)
[root@localhost ~]# ctr container create nginx:latest nginx1  # 镜像名+容器ID
[root@localhost ~]# ctr c ls  # 查看静态容器
2.2 启动容器(转为动态容器)
[root@localhost ~]# ctr task start -d nginx1  # -d 后台运行
[root@localhost ~]# ctr t ls  # 查看运行中的容器(任务)
2.3 进入容器
[root@localhost ~]# ctr task exec --exec-id $RANDOM -t nginx1 /bin/sh  # $RANDOM 生成随机ID
2.4 直接运行容器(创建 + 启动)
[root@localhost ~]# ctr run -d --net-host nginx:latest nginx2  # --net-host 共享宿主机网络
2.5 容器生命周期操作
# 暂停容器
[root@localhost ~]# ctr tasks pause nginx2

# 恢复容器
[root@localhost ~]# ctr tasks resume nginx2

# 停止容器
[root@localhost ~]# ctr tasks kill nginx2

# 删除容器(需先停止任务)
[root@localhost ~]# ctr tasks delete nginx2
[root@localhost ~]# ctr container delete nginx2

3. 命名空间管理

命名空间用于隔离容器资源,默认使用 default 命名空间:

# 列出命名空间
[root@localhost ~]# ctr namespace ls

# 创建命名空间
[root@localhost ~]# ctr namespace create myns

# 在指定命名空间拉取镜像
[root@localhost ~]# ctr -n myns images pull 054b8ac70e8010d90f2ac00ef29e6580.mirror.swr.myhuaweicloud.com/library/nginx:latest

# 在指定命名空间创建容器
[root@localhost ~]# ctr -n myns container create nginx:latest mynginx

# 删除命名空间
[root@localhost ~]# ctr namespace rm myns

4. 私有镜像仓库对接(Harbor)

# 1. 配置 Harbor 域名解析
[root@localhost ~]# vim /etc/hosts
192.168.108.30 my.harbor.com

# 2. 拉取 Harbor 镜像(非 HTTPS 需加 --plain-http)
[root@localhost ~]# ctr image pull --plain-http 192.168.108.30/cloud/nginx:latest

# 3. 推送镜像到 Harbor
[root@localhost ~]# ctr images tag nginx:latest my.harbor.com/cloud/nginx:latest
[root@localhost ~]# ctr image push --platform linux/amd64 --plain-http --user "images_admin:Cloud12#$" my.harbor.com/cloud/nginx:latest

五、增强工具:nerdctl 与 crictl

1. nerdctl(Docker 兼容 CLI)

1.1 安装
# 安装 nerdctl
[root@localhost ~]# wget https://github.com/containerd/nerdctl/releases/download/v1.4.0/nerdctl-1.4.0-linux-amd64.tar.gz
[root@localhost ~]# tar -xf nerdctl-1.4.0-linux-amd64.tar.gz -C /usr/bin/

# 配置命令补全
[root@localhost ~]# nerdctl completion bash > /etc/bash_completion.d/nerdctl
[root@localhost ~]# source /etc/bash_completion.d/nerdctl

# 安装 CNI 插件
[root@localhost ~]# wget https://github.com/containernetworking/plugins/releases/download/v1.3.0/cni-plugins-linux-amd64-v1.3.0.tgz
[root@localhost ~]# mkdir -p /opt/cni/bin
[root@localhost ~]# tar -xf cni-plugins-linux-amd64-v1.3.0.tgz -C /opt/cni/bin
1.2 核心操作(与 Docker 命令一致)
# 拉取镜像
[root@localhost ~]# nerdctl pull nginx

# 运行容器
[root@localhost ~]# nerdctl run -d -p 8080:80 --name nginx-nerdctl nginx

# 查看容器
[root@localhost ~]# nerdctl ps

# 查看日志
[root@localhost ~]# nerdctl logs nginx-nerdctl

# 进入容器
[root@localhost ~]# nerdctl exec -it nginx-nerdctl bash

2. crictl(Kubernetes CRI 专用工具)

2.1 安装与配置
# 配置 Kubernetes YUM 源
[root@localhost ~]# vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.30/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.30/rpm/repodata/repomd.xml.key

# 安装 crictl
[root@localhost ~]# yum install -y cri-tools

# 配置对接 Containerd
[root@localhost ~]# vim /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 5
debug: false
2.2 核心操作
# 拉取镜像
[root@localhost ~]# crictl pull nginx

# 查看镜像
[root@localhost ~]# crictl images

# 查看容器
[root@localhost ~]# crictl ps

# 查看容器日志
[root@localhost ~]# crictl logs <容器ID>

六、工具对比与总结

1. 命令行工具对比表

功能 Docker nerdctl ctr crictl
查看容器状态 docker ps nerdctl ps ctr task ls/ctr c ls crictl ps
查看镜像 docker images nerdctl images ctr i ls crictl images
运行容器 docker run nerdctl run ctr run
拉取镜像 docker pull nerdctl pull ctr i pull crictl pull
进入容器 docker exec nerdctl exec ctr task exec crictl exec
导出镜像 docker save nerdctl save ctr i export

2. 核心总结

  • Containerd:轻量、稳定、高性能的容器运行时,是 Kubernetes 推荐选择,适合生产环境部署。

  • 部署方式:YUM 适合单机测试,二进制适合集群环境,需配套安装 runC 和 CNI 插件。

  • 工具选择

    • 习惯 Docker 命令:使用 nerdctl(完全兼容 Docker CLI)。
    • Kubernetes 集群调试:使用 crictl(原生支持 CRI 接口)。
    • 底层调试:使用 ctr(Containerd 原生 CLI)。
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐