1、清理Terminating状态的Pod
kubectl get pods -A -o wide | grep Terminating | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n
2、强制清理Terminating状态的Pod
kubectl get pods -A -o wide | grep Terminating | awk '{print $1,$2}' | xargs -L1 kubectl delete pod --force --grace-period=0 -n
3、清理Error状态的Pod
kubectl get pods -A -o wide | grep Error | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n
4、清理Evicted状态的Pod
kubectl get pod -o wide -A | awk '{if($4=="Evicted"){cmd="kubectl -n "$1" delete pod "$2; system(cmd)}}'
5、清理未绑定的Pvc
kubectl get pvc -A | tail -n +2 | grep -v Bound | awk '{print $1,$2}' | xargs -L1 kubectl delete pvc -n
6、清理未绑定的Pv
kubectl get pv | tail -n +2 | grep -v Bound | awk '{print $1}' | xargs -L1 kubectl delete pv
7、清理已经完成Job
kubectl get pods -A-o wide | grep Completed | awk '{print $1,$2}' | xargs -L1 kubectl delete pod -n
8、清理Terminating状态的Namespace
获取 namespace 的详情信息并转为 json
$ kubectl get ns test -o json > test.json
编辑 test.json 文件去掉 spec 字段的内容,如下图:
"spec": {
"finalizers": [ # 删除该行
"kubernetes" # 删除该行
] # 删除该行
},
执行清理命令,彻底删除这个 Namespace
kubectl replace --raw "/api/v1/namespaces/test/finalize" -f ./test.json
9、各节点占用的 podCIDR
$ kubectl get no -o=custom-columns=INTERNAL-IP:.metadata.name,EXTERNAL-IP:.status.addresses[1].address,CIDR:.spec.podCIDR
10、各节点总可用资源
kubectl get no -o=custom-columns="NODE:.metadata.name,ALLOCATABLE CPU:.status.allocatable.cpu,ALLOCATABLE MEMORY:.status.allocatable.memory"
11、各节点 CPU已分配情况
kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo -ne "{}\t" ; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- | grep cpu | awk '\''{print $2$3}'\'';'
12、各节点内存已分配情况
kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo -ne "{}\t" ; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- | grep memory | awk '\''{print $2$3}'\'';'
13、各节点已分配资源情况
kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c "echo {} ; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve --;"
评论区