删掉所有 Terminating 状态的pods
kubectl get pods -A -owide |grep Terminating | awk '{print "kubectl delete pods -n",$1,$2,"--grace-period=0 --force"}' |bash
打印出k8s集群中的节点资源分配情况
for i in $(kubectl get nodes --no-headers |awk '{print $1}');do echo $i; kubectl describe nodes $i |grep -A 5 'Allocated resources:' ;echo -e "--------------\n"; done
k8s资源的备份
#!/bin/bash
# 获取所有命名空间列表
namespaces=$(kubectl get namespaces -o jsonpath='{.items[*].metadata.name}')
# 遍历命名空间
for ns in $namespaces; do
echo "导出命名空间 '$ns' 的资源..."
mkdir -p "$ns" # 创建命名空间文件夹
# 需要导出pvc可以自己加
kubectl get pvc -n "$ns" -o yaml > "$ns/pvc.yaml"
# 导出 Deployment 资源
kubectl get deployments -n "$ns" -o yaml > "$ns/deployments.yaml"
# 导出 daemonset 资源
kubectl get daemonset -n "$ns" -o yaml > "$ns/daemonset.yaml"
# 导出 statefulset 资源
kubectl get statefulset -n "$ns"