Containerd 初体验

一、containerd 概述

1. Containerd 基本概念

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

        Containerd的主要特点和功能:

容器生命周期管理:包括容器创建,运行,暂停,恢复,停止,销毁。

标准化接口:提供标准化接口,可以和多个容器集成。

镜像管理:支持容器镜像拉取,推送,保存。使用OCI (open   container   initiative,开放式容器倡议)规范镜像格式。

插件体系结构:具备扩展插件体系结构,可以扩展功能。如:存储驱动,网络插件。

跨平台支持:可以在不同操作系统上运行。

与kubernetes集成:containerd是kubernetes默认容器运行。

安全性和隔离性:确保容器隔离性和对主机系统的安全性。

总结,Containerd提供轻量级,可定制容器运行,为容器提供稳定,可靠基础。且提供丰富功能。

 2.Containerd架构概述

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

       可以看出 Containerd 采用的也是 C/S 架构,服务端通过 unix domain socket 暴露低层的 aRPC API 接口出去,客户端通过这些 API 管理节点上的容器,每个 Containerd 只负责一台机器,Pull 镜像,对容器的操作 (启动、停止等),网络,存储都是由 Containerd完成。具体运行容器由 runc 负责。
      为了解耦,Containerd 将系统划分成了不同的组件,每个组件都由一个或多个模块协
作完成 (Core 部分),每一种类型的模块都以插件的形式集成到 Containerd 中。

3.核心组件解析

Containerd组件可以分为存储Storage,元数据Metadata和运行时Runtime。

 Storage

Content(内容):存储镜像实际数据,用于创建和管理容器基础文件系统。

Snapshot(快照):存储快照数据,每个容器可以有一个多个快照,进行共享文件系统,提高效率。

Diff(差异):文件系统之间的差异,运行需要修改文件系统时,会在文件系统上创建一个差异层,保存变更。

Metadata

Images(镜像):存储容器镜像的元数据,包括标签,大小,创建时间。

Containers(容器):存储容器元数据,包括容器状态,配置状态,网络配置。

Runtime

Tasks(任务):负责容器内所有进程。

Events(事件):记录各种事件,如:容器创建,启动,停止。用于监控和日志记录。

二、安装和配置Containerd

1.安装containerd

安装aliyun仓库

rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum clean all

安装docker-ce

sudo yum install -y yum-utils device-mapper-persistent-data lvm2 # Step 2: 添加软件源信息 sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # Step 3 sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo

[root@localhost ~]# yum clean  all      (清除yum缓存)

[root@localhost ~]# yum list containerd.io            (检查版本信息)

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

生成配置文件
[root@localhost ~]# mkdir -p /etc/containerd/
[root@localhost ~]# cd /etc/containerd/
[root@localhost containerd]# ls
config.toml
[root@localhost containerd]# containerd config default > config.toml 
[root@localhost containerd]# vim config.toml 
      [plugins."io.containerd.grpc.v1.cri".registry.mirrors]                (添加#155)
        [plugins."io.containerd.grpc.v1.cri".registry.config."docker.io"]
          endpoint = ["https://registry.cn-hangzhou.aliyuncs.com" ,"https://registry-1.docker.io"]

[root@localhost containerd]# systemctl start containerd                (开启服务)
[root@localhost containerd]# systemctl enable containerd              (开机自启)
[root@localhost containerd]# ctr version                 (containerd   版本)
 

2.containerd的基本操作

(1)镜像类操作

拉取镜像    ctr  images  pull  镜像名称
[root@localhost ~]# ctr images pull hub.atomgit.com/amd64/nginx:1.25.2-perl

备注:
本案例以国内可信镜像中心nginx为例。

[root@localhost ~]# ctr images ls         (查看镜像)

重命名
ctr  images tag  当前镜像名称  新镜像名称
[root@localhost ~]# ctr images tag hub.atomgit.com/amd64/nginx:1.25.2-perl nginx:v1

删除
[root@localhost ~]# ctr images rm nginx:v2

挂载
[root@localhost ~]# ctr images mount nginx:v1 /mnt   (把镜像挂载到/mnt目录上)
 

镜像的导出
[root@localhost ~]# ctr images export --all-platforms ngxin-v1.tar nginx:v1
[root@localhost ~]# ls
anaconda-ks.cfg  ngxin-v1.tar
镜像的导出
[root@localhost ~]# ctr images import ngxin-v1.tar

(2)容器类操作

创建容器
[root@localhost ~]# ctr containers create nginx:v1 nginx

nginx:v1  镜像
nginx   容器名称


[root@localhost ~]# ctr containers ls          (查看容器的详细信息)

删除容器
[root@localhost ~]# ctr containers rm ccc
ccc  容器名称

[root@localhost ~]# ctr containers info nginx         (查看详细信息)

(3)任务类操作

启动容器
[root@localhost ~]# ctr task start -d nginx
[root@localhost ~]# ctr task ls        (查看容器)
TASK     PID     STATUS    
nginx    1602    RUNNING(运行时)

备注:
-d  在后台运行

进入容器
[root@localhost ~]# ctr task exec --exec-id 0 -t nginx sh  

暂停容器
[root@localhost ~]# ctr task pause nginx
[root@localhost ~]# ctr task ls
TASK     PID     STATUS    
nginx    1602    PAUSED

恢复容器
[root@localhost ~]# ctr task resume nginx
[root@localhost ~]# ctr task ls
TASK     PID     STATUS    
nginx    1602    RUNNING

杀死容器
[root@localhost ~]# ctr task kill nginx

删除任务
[root@localhost ~]# ctr task rm nginx

备注:
删除任务时,也创建了一个同名快照,即使删除任务,也可以使用“ctr  task  start  -d  nginx”,将删除的任务启动起来,使容器进行恢复。

查看cpu限额使用量
[root@localhost ~]# ctr task metrics nginx
备注:
# memory.usage_in_bytes: 容器使用的内存大小,以字节为单位。在这个例子中,内存使用量为 9 字节。
# memory.limit in bytes: 容器的内存限制,以字节为单位。在这个例子中,内存限制为9223372036854771712 字节(这是一个很大的数值,通常表示无限制)。
# memory.stat.cache: 内存中的缓存大小,以字节为单位。在这个例子中,缓存大小为  字节。# cpuacct.usage: CPU 使用的时间,以纳秒为单位。在这个例子中,CPU 使用时间为 3636502 纳秒。# cpuacct.usage_percpu: 每个 CPU 核的 CPU 使用时间,以纳秒为单位。在这个例子中,有两个 CPU核,它们的使用时间分别为 16788433 纳秒和 19576569 纳秒。
# pids.current: 当前正在容器中运行的进程数量。在这个例子中,进程数为 。# pids.limit: 容器的进程数限制。在这个例子中,进程数限制为 (通常表示没有限制)

[root@localhost ~]# ctr task ps nginx        (查看宿主机PID)
PID     INFO
1602    -
1636    -
1637    -
[root@localhost ~]# ps -ef | grep 1602
root       1602   1579  0 20:22 ?        00:00:00 nginx: master process nginx -g daemon off;
101        1636   1602  0 20:22 ?        00:00:00 nginx: worker process
101        1637   1602  0 20:22 ?        00:00:00 nginx: worker process
root       4804   1420  0 21:29 pts/0    00:00:00 grep --color=auto 1602

(4)其他操作

插件

在 Containerd 中,插件是一种机制,允许你通过加载和卸载插件来扩展 Containerd 的功能。插件可以提供不同的功能,例如容器存储、网络、监控等。Containerd 使用插件化的设计,使其可以适应各种不同的容器运行时和容器生态系统需求。

以下是 Containerd 中的一些常见插件类型:

  • Shim 插件: Shim 插件负责管理容器的生命周期。当一个容器任务启动时,Containerd 会调用相应的 Shim 插件来创建并监管容器进程。Shim 插件负责与容器进程的通信,以及监控容器的状态。
  • Snapshotter 插件: Snapshotter 插件处理容器的文件系统快照 (Snapshots)。它定义了容器文件系统的结构,支持创建、管理和销毁快照,以及在不同容器之间共享文件系统层。
  • Task 插件: Task 插件用于管理容器中的任务 (Task)。任务表示容器内运行的一个或多个进程,它与容器的生命周期直接相关。
  • Image 插件: Image 插件负责处理容器镜像的拉取、推送、删除等操作。它定义了容器镜像的存储和元数据结构。
  • Content store 插件: Content store 插件管理容器内容 (Content),包括镜像层和元数据它定义了内容的存储和检索方式。

[root@localhost ~]#  ctr plugins ls   (列出当前所有插件)

命名空间

      在 Containerd 中,命名空间(Namespace) 是一种用于隔离资源和进程视图的 Linux 内核特性。
      命名空间允许在同一主机上运行的进程拥有不同的资源视图,从而实现资源隔离。
Containerd 利用 Linux 的命名空间实现容器的隔离。

       以下是 Containerd 中使用的一些主要命名空间类型:

  • PID 命名空间: 它隔离了进程 ID 空间,使得在不同 PID 命名空间中的进程看起来像是在独立的系统中运行一样。这有助于确保容器中的进程无法看到或影响主机系统上的其他进程。
  • Network 命名空间: 它隔离了网络栈,使得容器内的网络环境独立于主机系统。每个容器可以有自己的网络接口、IP 地址和端口等,而不会影响其他容器或主机上的网络配置。
  • Mount 命名空间: 它隔离了文件系统挂载点,使得容器拥有自己的文件系统视图。这样,每个容器都有自己的文件系统,不受其他影响。
  • UTS 命名空间:它隔离了主机名和域名,允许容器具有自己的主机名和域名。
  • IPC 命名空间:它隔离了进程间通信(IPC) 资源,使得容器内的进程无法直接与主机系统或其他容器共享 IPC 资源。
  • User 命名空间: 它隔离了用户和用户组的视图,使得容器中的进程可以在容器内部以不同的用户和组身份运行,而在主机系统上的实际用户和组不受影响。
  • 这些命名空间的组合使得容器可以在相对独立的环境中运行,避免了与主机系统或其他容器的干扰。Containerd 利用这些命名空间实现容器的隔离和资源管理,确保容器化应用在共享主机资源的同时获得适当的隔离。

[root@localhost ~]#  ctr ns ls          (查看命名空间)

[root@localhost ~]#   ctr  ns  create  test          (创建命名空间)

[root@localhost ~]#  ctr  ns rm  test              (删除命名空间)

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

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

[root@localhost ~]# ctr  -n  test containers  create nginx:v1 nginx03

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值