Loading... > 最近尝试用 Containerd 去部署k8s集群,现在contailerd已经成为k8s推荐的容器运行时之一,这篇文章总结了一些 Containerd 常用的管理命令,先来看一下 Docker 与 Containerd 的区别,**(内容来自https://cloud.tencent.com/developer/article/2327654)** 在 Kubernetes 提出 CRI 操作规范时,Docker刚拆出 containerd,并不支持 CRI 标准。由于当时Docker是容器技术最主流也是最权威的存在,Kuberentes虽然提出了CRI接口规范,但仍然需要去适配CRI与Docker的对接,因此它需要一个中间层或 shim 来对接 Kubelet 和 Docker 的 contianer runtime。 于是 kubelet 中加入了 Dockershim (shim为临时、兼容的意思)。使用 docker 作为 runtime 时,实际启动一个容器的过程是:  在这个阶段 dockershim组件在Kubelet 的代码中,这也就意味着Dockershim是由K8S组织进行开发和维护!由于Docker公司的版本发布K8S组织是无法控制和管理,所以每次Docker发布新的Release,K8S组织都要集中精力去快速地更新维护Dockershim。 Kubernetes1.24版本正式删除和弃用dockershim。这件事情的本质是废弃了内置的 dockershim 功能,直接对接Containerd(后续已经支持 CRI)。这种方式更加标准,调用的链路更加的简洁。  ### 安装 containerd ```bash yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo yum makecache fast yum install containerd -y systemctl enable --now containerd ``` 生成默认配置文件 ```bash sudo mkdir -p /etc/containerd containerd config default | sudo tee /etc/containerd/config.toml ``` 如果需要启用 systemd cgroup则编辑`/etc/containerd/config.toml` ```toml [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] SystemdCgroup = true # 设置为true ``` ### 常用命令 #### ctr | 功能 | 命令 | | -------------------- | -------------------------------------------- | | **查看镜像列表** | `ctr image ls` | | **拉取镜像** | `ctr image pull 镜像名称:tag` | | **推送镜像** | `ctr image push 镜像名称:tag` | | **导入镜像** | `ctr image import 镜像文件` | | **导出镜像** | `ctr image export 镜像文件 镜像名称:tag` | | **查看容器列表** | `ctr container ls` 或 `ctr task ls` | | **创建容器** | `ctr container create 镜像名称:tag 容器名称` | | **启动容器** | `ctr task start 容器ID` | | **停止容器** | `ctr task kill 容器ID` | | **删除容器** | `ctr container rm 容器ID` | | **查看容器信息** | `ctr container info 容器ID` | | **查看命名空间列表** | `ctr ns ls` | #### crictl | 功能 | 命令 | | --------------------- | ---------------------------------- | | **查看运行的容器** | `crictl ps` | | **查看所有容器** | `crictl ps -a` | | **查看镜像列表** | `crictl images` | | **拉取镜像** | `crictl pull 镜像名称:tag` | | **查看容器日志** | `crictl logs 容器ID` | | **在容器中执行命令** | `crictl exec -i -t 容器ID command` | | **启动容器** | `crictl start 容器ID` | | **停止容器** | `crictl stop 容器ID` | | **删除容器** | `crictl rm 容器ID` | | **查看容器资源** | `crictl stats 容器ID` | | **查看 Pod 列表** | `crictl pods` | | **查看 Pod 详细信息** | `crictl inspect [PodID]` | #### ctr 与 crictl 区别 > ctr是 containerd 的原生命令行工具,直接与containerd交互 > > crictl是 k8s的CRI命令行工具,用于 k8s环境中的容器和Pod管理 参考: - https://cloud.tencent.com/developer/article/2327654 Last modification:April 25, 2025 © Allow specification reprint Support Appreciate the author Like 如果觉得我的文章对你有用,请随意赞赏