Containerd安装配置及基本操作
Containerd安装配置及基本操作
2024-12-02 00:46
Containerd 安装与配置操作详解 Containerd 是一个高性能的 容器运行时(Container Runtime)工具,广泛用于管理和运行容器。它不仅可以与 Kubernetes 配合工作,也支持独立部署。在本文中,我们将详细介绍如何安装、配置和使用 Containerd 来进行容器管理。
Containerd 安装与配置操作详解
Containerd 是一个高性能的 容器运行时(Container Runtime)工具,广泛用于管理和运行容器。它不仅可以与 Kubernetes 配合工作,也支持独立部署。在本文中,我们将详细介绍如何安装、配置和使用 Containerd 来进行容器管理。
1. 安装 Containerd
根据操作系统的不同,安装 Containerd 的步骤有所不同。这里分别介绍在 Ubuntu 和 CentOS 上的安装方法。
在 Ubuntu 上安装 Containerd
- 更新系统软件包:
sudo apt-get update sudo apt-get upgrade -y
- 安装必要的依赖包:
sudo apt-get install -y \ curl \ gnupg \ lsb-release
- 导入 Docker 官方 GPG 密钥:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/trusted.gpg.d/docker.asc
- 添加 Docker 的 APT 仓库:
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
- 安装 Containerd:
sudo apt-get update sudo apt-get install -y containerd.io
- 启动并启用 Containerd 服务:
sudo systemctl start containerd sudo systemctl enable containerd
在 CentOS 上安装 Containerd
- 安装依赖包:
sudo yum install -y yum-utils
- 添加 Docker 仓库:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- 安装 Containerd:
sudo yum install -y containerd.io
- 启动并启用 Containerd 服务:
sudo systemctl start containerd sudo systemctl enable containerd
2. 配置 Containerd
Containerd 的配置文件位于
/etc/containerd/config.toml
。如果该文件不存在,可以使用命令生成默认配置。生成配置文件
sudo containerd config default > /etc/containerd/config.toml
配置说明
编辑
config.toml
文件时,可以对 网络设置、存储驱动、镜像存储位置 等进行自定义。以下是一些常见配置:
- 容器运行时:配置文件中的
runtime
部分可以指定要使用的容器运行时(如runc
或kata
)。[plugins."io.containerd.runc.v1"] runtime_type = "io.containerd.runc.v1"
- 镜像存储:修改镜像存储路径。
[image] [image.store] root = "/var/lib/containerd/io.containerd.content.v1.content" snapshotter = "overlayfs"
- 网络插件:可以在
[plugins."io.containerd.grpc.v1.cri".network]
部分进行自定义网络设置。
保存并关闭配置文件
完成编辑后,保存并退出配置文件,使用以下命令重新启动 Containerd 服务,使配置生效:
sudo systemctl restart containerd
3. 启动 Containerd 服务
Containerd 启动后,它会默认运行在后台。可以使用以下命令检查服务状态:
sudo systemctl status containerd
如果显示为 active,说明 Containerd 已成功启动。
4. 使用 Containerd 管理容器
Containerd 提供了命令行工具 ctr 来管理容器。以下是常用操作示例:
列出所有容器
ctr containers list
此命令会列出当前运行的所有容器。
创建一个容器
创建容器时,首先需要指定容器的镜像。例如,创建一个基于 Ubuntu 镜像的容器:
ctr run --rm -t docker.io/library/ubuntu:latest my-ubuntu-container bash
解释:
ctr run
:运行容器命令。--rm
:容器停止后自动删除。-t
:为容器分配终端。docker.io/library/ubuntu:latest
:使用的镜像。my-ubuntu-container
:容器名称。bash
:运行的命令。
停止并删除容器
ctr container stop my-ubuntu-container
ctr container delete my-ubuntu-container
查看容器日志
ctr containers info my-ubuntu-container
5. 配置容器运行时(Runtime)
Containerd 支持多种容器运行时,最常用的是 runc。如果需要配置其他运行时(例如 Kata Containers),可以在配置文件中进行修改。
配置 runc
Containerd 默认使用 runc 作为容器运行时。如果需要修改或确认运行时,编辑 /etc/containerd/config.toml
文件:
[plugins."io.containerd.runc.v1"]
runtime_type = "io.containerd.runc.v1"
配置 Kata Containers
Kata Containers 提供了更加隔离的虚拟化容器。启用 Kata 时,可以这样配置:
[plugins."io.containerd.kata.v2"]
runtime_type = "io.containerd.kata.v2"
完成后,重启 Containerd 以应用新配置。
总结
通过以上步骤,你可以轻松安装、配置和使用
label :
- Containerd