定时清理镜像
方式一:
cat >> /etc/cron.hourly/clean-unuse-images.sh << 'EOF'
#!/usr/bin/env bash
main () {
(crictl img | grep "<none>") && (crictl img | grep "<none>" | xargs crictl rmi)
images=( $(crictl img | grep "dev.flyrise.cn" | awk '{print $3}') )
pods=( $(crictl ps | grep -v IMAGE | awk '{print $2}') )
for item in "${images[@]}"
do
if [[ ! " ${pods[@]} " =~ " $item " ]]; then
crictl rmi $item
fi
done
}
main 2>&1 | tee /var/log/clean-unuse-images.log
EOF
# 必须添加执行权限,否则不会执行
chmod +x /etc/cron.hourly/clean-unuse-images.sh
方式二:
cat >> /etc/cron.hourly/clean-unuse-images.sh << 'EOF'
#!/bin/bash
main () {
crictl images|grep dev.flyrise|grep "<none>"|awk '{print $3}'|xargs crictl rmi
filtered_images=$(crictl images | grep "dev.flyrise.cn" | awk '{print $1":"$2}')
# 遍历每个镜像并删除
for image in $filtered_images
do
running=$(crictl ps -a --image "$image" --quiet)
if [[ -n $running ]]; then
echo "已保留镜像: $image"
else
crictl rmi "$image"
#echo "已删除镜像: $image"
fi
done
}
main 2>&1 | tee /var/log/clean-unuse-images.log
EOF
# 必须添加执行权限,否则不会执行
chmod +x /etc/cron.hourly/clean-unuse-images.sh
文档更新时间: 2024-07-19 13:37 作者:管理员