怎么删除过期的容器和镜像?
我在k8s和docker社区内看到这个问题,但好像没人解决。所以分享一个小脚本
cat docker.cleanup.sh
!/bin/sh
docker ps -a -q | while read cid
do
fini=$(docker inspect $cid | grep FinishedAt | awk -F\" '{printf("%.19s", $4)}')
diff=$(expr $(date +"%s") - $(date --date="$fini" +"%s"))
echo $diff
if
then
docker rm $cid
fi
done
docker rmi $(docker images -a)
这个脚本会删除停止超过1天的容器,和所有不用的镜像,可以用crontab定期运行
6 个回复
难易 - PaaS开发者
赞同来自:
上面这个脚本有bug,我重新修复了一些问题,放在这里
https://github.com/HardySimpson/docker-cleanup
小飞侠 - TenxCloud合伙人
赞同来自:
kubernetes 有自己的container GC 配置,比如:
--maximum_dead_containers=100: Maximum number of old instances of a containers to retain globally. Each container takes up some disk space. Default: 100.
难易 - PaaS开发者
赞同来自:
发现几个参数,但不是很懂含义
<pre>
fs.DurationVar(&s.MinimumGCAge, "minimum-container-ttl-duration", s.MinimumGCAge, "Minimum age for a finished container before it is garbage collected. Examples: '30
0ms', '10s' or '2h45m'")
// 一个container关掉后最少多少时间后开始进行清理删除
fs.IntVar(&s.MaxPerPodContainerCount, "maximum-dead-containers-per-container", s.MaxPerPodContainerCount, "Maximum number of old instances of a container to retain pe
r container. Each container takes up some disk space. Default: 5.")
// 这个不是很懂,每容器最大的实例?容器不就是实例吗?
fs.IntVar(&s.MaxContainerCount, "maximum-dead-containers", s.MaxContainerCount, "Maximum number of old instances of a containers to retain globally. Each container t
akes up some disk space. Default: 100.")
// 这里的表述也一样奇怪,每容器的实例,
</pre>
<pre>
fs.IntVar(&s.ImageGCHighThresholdPercent, "image-gc-high-threshold", s.ImageGCHighThresholdPercent, "The percent of disk usage after which image garbage collection is
always run. Default: 90%%")
fs.IntVar(&s.ImageGCLowThresholdPercent, "image-gc-low-threshold", s.ImageGCLowThresholdPercent, "The percent of disk usage before which image garbage collection is n
ever run. Lowest disk usage to garbage collect to. Default: 80%%")
fs.IntVar(&s.LowDiskSpaceThresholdMB, "low-diskspace-threshold-mb", s.LowDiskSpaceThresholdMB, "The absolute free disk space, in MB, to maintain. When disk space fall
s below this threshold, new pods would be rejected. Default: 256")
</pre>
这3个参数就比较好理解一点,镜像清理
分区使用率超过"image-gc-high-threshold"开始清理,
少于"image-gc-low-threshold"不做清理,
分区使用率少于"low-diskspace-threshold-mb"新的pod不被创建
小飞侠 - TenxCloud合伙人
赞同来自:
fs.IntVar(&s.MaxPerPodContainerCount, "maximum-dead-containers-per-container", s.MaxPerPodContainerCount, "Maximum number of old instances of a container to retain pe
r container. Each container takes up some disk space. Default: 5.")
// 这个不是很懂,每容器最大的实例?容器不就是实例吗?
<strong><wl> 这个应该是每种container(或者某一个镜像创建出来的)保留的退出容器的最大个数,比如某个容器总是创建失败,那么只保留创建失败的最后5个.</wl></strong>
fs.IntVar(&s.MaxContainerCount, "maximum-dead-containers", s.MaxContainerCount, "Maximum number of old instances of a containers to retain globally. Each container t
akes up some disk space. Default: 100.")
// 这里的表述也一样奇怪,每容器的实例,
<strong><wl>似乎应该是 Maximum number of old instances of containers to retain globally. 应该就是所有退出的容器(各种镜像创建出来的)允许保留的最大总数(某个节点上)</wl>.</strong>
上面是我的理解,需要验证一下 :)
难易 - PaaS开发者
赞同来自:
刚读了源代码,发现k8s对每种container的定义有点奇怪,是按照PodUid+ContainerName为标识分组的。里面所谓的
Maximum number of old instances of a container to retain per container, 应该是per pod
k8s现在的做法是一个pod中的单个容器没有启动起来就一直重复的起,所以删也是这单个容器中的container
小飞侠 - TenxCloud合伙人
赞同来自:
嗯,这个是目前k8s的design,还需要继续深挖相关细节 :)