Containerd 介绍

news2024/11/15 5:52:45

早之前的 Docker Engine 中就有了 containerd,只不过现在是将 containerd 从 Docker Engine 里分离出来,作为一个独立的开源项目,目标是提供一个更加开放、稳定的容器运行基础设施。分离出来的 containerd 将具有更多的功能,涵盖整个容器运行时管理的所有需求,提供更强大的支持。

containerd 是一个工业级标准的容器运行时,它强调简单性健壮性可移植性,containerd 可以负责干下面这些事情:

  • 管理容器的生命周期(从创建容器到销毁容器)
  • 拉取/推送容器镜像
  • 存储管理(管理镜像及容器数据的存储)
  • 调用 runc 运行容器(与 runc 等容器运行时交互)
  • 管理容器网络接口及网络

containerd 可用作 Linux 和 Windows 的守护程序,它管理其主机系统完整的容器生命周期,从镜像传输和存储到容器执行和监测,再到底层存储到网络附件等等。

3.1 Containerd的优势

  • 简洁的基于 gRPC 的 API 和 client library
  • 完整的 OCI 支持(runtime 和 image spec)
  • 同时具备稳定性和高性能的定义良好的容器核心功能
  • 一个解耦的系统(让 image、filesystem、runtime 解耦合),实现插件式的扩展和重用

3.2 为什么需要独立的 Containerd

  • 以往隶属于docker项目中,现如今从整体 docker 引擎中分离出的项目(开源项目的思路)
  • 可以被 Kubernets CRI 等项目使用(通用化)
  • 为广泛的行业合作打下基础(就像 runC 一样)

3.3 Containerd 架构图

Containerd的架构设计图:

image-20230331110918435

4.Containerd安装

安装及使用视频请参考:11_Containerd容器镜像管理_容器镜像管理命令_修改容器镜像tag_哔哩哔哩_bilibili

Containerd安装分为两种方式:

  1. yum安装方式
  2. 二进制包安装方式

以下分别演示两种不同的安装。

4.1 yum方式安装

4.1.1 环境介绍

系统及软件版本号
Centos6.9
Containerd1.6.19

4.1.2 获取YUM源

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">备份源主机的yum源
><span style="color:#4ec9b0">cd</span> /etc/yum.repos.d/
><span style="color:#4ec9b0">mkdir</span> bak
><span style="color:#4ec9b0">mv</span> *.repo bak/
</code></span></span>
<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">添加阿里云yum
>curl https://mirrors.aliyun.com/repo/Centos-7.repo -o /etc/yum.repos.d/Centos-7.repo
>curl https://mirrors.aliyun.com/repo/epel-7.repo -o /etc/yum.repos.d/epel-7.repo
>curl https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  -o /etc/yum.repos.d/docker-ce.repo
</code></span></span>
<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">查看yum源中containerd.io软件
>yum info containerd.io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
Name        : containerd.io
Arch        : x86_64
Version     : 1.6.19
Release     : 3.1.el7
Size        : 34 M
Repo        : docker-ce-stable
Summary     : An industry-standard container runtime
URL         : https://containerd.io
License     : ASL 2.0
Description : containerd is an industry-standard container runtime with an emphasis on
            : simplicity, robustness and portability. It is available as a daemon <span style="color:#569cd6">for</span> Linux
            : and Windows, <span style="color:#4ec9b0">which</span> can manage the complete container lifecycle of its host
            : system: image transfer and storage, container execution and supervision,
            : low-level storage and network attachments, etc.
</code></span></span>

4.1.3 使用yum安装

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">安装containerd.io 软件
>yum install -y containerd.io
</code></span></span>

4.1.4 验证安装及启动服务

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>rpm -ql containerd.io
/etc/containerd
/etc/containerd/config.toml
/usr/bin/containerd
/usr/bin/containerd-shim
/usr/bin/containerd-shim-runc-v1
/usr/bin/containerd-shim-runc-v2
/usr/bin/ctr
/usr/bin/runc
/usr/lib/systemd/system/containerd.service
/usr/share/doc/containerd.io-1.6.19
/usr/share/doc/containerd.io-1.6.19/README.md
/usr/share/licenses/containerd.io-1.6.19
/usr/share/licenses/containerd.io-1.6.19/LICENSE
/usr/share/man/man5/containerd-config.toml.5
/usr/share/man/man8/containerd-config.8
/usr/share/man/man8/containerd.8
/usr/share/man/man8/ctr.8
</code></span></span>
<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">启动服务及开机自启
>systemctl <span style="color:#4ec9b0">enable</span> containerd ; systemctl start containerd
</code></span></span>

4.1.5 验证可用性

安装 containerd 时 ctr 命令作为客户端工具主要用于管理容器及容器镜像等。使用 ctr 命令查看 containerd客户端及服务器信息。

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr version
Client:
  Version:  1.6.19
  Revision: 1e1ea6e986c6c86565bc33d52e34b81b3e2bc71f
  Go version: go1.19.7

Server:
  Version:  1.6.19
  Revision: 1e1ea6e986c6c86565bc33d52e34b81b3e2bc71f
  UUID: 39c52ad7-5c2d-4d74-acd7-d027b90aec83
</code></span></span>

4.2 二进制方式安装

系统及软件版本号
Centos6.9
Containerd1.7.0

Containerd 有两种安装包:

  • 第一种是 containerd-xxx 这种包用于单机测试没问题,不包含runC,需要提前安装;
  • 第二种是 cri-containerd-cni-xxx ,包含runC和k8s里所需的相关文件。k8s集群里需要用到此包,虽然包含runC,但是依赖系统中的 seccomp

4.2.1 获取安装包

下载地址:https://github.com/containerd/containerd/releases

image-20230331151105639

下载 containerd 包

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">wget https://github.com/containerd/containerd/releases/download/v1.7.0/cri-containerd-cni-1.7.0-linux-amd64.tar.gz
</code></span></span>

4.2.2 安装containerd

安装 containerd

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">>tar xf cri-containerd-cni-1.7.0-linux-amd64.tar.gz -C /
</code></span></span>

4.2.3 生成配置文件

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">创建目录
><span style="color:#4ec9b0">mkdir</span> /etc/containerd
</code></span></span>
<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">生成配置文件
>containerd config default > /etc/containerd/config.toml
</code></span></span>

修改配置文件关键参数

1. 修改SystemCgroup 为 true

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>vim /etc/containerd/config.toml

...
SystemdCgroup = <span style="color:#569cd6">true</span>
...

</code></span></span>

2. 添加镜像加速

  1. 修改 config.toml 配置文件
<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">[plugins.<span style="color:#d69d85">"io.containerd.grpc.v1.cri"</span>.registry]
      config_path = <span style="color:#d69d85">"/etc/containerd/certs.d"</span>  <span style="color:#57a64a"><em># 镜像地址配置文件</em></span>
</code></span></span>
  1. 创建对应目录
<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">><span style="color:#4ec9b0">mkdir</span> -p /etc/containerd/certs.d/docker.io
</code></span></span>
  1. 配置加速
<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">><span style="color:#4ec9b0">cat</span> << <span style="color:#d69d85">EOF >> /etc/containerd/certs.d/docker.io/hosts.toml
server = "https://docker.io"
[host."https://docker.mirrors.ustc.edu.cn"]
EOF</span>
</code></span></span>
  1. 重启 containerd
<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">>systemctl restart containerd
</code></span></span>

4.2.4 启动Containerd

启动 containerd

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">>systemctl <span style="color:#4ec9b0">enable</span> containerd ; systemctl start containerd
</code></span></span>

4.2.5 查看并验证

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr version
Client:
  Version:  v1.7.0
  Revision: 1fbd70374134b891f97ce19c70b6e50c7b9f4e0d
  Go version: go1.20.2

Server:
  Version:  v1.7.0
  Revision: 1fbd70374134b891f97ce19c70b6e50c7b9f4e0d
  UUID: 1f4630ff-27d5-46a4-b444-ca288c516127
</code></span></span>

4.2.6 安装runC并验证结果

由于二进制包中提供的runC默认需要系统中安装seccomp支持,需要单独安装,且不同版本runC对seccomp版本要求一致,所以建议单独下载runC二进制包进行安装,里面包含了 seccomp 模块支持。

下载地址:https://github.com/opencontainers/runc/releases

image-20230331154050685

下载runC

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">>wget https://github.com/opencontainers/runc/releases/download/v1.1.5/runc.amd64
</code></span></span>

安装runC

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">><span style="color:#4ec9b0">cp</span> -a runc.amd64 /usr/local/sbin/runc
><span style="color:#4ec9b0">chmod</span> +x /usr/local/sbin/runc
</code></span></span>

验证runC

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>runc -v
runc version 1.0.0-rc95
spec: 1.0.2-dev
go: go1.14.15
libseccomp: 2.5.1
</code></span></span>

5.Containerd镜像管理

docker-cli 工具提供了需要增强用户体验的功能,containerd 同样也提供一个对应 CLI工具:ctr ,不过 ctr 的功能没有 docker 完善,但是关于镜像和容器的基本功能都是有的。接下来介绍下 ctr 的使用。

5.1 Containerd容器镜像管理命令

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">ctr i <span style="color:#4ec9b0">ls</span> - 查看镜像
ctr i pull docker.io/library/nginx:alpine - 下载镜像
ctr i mount docker.io/library/nginx:alpine /mnt/ - 挂载镜像
ctr i <span style="color:#4ec9b0">export</span> --platform linux/amd64 nginx.img docker.io/library/nginx:alpine - 导出镜像
ctr i <span style="color:#4ec9b0">rm</span> docker.io/library/nginx:alpine - 
</code></span></span>

5.2 查看镜像

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">i: 等价于 images
>ctr i <span style="color:#4ec9b0">ls</span>
REF TYPE DIGEST SIZE PLATFORMS LABELS
</code></span></span>

5.3 下载镜像

containerd 支持 OCI 标准镜像,所有可以直接使用 docker 官方或 dockerfile构建的镜像

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr i pull docker.io/library/nginx:alpine
docker.io/library/nginx:alpine:                                                   resolved       |++++++++++++++++++++++++++++++++++++++|
index-sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:01ccf4035840dd6c25042b2b5f6b09dd265b4ed5aa7b93ccc4714027c0ce5685: <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:c23b4f8cf279507bb1dd3d6eb2d15ca84fac9eac215ab5b529aa8b5a060294c8:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
config-sha256:8e75cbc5b25c8438fcfe2e7c12c98409d5f161cbb668d6c444e02796691ada70:   <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:f56be85fc22e46face30e2c3de3f7fe7c15f8fd7c4e5add29d7f64b87abdaa09:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:2ce963c369bc5690378d31c51dc575c7035f6adfcc1e286051b5a5d9a7b0cc5c:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:59b9d2200e632e457f800814693b3a01adf09a244c38ebe8d3beef5c476c4c55:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:3e1e579c95fece6bbe0cb9c8c2949512a3f8caaf9dbe6219dc6495abb9902040:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:547a97583f72a32903ca1357d48fa302e91e8f83ffa18e0c40fd87adb5c06025:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:1f21f983520d9a440d410ea62eb0bda61a2b50dd79878071181b56b82efa9ef3:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
elapsed: 8.7 s                                                                    total:  16.0 M (1.8 MiB/s)
unpacking linux/amd64 sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f...
<span style="color:#569cd6">done</span>: 1.595243191s
</code></span></span>

可根据系统架构进行下载,上面默认采用的是 linux/amd64 平台下载。

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">下载linux/arm64平台镜像
>ctr i pull --platform linux/arm64 docker.io/library/nginx:alpine
docker.io/library/nginx:alpine:                                                   resolved       |++++++++++++++++++++++++++++++++++++++|
index-sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f:    exists         |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:5a3980760a3e6bd779d6ff3a029d24044e7660a1600dfd2f72298bf4657f1f6c: <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:7bcac465295e8cfefa26d0ad33a638a0415ad7c4e1afba500b9633f97e277c3c:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
config-sha256:510900496a6c312a512d8f4ba0c69586e0fbd540955d65869b6010174362c313:   <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:c41833b44d910632b415cd89a9cdaa4d62c9725dc56c99a7ddadafd6719960f9:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:2c2c9b85ac58c9f389d42b1033672337110dba86c12d1b0d5c7c384a7cfe110b:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:40f94fa3619489012a181c2b217548ea718fe485578eec4afdef4b14b3bc536e:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:ae26f20697dc7e3b86701a83a1ed42b81b1755f0763130d7f6f816a39adaf388:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:e4fa283fba0e8150c05ba453aed98ff4f4bdd65a6248837101fc16b489d1101e:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
layer-sha256:4c53b6cdc37bcca61cf31d3308b58fda6d7d3192ddd56559cca2f67eafcb0cc1:    <span style="color:#569cd6">done</span>           |++++++++++++++++++++++++++++++++++++++|
elapsed: 9.7 s                                                                    total:  15.4 M (1.6 MiB/s)
unpacking linux/arm64 sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f...
<span style="color:#569cd6">done</span>: 1.660794241s
</code></span></span>

查看

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">>ctr i <span style="color:#4ec9b0">ls</span>
REF                            TYPE                                                      DIGEST                                                                  SIZE     PLATFORMS                                                                          LABELS
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f 16.0 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x -
</code></span></span>

5.4 镜像挂载

方便查看镜像中包含的内容。

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">把已下载的容器镜像挂载至当前文件系统
>ctr i mount docker.io/library/nginx:alpine /mnt/
sha256:f301a4112756ab559d9c78e8ed3625dab81f91803dfeabbc4f9184c878b1f3b1
/mnt/

><span style="color:#4ec9b0">ls</span> /mnt/
bin/  dev/  docker-entrypoint.d/  docker-entrypoint.sh*  etc/  home/  lib/  media/  mnt/  opt/  proc/  root/  run/  sbin/  srv/  sys/  tmp/  usr/  var/
</code></span></span>
<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">卸载
>umount /mnt
</code></span></span>

5.5 镜像导出

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">把镜像导出
>ctr i <span style="color:#4ec9b0">export</span> --platform linux/amd64 nginx.img docker.io/library/nginx:alpine
><span style="color:#4ec9b0">du</span> -sh nginx.img
17M     nginx.img
</code></span></span>

5.6 镜像删除

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">删除指定容器镜像
>ctr i <span style="color:#4ec9b0">rm</span> docker.io/library/nginx:alpine
docker.io/library/nginx:alpine

再次查看容器镜像
>ctr i <span style="color:#4ec9b0">ls</span>
REF TYPE DIGEST SIZE PLATFORMS LABELS
</code></span></span>

5.7 镜像导入

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">导入容器镜像
>ctr i import --platform linux/amd64 nginx.img
</code></span></span>

注意:导出导入都必须指定 --platform 且一致,否则会报错!!!

5.8 修改镜像TAG

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr i tag docker.io/library/nginx:alpine nginx:alpine
>ctr i <span style="color:#4ec9b0">ls</span>
REF                            TYPE                                                      DIGEST                                                                  SIZE     PLATFORMS                                                                          LABELS
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f 16.0 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x -
nginx:alpine                   application/vnd.docker.distribution.manifest.list.v2+json sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f 16.0 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x -
</code></span></span>

修改完成后对镜像做对比

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr i check
REF                            TYPE                                                      DIGEST                                                                  STATUS         SIZE         UNPACKED
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f complete (8/8) 16.0 MiB/16.0 MiB <span style="color:#569cd6">true</span>
nginx:alpine                   application/vnd.docker.distribution.manifest.list.v2+json sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f complete (8/8) 16.0 MiB/16.0 MiB <span style="color:#569cd6">true</span>
</code></span></span>

6.Containerd容器管理

在 containerd 中,容器分为两种:静态容器 和 动态容器

  • 静态容器:命令创建容器后,容器并没有处于运行状态,其只是一个静态容器,这个 container对象只是包含了一个容器所需的资源及配置的数据结构
  • 动态容器:处于运行当中,有用户进程的容器

注意:在 container中,无法在没有镜像的情况下直接启动一个容器,必须遵循:1. pull镜像;2.启动容器。

6.1 查看容器

container表示静态容器,可用 c 缩写代表container

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr c <span style="color:#4ec9b0">ls</span>
CONTAINER    IMAGE    RUNTIME

或者
>ctr container <span style="color:#4ec9b0">ls</span>
</code></span></span>

6.2 查看任务

task表示容器里跑的进程,可用 t 缩写代表 task

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">>ctr task <span style="color:#4ec9b0">ls</span> 
或者
>ctr t <span style="color:#4ec9b0">ls</span>
</code></span></span>

6.3 创建静态容器

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr c create docker.io/library/nginx:alpine ngx
>ctr c <span style="color:#4ec9b0">ls</span>
CONTAINER    IMAGE                             RUNTIME
ngx          docker.io/library/nginx:alpine    io.containerd.runc.v2
</code></span></span>
<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">查看容器详细信息
>ctr c info ngx
</code></span></span>

6.4 静态容器启动为动态容器

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">启动task,即表示在容器中运行进程,即为动态容器
>ctr t <span style="color:#4ec9b0">ls</span>
TASK    PID    STATUS
>ctr t start -d ngx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking <span style="color:#569cd6">for</span> shell scripts <span style="color:#569cd6">in</span> /docker-entrypoint.d/

说明:-d 表示在后台运行,与docker一致


查看容器所在宿主机的进程,是宿主机进程的方式存在的
>ctr t <span style="color:#4ec9b0">ls</span>
TASK    PID      STATUS
ngx     16045    RUNNING

</code></span></span>
<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">查看容器的进程(都是物理机的进程)
>ctr t ps ngx
PID      INFO
16045    -
16080    -
16081    -
</code></span></span>

6.5 进入容器操作

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr t <span style="color:#4ec9b0">exec</span> --exec-id 1 -t ngx sh
/ <span style="color:#57a64a"><em>#ifconfig</em></span>
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
         
         
直接访问ngx
/ <span style="color:#57a64a"><em>#curl -Is 127.0.0.1</em></span>
HTTP/1.1 200 OK
Server: nginx/1.23.4
Date: Mon, 03 Apr 2023 01:48:04 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 28 Mar 2023 17:09:24 GMT
Connection: keep-alive
ETag: <span style="color:#d69d85">"64231f44-267"</span>
Accept-Ranges: bytes
</code></span></span>

6.6 直接运行一个动态容器

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr run -d --net-host docker.io/library/nginx:alpine ngx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking <span style="color:#569cd6">for</span> shell scripts <span style="color:#569cd6">in</span> /docker-entrypoint.d/

说明:
-d 代表后台运行
--net-host 代表容器的IP就是宿主机的IP(相当于docker里面的 host 类型网络)

测试是否运行成功
>curl -Is 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.23.4
Date: Mon, 03 Apr 2023 01:52:20 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 28 Mar 2023 17:09:24 GMT
Connection: keep-alive
ETag: <span style="color:#d69d85">"64231f44-267"</span>
Accept-Ranges: bytes
</code></span></span>
<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">查看静态容器
>ctr c <span style="color:#4ec9b0">ls</span>
CONTAINER    IMAGE                             RUNTIME
ngx          docker.io/library/nginx:alpine    io.containerd.runc.v2

查看动态容器
root@containerd(192.168.199.101)~>ctr t <span style="color:#4ec9b0">ls</span>
TASK    PID      STATUS
ngx     16366    RUNNING

进入容器内部查看
>ctr t <span style="color:#4ec9b0">exec</span> --exec-id 1 ngx sh

ifconfig
eth0      Link encap:Ethernet  HWaddr 52:54:00:E8:88:2B
          inet addr:192.168.199.101  Bcast:192.168.199.255  Mask:255.255.255.0
          inet6 addr: fe80::5054:ff:fee8:882b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:992892 errors:0 dropped:158 overruns:0 frame:0
          TX packets:72942 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:465982699 (444.3 MiB)  TX bytes:7003786 (6.6 MiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:848 (848.0 B)  TX bytes:848 (848.0 B)
</code></span></span>
<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">为容器中运行的网站添加网站文件
/ <span style="color:#57a64a"><em>#echo "nginx" > /usr/share/nginx/html/index.html</em></span>
/ <span style="color:#57a64a"><em>#curl -s 127.0.0.1</em></span>
nginx
/ <span style="color:#57a64a"><em>#exit</em></span>

宿主机访问:
>curl localhost
nginx
>curl 192.168.199.101
nginx
</code></span></span>

6.7 暂停容器

如果只是希望容器暂停工作一段时间,比如要对容器的文件系统做个快照,host需要使用CPU,处于暂停的容器不占用CPU资源。

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">查看容器状态
>ctr t <span style="color:#4ec9b0">ls</span>
TASK    PID      STATUS
ngx     16366    RUNNING

暂停容器
>ctr t pause ngx
>ctr t <span style="color:#4ec9b0">ls</span>
TASK    PID      STATUS
ngx     16366    PAUSED

宿主机无法访问网站
>curl -s 192.168.199.101
</code></span></span>

6.8 恢复容器

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">使用repause命令恢复容器
>ctr t resume ngx
>ctr t <span style="color:#4ec9b0">ls</span>
TASK    PID      STATUS
ngx     16366    RUNNING
</code></span></span>

6.9 停止容器

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">使用<span style="color:#4ec9b0">kill</span>命令停止容器中运行的进程,即为停止容器
>ctr t <span style="color:#4ec9b0">kill</span> ngx
状态从 RUNNING 变为 STOPPED
>ctr t <span style="color:#4ec9b0">ls</span>
TASK    PID      STATUS
ngx     16366    STOPPED
</code></span></span>

6.10 删除容器

删除容器之前必须停止容器。

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr t <span style="color:#4ec9b0">ls</span>
TASK    PID      STATUS
ngx     16366    STOPPED
>ctr t <span style="color:#4ec9b0">rm</span> ngx
>ctr t <span style="color:#4ec9b0">ls</span>
TASK    PID    STATUS

查看静态容器还存在系统中
>ctr c <span style="color:#4ec9b0">ls</span>
CONTAINER    IMAGE                             RUNTIME
ngx          docker.io/library/nginx:alpine    io.containerd.runc.v2
>ctr c <span style="color:#4ec9b0">rm</span> ngx
>ctr c <span style="color:#4ec9b0">ls</span>
CONTAINER    IMAGE    RUNTIME
</code></span></span>

7.命名空间

containerd 中是支持命名空间的概念.

7.1 查看命名空间

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr namespace <span style="color:#4ec9b0">ls</span>
NAME    LABELS
default

或者
>ctr ns <span style="color:#4ec9b0">ls</span>
NAME    LABELS
default
</code></span></span>

7.2 创建名称空间

如果不指定, ctr 默认使用 default 空间,同样也可以使用 ns create 命令创建一个命名空间:

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr ns create <span style="color:#4ec9b0">test</span>
>ctr ns <span style="color:#4ec9b0">ls</span>
NAME    LABELS
default
<span style="color:#4ec9b0">test</span>
</code></span></span>

7.3 指定名称空间启动容器

验证问题:当default 空间中有镜像,能否启动容器在test空间?

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr -n default i <span style="color:#4ec9b0">ls</span>
REF                            TYPE                                                      DIGEST                                                                  SIZE     PLATFORMS                                                                          LABELS
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f 16.0 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x -

在<span style="color:#4ec9b0">test</span>空间中启动容器
>ctr -n <span style="color:#4ec9b0">test</span> run -d docker.io/library/nginx:alpine ngx
ctr: image <span style="color:#d69d85">"docker.io/library/nginx:alpine"</span>: not found
</code></span></span>

上面报错信息为:找不到 镜像。看来名称空间将镜像也隔离使用。

第一步,pull 镜像到 test 空间

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">注意命令选项位置:
>ctr -n <span style="color:#4ec9b0">test</span> i pull docker.io/library/nginx:alpine

查看镜像
>ctr -n <span style="color:#4ec9b0">test</span> i <span style="color:#4ec9b0">ls</span>
REF                            TYPE                                                      DIGEST                                                                  SIZE     PLATFORMS                                                                          LABELS
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:c94a22b036afa972426b82d5b0a49c959786005b4f6f81ac7467ca5538d0158f 16.0 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x -
</code></span></span>

第二步,启动容器

启动容器前,请确认其他命名空间容器端口不存在冲突,否则容器状态为 STOPPED 

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">>ctr -n <span style="color:#4ec9b0">test</span> run -d --net-host docker.io/library/nginx:alpine ngx
>ctr -n <span style="color:#4ec9b0">test</span> t <span style="color:#4ec9b0">ls</span>
TASK    PID      STATUS
ngx     17853    RUNNING

>curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.23.4
Date: Mon, 03 Apr 2023 03:53:51 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 28 Mar 2023 17:09:24 GMT
Connection: keep-alive
ETag: <span style="color:#d69d85">"64231f44-267"</span>
Accept-Ranges: bytes
</code></span></span>

7.4 删除命名空间

尝试删除有容器、有镜像的命名空间。

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">>ctr ns <span style="color:#4ec9b0">rm</span> <span style="color:#4ec9b0">test</span>
ERRO[0000] unable to delete <span style="color:#4ec9b0">test</span>                         error=<span style="color:#d69d85">"namespace \"test\" must be empty, but it still has images, blobs, containers, snapshots on \"overlayfs\" snapshotter: failed precondition"</span>
ctr: unable to delete <span style="color:#4ec9b0">test</span>: namespace <span style="color:#d69d85">"test"</span> must be empty, but it still has images, blobs, containers, snapshots on <span style="color:#d69d85">"overlayfs"</span> snapshotter: failed precondition
</code></span></span>

报错了, 因为 test 命名空间非空,需要先删除容器和镜像。

<span style="color:#c2c2c2"><span style="background-color:#252525"><div style="text-align:start"></div><code class="language-bash">删除容器
>ctr -n <span style="color:#4ec9b0">test</span> t <span style="color:#4ec9b0">kill</span> ngx
>ctr  -n <span style="color:#4ec9b0">test</span> t <span style="color:#4ec9b0">rm</span> ngx
>ctr -n <span style="color:#4ec9b0">test</span> c <span style="color:#4ec9b0">rm</span> ngx

删除镜像
>ctr -n <span style="color:#4ec9b0">test</span> i <span style="color:#4ec9b0">rm</span> docker.io/library/nginx:alpine
docker.io/library/nginx:alpine

查看容器及镜像
>ctr -n <span style="color:#4ec9b0">test</span> t <span style="color:#4ec9b0">ls</span> ; ctr -n <span style="color:#4ec9b0">test</span> c <span style="color:#4ec9b0">ls</span>; ctr -n <span style="color:#4ec9b0">test</span> i <span style="color:#4ec9b0">ls</span>
TASK    PID    STATUS
CONTAINER    IMAGE    RUNTIME
REF TYPE DIGEST SIZE PLATFORMS LABELS

删除命名空间
>ctr ns <span style="color:#4ec9b0">rm</span> <span style="color:#4ec9b0">test</span>
<span style="color:#4ec9b0">test</span>
>ctr ns <span style="color:#4ec9b0">ls</span>
NAME    LABELS
default
</code></span></span>

7.5 命名空间的异同

Docker 其实也是默认调用的 containerd,事实上 Docker 使用的 containerd 下面的命名空间默认是 moby,而不是 default,所以假如我们有用 docker 启动容器,那么我们也可以通过 ctr -n moby 来定位下面的容器:

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">>ctr -n moby c <span style="color:#4ec9b0">ls</span>
CONTAINER    IMAGE    RUNTIME
</code></span></span>

同样 Kubernetes 下使用的 containerd 默认命名空间是 k8s.io,所以我们可以使用 ctr -n k8s.io 来查看 Kubernetes 下面创建的容器。

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">>ctr -n k8s.io c <span style="color:#4ec9b0">ls</span>
CONTAINER    IMAGE    RUNTIME
</code></span></span>

注意:这三者之间的 ns 是不同的,需要区分开。

<span style="color:#c2c2c2"><span style="background-color:#252525"><code class="language-bash">containerd -> namespace(隔离) -> (和k8s里面的命名空间要区分开) -> namespace + cgroup + rootfs</code></span></span>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2042019.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

centos7安装Oracle 11g数据库

目录 一、安装前准备1、安装前置工具&#xff08;安装过可以忽略&#xff09;2、更配yum源2.1、备份原有源&#xff1b;2.2、下载阿里云base源和epel源&#xff1b;2.3、清理yum缓存2.4、生成新的缓存2.5、更新系统中所有软件到最新版&#xff08;按需谨慎操作&#xff09; 3 修…

做代理海外仓赚钱?代理仓如何实现盈利?

随着跨境电商与物流的火热&#xff0c;海外仓作为跨境贸易的新基建&#xff0c;也成为了一门生意。具体来说海外仓商业模式是一种通过在跨境贸易中设置离岸仓库&#xff0c;为客户提供包括商品存储、包装、发货、退货和售后服务等一系列跨境电商服务的商业模式。 海外仓的成本主…

跟《经济学人》学英文:2024年08月10日这期 A history-lover’s guide to the market panic over AI

A history-lover’s guide to the market panic over AI Past technologies offer clues to what comes next 原文&#xff1a; Andrew Odlyzko, a professor of mathematics at the University of Minnesota, has a side hustle: he has become one of the world’s foremo…

19523 最长上升子序列长度

### 分析 1. **问题描述**&#xff1a; - 给定一个序列&#xff0c;要求找到最长上升子序列的长度。 - 子序列可以是不连续的&#xff0c;但必须保持顺序。 2. **解决方案**&#xff1a; - 使用动态规划&#xff08;Dynamic Programming, DP&#xff09;来解决这个问…

RCE---无字母数字webshell

<?php if(isset($_GET[code])){$code $_GET[code];if(strlen($code)>35){die("Long.");}if(preg_match("/[A-Za-z0-9_$]/",$code)){die("NO.");}eval($code); }else{highlight_file(__FILE__); } 分析代码&#xff1a;传参不大于35&…

让可视化大屏摆脱面子工程的12个方法

提到可视化大屏&#xff0c;很多老铁就认为这是面子工程&#xff0c;花里胡哨&#xff0c;没啥用处&#xff0c;这固然和认知有关系&#xff0c;那么有没有办法让可视化大屏摆脱这种认知吗&#xff0c;千汇数据工场介绍往日经验&#xff0c;与大家探讨下。 可视化大屏面子工程…

C语言典型例题37

《C程序设计教程&#xff08;第四版&#xff09;——谭浩强》 例题3.5 按照按照考试成绩的等级输出百分制分数段&#xff0c;A等为85分以上&#xff0c;B等为70~84分&#xff0c;C等为 60~69分&#xff0c;D等在60分以下&#xff0c;成绩的等级从键盘输入 代码&#xff1a; //…

2024最新上门按摩系统源码APP打包教程!

**xhadmin、免费、开源、可商用** 上门按摩这两年很火&#xff0c;某宝、某鱼上盗版系统盛行&#xff0c;大都是留有后门的系统&#xff0c;加密授权&#xff0c;根本二开不了。 近期很多人反馈我们的上门按摩系统APP打包困难&#xff0c;今天我手把手教大家如何打包上门按摩A…

【CanMVK230】CanMV K230 开箱

【CanMVK230】CanMV K230 开箱 CanMV 是什么CanMV K230开发板硬件资源能做什么 开箱&#xff01;配套资料其他学习资料 K230我买到啦~。话不多说&#xff0c;开始分享我的使用过程。欢迎大神指点。 CanMV 是什么 CanMV开源项目由嘉楠科技&#xff08;Canaan&#xff09;官方创建…

【关于CVE-2024-38077 Windows Server 2012和Windows Server 2018安装安全补丁指南】

文章目录 背景问题描述产生原因解决方案解决步骤1. 安装BypassESU工具2. 补丁安装方法一&#xff1a;使用 Windows 更新功能方法二&#xff1a;手动下载补丁并安装 补丁验证方法一&#xff1a;在“控制面板”-“程序”-”程序和功能”-“已安装更新”中检查是否存在 KBS040434 …

<Linux>进程概念-下

文章目录 目录 前言 一、环境变量 1. PATH 2. HOME 3. 其他环境变量 系统调用接口--getenv 4. 命令行参数 4.1 双参数main 4.2 三参数main 5. 设置环境变量 5.1 本地环境变量 5.1.1 内建命令 5.2 固定环境变量 6. 取消环境变量 7. 小总结 二、程序地址空间 1. 空间划分 2. 进…

haproxy负载均衡(twenty-eight day)

官网&#xff1a; https://www.haproxy.com/ 自由及开放源代码软件 HAPrOxy是一个使用C语言编写的自由及开旅酒代码软性&#xff0c;其提供高可用性、负我均衡&#xff0c;以及基于TCP和HTTP的应用程座代理 HAProxy特别适用于那些负载特大的webi些站点通常又需要会话保挂或七层…

单片机中时钟源(Clock Source)和时基源(Timebase Source)和的联系和区别

问题描述 在单片机中&#xff0c;时钟源&#xff08;Clock Source&#xff09;和时基源&#xff08;Timebase Source&#xff09;是两个与时间相关的基本概念&#xff0c;它们在单片机的时钟系统设计中扮演着重要角色。 区别与联系 1.区别 1.1定义 时钟源&#xff1a;是单片机…

【C语言篇】编译和链接以及预处理介绍(上篇)

文章目录 前言翻译环境和运行环境翻译环境编译预处理&#xff08;预编译&#xff09;编译词法分析语法分析语义分析 汇编 链接 运行环境预处理&#xff08;预编译&#xff09;详解预定义符号#define定义常量#define定义宏带有副作用的宏参数宏替换的规则宏和函数的对比 写在最后…

【git】gitee 提交错误,如何回退

文章目录 查看提交记录设定退回到位置提交 查看提交记录 git log git log如下图所示共2次提交记录 最近一次是错误提交&#xff08;笔者提交是在错误的工作路径上传了&#xff09; 设定退回到位置 git reset --hard hash值 git reset --soft 83fcc380d5250599eca********…

rabbit消息队列

一&#xff1a;消息队列简介 1&#xff1a;主流的消息队列 目前主流的几大消息队列有&#xff1a;RabitMQ、ActiveMQ、RocketMQ、Kafka、ZeroMQ等&#xff0c;也有一些小众的比如Beanstalk&#xff0c;当然我们之前学过的Redis也可以实现消息队列的功能。 &#xff08;1&…

Android全面解析之Context机制(一) :初识Android context

什么是Context 回想一下最初学习Android开发的时候&#xff0c;第一用到context是什么时候&#xff1f;如果你跟我一样是通过郭霖的《第一行代码》来入门android&#xff0c;那么一般是Toast。Toast的常规用法是&#xff1a; Toast.makeText(this, "我是toast", To…

详解【网路编程】之Socket套接字编程

谢谢帅气美丽且优秀的你看完我的文章还要点赞、收藏加关注 没错&#xff0c;说的就是你&#xff0c;不用再怀疑&#xff01;&#xff01;&#xff01; 希望我的文章内容能对你有帮助&#xff0c;一起努力吧&#xff01;&#xff01;&#xff01; 1、Socket套接字 Socket 是一个…

4G 和 5G 中的单域注册(VoLTE和VoNR适用)VoNR 中的 CSRetry

目录 1. 4G 和 5G 中的单域注册&#xff08;VoLTE和VoNR适用&#xff09; 1.1 主要内容 1.2 什么是 4/5G 网络中的单域注册 1.3 为什么需要单域注册 1.4 单域注册主要参数之&#xff1a;Dual-Registration-5G-Indicator 1.5 单域注册主要参数之&#xff1a;DualRegistrat…

基于微信小程序地图实现点位标注、覆盖物、地图聊天

目录 小程序部分map标签的使用获取用户经纬度并转换地址地图点击事件覆盖物标注点击并实现弹窗交互数据库及接口部分数据库表结构设计API搭建小程序接口使用注意事项wx.getLocation深入控制地图小程序部分 map标签的使用 创建小程序的步骤这里不再重复赘述,在wxml页面中放一个…