I need to understand how we can delete a docker image using Kubernetes. I'm using Jenkins for pipeline automation and Jenkins only has access to the master node, not the slaves. So when I generate the deploy everything works fine, the deploy makes the slaves pull from the repository and I get everything going.
But if Jenkins kills the deploy and tries to remove the image, it only deletes the image on the master node and not on other slaves. So I don't want to manually delete the image.
Is there a way to delete images on slave nodes from the master node?
Kubelet does eventually garbage collect old unused images when disk usage gets beyond 85% (default setting). Otherwise kubernetes doesn't provide a remote interface for deleting images.
To do it would require something like an SSH to each node or to run a Daemonset that has permissions to manage the underlying container runtime. See Mr.Axe answer for the Docker/SSH variant.
From master node, You can write shell script/ansible playbook to ssh into slave nodes and remove images-
docker images -f "dangling=true" -q | xargs --no-run-if-empty docker rmi
Kubernetes is responsible for deleting images, It is kubelet that makes garbage collection on nodes, including image deletion, it is customizable. Deleting images by external methods is not recomended as these tools can potentially break the behavior of kubelet by removing containers expected to exist.
Kubelet verify if the storage available for the images is more than 85% full, in that case it delete some images to make room. Min and Max threshold can be customized in the file /var/lib/kubelet/config.yaml
imageGCHighThresholdPercent is the percent of disk usage after which image garbage collection is always run.
ImageGCHighThresholdPercent is the percent of disk usage before which image garbage collection is never run. Lowest disk usage to garbage collect to.
Default values are :
ImageGCHighThresholdPercent 85
ImageGCLowThresholdPercent 80