背景

笔者面临这么一种k8s操作的场景:我期望批量删除集群中的某一个命令空间下的所有pod。

#一开始我是通过以下命令实现的
kubectl get po -n xxx|awk '{print $1}'|xargs kubectl delete po -n xxx

这时候发现会出席一异常报错,后来发下是因为,有标题行存在,导致程序异常

[root@testhd-server kubernetes]# kubectl get po -n kube-system
NAME                                  READY   STATUS    RESTARTS       AGE
coredns-65c54cc984-6x9jb              1/1     Running   1              24h
coredns-65c54cc984-94s64              1/1     Running   1              24h
etcd-yg2-control                      1/1     Running   11 (23h ago)   23h
kube-apiserver-yg2-control            1/1     Running   8 (23h ago)    24h
kube-controller-manager-yg2-control   1/1     Running   12 (23h ago)   24h
kube-proxy-n969x                      1/1     Running   1              24h
kube-scheduler-yg2-control            1/1     Running   12 (23h ago)   24h

修复

[root@testhd-server kubernetes]# kubectl get po -n kube-system --no-headers
coredns-65c54cc984-6x9jb              1/1   Running   1              24h
coredns-65c54cc984-94s64              1/1   Running   1              24h
etcd-yg2-control                      1/1   Running   11 (23h ago)   23h
kube-apiserver-yg2-control            1/1   Running   8 (23h ago)    24h
kube-controller-manager-yg2-control   1/1   Running   12 (23h ago)   24h
kube-proxy-n969x                      1/1   Running   1              24h
kube-scheduler-yg2-control            1/1   Running   12 (23h ago)   24h

–no-header可以删除标题行,这个flag在kubectl和helm中都存在,所以可以使用如下命令清理某个namespace下的所有pod

kubectl get po -n xxx --no-headers|awk '{print $1}'|xargs kubectl delete po -n xxx

请把xxx替换成自己需要的namespace

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐