
Clusters that run for a long time often face various kinds of resource exhaustion. On top of that, when disk space runs low, Kubelet will proactively clean up images on its own, adding another source of uncertainty. This article provides some command snippets for cleanup work.
1. Cleaning Up Basic Kubernetes Objects
- Clean up Pods in Evicted state
1
| sudo kubectl get pods --all-namespaces -o wide | grep Evicted | awk '{print $1,$2}' | sudo xargs -L1 kubectl delete pod -n
|
- Clean up Pods in Error state
1
| sudo kubectl get pods --all-namespaces -o wide | grep Error | awk '{print $1,$2}' | sudo xargs -L1 kubectl delete pod -n
|
- Clean up Pods in Completed state
1
| sudo kubectl get pods --all-namespaces -o wide | grep Completed | awk '{print $1,$2}' | sudo xargs -L1 kubectl delete pod -n
|
1
| sudo kubectl describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Used By:.*$" | grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*$" | cut -f2 -d: | paste -d " " - - | sudo xargs -n2 bash -c 'kubectl -n ${1} delete pvc ${0}'
|
1
| sudo kubectl get pvc --all-namespaces | tail -n +2 | grep -v Bound | awk '{print $1,$2}' | sudo xargs -L1 kubectl delete pvc -n
|
1
| sudo kubectl get pv | tail -n +2 | grep -v Bound | awk '{print $1}' | sudo xargs -L1 kubectl delete pv
|
2. Linux Cleanup
1
2
3
4
| sudo df -hl /
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 47G 54G 47% /
|
- Check the space used by a given directory
1
2
3
| sudo du -sh .
24G .
|
- Delete folders with a given prefix
1
2
| cd /nfsdata
ls | grep archived- |xargs -L1 rm -r
|
- Clean up zombie processes
1
| sudo ps -A -ostat,ppid | grep -e '^[Zz]' | awk '{print }' | xargs kill -HUP > /dev/null 2>&1
|
3. Docker Cleanup
1
2
3
4
5
6
7
| sudo docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 361 23 178.5GB 173.8GB (97%)
Containers 29 9 6.682GB 6.212GB (92%)
Local Volumes 4 0 3.139MB 3.139MB (100%)
Build Cache 0 0 0B 0B
|
1
| sudo docker images | grep none | awk '{print $3}' | sudo xargs docker rmi
|
- Clean up data volumes that are no longer used
1
| sudo docker volume rm $(docker volume ls -q)
|
Or
1
| sudo docker volume prune
|
1
| sudo docker builder prune
|
Deletes stopped containers, unused volumes, unused networks, and dangling images (images with no tag)
- Clean up images matching a regular expression
This cleans up images in the master-8bcf8d7-20211206-111155163 format.
1
| sudo docker images |grep -E "([0-9a-z]*[-]){3,}[0-9]{9}" |awk '{print $3}' | sudo xargs docker rmi
|
4. Setting Up a Schedule
Add scheduled tasks in the text
1
2
| */35 */6 * * * sudo /usr/bin/docker images | grep none | awk '{print $3}' | xargs /usr/bin/docker rmi
45 1 * * * sudo /usr/bin/docker system prune -f
|
Here the first task runs at the 35th minute of every six hours, and the second task runs at 1:45 every day. Note that the commands must use absolute paths, otherwise they may fail to run.
Schedule format: * * * * * shell
First asterisk, minute, values 0-59
Second asterisk, hour, values from 0-23
Third asterisk, day, values from 1-31
Fourth asterisk, month, values from 1-12, or English abbreviations such as Nov, Feb
Fifth asterisk, week, values from 0-6 or English abbreviations such as Wed, Thu, representing the day of the week, where 0 represents the weekend