明年的k8s将剔除docker shim,在未来dockerd大概率会淘汰,是时候搞起ctr。ctr是containerd客户端,虽然目前很多功能做的还没有docker那么完善,但基本功能已经具备了。下面我就来实操一下。

主要有几个常用的自命名

镜像

镜像下载

在这里插入图片描述

镜像列表查询

在这里插入图片描述
这里需要注意PLATFORMS,它是镜像的能够运行的平台标识。
其他从操作也都类似

# ctr i -h
NAME:
   ctr images - manage images

USAGE:
   ctr images command [command options] [arguments...]

COMMANDS:
   check       check that an image has all content available locally
   export      export images
   import      import images
   list, ls    list images known to containerd
   pull        pull an image from a remote
   push        push an image to a remote
   remove, rm  remove one or more images by reference
   tag         tag an image
   label       set and clear labels for an image

export导出镜像,import导入镜像

容器

创建容器

# ctr c create  docker.io/library/nginx:latest nginx

其他操作也是类似

# ctr c -h
NAME:
   ctr containers - manage containers

USAGE:
   ctr containers command [command options] [arguments...]

COMMANDS:
   create           create container
   delete, del, 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

info命令和docker的inspect命令相似,都是查看容器的详细配置。

任务

上面create的命令创建了容器后,并没有处于运行状态,只是一个静态的容器。需要启动容器,

# ctr t start -d  nginx

task代表任务的意思,

# ctr t -h
NAME:
   ctr tasks - manage tasks

USAGE:
   ctr tasks command [command options] [arguments...]

COMMANDS:
   attach           attach to the IO of a running container
   checkpoint       checkpoint a container
   delete, rm       delete one or more tasks
   exec             execute additional processes in an existing container
   list, ls         list tasks
   kill             signal a container (default: SIGTERM)
   pause            pause an existing container
   ps               list processes for container
   resume           resume a paused container
   start            start a container that have been created
   metrics, metric  get a single data point of metrics for a task with the built-in Linux runtime

后续便可以执行其他task操作,包括attach和exec 和dokcer的语义都是相同的。譬如exec进入容器可以通过

# ctr t exec --exec-id 0  -t nginx sh
#

其中,exec-id 只要唯一就行。

命名空间

除了k8s有命名空间以外,containerd也支持命名空间。docker创建的默认的都在moby空间,而k8s默认是k8s.io这个空间下面,不同空间的容器互相隔离。

# ctr ns ls
NAME    LABELS
default
moby

和k8s一样,如果不指定,ctr默认是default空间。

目前containerd的定位还是解决运行时,所以目前他还不能完全替代dockerd。譬如基于dockerfile打镜像containerd就不具备这个能力。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐