containerd容器


1 云原生与容器运行时

1.1 云原生概述

  云原生是充分利用云计算的优势,在云计算中构建、部署和管理现代引用程序的软件方法。

  CloudNative=Cloud + Native,其中,Cloud表示应用程序位于云中、Native表示应用程序从设计之初就考虑云的环境。

  云原生的特征为:DevOps、持续交付、微服务、容器技术。

  DevOPs:是一个组合词,即Dev+Ops,表示开发和运维之间的协作。

  持续交付:持续交付是相对于传统瀑布式开发模型而言的,特征是不停机更新。

  微服务:微服务是相对于单体应用的,每个微服务可独立于同一应用程序中的其他服务进行部署、升级、扩展和重启,使服务高内聚、低耦合,使得变更更容易。

  容器技术:容器技术可以提供更快的启动速度和更高的效率,容器技术是云原生的根基、没有容器技术就没有云原生。

1.2 云原生技术栈

  云原生技术栈是用于构建、管理和运行云原生应用程序的云原生技术分层。

  云原生技术栈中的几个重要组成部分:容器编排引擎、容器运行时、容器存储、容器网络

  容器编排引擎:容器编排引擎就是Kubernetes,向上对接容器管理平台,提供容器编排接口,向下通过容器运行时接口、容器存储接口、容器网络接口打通与物理基础设施的联动,作为全局资源的调度指挥官。

  容器运行时:容器运行时是抽象计算层资源的接口与实现,通过Linux namespace、cgroup操作计算层资源,为进程设置安全、隔离和可计量的执行环境,是应用真正的执行者,是整个云生技术栈的基石,可以说脱离了容器运行时,整个云原生技术栈也将毫无价值。

  容器存储:容器存储将底层存储服务暴露给容器和微服务使用,与软件定义存储(sofware defined storage,SDS)类似,通过容器存储层的抽象来屏蔽不同介质的存储资源。容器存储通过提供持久化的存储卷为有状态的容器应用提供存储服务。容器运行时、容器存储、容器网络共同构成了操作系统之上的抽象层。云原生生态系统通过容器存储接口(CSI)定义存储规范,鼓励各个存储提供商采用标准、可移植的方式为容器工作负载提供存储服务。

  容器网络:与容器存储类似,容器网络将物理网络基础设施抽象化,暴露给容器一个扁平网络,提供 pod 到 pod互访,node 到node 互访,pod 到服务互访,以及 pod 和外部通信的能力。与容器存储接口类似,云原生生态系统同样为容器网络提供了可扩展的通用接口(CNI)。通过CNI接口可以屏蔽底层网络实现的具体实现,便于接入多种不同的网络方案,如vxlan、vlan、ipvlan等。

1.3 containerd概述

  containerd 是一个工业标准的容器运行时,强调简单性、健壮性和可移植性。

  containerd在2019年2月28日从CNCF“毕业”,成为继Kubernetes、Prometheus、Envoy 和 CoreDNS 之后,第五个从CNCF“毕业”的项目。目前,containerd 作为业界标准的容器运行时已被广泛采用。

  containerd 可以作为Linux和Windows的守护进程,支持的功能如下:

   管理单个主机系统中容器的完整生命周期,如容器创建、启动、销毁等

   负责容器镜像的拉取和准备。

   负责容器的执行和状态指标监控。

   负责容器运行时的低级存储:镜像和容器数据的存储。

   管理容器网络接口和网络。

  containerd 旨在设计成被嵌入更大的系统中,如 Docker Kubernetes buildkit 等,而不是由开发人员直接使用。containerd 的总体架构如下图所示:

  containerd总体架构分为3层:ecosystem(生态层)、containerd(containerd内部架构)、system(系统层)。

  1.ecosystem(生态层)

  ecosystem分为Platform(平台)和Client(客户端)两层

   Platfomm:平台层与containerd的设计理念相吻合(即嵌入更大的系统中),作为工业标准的容器运行时通过屏蔽底层差异向上支撑多个平台,如谷歌GCP、亚马逊 Fargate、微软 Azure、Rancher等。

   Client:客户端是生态层连接containerd 的适配层,containerd 技术上还是经典的CS 架构,containerd 客户端通过gRPC调用containerd 服务端的 API进行操作。containerd暴露的接口有两类,一类是CRI,该接口是Kubermetes 定义的,用于对接不同容器运行时规范与抽象,containerd通过内置的CRI Plugin实现了CRI,该接口主要是向上对接Kubernetes 集群或 crictl;另一类是通过 containerd 提供的 Client SDK 来访问 containerd 自已定义的接口,该接口向上对接的主要是非Kubernetes类的上层PaaS或更高级的运行时,如Docker、BuildKit、ctr等。

  2.containerd(containerd 内部架构)

  containerd 层主要是 containerd 的 Server 实现层,逻辑上分为3层:API层、Core 层、Backend 层。

   API层:提供北向服务GRPC调用接口和Prometheus数据采集接口,API支持Kubernetes CRI标准和 containerd client 两种形式。

   Core层:该层是核心逻辑层,包含服务和元数据。

   Backend层:主要是南向对接操作系统容器运行时,支持通过不同的plugin 来扩展,这里比较重要的是 containerd-shim,containerd 通过 shim 对接不同的容器运行时,如kata、runc、runhs、gVisor、Firecracker等。

  3.system(系统层)

   system 层主要是 containerd 支持的底层操作系统及架构,当前支持 Windows 和 Linux,架构上支持x86和ARM。

2 使用containerd

  containerd 作为一个高级容器运行时,简单来说,是一个守护进程,在单个主机上管理完整的容器生命周期,包括创建、启动、停止容器以及存储镜像、配置挂载、配置网络等。

  containerd 本身设计旨在嵌入更大的系统中。例如,Docker底层通过containerd 来运行容器,Kubernetes 通过 CRI使用 containerd 来管理容器。除了Docker 与Kubernetes这种更上层的系统调用方式,还可以通过客户端命令行的方式来调用containerd,如ctr、nerdctl、crictl等命令行。containerd常见的使用方式如下图所示:

2.1 containerd的安装与部署

2.1.1 containerd的安装

  当前containerd官方提供了3种类型的安装包:containerd、cri-containerd、cri-containerd-cni。3种类型如下所示:

  containerd:仅包含containerd 相关二进制的精简安装包,格式为 containerd-${VERSION}.$(OS}-${ARCH}.tar.gz

  cri-containerd:包含containerd、cri与runc 工具的安装包,格式为 cri-containerd-${VERSION}.$(OS}-${ARCH}.tar.gz

  cri-containerd-cni:包含containerd、cri、runc、cni 具的安装包,格式为 cri-containerd-cni-${VERSION).$ (OS}-${ARCH}.tar.gz

  containerd当前支持Linux与Windows两种平台,对于macOS 的支持可通过“虚拟机(如lima”)+containerd”的方式实现,不过该方式不在官方的支持范围内

  系统运行环境如下:

root@containerd:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.3 LTS
Release:        24.04
Codename:       noble
root@containerd:~# uname -a
Linux containerd 6.8.0-83-generic #83-Ubuntu SMP PREEMPT_DYNAMIC Fri Sep  5 21:46:54 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
操作系统版本 内核版本 IP地址 用户名及密码
Ubuntu 24.04.3 6.8.0-83-generic 192.168.8.3 root/ubuntu
containerd/ubuntu

  1.下载containerd release安装包

root@containerd:~# wget https://github.com/containerd/containerd/releases/download/v1.7.1/cri-containerd-cni-1.7.1-linux-amd64.tar.gz

  2.将containerd解压到指定目录

# 可以看到cri-containerd-cni安装包包含的内容主要有containerd相关二进制、crictl、runc与常用的cni插件
root@containerd:~# tar xzvf cri-containerd-cni-1.7.1-linux-amd64.tar.gz -C /
cri-containerd.DEPRECATED.txt
etc/
etc/systemd/
etc/systemd/system/
etc/systemd/system/containerd.service
etc/crictl.yaml
etc/cni/
etc/cni/net.d/
etc/cni/net.d/10-containerd-net.conflist
usr/
usr/local/
usr/local/bin/
usr/local/bin/ctd-decoder
usr/local/bin/critest
usr/local/bin/containerd-stress
usr/local/bin/crictl
usr/local/bin/ctr
usr/local/bin/containerd-shim-runc-v2
usr/local/bin/containerd
usr/local/bin/containerd-shim
usr/local/bin/containerd-shim-runc-v1
usr/local/sbin/
usr/local/sbin/runc
opt/
opt/cni/
opt/cni/bin/
opt/cni/bin/loopback
opt/cni/bin/vlan
opt/cni/bin/dhcp
opt/cni/bin/bridge
opt/cni/bin/tuning
opt/cni/bin/bandwidth
opt/cni/bin/ptp
opt/cni/bin/ipvlan
opt/cni/bin/static
opt/cni/bin/vrf
opt/cni/bin/sbr
opt/cni/bin/dummy
opt/cni/bin/macvlan
opt/cni/bin/portmap
opt/cni/bin/host-local
opt/cni/bin/firewall
opt/cni/bin/host-device
opt/containerd/
opt/containerd/cluster/
opt/containerd/cluster/gce/
opt/containerd/cluster/gce/env
opt/containerd/cluster/gce/configure.sh
opt/containerd/cluster/gce/cloud-init/
opt/containerd/cluster/gce/cloud-init/node.yaml
opt/containerd/cluster/gce/cloud-init/master.yaml
opt/containerd/cluster/gce/cni.template
opt/containerd/cluster/version

  3.生成containerd默认配置文件

root@containerd:~# mkdir /etc/containerd
root@containerd:~# containerd config default > /etc/containerd/config.toml

  4.启动containerd

  containerd是通过systemd来管理的,安装包中包含对应的containerd.service文件:/etc/systemd/system/containerd.service

  通过systemctl启动containerd并设置开机自启:

root@containerd:~# systemctl start containerd && systemctl enable containerd
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /etc/systemd/system/containerd.service.


# 配置开机启动 containerd.service
#!/bin/sh
 
wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
# wget https://nas.liu12.com/k8s/containerd/containerd.service 
cp containerd.service /usr/local/lib/systemd/system/containerd.service
systemctl daemon-reload
systemctl enable containerd

# 启动成功后通过ctr version查看
root@containerd:~# ctr version
Client:
  Version:  v1.7.1
  Revision: 1677a17964311325ed1c31e2c0a3589ce6d5c30d
  Go version: go1.20.4

Server:
  Version:  v1.7.1
  Revision: 1677a17964311325ed1c31e2c0a3589ce6d5c30d
  UUID: ce8a3819-bab6-4ff0-8013-8b96658ddbe4

2.1.2 配置containerd.service


文章作者: 罗宇
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 罗宇 !
 上一篇
Kubernetes网络系统原理 Kubernetes网络系统原理
Kubernetes网络采用的是CNI标准,CNI的基本思想是:在创建容器时,先创建好网络命名空间,然后调用CNI插件为这个命名空间配置网络,最后再启动容器内的进程
2025-06-26
下一篇 
podman容器 podman容器
容器是由一个或多个与系统其余部分隔离的进程组成的集合,软件容器是打包应用以简化其部署和管理的一种方式
2025-05-28
  目录