Kubernetes 清理资源常用的 Kubernetes 清理命

  • 清理特定状态的 Pod:

    • 清理 Evicted 状态的 Pod:

      kubectl get pods --all-namespaces -o wide | grep Evicted | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n
      
    • 清理 Error 状态的 Pod:

      kubectl get pods --all-namespaces -o wide | grep Error | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n
      
    • 清理 Completed 状态的 Pod:

       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}'
      
  • 清理未使用的存储资源:

    • 清理没有被使用的 PV(Persistent Volumes):

      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}'
      
    • 清理没有被绑定的 PVC(Persistent Volume Claims):

      kubectl get pvc --all-namespaces | tail -n +2 | grep -v Bound | awk '{print $1,$2}' | xargs -L1 kubectl delete pvc -n
      
    • 清理没有被绑定的 PV:

       kubectl get pv | tail -n +2 | grep -v Bound | awk '{print $1}' | xargs -L1 kubectl delete pv
      
  • Linux 系统清理:

    • 查看磁盘全部空间:

      df -hl /
      
    • 查看指定目录占用:

      du -sh .
      
    • 删除指定前缀的文件夹:

      ls | grep archived- |xargs -L1 rm -r
      
    • 清理僵尸进程:

      ps -A -ostat,ppid | grep -e '^[Zz]' | awk '{print }' | xargs kill -HUP > /dev/null 2>&1
      
  • Docker 清理(如果您的集群使用 Docker 作为容器运行时):

    • 查看磁盘使用情况:

      docker system df
      
    • 清理 none 镜像:

      docker system df
      
    • 清理不再使用的数据卷:

      docker volume rm $(docker volume ls -q)或者 docker volume prune
      
    • 清理缓存:

      docker builder prune
      
    • 全面清理:

      docker system prune -f
      
    • 清理正则匹配上的镜像:

       docker images |grep -E "([0-9a-z]*[-]){3,}[0-9]{9}" |awk '{print $3}' | xargs  docker rmi
      
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值