图解 containerd 核心架构:从 Snapshotter 到 Task

1. 整体架构概览

containerd 采用分层架构设计,核心组件协同管理容器生命周期:

+-------------------+
|     Client API    |  ← 用户接口层 (ctr, nerdctl等)
+-------------------+
         ↓
+-------------------+
|     Service       |  ← 核心服务层 (GRPC服务)
+-------------------+
         ↓
+-------------------+
|   Container       |  ← 容器配置管理层
|   +------------+ |
|   | Snapshotter| |  ← 文件系统快照管理
|   +------------+ |
|   +------------+ |
|   |   Image    | |  ← 镜像管理
|   +------------+ |
|   +------------+ |
|   |    Task    | |  ← 容器运行时实例
|   +------------+ |
+-------------------+
         ↓
+-------------------+
|   Runtime (runc)  |  ← 底层运行时实现
+-------------------+

2. 核心组件详解

a) Snapshotter(快照管理器)

  • 作用:管理容器文件系统的分层结构
  • 工作流程
    1. 从镜像中提取文件系统层(Layer)
    2. 创建可写层(upperdir)
    3. 构建联合挂载视图(mergeddir)
  • 关键操作
    // 创建快照示例
    mount, err := snapshotter.Prepare(ctx, "snapshot-id", parentID)
    

b) Image(镜像管理器)

  • 作用:存储镜像元数据和内容寻址块
  • 核心流程
    graph LR
    A[镜像拉取] --> B[内容校验]
    B --> C[元数据存储]
    C --> D[解压到Snapshotter]
    

c) Container(容器对象)

  • 作用:存储容器静态配置
  • 关键属性
    {
      "id": "container-id",
      "image": "nginx:latest",
      "spec": { ... },  // OCI 运行时规范
      "snapshotKey": "snap-1"
    }
    

d) Task(任务实例)

  • 作用:管理容器运行时进程
  • 生命周期操作
    task, err := container.NewTask(ctx, cio.NewCreator(cio.Stdio))
    task.Start(ctx)      // 启动进程
    task.Pause(ctx)      // 暂停进程
    task.Resume(ctx)     // 恢复进程
    task.Kill(ctx, syscall.SIGTERM) // 终止进程
    

3. 容器生命周期全流程
sequenceDiagram
    participant C as Client
    participant S as Snapshotter
    participant I as Image
    participant CT as Container
    participant T as Task

    C->>I: 拉取镜像(pull)
    I->>S: 解压镜像层(unpack)
    S-->>I: 返回快照ID
    C->>CT: 创建容器(create)
    CT->>S: 引用快照
    C->>T: 创建任务(newTask)
    T->>Runtime: 启动进程(runc)
    Runtime-->>T: 返回PID
    C->>T: 执行操作(start/pause/stop)
    T->>Runtime: 调用运行时接口

4. 关键数据流示例

容器启动时资源准备:

+--------+     +-------+     +------------+     +------+
| Image  | →   | Layer | →   | Snapshotter| →   | Task |
+--------+     +-------+     +------------+     +------+
     ↓              ↓               ↓               ↓
OCI镜像索引  内容可寻址存储  联合文件系统视图  进程命名空间+cgroups

5. 典型操作代码
// 完整生命周期示例
ctx := context.Background()

// 1. 拉取镜像
image, _ := client.Pull(ctx, "docker.io/library/nginx:latest")

// 2. 创建快照
snapshotter := client.SnapshotService("overlayfs")
diffIDs, _ := image.RootFS(ctx, client.ContentStore())
snapshotKey := "nginx-snapshot"
mounts, _ := snapshotter.Prepare(ctx, snapshotKey, "", snapshots.WithLabels(diffIDs))

// 3. 创建容器
container, _ := client.NewContainer(ctx, "nginx-container",
	containerd.WithImage(image),
	containerd.WithSnapshot(snapshotKey),
	containerd.WithNewSnapshotView(),
)

// 4. 创建任务
task, _ := container.NewTask(ctx, cio.NewCreator(cio.Stdio))
task.Start(ctx)

// 5. 停止容器
task.Kill(ctx, syscall.SIGTERM)
task.Wait(ctx)
task.Delete(ctx)
container.Delete(ctx)

6. 组件交互关系

$$ \text{Client} \xrightarrow{\text{GRPC}} \text{Service} \xrightarrow{\text{管理}} \begin{pmatrix} \text{Snapshotter} \ \text{Image} \ \text{Container} \ \text{Task} \end{pmatrix} \xrightarrow{\text{驱动}} \text{Runtime} $$

通过此架构,containerd 实现了:

  • 镜像与运行时的解耦
  • 原子化的生命周期操作
  • 标准化的 OCI 接口支持
  • 高效的文件系统管理
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐