–
为了解决node上面磁盘占用问题,将历史的不用的image定期进行清理,删除历史的image
从k8s里面拿正在运行的pod的镜像,然后保留3个最近的镜像,然后删除其余的全部镜像
[root@k8s-test-4 /data/script]#cat delete_images.sh #!/bin/bash # 0 2 * * * /bin/bash /data/script/delete_images.sh PATH=/sbin:/bin:/usr/sbin:/usr/bin:/opt/k8s/bin cd $(dirname $0) daily_day=3 tmp_day=3 qua_day=3 pre_day=3 prod_day=3 func_delete(){ echo echo echo "$(date +%F_%T) start delete images" docker ps -a | grep Exited | awk '{print $1}' |xargs docker rm docker image prune -f echo echo echo > delete_images.tmp deployment_images=$(kubectl get deployment --all-namespaces -o yaml | egrep -o [[:space:]:]harbor.*[0-9] | egrep -o harbor.*[0-9] | sort | uniq -c | awk '{print $2}') for image in $deployment_images; do project=$(echo $image | awk -F : '{print $1}') release=$(echo $image | awk -F : '{print $2}') echo $release | grep -q "daily" [[ $? -eq 0 ]] && docker image ls $project | awk '$2~/daily/' | head -n $daily_day | awk '{print $1":"$2}' >> delete_images.tmp echo $release | grep -q "tmp" [[ $? -eq 0 ]] && docker image ls $project | awk '$2~/tmp/' | head -n $tmp_day | awk '{print $1":"$2}' >> delete_images.tmp echo $release | grep -q "qua" [[ $? -eq 0 ]] && docker image ls $project | awk '$2~/qua/' | head -n $qua_day | awk '{print $1":"$2}' >> delete_images.tmp echo $release | grep -q "pre" [[ $? -eq 0 ]] && docker image ls $project | awk '$2~/pre/' | head -n $pre_day | awk '{print $1":"$2}' >> delete_images.tmp echo $release | grep -q "prod" [[ $? -eq 0 ]] && docker image ls $project | awk '$2~/prod/' | head -n $prod_day | awk '{print $1":"$2}' >> delete_images.tmp done for image in $(docker image ls | egrep -v "k8s|public|REPOSITORY" | awk '{print $1":"$2}' | sort); do grep -q $image delete_images.tmp if [[ $? -ne 0 ]]; then echo "delete $image" docker rmi $image fi done echo "$(date +%F_%T) stop delete images" } func_delete &>> delete_images.log
–
–
–
评论前必须登录!
注册