小阿轩yx-Containerd初体验

39 篇文章 0 订阅
4 篇文章 0 订阅

小阿轩yx-Containerd初体验

Containerd 概述

什么是 Containerd

  • (Containerd Daemon)是一个开源运行时
  • 提供了一种标准化方式管理容器的声明周期
  • 该项目最初是由 Docker 开发团队创建,并在后来成为一个独立的项目被纳入了 Cloud Native Computing Foundation(云原生计算基金会 CNCF)的孵化项目中
主要特点

容器生命周期管理包括

  • 创建
  • 运行
  • 暂停
  • 恢复
  • 停止
  • 销毁

标准化接口

  • Containerd 提供了一个标准化容器运行时接口,使它可以与多个容器编排系统和工具集成,例如 kubernetes、Doker Compose 等

镜像管理

  • 它支持容器镜像的拉取、推送、保存和加载等操作。Containerd 使用 OCI(Open Container Initiaive)规范定义容器镜像的格式

插件体系结构

  • Containerd 可以在不同的操作系统上运行,允许用户通过插件扩展其功能,例如存储驱动、网路插件等

跨平台支持

  • Containerd 可以在不同的操作系统上运行,从而提供了跨平台的支持

与 Kubernetes 集成

  • Containerd 作为 Kubernetes 的默认容器运行时,与 Kubernetes 紧密集成,为容器工作负载的管理提供了良好的支持

安全性和隔离

  • Containerd 实现了严格的容器隔离和安全性措施,确保容器之间的隔离性以及对主机系统的支持

Containerd

  • 提供了一个轻量级、高度可定制的容器运行时,为容器生态系统发展提供了稳定和可靠的基础。
  • 在容器生命周期管理、镜像管理和插件支持等方面为用户提供了丰富的功能

Containerd 的起源与背景

起源

  • 可以追溯到 Docker 项目

Docker

  • 最初作为开源项目推出,旨在简化应用程序的打包、分发和部署过程
  • 引入了容器的概念,将应用程序和依赖打包到一个容器中,使得应用在不同环境中可以一致运行
  • 随着 Docker 发展,架构逐渐变的复杂,包含的功能有镜像构建、服务编排等
  • 为了更好组织和管理,Docker 团队将 Docker 引擎拆分成多个组件,其中一个关键组件就是 Containerd

Docker 架构拆分

  • 从单一的大型引擎拆分为一系列小型、可复用的组件
  • 这种拆分的目标是提高可维护性、模块化和可扩展性

Containerd 作为核心运行时

  • 在 Docker 架构拆分后,containerd 被定位为 Docker 的核心容器运行时。
  • 它负责管理容器的生命周期、镜像操作和基本运行时功能。

贡献给 CNCF

  • 为了推动 Containerd 的发展,Docker 团队将 containerd 的代码捐赠给了Cloud Native computing Foundation(CNCF),使其成为 CNCF 的孵化项目。

容器生态系统的标准化

  • Containerd 的设计遵循 0pen container Initiative(开放容器倡议OCI)规范,这是一个关注容器运行时和镜像格式标准化的开放标准组织。
  • 这意味着 containerd 可以与符合 OCI 规范的其他容器工具和运行时进行互操作。

独立的容器运行时

  • containerd 不仅仅局限于 Docker,它可以作为独立的容器运行时,与多个容器编排系统和工具集成,从而为用户提供更多选择。

containerd

  • 起源是为了简化容器运行时的管理,并为容器生态系统提供一个开放、标准化的基础。
  • 其发展不仅服务于 Docker 生态系统,还为整个容器领域提供了一个通用的、可扩展的容器运行时。

Containerd 架构

Containerd 架构概述

  • 是 modularty(模块化)和可扩展性的体现,它被设计为一个轻量级、高度可定制的容器运行时

  • Containerd 也是采用 C/S 架构
  • 服务端通过 unix domain socket 暴露底层的 gRPC API 接口出去
  • 客户端通过这些 API 两个节点上的容器
  • 每个 Containerd 只负责一台机器,Pull 镜像,对容器的操作(启动、停止等),网络,存储
  • 具体运行容器由 runc 负责

解耦

  • 将系统划分成不同的组件,每个组件由一个或多个模块协作完成(Core部分),每种类型的模块都以插件形式集成到 Containerd 中

核心组件解析

Containerd 组件分为三个方面

Stroage(存储)
Content(内容)

功能

  • 存储了容器镜像的实际数据
  • 包括文件系统层和元数据,用于创建和管理容器的基础文件系统
Snapshot(快照)

功能

  • Snapshot 存储容器的快照数据。
  • 每个容器都可以有一个或多个快照,允许它们共享相同的文件系统层,提高效率。
Diff(差异)

功能

  • Diff 存储容器文件系统层之间的差异。
  • 当容器运行时需要修改文件系统时,会在已有的文件系统层上创建一个差异层,以保存变更。
Metadata(元数据)
Images(镜像)

功能

  • Images 存储容器镜像的元数据,包括镜像的标签、大小、创建时间等信息。
  • Metadata 中的 Images组件允许容器对镜像进行管理和操作。
Containers(容器)
  • Containers 存储容器的元数据,包括容器的状态、配置信息、网络设置等。
  • 这部分元数据使得功能:(容器能够有效地管理容器的生命周期。
Runtime(运行时)
Tasks(任务)

功能

  • Tasks 包含容器内的进程组。
  • 每个容器运行时都有关联的 Task,它负责管理容器内的所有进程Tasks 与 Shim 一起工作,维护容器的状态。
Events(事件)

功能

  • Events 组件记录了容器的各种事件,如容器的创建、启动、停止等。
  • 这些事件可以用于监控和日志记录,帮助用户了解容器系统的运行状况。

安装 Containerd

恢复OK快照,直接使用 Containerd

设置阿里云仓库

[root@localhost ~]# rm -rf /etc/yum.repos.d/*
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum clean all

使用阿里云开源镜像站容器部署 docker 仓库(只部署不安装)

http://aliyun.com

# step 1: 安装必要的一些系统工具
[root@loaclhost ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
[root@loaclhost ~]# sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
[root@loaclhost ~]# sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo

清空 yum 缓存

[root@localhost ~]# yum clean all

用 yum 查看安装包信息

[root@localhost ~]# yum list containerd.io
......
可安装的软件包
containerd.io.x86 64    1.6.33-3.1.el7    docker-ce-stable

查看历史版本信息

[root@localhost ~]# yum list containerd.io --showduplicates

安装最新版 containerd.io

[root@localhost ~]# yum -y install containerd.io

配置 Containerd

显示默认配置参数

[root@localhost ~]# containerd config default

进入 Containerd 目录

[root@localhost ~]# cd /etc/containerd/

使用重定向生成配置文件

[root@localhost ~]# containerd config default > config.toml

配置镜像加速

修改配置文件

[root@localhost ~]# vim config.toml
# 找到[plugins."io.containerd.grpc.v1.cri".registry.mirrors]在下面添加阿里云的镜像源
# 156行
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
    [plugins."io.containerd.grpc.v1.cri".registry.config."docker.io"]
        endpoint = ["https://registry.cn-hangzhou.aliyuncs.com" ,"https://registry-1.docker.io"]

启动服务并设置开机自启

[root@localhost ~]# systemctl start containerd
[root@localhost ~]# systemctl enable containerd

退出到家目录并查看版本

[root@localhost ~]# cd
[root@localhost ~]# ctr version
Client:
    Version:1.6.33
    Revision:10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
    Go version: go1.21.11

Server:
    Version:1.6.33
    Revision:10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
    UUID:067172f0-071d-44c2-bdea-9c402ad39745
  • go:是 Go 语言

Containerd 基本操作

镜像类操作

拉取镜像
  • 使用 ctr images pull 目录可以下载镜像

命令格式

ctr images pull 镜像名称:[镜像标签]

使用国内提供的可信镜像中心

https://atomhub.openatom.cn

  • 搜索并选用 amd64/nginx 镜像
[root@localhost ~]# ctr images pull hub.atomgit.com/amd64/nginx:1.25.2-perl
unpacking linux/amd64 sha256:615eb6b7237368628ba586460fdab74bccbd4610533f59717ef2cc7c25458efc..
done: 4.042630851s

查看本地有哪些镜像可以使用

[root@localhost ~]# ctr images ls
REF                                                                                TYPE
DIGEST                                                                             SIZE
PLATFORMS
LABELS
docker.io/library/nginx:latest
application/vnd.docker.distribution.manifest.list.v2+json
sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee 67.3 MiBlinux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x-
修改镜像名称

命令格式

ctr images rm 镜像名称:[镜像标签]
[root@localhost ~]# ctr images tag hub.atomgit.com/amd64/nginx:1.25.2-perl nginx:v1
nginx:v1
[root@localhost ~]# ctr images tag hub.atomgit.com/amd64/nginx:1.25.2-perl nginx:v2
nginx:v2
删除镜像

命令格式

ctr images rm 镜像名称:[镜像标签]
[root@localhost ~]# ctr images nginx:v2
nginx:v2
镜像挂载到主机目录
  • Containerd 可以把镜像直接挂载到宿主机得到某个目录

命令格式

ctr images mount 镜像 挂载点
[root@localhost ~]# ctr images mount nginx:v1 /mnt
sha256:fe7723b2df19ccf75328cb1f39c90c2332679144231501f3d4d00f51b16c2867
/mnt

查看是否挂载成功

[root@localhsot ~]# ls -l /mnt/
total 8
lrwxrwxrwx 1 root root    7 Nov 19 19:00 bin ->usr/bin 
drwxr-xr-x 2 root root    6 Sep 29 16:04 boot
drwxr-xr-x 2 root root    6 Nov 19 19:00 dev
drwxr-xr-x 1 root root    41 Nov 21 04:05 docker-entrypoint.d
-rwxrwxr-x 1 root root    1620 Nov 21 04:05 docker-entrypoint.sh
drwxr-xr-x 1 root root    4096 Nov 21 04:05 etc
drwxr-xr-x 2 root root    6 Sep 29 16:04 home
lrwxrwxrwx 1 root root    7 Nov 19 19:00 lib ->usr/lib.
lrwxrwxrwx 1 root root    9 Nov 19 19:00 lib32 ->usr/lib32
lrwxrwxrwx 1 root root    9 Nov 19 19:00 lib64 ->usr/lib64
lrwxrwxrwx 1 root root    10 Nov 19 19:00 libx32 ->usr/libx32
drwxr-xr-x 2 root root    6 Nov 19 19:00 media
drwxr-xr-x 2 root root    6 Nov 19 19:00 mnt
drwxr-xr-x 2 root root    6 Nov 19 19:00 opt
drwxr-xr-x 2 root root    6 Sep 29 16:04 proc
drwx------ 2 root root    37 Nov 19 19:00 root
drwxr-xr-x 3 root root    18 Nov 19 19:00 run
lrwxrwxrwx 1 root root    8 Nov 19 19:00 sbin ->usr/sbin
drwxr-xr-x 2 root root    6 Nov 19 19:00 srv
drwxr-xr-x 2 root root    6 Sep 29 16:04 sys
drwxrwxrwt 1 root root    6 Nov 21 04:05  tmp
drwxr-xr-x 1 root root    66 Nov 19 19:00 var
drwxr-xr-x 1 root root    41 Nov 19 19:00 var
镜像从主机目录卸载
  • 用于将挂载到宿主机的镜像卸载下来

命令格式

ctr images unmount 挂载点
[root@localhost ~]# ctr images unmount /mnt
  • 这一步要重启主机才能取消挂载成功
镜像导出
  • 该命令主要用于把镜像保存成文件

命令格式

ctr images export --all-platforms 文件名 镜像名称:[镜像标签]
[root@localhost ~]# ctr images export --all-platforms nginx_v1.tar nginx:v1
ctr:                               content                                  digest
sha256:b50f4e222b2d749d6a999baf30df1d6090d47f2ae855ae80ede69d6dddf5b58c: not found

有时候镜像导入导出的时候会有报错

ctr:content digest sha256:xxxxxx not found

解决办法如下

[root@localhost ~]# ctr images rm nginx:v1
[root@localhost ~]# ctr images pull --all-platforms nginx:v1
[root@localhost ~]# ctr images export --all-platforms nginx latest.tar nginx:v1
docker.io/library/nginx:latest
镜像导入
  • 该命令主要用于把文件加载为镜像

命令格式

ctr images import 文件名
[root@localhost ~]# ctr images import nginx_v1.tar
unpacking    docker.io/library/nginx:latest
(sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee)...done

容器类操作

  • Containerd 本身并没有直接支持端口映射的功能。
  • 端口映射通常是由容器运行时(比如 Docker、CRI-0)负责的,而不是容器的底层运行时(Containerd)。
创建容器

基于 docker.io/library/nginx:latest 镜像创建一个叫 nginx 的容器

[root@localhost ~]# ctr containers create nginx:v1 nginx

查看容器

[root@localhost ~]# ctr containers ls
CONTAINER    IMAGE            RUNTIME
nginx        nginx:v1         io.containerd.runc.v2

查看容器详细信息

[root@localhost ~]# ctr containers info nginx

任务类操作

  • 通过 container create 命令创建的容器,并没有处于运行状态,只是一个静态的容器(仅仅只是一个创建容器的声明)。
  • 一个 container 对象只是包含了运行一个容器所需的资源及相关配置数据表示 namespaces、rootfs 和容器的配置都已经初始化成功了,只是用户进程还没有启动。
  • 一个容器真正运行起来是由 Task 任务实现的。
启动容器
[root@localhost ~]# ctr task start -d nginx

查看容器状态

[root@localhost ~]# ctr task ls
TASK        PID        STATUS
nginx       1616       RUNNING

进入容器

  • 进入到容器里面
  • 不过这里需要注意必须要指定 --exec-id 参数,这个 id 可以随便写,只要唯一就行
[root@localhost ~]# ctr task exec --exec-id 0 -t nginx sh
# ls
bin    docker-entrypoint.d    home    lib64    mnt    root    srv    usr
boot   docker-entrypoint.sh   lib     linx32   opt    run     sys    var
dev etc                       lib32   media    aproc  sbin    tmp

暂停容器

[root@localhost ~]# ctr task pause nginx

查看状态

[root@localhost ~]# ctr task ls
TASK        PID        STATUS
nginx       1616       PAUSED

恢复容器

[root@localhost ~]# ctr task resume nginx

查看状态

[root@localhost ~]# ctr task ls
TASK        PID        STATUS
nginx       1616      RUNNING
杀死容器
  • 杀死容器,ctr 没有 stop 容器的功能,只能暂停或者杀死容器
[root@localhost ~]# ctr task kill nginx

查看状态

[root@localhost ~]# ctr task ls
TASK    PID        STATUS
nginx   1616      STOPPED
删除容器
[root@localhost ~]# ctr task rm nginx
  • 虽然已经删除了任务,但是创建容器的时候,也创建了一个同名的快照,即便已经删除了任务,也可以使用“ ctr task start -d nginx ”命令,利用此快照将已删除的任务启动起来,使得此容器恢复运行 

查看状态

[root@localhost ~]# ctr task ls
TASK        PID        STATUS

利用快照启动容器

[root@localhost ~]# ctr task start -d nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt toperform configuration/
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/

查看状态

[root@localhost ~]# ctr task ls
TASK    PID       STATUS
nginx   1817      RUNNING
彻底删除容器

杀死容器并删除容器

[root@localhost ~]# ctr task kill nginx
[root@localhost ~]# ctr task rm nginx

查看快照

[root@localhost ~]# ctr snapshot ls

删除快照

[root@localhost ~]# ctr snapshot rm nginx
  • 删除容器后快照也就没有了

再重新创建容器

[root@localhost ~]# ctr containers create nginx:v1 nginx

再启动容器

[root@localhost ~]# ctr task start -d nginx

获取容器的内存、CPU 和 PID 的限额与使用量

[root@localhost ~]# ctr task metrics nginx
ID                        TIMESTAMP
nginx                     2023-11-27 08:58:19.17333324 +0000 UTC
METRIC                    VALUE
memory.usage_in_bytes     36365002
memory.limit in bytes     9223372036854771712
memory.stat.cache         86016
cpuacct.usage             58094617
cpuacct.usage_percpu      [16788433 19576569]
pids.current              3
pids.limit                0

查看所有进程在宿主机的 pid

[root@localhost ~]# ctr task ps nginx
PID        INFO
12980        -
13007        -
13008        -
[root@localhost ~]# ps -ef | grep 12980
root    12980    12961 0    04:00 ?        00:00:00 nginx:master process nginx-gdaemon off;
101     13007    12980 0    04:00 ?        00:00:00 nginx:worker process
101     13008    12980 0    04:00 ?        00:00:00 nginx:worker process
root    13034    1512  0    04:01pts/0     00:00:00 grep --color=auto 12980

其它操作

插件
  • Containerd 中,插件是一种机制,允许你通过加载和卸载插件来扩展 Containerd 的功能。
  • Containerd 使用插件化的设计,使其可以适应各种不同的容器运行时和容器生态系统需求。
  • 这些插件允许用户根据实际需求选择性地配置 Containerd,并与其他容器运行时和编排系统进行集成。
  • 插件化的设计使得 Containerd 可以灵活地适应不同的使用场景,并且可以方便地进行定制和扩展。
  • 每个插件都提供了标准化的接口,使得插件可以被轻松地替换或新增,从而满足不同用户和组织的需求。

插件可以提供不同的功能

  • 容器存储
  • 网络
  • 监控

常见的一些插件类型

Shim 插件

  • 负责管理容器的生命周期。
  • 当一个容器任务启动时,Containerd 会调用相应的 Shim 插件来创建并监管容器进程。
  • 负责与容器进程的通信,以及监控容器的状态。

Snapshotter 插件

  • 处理容器的文件系统快照(Snapshots)。
  • 它定义了容器文件系统的结构,支持创建、管理和销毁快照,以及在不同容器之间共享文件系统层。

Task 插件

  • 用于管理容器中的任务(Task)。
  • 任务表示容器内运行的一个或多个进程,它与容器的生命周期直接相关。

Image 插件

  • 负责处理容器镜像的拉取、推送、删除等操作。
  • 它定义了容器镜像的存储和元数据结构。包括镜像层和元数据。

Content store 插件

  • 管理容器内容(Content),它定义了内容的存储和检索方式。

查看当前所有的插件

[root@localhost ~]# ctr plugins ls

命名空间

  • Containerd 中,命名空间(Namespace)是一种用于隔离资源和进程视图的 Linux 内核特性。
  • 命名空间允许在同一主机上运行的进程拥有不同的资源视图,从而实现资源隔离。
  • Containerd 利用 Linux 的命名空间实现容器的隔离。
  • 这些命名空间的组合使得容器可以在相对独立的环境中运行,避免了与主机系统或其他容器的干扰。
  • Containerd 利用这些命名空间实现容器的隔离和资源管理,确保容器化应用在共享主机资源的同时获得适当的隔离。

Containerd 中使用的一些主要命名空间类型

PID 命名空间

  • 它隔离了进程 ID 空间,使得在不同 PID 命名空间中的进程看起来像是在独立的系统中运行一样。这有助于确保容器中的进程无法看到或影响主机系统上的其他进程。

Network 命名空间

  • 它隔离了网络栈,使得容器内的网络环境独立于主机系统。
  • 每个容器可以有自己的网络接口、IP 地址和端口等,而不会影响其他容器或主机上的网络配置。

Mount 命名空间

  • 它隔离了文件系统挂载点,使得容器拥有自己的文件系统视图。
  • 这样每个容器都可以有自己的文件系统,不受其他容器的影响。

UTS 命名空间

  • 它隔离了主机名和域名,允许容器具有自己的主机名和域名。

IPC 命名空间

  • 它隔离了进程间通信(IPC)资源,使得容器内的进程无法直接与主机系统或其他容器共享 IPC 资源。

User 命名空间

  • 它隔离了用户和用户组的视图,使得容器中的进程可以在容器内部以不同的用户和组身份运行,而在主机系统上的实际用户和组不受影响。

查看都有哪些命名空间

[root@localhost ~]# ctr ns ls
NAME        LABELS
default

创建命名空间并查看

[root@localhost ~]# ctr ns create test
[root@localhost ~]# ctr ns ls
NAME        LABELS
default
test

在做操作的时候可以使用 -n 来指定命名空间,如果不指定表示操作的是默认的命名空间,比如查看 k8s.io 命名空间的镜像

[root@localhost ~]# ctr -n test images pull hub.atomgit.com/amd64/nginx:1.25.2-perl

修改镜像名称

[root@localhost ~]# ctr -n test containers create hub.atomgit.com/amd64/nginx:1.25.2-perl nginx

小阿轩yx-Containerd初体验

  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值