Package: docker-image-cleaner
Docker Image CleanerA Python package (docker-image-cleaner) and associated Docker image
(quay.io/jupyterhub/docker-image-cleaner) to clean up old docker images when a
disk is running low on inodes or space.The script has initially been developed to help installations of BinderHub clean
up space on nodes as it otherwise can run out of space and stop being able to
build now docker images.Why?Container images are one of the biggest consumers of disk space
and inodes on kubernetes nodes. Kubernetes tries to make sure there is enough
disk space on each node bygarbage
collectingunused container images and containers. Tuning this is important
forbinderhubinstallations,
as many images are built and used only a couple times. However, on
most managed kubernetes installations (like GKE, EKS, etc), we can not
tune these parameters!This script approximates the specific parts of the kubernetes container image
garbage collection in a configurable way.RequirementsOnly kubernetes nodes using thedockerruntime are supported.containerdorcri-ocontainer backends are not supported.The script expects to run in a kubernetesDaemonSet, with/var/lib/dockerfrom the node mounted inside the container. This lets the script figure
out how much disk space docker container images are actually using.TheDaemonSetshould have aServiceAccountattached that has permissions
to talk to the kubernetes API and cordon / uncordon nodes. This makes sure
new pods are not scheduled on to the node while image cleaning is happening,
as it can take a while.How does it work?Compute how much space/var/lib/dockerdirectory (specified by theDOCKER_IMAGE_CLEANER_PATH_TO_CHECKenvironment variable) is taking up.If the disk space used is greater than the garbage collection trigger threshold
(specified byDOCKER_IMAGE_CLEANER_THRESHOLD_HIGH), garbage collection is triggered.
If not, the script just waits another 5 minutes (set byDOCKER_IMAGE_CLEANER_INTERVAL_SECONDS).If garbage collection is triggered, the kubernetes node is first cordoned
to prevent any new pods from being scheduled on it for the duration of the
garbage collection.Stopped containers are removed viadocker container prune.Dangling images are removed viadocker image pruneIf no dangling images are found to prune,allimages are pruned (docker image prune -a)After the garbage collection is done, the kubernetes node is also uncordoned.When done, we wait another 5 minutes (set byDOCKER_IMAGE_CLEANER_INTERVAL_SECONDS), and repeat
the whole process.Configuration optionsCurrently, environment variables are used to set configuration for now.Env variableDescriptionDefaultDOCKER_IMAGE_CLEANER_NODE_NAMEThe k8s node where the docker image cleaner is running, so it can be cordoned via the k8s apiDOCKER_IMAGE_CLEANER_PATH_TO_CHECKPath to/var/lib/dockerdirectory used by the docker daemon/var/lib/dockerDOCKER_IMAGE_CLEANER_INTERVAL_SECONDSAmount of time (in seconds) to wait between checking if GC needs to be triggered300DOCKER_IMAGE_CLEANER_DELAY_SECONDSAmount of time (in seconds) to wait between deleting container images, so we don't DOS the docker API1DOCKER_IMAGE_CLEANER_THRESHOLD_TYPEDetermine if GC should be triggered based on relative or absolute disk usagerelativeDOCKER_IMAGE_CLEANER_THRESHOLD_HIGH% or absolute disk space available (based onDOCKER_IMAGE_CLEANER_THRESHOLD_TYPE) when we start deleting container images80DOCKER_IMAGE_CLEANER_TIMEOUT_SECONDSRequest timeout (in seconds) for docker API requests. Pruning images often takes minutes. Default: 300 (5 minutes)