ContainerD 容器目录结构探究

启动容器

作为开始,我们需要去启动一个容器。

你可以通过命令行的方式来启动一个容器,例如:

ctr i pull docker.io/library/nginx:alpine
ctr c create docker.io/library/nginx:alpine nginx
ctr task start -d nginx

或者一步到位:

ctr run -d docker.io/library/nginx:alpine nginx

也可以通过编程的方式来启动

import (
"context" "github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/namespaces"
) func main() {
client, err := containerd.New("/run/containerd/containerd.sock")
defer client.Close() context = context.Background()
// create a context for docker
redis, err := client.NewContainer(context, "redis-master")
defer redis.Delete(context)
// create a new task
task, err := redis.NewTask(context, cio.NewCreator(cio.WithStdio))
defer task.Delete(context) // the task is now running and has a pid that can be used to setup networking
// or other runtime settings outside of containerd
pid := task.Pid() // start the redis-server process inside the container
err := task.Start(context) // wait for the task to exit and get the exit status
status, err := task.Wait(context)
}

相关概念

在 containerd 中,container 这个概念特指一个元数据对象。诸如OCI运行时规范、镜像、根文件系统和其他元数据等资源可以附加到一个 container上。例如:

redis, err := client.NewContainer(context, "redis-master",
containerd.WithNewSnapshot("redis-rootfs", image),
containerd.WithNewSpec(oci.WithImageConfig(image)),
)

要启动像 docker 那样的容器,在 containerd 中称为 task ,一个 task 就是对进程(容器就是一个进程)的封装。所以在上述代码中,我们都是对一个 task 来进行这么一个 创建/进入/暂停/删除 等操作。

在第二个参考资料中有人探讨了为何这么设计。

容器存储

containerd 有两个不同的存储路径,一个用来持久化,一个用来保存运行时状态。

root = "/var/lib/containerd"
state = "/run/containerd"

我们知道docker使用的是overlayfs2,那containerd呢?官网有这么一句话:

\(\text{containerd allows you to use overlay or snapshot filesystems with your containers. It comes with built-in support for overlayfs and btrfs.}\)

containerd 内置支持 overlayfs 和 btrfs ,overlayfs我们再熟悉不过了, btrfs 可能不太熟悉,不用慌,下面稍微介绍一下 btrfs。

我们现在安装linux系统时有时会让你选择文件系统例如 ext2/ext3/ext4 等,而 btrfs 被 ext4 的作者称赞且认为其是下一代linux标准文件系统。但是目前 btrfs 还是在开发中,生产环境中 btrfs 还是很少。

对于 overlayfs ,我们知道docker的容器分为 LowerDir 和 UpperDir ,LowerDir 为镜像层文件,UpperDir 为容器目录,合起来就是 MergeDir ,就是最终看到的容器目录。那 containerd 的 LowerDir 和 UpperDir 分别在哪呢?

root@aliyun:~# mount | grep /var/lib/containerd
overlay on /run/containerd/io.containerd.runtime.v2.task/default/nginx/rootfs type overlay (rw,relatime,lowerdir=/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/7/fs:/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/6/fs:/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/5/fs:/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/4/fs:/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/3/fs:/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/2/fs:/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/1/fs,upperdir=/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/8/fs,workdir=/var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/8/work,xino=off)

使用 mount 命令,他直接就给你显示了

参考资料

  1. https://icloudnative.io/posts/getting-started-with-containerd
  2. https://github.com/containerd/containerd/discussions/4458
  3. [https://github.com/containerd/containerd
  4. https://blog.frognew.com/2021/06/relearning-container-09.html

containerd容器存储探究的更多相关文章

  1. Kubernetes将弃用Docker!与 containerd容器引擎

    时间戳:2022-06-07 20:32:19 星期二 撰写文档参考:(阿良-腾讯课堂)Kubernetes将弃用Docker 参考博客k8s入坑之路(3)containerd容器 container ...

  2. SQLServer2012 (非)聚集索引存储探究

    SQLServer2012 (非)聚集索引存储探究 Author:zfive5(zidong) Email:zfive5@163.com 引子 因为写了前一篇文字<SQLServer2012 表 ...

  3. log4j容器初始化探究

    Log4j容器初始化探究 Log4j第一步就是初始化Logger容器Repository,这一章我们来探究Logger容器,从别从独立应用以及servlet容器下启动初始化两方面探究. 1 独立应用 ...

  4. kubernetes/k8s CSI分析-容器存储接口分析

    更多 k8s CSI 的分析,可以查看这篇博客kubernetes ceph-csi分析,以 ceph-csi 为例,做了详细的源码分析. 概述 kubernetes的设计初衷是支持可插拔架构,从而利 ...

  5. docker容器存储

    写在前面 我们在上篇学习了容器网络,对容器网络驱动bridge工作原理做了较为详细的介绍,今天小作文一起看看容器中另一个关键域-存储. 容器的存储可以分为两大类: 一种是与镜像相关的即我们在<d ...

  6. Longhorn 企业级云原生分布式容器存储-券(Volume)和节点(Node)

    内容来源于官方 Longhorn 1.1.2 英文技术手册. 系列 Longhorn 是什么? Longhorn 云原生分布式块存储解决方案设计架构和概念 Longhorn 企业级云原生容器存储解决方 ...

  7. 使用kubeoperator安装的k8s集群以及采用的containerd容器运行时,关于采用的是cgroup 驱动还是systemd 驱动的说明

    使用kubeoperator安装的k8s集群,默认使用的是systemd驱动 # kubectl get cm -n kube-system NAME DATA AGE calico-config 4 ...

  8. Longhorn 企业级云原生容器存储解决方案-部署篇

    内容来源于官方 Longhorn 1.1.2 英文技术手册. 系列 Longhorn 是什么? Longhorn 云原生分布式块存储解决方案设计架构和概念 安装 Longhorn 可以通过多种方式安装 ...

  9. docker镜像与容器存储结构分析

    注意:转载请注明出处:http://www.programfish.com/blog/?p=9 Docker是一个开源的应用容器引擎,主要利用linux内核namespace实现沙盒隔离,用cgrou ...

  10. STL容器存储的内容动态分配情况下的内存管理

    主要分两种情况:存储的内容是指针:存储的内容是实际对象. 看以下两段代码, typedef pair<VirObjTYPE, std::list<CheckID>*> VirO ...

随机推荐

  1. Java8Stream流

    Stream流呢,以前我也有所了解,像一些面试题中也出现过,Java8的新特性,有一块就是这个Stream操作集合,而且在看一些项目中也使用的比较多.但总感觉自己学的一知半解,所以今天打算系统的过一下 ...

  2. .NET Core MongoDB的简单使用

    一.创建测试库.测试表.添加测试数据 使用之前文章提到的MongoDB Compass用法分别添加数据库[myDb]和集合(表)[userinfos]信息, 参考链接为:MongoDB Compass ...

  3. python3中,len()、isalpha()、isspace()、isdigit()、isalnum()实例

    # 实例:使用while循环 import string s1 = input('请输入一个字符串:\n') letters = 0 space = 0 digit = 0 others = 0 i  ...

  4. The Missing Semester - 第四讲 学习笔记

    第四讲 数据整理 课程视频地址:https://www.bilibili.com/video/BV1ym4y197iZ 课程讲义地址:https://missing-semester-cn.githu ...

  5. Zstack迁移实战记录1

    https://blog.csdn.net/weixin_43767046/article/details/113748775 这段时间除了那个重度烤机测试(上面链接),还在做另一件事,想再做一个服务 ...

  6. JZOJ 3281. 【GDOI2013】字母连接

    \(\text{Solution}\) 一眼不会,限制有点多... 那就网络流 发下确实是很简单的建图 枚举起点集合 拆点后就很好满足限制了 \(\text{Code}\) #include < ...

  7. KMP字符串 AcWing 831

    题目:https://www.acwing.com/problem/content/833/ 题意:求子串在母串中每次出现时的下标位置. 题解:哈哈哈,敲题时想到之前看到一个人叫 kmp 算法为 看毛 ...

  8. 自己从零写操作系统GrapeOS系列教程——4.GrapeOS开发环境介绍

    1. 开发环境简介 为了减少开发过程中不必要的麻烦,希望大家的开发环境尽量与我的保持一致. 我的开发环境如下: Windows10电脑一台 Visual Studio Code(最好是最新版) Vir ...

  9. Postgresql实现不同用跨模式访问

    1.修改参数 2.创建两个用户 创建a1 create user a1 connection limit-1 password '123456'; alter user a1 SUPERUSER; g ...

  10. 重磅!flink-table-store 将作为独立数据湖项目重新加入 Apache

    数据湖是大数据近年来的网红项目,大家熟知的开源数据湖三剑客 Apache hudi.Apache iceberg .Databricks delta 近年来野蛮生长,目前各自背后也都有商业公司支持,投 ...