当前位置:网站首页>Common commands for cleaning up kubernetes cluster resources

Common commands for cleaning up kubernetes cluster resources

2022-06-23 21:34:00 Chenshaowen

Long running clusters , We often face the problem of exhaustion of various resources , In addition, when the disk is insufficient Kubelet It also actively cleans up the image, increasing uncertainty , This article provides some command snippets for cleaning up .

1. Kubernetes Base object cleanup

  • clear Evicted State of Pod

1

kubectl get pods --all-namespaces -o wide | grep Evicted | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n

  • clear Error State of Pod

1

kubectl get pods --all-namespaces -o wide | grep Error | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n

  • clear Completed State of Pod

1

kubectl get pods --all-namespaces -o wide | grep Completed | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n

  • Clean up unused PV

1

kubectl describe -A pvc | grep -E "^Name:.*$|^Namespace:.*$|^Used By:.*$" | grep -B 2 "<none>" | grep -E "^Name:.*$|^Namespace:.*$" | cut -f2 -d: | paste -d " " - - | xargs -n2 bash -c 'kubectl -n ${1} delete pvc ${0}'

  • Clean up unbound PVC

1

kubectl get pvc --all-namespaces | tail -n +2 | grep -v Bound | awk '{print $1,$2}' | xargs -L1 kubectl delete pvc -n

  • Clean up unbound PV

1

kubectl get pv | tail -n +2 | grep -v Bound | awk '{print $1}' | xargs -L1 kubectl delete pv

2. Linux clear

  • View all disk space

1 2 3 4

df -hl / Filesystem Size Used Avail Use% Mounted on /dev/sda2 100G 47G 54G 47% /

  • View the occupation of the specified directory

1 2 3

du -sh . 24G .

  • Delete the folder with the specified prefix

1 2

cd /nfsdata ls | grep archived- |xargs -L1 rm -r

  • Clean up the zombie process

1

ps -A -ostat,ppid | grep -e '^[Zz]' | awk '{print }' | xargs kill -HUP > /dev/null 2>&1

3. Docker clear

  • Check disk usage

1 2 3 4 5 6 7

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

  • clear none Mirror image

1

docker images | grep none | awk '{print $3}' | xargs docker rmi

  • Clean up data volumes that are no longer in use

1

docker volume rm $(docker volume ls -q)

perhaps

1

docker volume prune

  • Clean cache

1

docker builder prune

  • Comprehensive cleaning

Delete closed containers 、 Useless storage volumes 、 Useless network 、dangling Mirror image ( nothing tag Mirror image )

1

docker system prune -f

  • Clean up images on regular matches

What's cleared here is master-8bcf8d7-20211206-111155163 Image of format .

1

docker images |grep -E "([0-9a-z]*[-]){3,}[0-9]{9}" |awk '{print $3}' | xargs docker rmi

4. Set the timing

  • View scheduled tasks

1

crontab -l

  • Set timing task

1

crontab -e

Text add scheduled task

1 2

*/35 */6 * * * docker images | grep none | awk '{print $3}' | xargs docker rmi 45 1 * * * docker system prune -f

The first task here is... Every six hours 35 Minute execution , The second task is... Every day 1 when 45 Points to perform .

  • Format of scheduled tasks

Set timing format : * * * * * shell

The first asterisk ,minute, minute , The value is 0-59 The second asterisk ,hour, Hours , Value from 0-23 The third asterisk ,day, God , Value from 1-31 The fourth asterisk ,month, month , Value from 1-12 month , Or abbreviated English , such as Nov、Feb etc. The fifth asterisk ,week Zhou , Value from 0-6 Or abbreviated English ,Wen、Tur etc. , Represents the day of the week , among 0 On behalf of the weekend

https://www.chenshaowen.com/blog/common-commands-for-cleaning-up-kubernetes-cluster-resources.html

原网站

版权声明
本文为[Chenshaowen]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112221838211438.html