Containerd-1.6.5 镜像命令空间和容器操作使用

一、Containerd 镜像操作

1 基本参数

[root@ecs-65685 ~]# ctr c NAME:    ctr containers - manage containers  USAGE:    ctr containers command [command options] [arguments...]  COMMANDS:    create                   create container    delete, del, remove, rm  delete one or more existing containers    info                     get info about a container    list, ls                 list containers    label                    set and clear labels for a container    checkpoint               checkpoint a container    restore                  restore a container from checkpoint  OPTIONS:    --help, -h  show help

2 镜像操作

在containerd中拉取docker的相关镜像也需要补全。

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

--platform 选项指定对应平台的镜像。当然对应的也有推送镜像的命令 ctr image push,如果是私有镜像则在推送的时候可以通过 --user 来自定义仓库的用户名和密码。

拉取镜像添加了--all-platforms会将所有平台都下载下来(amd64 、arm、386 、ppc64le ),否则默认下载当前平台。

拉取镜像可以使用 ctr image pull 来完成,比如拉取 Docker Hub 官方镜像 nginx:alpine,需要注意的是镜像地址需要加上 docker.io Host 地址。

 ctr i pull docker.io/library/nginx:alpine --all-platforms

3 查看镜像

  • 查看镜像可以使用 i 或者image

  • -q 只打印镜像名称

ctr i ls ctr i ls -q

4 检测本地镜像

[root@ecs-65685 ~]# ctr image check REF                            TYPE                                                      DIGEST                                                                  STATUS         SIZE            UNPACKED  docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:b87c350e6c69e0dc7069093dcda226c4430f3836682af4f649f2af9e9b5f1c74 complete (7/7) 9.7 MiB/9.7 MiB true

主要查看其中的 STATUScomplete 表示镜像是完整可用的状态。

5 tag重新打标签

[root@ecs-65685 ~]# ctr i tag docker.io/library/nginx:alpine docker.io/library/nginx:nginxxyz docker.io/library/nginx:nginxxyz [root@ecs-65685 ~]# ctr i ls -q docker.io/library/nginx:alpine docker.io/library/nginx:nginxxyz # 新增tag

6 删除镜像

使用:delete, del, remove, rm remove one or more images by reference。都可以进行删除。

[root@ecs-65685 ~]# ctr i rm docker.io/library/nginx:nginxxyz docker.io/library/nginx:nginxxyz [root@ecs-65685 ~]# ctr i ls -q docker.io/frps9/tools:SynologyNAS docker.io/library/nginx:alpine

7 mount镜像

mount镜像实际上将镜像中的文件,挂载到宿主机的目录中去。

mount参数系统为只读状态,只可以读取,不可以写入数据。

使用--rw Enable write support on the mount 可以开启只读。

[root@ecs-65685 ~]# mkdir /home/xyz [root@ecs-65685 ~]# ctr i ls -q docker.io/library/nginx:alpine [root@ecs-65685 ~]# ctr i mount  docker.io/library/nginx:alpine /home/xyz/ sha256:26bec62cc7fc1f93f810d51c3f51291dae2d0f9e192e054cc61d1914365003d8 /home/xyz/ [root@ecs-65685 ~]# ls /home/xyz bin  docker-entrypoint.d   etc   lib    mnt  proc  run   srv  tmp  var dev  docker-entrypoint.sh  home  media  opt  root  sbin  sys  usr

mount卸载

[root@ecs-65685 ~]# ctr i unmount /home/xyz /home/xyz [root@ecs-65685 ~]# ls /home/xyz

8 推送镜像

[root@ecs-65685 ~]# ctr i push -k docker.io/library/nginx:alpine index-sha256:b87c350e6c69e0dc7069093dcda226c4430f3836682af4f649f2af9e9b5f1c74: waiting        |--------------------------------------|  elapsed: 0.1 s                                                                 total:   0.0 B (0.0 B/s)                                          ctr: content digest sha256:36f21d30317cff60a6ab8328cca93dbe554020ba0320c78e964040512d0f27a0: not found

跳过ssl验证--skip-verify, -k skip SSL certificate validation。

9 导入导出

  • import
  • export
  • 默认export导出的为OCI tar,只要我们容器支持OCI,基本上都是可以使用的
  • 同时导出可以使用--platform导出其它平台的(例如arm)
  • --all-platforms为导出所有平台
[root@ecs-65685 xyz]# ls [root@ecs-65685 xyz]# ctr i ls -q docker.io/library/nginx:alpine [root@ecs-65685 xyz]# ctr i export --all-platforms nginx.tar docker.io/library/nginx:alpine [root@ecs-65685 xyz]# ctr i export nginx_i4t.com.tar docker.io/library/nginx:alpine [root@ecs-65685 xyz]# ls nginx_i4t.com.tar  nginx.tar [root@ecs-65685 xyz]#  [root@ecs-65685 xyz]# ctr i rm docker.io/library/nginx:alpine docker.io/library/nginx:alpine [root@ecs-65685 xyz]# ctr i import nginx.tar  unpacking docker.io/library/nginx:alpine (sha256:b87c350e6c69e0dc7069093dcda226c4430f3836682af4f649f2af9e9b5f1c74)...done [root@ecs-65685 xyz]# ctr i ls -q docker.io/library/nginx:alpine

 

二、命令空间

1 每个顶级配置块的命名都是plugin."io.containerd.xxx.xxx.xxx"这种形式,其实每个顶级配置块都代表一个插件。

其中io.containerd.xxx.xxx表示插件类型,xxx后面的xxx表示 插件ID。

通过命令ctr plugin ls查看到

[root@ecs-65685 ~]# ctr plugin ls TYPE                                  ID                       PLATFORMS      STATUS     io.containerd.content.v1              content                  -              ok         io.containerd.snapshotter.v1          aufs                     linux/amd64    skip       io.containerd.snapshotter.v1          btrfs                    linux/amd64    skip       io.containerd.snapshotter.v1          devmapper                linux/amd64    error      io.containerd.snapshotter.v1          native                   linux/amd64    ok ...

2 containerd相比于docker,多了namespace概念,每个image和containe都会在各自的namespace下可见。

[root@ecs-65685 ~]# ctr ns ls NAME    LABELS  default         moby            [root@ecs-65685 ~]# ctr ns  NAME:    ctr namespaces - manage namespaces  USAGE:    ctr namespaces command [command options] [arguments...]  COMMANDS:    create, c   create a new namespace    list, ls    list namespaces    remove, rm  remove one or more namespaces    label       set and clear labels for a namespace  OPTIONS:    --help, -h  show help

3 默认空间

k8s会使用k8s.io作为命名空间,默认containerd会使用default

docker 其实也是默认调用的 containerd,事实上 docker 使用的 containerd 下面的命名空间默认是 moby

4 查看当前所有namespace

[root@ecs-65685 ~]# ctr ns ls NAME    LABELS  default         moby

5 创建containerd namespace,不指定namespace,默认就会使用default

[root@ecs-65685 ~]# ctr ns create xyz [root@ecs-65685 ~]# ctr ns ls NAME    LABELS  default         moby            xyz

6 测试命名空间

-n xyz 下载镜像进行测试,在ctr -n 指定命名空间即可。

命名空间只如果不指定,默认是看不到,这点和k8s namespace作用相同。

[root@ecs-65685 ~]# ctr -n xyz i pull docker.io/library/nginx:alpine --all-platforms ... [root@ecs-65685 ~]# ctr -n xyz i ls -q docker.io/library/nginx:alpine

7 分别查看镜像名称

[root@ecs-65685 ~]# ctr -n default image ls -q docker.io/library/nginx:alpine  [root@ecs-65685 ~]# ctr -n xyz image ls -q docker.io/library/nginx:alpine docker.io/library/redis:alpine

8 删除命名空间

[root@ecs-65685 ~]# ctr ns delete [NameSpace]

 

三、Containerd 容器操作

1 基本参数

[root@ecs-65685 ~]# ctr c create -h NAME:    ctr containers create - create container  USAGE:    ctr containers create [command options] [flags] Image|RootFS CONTAINER [COMMAND] [ARG...]  OPTIONS:    --snapshotter value               snapshotter name. Empty value stands for the default value. [$CONTAINERD_SNAPSHOTTER]    --snapshotter-label value         labels added to the new snapshot for this container.    --config value, -c value          path to the runtime-specific spec config file    --cwd value                       specify the working directory of the process    --env value                       specify additional container environment variables (e.g. FOO=bar)    --env-file value                  specify additional container environment variables in a file(e.g. FOO=bar, one per line)    --label value                     specify additional labels (e.g. foo=bar)    --mount value                     specify additional container mount (e.g. type=bind,src=/tmp,dst=/host,options=rbind:ro)    --net-host                        enable host networking for the container    --privileged                      run privileged container    --read-only                       set the containers filesystem as readonly    --runtime value                   runtime name (default: "io.containerd.runc.v2")    --runtime-config-path value       optional runtime config path    --tty, -t                         allocate a TTY for the container    --with-ns value                   specify existing Linux namespaces to join at container runtime (format '<nstype>:<path>')    --pid-file value                  file path to write the task's pid    --gpus value                      add gpus to the container    --allow-new-privs                 turn off OCI spec's NoNewPrivileges feature flag    --memory-limit value              memory limit (in bytes) for the container (default: 0)    --device value                    file path to a device to add to the container; or a path to a directory tree of devices to add to the container    --cap-add value                   add Linux capabilities (Set capabilities with 'CAP_' prefix)    --cap-drop value                  drop Linux capabilities (Set capabilities with 'CAP_' prefix)    --seccomp                         enable the default seccomp profile    --seccomp-profile value           file path to custom seccomp profile. seccomp must be set to true, before using seccomp-profile    --apparmor-default-profile value  enable AppArmor with the default profile with the specified name, e.g. "cri-containerd.apparmor.d"    --apparmor-profile value          enable AppArmor with an existing custom profile    --rdt-class value                 name of the RDT class to associate the container with. Specifies a Class of Service (CLOS) for cache and memory bandwidth management.    --rootfs                          use custom rootfs that is not managed by containerd snapshotter    --no-pivot                        disable use of pivot-root (linux only)    --cpu-quota value                 Limit CPU CFS quota (default: -1)    --cpu-period value                Limit CPU CFS period (default: 0)    --rootfs-propagation value        set the propagation of the container rootfs

2 创建一个nginx容器

  • -n 指定命名空间
  • c 是简写 containers, c, container   manage containers
  • create 创建容器
  • --net-host 使用宿主机网络
  • docker.io/xx/xxx:xxx 镜像地址
  • nginx 容器名称
ctr -n xyz c create --net-host docker.io/library/nginx:alpine nginx

3 查看容器列表

[root@ecs-65685 ~]# ctr -n xyz c ls  CONTAINER    IMAGE                             RUNTIME                   nginx        docker.io/library/nginx:alpine    io.containerd.runc.v2     nginx1       docker.io/library/nginx:alpine    io.containerd.runc.v2

4 只获取容器名称

[root@ecs-65685 ~]# ctr -n xyz c ls -q nginx nginx1

5 通过info参数查看容器的相关信息(类似于 docker inspect 功能)

ctr -n xyz c info nginx|less

6 删除容器

[root@ecs-65685 ~]# ctr -n xyz c ls -q nginx nginx1 [root@ecs-65685 ~]# ctr -n xyz container rm nginx1 [root@ecs-65685 ~]# ctr -n xyz c ls -q nginx

Containerd-1.6.5 镜像命令空间和容器操作使用

除了使用 rm 子命令之外也可以使用 delete 或者 del 删除容器。

 

四、报错

ctr export导出或者import导入提示:ctr: content digest sha256:xxxxx: not found。

[root@ecs-65685 ~]# ctr image export --all-platforms nginx.tar docker.io/library/nginx:alpine ctr: content digest sha256:36f21d30317cff60a6ab8328cca93dbe554020ba0320c78e964040512d0f27a0: not found

2 现象:

  • 直接导入某个镜像可能会出现类似于 ctr: content digest sha256:xxxxx: not found

解决办法:

  • 删除下载好的镜像
  • 拉取镜像、导出镜像时,都加上--all-platforms
  • 再次import和export,不需要添加--all-platforms

Containerd-1.6.5 镜像命令空间和容器操作使用

 

五、Containerd-1.6.5 之Task任务

https://www.cnblogs.com/huaxiayuyi/p/16790210.html

 

发表评论

相关文章