K8S 使用 Containerd 作为容器运行时,运行一段时间后随着业务不断发版,各节点本地下载保留的镜像越来越多,磁盘空间慢慢上去了。这里写个脚本加入到计划任务每天定时执行自动清理下镜像。
shell 脚本内容
查询所有镜像列表时,提前过滤了镜像仓库namespace为 mysaas ,因为这个 namespace 是放具体业务应用的。其他 namespace 放运维用的镜像的这里先不删。
[root@imzcy ~]# cat images_clean.sh
#!/bin/bash
# auther: zcy
# site: https://www.imzcy.cn
retainNumber=5
imageList=$(/usr/local/bin/crictl images |grep "mysaas" |awk 'NR>1{print $1}' |sort |uniq)
for image in $imageList
do
echo "# 当前处理镜像为:$image"
nowImageList=$(/usr/local/bin/crictl images | awk -v image=$image '$1 == image {print $1":"$2}' |sort -r -V)
nowImageNumber=$(echo "$nowImageList" |wc -l)
if [ $nowImageNumber -gt $retainNumber ];then
deleteImageList="$(echo "$nowImageList" |sed -n ''"${retainNumber}"',$p')"
for deleteImage in $deleteImageList
do
#echo $(date +%Y%m%d_%H%M%S) $deleteImage 将删除
echo " $deleteImage 将删除"
rmi_info=$( /usr/local/bin/crictl rmi $deleteImage 2>&1 )
echo "$rmi_info" |sed 's/^/ /g'
done
else
#echo "$(date +%Y%m%d_%H%M%S) $image 镜像数量为 $nowImageNumber 少于保留个数 $retainNumber 无需处理"
echo " $image 镜像数量为 $nowImageNumber 少于保留个数 $retainNumber 无需处理"
fi
sleep 1
done
[root@imzcy ~]# 执行示例
[root@imzcy ~]# sh images_clean.sh
# 当前处理镜像为:ccr.ccs.tencentyun.com/mysaas/java-basedata
ccr.ccs.tencentyun.com/mysaas/java-basedata 镜像数量为 5 少于保留个数 5 无需处理
# 当前处理镜像为:ccr.ccs.tencentyun.com/mysaas/java-logisticsconsumer
ccr.ccs.tencentyun.com/mysaas/java-logisticsconsumer 镜像数量为 2 少于保留个数 5 无需处理
# 当前处理镜像为:ccr.ccs.tencentyun.com/mysaas/java-logisticslogic
ccr.ccs.tencentyun.com/mysaas/java-logisticslogic:v20_202307110956 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-logisticslogic:v20_202307110956
ccr.ccs.tencentyun.com/mysaas/java-logisticslogic:v19_202306021509 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-logisticslogic:v19_202306021509
# 当前处理镜像为:ccr.ccs.tencentyun.com/mysaas/java-logisticsproducer
ccr.ccs.tencentyun.com/mysaas/java-logisticsproducer:v44_202307110939 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-logisticsproducer:v44_202307110939
ccr.ccs.tencentyun.com/mysaas/java-logisticsproducer:v43_202306051805 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-logisticsproducer:v43_202306051805
# 当前处理镜像为:ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer
ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v23_202307311009 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v23_202307311009
ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v21_202307211043 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v21_202307211043
ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v18_202307181008 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v18_202307181008
ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v17_202307171143 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v17_202307171143
ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v12_202307051817 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v12_202307051817
ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v8_202306271723 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v8_202306271723
ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v7_202306251526 将删除
Deleted: ccr.ccs.tencentyun.com/mysaas/java-notificationconsumer:v7_202306251526
# 当前处理镜像为:ccr.ccs.tencentyun.com/mysaas/java-notificationproducer
ccr.ccs.tencentyun.com/mysaas/java-notificationproducer 镜像数量为 4 少于保留个数 5 无需处理
[root@imzcy ~]#
本文采用 知识共享署名4.0 国际许可协议进行许可。
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。
如果您的问题未解决,欢迎微信扫描右侧二维码与我联系。