【问题标题】:loop on output and delete in bash循环输出并在 bash 中删除
【发布时间】:2018-11-27 20:21:39
【问题描述】:

团队, 我需要删除有错误的 k8s 集群上的 10 个 pod。 我得到它们如下:

kubectl get pods --all-namespaces | grep -i -e Evict -e Error | awk -F ' ' '{print $1, $2, $4}'
test-asdfasdf asdfasdf2215251 Error
test-asdfasdf asdfasdf2215252 Error
test-asdfasdf asdfasdf2215253 Error
test-asdfasdf asdfasdf2215254 Error
test-asdfasdf asdfasdf2215255 Error
test-asdfasdf asdfasdf2215256 Error

像这样手动删除它们:

kubectl delete pod asdfasdf2215251 -n test-asdfasdf

但是我可以编写一个脚本来查找任何 pod 上的错误并删除所有这些吗?我自己正在编写脚本,但对此很陌生,因此迟到了..

【问题讨论】:

  • ubectl 获取 pod --all-namespaces | grep -i -e 驱逐 -e 错误 | awk -F ' ' '{打印 $1, $2, $4}' | kubectl delete pod $2 -n $1 这没有帮助。
  • ` awk -F ' ' '{print $1, $2, $4}'` 在-F '''{print $1, $2, $4}' 之间没有空格
  • kubectl 获取 pod --all-namespaces | grep -i -e 驱逐 -e 错误 | awk -F ' ' '{打印 $1, $2, $4}' | kubectl delete pod $2 -n $1 this does not identify -n which is used as namespace flag.

标签: bash kubernetes kubectl


【解决方案1】:

起点:

kubectl get pods --all-namespaces |
grep -i -e Evict -e Error |
awk -F ' ' '{print $1, $2}' |

将产生一个流:

test-asdfasdf asdfasdf2215251
test-asdfasdf asdfasdf2215252
test-asdfasdf asdfasdf2215253
test-asdfasdf asdfasdf2215254
test-asdfasdf asdfasdf2215255
test-asdfasdf asdfasdf2215256

我们可以去这里:

while IFS=' ' read -r arg1 arg2; do
    kubectl delete pod "$arg2" -n "$arg1"
done

我们可以去这里:

xargs -l1 -- sh -c 'kubectl delete pod "$2" -n "$1"' --

或使用parallel 或任何其他工具来执行此操作。

【讨论】:

  • aah.. 有效。 (kubectl get pods --all-namespaces | grep -i -e Evict -e Error | awk -F ' ' '{print $1, $2, $4}' | xargs -l1 -- sh -c 'kubectl delete pod "$2 " -n "$1"' --)
猜你喜欢
  • 2014-07-08
  • 1970-01-01
  • 2019-02-17
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多