# Containerd 常用命令
# 1. 安装 Containerd
1.1 配置安装源
| yum install wget jq psmisc vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 git -y |
| yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo |
1.2 安装 docker-ce、containerd
| yum install docker-ce containerd -y |
可以无需启动 Docker,只需要配置和启动 Containerd 即可。
1.3 配置 Containerd 所需的模块
1.4 加载模块
| |
| net.bridge.bridge-nf-call-iptables = 1 |
| net.ipv4.ip_forward = 1 |
| net.bridge.bridge-nf-call-ip6tables = 1 |
| EOF |
1.5 配置 Containerd 所需的内核
| |
| net.bridge.bridge-nf-call-iptables = 1 |
| net.ipv4.ip_forward = 1 |
| net.bridge.bridge-nf-call-ip6tables = 1 |
| EOF |
1.6 加载内核
1.7 生成 Containerd 的配置文件
| mkdir -p /etc/containerd |
| containerd config default | tee /etc/containerd/config.toml |
1.8 更改 Containerd 的 Cgroup 和 Pause 镜像
| sed -i 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml |
| sed -i 's#k8s.gcr.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g' /etc/containerd/config.toml |
| sed -i 's#registry.gcr.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g' /etc/containerd/config.toml |
| sed -i 's#registry.k8s.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g' /etc/containerd/config.toml |
1.9 启动 Containerd,并配置开机自启动
| systemctl daemon-reload |
| systemctl enable --now containerd |
| systemctl status containerd |
# 2. Containerd 配置镜像加速
打开 /etc/containerd/config.toml 文件,找到 [plugins."io.containerd.grpc.v1.cri".registry.mirrors] 部分,添加所需的镜像源配置
| |
| |
| [plugins."io.containerd.grpc.v1.cri".registry.mirrors] |
| [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"] |
| endpoint = [ |
| "https://docker.io", |
| "https://6qxc6b6n.mirror.aliyuncs.com", |
| "https://docker.m.daocloud.io", |
| "https://dockerproxy.com/" |
| ] |
| [plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"] |
| endpoint = [ |
| "https://gcr.m.daocloud.io", |
| "https://gcr.nju.edu.cn", |
| "https://gcr.dockerproxy.com" |
| ] |
重新启动 Containerd
| systemctl daemon-reload |
| systemctl restart containerd |
# 3. Containerd 常用操作命令实践
# 3.1 查看 Containerd 命名空间
namespace 来于指定类似于工作空间的隔离区域
| [root@k8s-node02 ~] |
| NAME LABELS |
| default |
| k8s.io |
| moby |
# 3.2 查看 Containerd 镜像
因为没有指定 namespace,所以查看的是默认命名空间下的镜像
查看指定命名空间 k8s.io 下的镜像
# 3.3 拉取 Containerd 镜像
拉取指定命名空间 k8s.io 镜像 pause-amd64:3.2
| ctr -n k8s.io images pull registry.aliyuncs.com/google_containers/pause-amd64:3.2 |
| ctr -n k8s.io images pull docker.io/library/nginx:1.21 |
# 3.4 删除 containerd 镜像
| ctr -n k8s.io images rm registry.aliyuncs.com/google_containers/pause-amd64:3.2 |
# 3.5 导出 Containerd 镜像
| ctr -n k8s.io images export pause.tar.gz registry.aliyuncs.com/google_containers/pause-amd64:3.2 |
# 3.6 导入 Containerd 镜像
| ctr -n k8s.io image import pause.tar.gz |
docker save -o 命令导出来的镜像可以用 ctr images import 导出,同理 ctr images export 导出来的镜像也可以有 docker load 还原。
# 3.7 标记 Containerd 镜像
| ctr -n k8s.io images tag registry.aliyuncs.com/google_containers/pause-amd64:3.2 pause:3.2 |
# 3.8 运行 Containerd 容器
在后台运行一个 centos 镜像的容器,名称叫做 centos_k8s
| ctr -n k8s.io run -d docker.io/library/nginx:1.21 web |
# 3.9 查看运行容器的 task
# 3.10 启动指定容器 task
| ctr -n k8s.io task start -d centos_k8s |
# 3.11 进入指定容器 task
| ctr -n k8s.io task exec --exec-id 3118 -t web /bin/bash |
# 3.12 删除指定容器 task
| ctr -n k8s.io task rm -f web |
# 3.13 停止指定容器 task
| ctr -n k8s.io task kill --signal 9 centos_k8s |
# 3.14 查看容器
# 3.15 删除容器
| ctr -n k8s.io c rm centos |
删除容器以前需要将 task 删除,不然会报以下错误
| [root@k8s-node02 ~] |
| ERRO[0000] failed to delete container "web" error="cannot delete a non stopped container: {running 0 0001-01-01 00:00:00 +0000 UTC}" |
| ctr: cannot delete a non stopped container: {running 0 0001-01-01 00:00:00 +0000 UTC} |
# 4. Docker 与 Containerd 常用命令对比
说明 | docker 命令 | containerd 命令 |
---|
查看本地镜像 | docker images | ctr images ls |
拉取镜像 | docker pull imagename | ctr images pull imagename |
推送镜像 | docker push imagename | ctr images push imagename |
给镜像打标签 | docker tag imagename tagname | ctr images tag imagename tagname |
导出镜像 | docker save filename imagename | ctr images export filename imagename |
导入镜像 | docker load filename | ctr image import filename |
运行并创建容器 | docker run [options] imagename commond | ctr run [options] imagenamecontainername |
进入容器 | docker exec [options] names commond | ctr task exec [options] names commond |
查看运行的容器 | docker ps | ctr task list |
删除容器 | docker rm [options] names | 1.ctr task rm -f names 2. ctr c rm -f names |