删除k8s环境过期镜像&docker 磁盘清理
docker system df镜像占用了超过3.718GB磁盘空间。docker system df-v1.images的SIZE是磁盘大小,SHARED SIZE,是共享大小,CONTAINERS是启动容器的个数,如果是0就代表是没有使用的镜像。2.Containers的size是修改层大小。3.volume 的SIZE是所占空间大小,像"Mounts": bind这种挂载空间是没有计算在内的
·
docker system df
镜像占用了超过3.718GB磁盘空间。
docker system df -v
-
1.images的SIZE是磁盘大小,SHARED SIZE,是共享大小,CONTAINERS是启动容器的个数,如果是0就代表是没有使用的镜像。
-
2.Containers的size是修改层大小。
-
3.volume 的SIZE是所占空间大小,像"Mounts": bind这种挂载空间是没有计算在内的
[root@node172 ~]# docker system df --verbose
#镜像空间使用情况
Images space usage:
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS
registry.cn-shenzhen.aliyuncs.com/lolos/loloss flask-demon f4a0d136e2c6 3 weeks ago 120.3MB 5.613MB 114.7MB 3
ghcr.io/linkerd/controller edge-21.2.1 e1cdfb82032f 4 weeks ago 44MB 0B 44MB 0
ghcr.io/linkerd/proxy edge-21.2.1 49ce29cf9e64 4 weeks ago 114.6MB 69.24MB 45.34MB 0
registry.cn-shenzhen.aliyuncs.com/jbjb/alpine simple-v01 652ba0e5f116 4 weeks ago 24.53MB 5.613MB 18.92MB 0
registry.cn-shenzhen.aliyuncs.com/jbjb/demons-d productcatalogue8 ef9e688776d8 5 weeks ago 285.4MB 267.7MB 17.62MB 0
registry.cn-shenzhen.aliyuncs.com/jbjb/demons-d stockmanager1 fc9138052165 5 weeks ago 311MB 267.7MB 43.31MB 0
registry.cn-shenzhen.aliyuncs.com/jbjb/demons-d shopfront1 93b876a3b2d8 5 weeks ago 313.8MB 267.7MB 46.09MB 0
registry.cn-shenzhen.aliyuncs.com/jbjb/demons-d productcatalogue7 0fca3532f297 5 weeks ago 285.4MB 267.7MB 17.62MB 0
feiyu563/prometheus-alert latest 72af7e46fae0 6 weeks ago 38.24MB 5.577MB 32.66MB 0
grafana/grafana latest 13afb861111c 7 weeks ago 187.3MB 5.577MB 181.7MB 0
#容器空间使用情况
Containers space usage:
CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES
9c5c77704f14 629df02b47c8 "/docker-entrypoint.…" 0 1.12kB 50 minutes ago Up 50 minutes k8s_nginx_nginx-6b69f5bc8b-s7c6n_default_ff2d472b-19b8-4da2-a886-c37f1e816494_0
6f1a7b49bf7a 629df02b47c8 "/docker-entrypoint.…" 0 420MB 50 minutes ago Up 50 minutes k8s_nginx_nginx-6b69f5bc8b-52rn8_default_6b3c4d0d-0aaa-4b1d-8c79-4ffb610f7985_0
f73844563277 629df02b47c8 "/docker-entrypoint.…" 0 1.12kB 50 minutes ago Up 50 minutes k8s_nginx_nginx-6b69f5bc8b-8ps4l_default_8fddf18a-928a-4435-9bbc-bd8ef584446d_0
066e8e715441 629df02b47c8 "/docker-entrypoint.…" 0 1.12kB 50 minutes ago Up 50 minutes k8s_nginx_nginx-6b69f5bc8b-kpgcj_default_
#本地卷使用情况
Local Volumes space usage:
VOLUME NAME LINKS SIZE
Build cache usage: 0B
CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED
查看完我们可以通过以下命令进行删除
1. 可以通过 Docker 内置的 CLI 指令docker system prune来进行自动空间清理
[root@c169 ~]# docker system prune --help
Usage: docker system prune [OPTIONS]
Remove unused data
Options:
-a, --all Remove all unused images not just dangling ones
--filter filter Provide filter values (e.g. 'label=<key>=<value>')
-f, --force Do not prompt for confirmation
--volumes Prune volumes
2. docker system prune 自动清理说明:
该指令默认会清除所有如下资源:
- 已停止的容器(container)
- 未被任何容器所使用的卷(volume)
- 未被任何容器所关联的网络(network)
- 所有悬空镜像(image)。
- 该指令默认只会清除悬空镜像,未被使用的镜像不会被删除。添加-a 或 --all参数后,可以一并清除所有未使用的镜像和悬空镜像。
- 可以添加-f 或 --force参数用以忽略相关告警确认信息
-
(温馨提示如果文件太多太大这条命令会卡住,建议做pod驱逐到其他节点再执行,或者使用脚本找到镜像规律逐个删除)
[root@dockercon ~]# docker system prune --all
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all images without at least one container associated to them
- all build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
f095899e7343e160d5b32d0688a6561a1a7f6af91c42ffe966649240b58ca23f
Deleted Images:
untagged: busybox:latest
untagged: busybox@sha256:e3789c406237e25d6139035a17981be5f1ccdae9c392d1623a02d31621a12bcc
deleted: sha256:6ad733544a6317992a6fac4eb19fe1df577d4dec7529efec28a5bd0edad0fd30
deleted: sha256:0271b8eebde3fa9a6126b1f2335e170f902731ab4942f9f1914e77016540c7bb
untagged: kalilinux/kali-linux-docker:latest
untagged: kalilinux/kali-linux-docker@sha256:28ff9e4bf40f7399e0570394a2d3d388a7b60c748be1b0a180c14c87afad1968
deleted: sha256:c927a54ec8a46164d7046b2a6dc09b2fce52b3066317d50cf73d14fa9778ca48
deleted: sha256:244c1920ef0442167cdbd095e5d29813cb5be0b70cc116faf8d7e50074f6c446
3 可以另外打开一个终端,查看实时事件(例如容器创建,删除等均会实时显示)
[root@node172 ~]# docker system events
2021-03-06T18:00:19.843255544+08:00 image untag sha256:f1a77cb119f480351fb7bdfcfeca2bcff57334a5205d9c770a7d5bc396287742 (name=sha256:f1a77cb119f480351fb7bdfcfeca2bcff57334a5205d9c770a7d5bc396287742)
2021-03-06T18:00:21.506665154+08:00 image delete sha256:f1a77cb119f480351fb7bdfcfeca2bcff57334a5205d9c770a7d5bc396287742 (name=sha256:f1a77cb119f480351fb7bdfcfeca2bcff57334a5205d9c770a7d5bc396287742)
脚本删除方式如下
cat <<END> delel-image
#!/bin/bash
#删除过期镜像,保留最新5个版本
CDATE=`date '+%Y-%m-%d_%H:%M:%S'`
#######比如经常发版的关键字是"rabbitmq"
for service in `docker images | grep 'rabbitmq' | awk '{print $1}' |sort -u`
do
for i in `docker images | grep $service | awk '{print $2}' | awk -F"-" '{print $1}' | sort -u`
do
banben=`docker images | grep $service | awk '{print $2}'`
mem=`docker images | grep $service | awk '{print $2}' |wc -l`
done
#echo $banben
#echo $mem
if [[ $mem -gt 3 ]];then
arr=(${banben// /})
#保留同样类型的5个镜像
for(( j=3;j<${#arr[@]};j++)) do
docker rmi $service:${arr[j]}
#echo $service:${arr[j]}
echo "docker rmi $service:${arr[j]} 执行时间:$CDATE" >> /home/log_delete_images.log
done
fi
done
END
更多推荐
已为社区贡献82条内容
所有评论(0)