随着 Kubernetes 生态系统的发展,containerd 已成为越来越多集群的默认容器运行时。然而,containerd 自带的 ctr 命令工具功能有限,尤其是在清理未使用的镜像方面缺乏像 Docker 中 docker image prune
这样便捷的功能。本教程将介绍如何使用 crictl 工具来弥补这一不足,实现 containerd 环境下镜像的高效清理。通过几个简单的步骤,你将掌握如何安装配置 crictl,并利用它的 prune 功能来保持系统整洁。
安装 crictl
VERSION="v1.31.1"
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-$VERSION-linux-amd64.tar.gz
sudo tar zxvf crictl-$VERSION-linux-amd64.tar.gz -C /usr/local/bin
rm -f crictl-$VERSION-linux-amd64.tar.gz
检查命令
[root@localhost ~]# crictl info
WARN[0000] runtime connect using default endpoints: [unix:///run/containerd/containerd.sock unix:///run/crio/crio.sock unix:///var/run/cri-dockerd.sock]. As the default settings are now deprecated, you should set the endpoint instead.
ERRO[0000] validate service connection: validate CRI v1 runtime API for endpoint "unix:///run/containerd/containerd.sock": rpc error: code = Unimplemented desc = unknown service runtime.v1.RuntimeService
ERRO[0000] validate service connection: validate CRI v1 runtime API for endpoint "unix:///run/crio/crio.sock": rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing: dial unix /run/crio/crio.sock: connect: no such file or directory"
ERRO[0000] validate service connection: validate CRI v1 runtime API for endpoint "unix:///var/run/cri-dockerd.sock": rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing: dial unix /var/run/cri-dockerd.sock: connect: no such file or directory"
FATA[0000] validate service connection: validate CRI v1 runtime API for endpoint "unix:///var/run/cri-dockerd.sock": rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing: dial unix /var/run/cri-dockerd.sock: connect: no such file or directory"
报错是因为 crictl 没有正确配置连接端点,我们需要为其设置一个连接端点
cat <<EOF | sudo tee /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF
# PS:
# 1. 假如使用的是 docker:
cat <<EOF | sudo tee /etc/crictl.yaml
runtime-endpoint: unix:///var/run/cri-dockerd.sock
image-endpoint: unix:///var/run/cri-dockerd.sock
timeout: 10
debug: false
EOF
# 2. cri-o:
cat <<EOF | sudo tee /etc/crictl.yaml
runtime-endpoint: unix:///var/run/crio/crio.sock
image-endpoint: unix:///var/run/crio/crio.sock
timeout: 10
debug: false
EOF
配置完成后,测试连接:
crictl info
常用命令
# 列出所有镜像
crictl img
# 查看镜像详情
crictl inspecti <image-id>
# 拉取镜像
crictl pull <image-name>
# 查看运行中的容器
crictl ps
# 查看所有容器
crictl ps -a
获取关于 crictl 的帮助
crictl help
crictl <subcommand> help
想要了解更多关于 crictl 的使用,可以参考 crictl 文档
在 crictl 成功安装完成配置后,我们可以使用以下命令清理未使用的镜像层
crictl rmi --prune
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。